Select to view content in your preferred language

Trying to change the fill color of a graphicelement...

217
5
Jump to solution
a month ago
Gurunara
Occasional Contributor

using following to change the fill color of graphicelement:

QueuedTask.Run(() =>
{
// Access the CIM definition of the graphic element
var cimElement = graphicElement.GetDefinition() as CIMElement;
if (cimElement == null)
return;

// Get the current background symbol
var currentSymbol = graphicElement.GetGraphic().Symbol;

// Check if the symbol is a fill symbol
if (currentSymbol is CIMSymbolReference polygonSymbol)
{
// Change the fill color
polygonSymbol.Symbol = SymbolFactory.Instance
.ConstructPolygonSymbol(ColorFactory.Instance.CreateColor(newColor));

// Apply the modified symbol back to the GraphicElement
graphicElement.GetGraphic().Symbol = polygonSymbol;
}

// Apply the changes back to the graphic element
graphicElement.SetDefinition(cimElement);
});

 

No compile/runtime error, but the color does not change on UI...
Any inputs..?

Is there another way to change the color or flash/highlight a given graphicelement on a graphicslayer?

0 Kudos
1 Solution

Accepted Solutions
CharlesMacleod
Esri Regular Contributor

Gurunara, if this answers your question please mark this post as the solution, thanks

 

View solution in original post

0 Kudos
5 Replies
CharlesMacleod
Esri Regular Contributor

You are close, - almost there. Use graphicElement.SetGraphic(...) - https://pro.arcgis.com/en/pro-app/latest/sdk/api-reference/topic11077.html - to apply the changes to the graphic symbol _back_ to the element. There is a code snippet there u can use.

 

 

 

 

0 Kudos
Gurunara
Occasional Contributor

Do the following?

QueuedTask.Run(() =>
{
// Access the CIM definition of the graphic element
var cimElement = graphicElement.GetDefinition() as CIMElement;
if (cimElement == null)
return;

// Get the current background symbol
var currentSymbol = graphicElement.GetGraphic().Symbol;

// Check if the symbol is a fill symbol
if (currentSymbol is CIMSymbolReference polygonSymbol)
{
// Change the fill color
polygonSymbol.Symbol = SymbolFactory.Instance
.ConstructPolygonSymbol(ColorFactory.Instance.CreateColor(newColor));

// Apply the modified symbol back to the GraphicElement
graphicElement.GetGraphic().Symbol = polygonSymbol;

graphicElement.SetGraphic(graphicElement.GetGraphic());
}

// Apply the changes back to the graphic element
graphicElement.SetDefinition(cimElement);
});

0 Kudos
CharlesMacleod
Esri Regular Contributor

No. This is the problem:

graphicElement.GetGraphic().Symbol = polygonSymbol;

The CIM uses a transactional pattern. You get a local copy, not a reference, of the deserialized state that u can modify (as needed) Changes must be _applied_ back via a commit.

So, get the graphic (local copy) and _hold_ a reference (to that copy).

var graphic = graphicElement.GetGraphic();

Next, apply your change to the copy whose reference you are holding:

graphic.Symbol = polySymbol [or polySymbol.MakeSymbolReference() if u hv a symbol]

Now, commit the change(s) made to the local copy _back_

graphicElement.SetGraphic(graphic);

0 Kudos
CharlesMacleod
Esri Regular Contributor

Gurunara, if this answers your question please mark this post as the solution, thanks

 

0 Kudos
Gurunara
Occasional Contributor

Got it to work (based on feedback):

// Check if the symbol is a fill symbol
if (currentSymbol is CIMSymbolReference polygonSymbol)
{
// Change the fill color
polygonSymbol.Symbol = SymbolFactory.Instance
.ConstructPolygonSymbol(ColorFactory.Instance.CreateColor(newColor));

var graphic = graphicElement.GetGraphic();

graphic.Symbol = polygonSymbol;

// Apply the changes back to the graphic element
graphicElement.SetGraphic(graphic);
}

0 Kudos