Create a graphic with name

881
4
Jump to solution
10-06-2020 06:39 AM
ModyBuchbinder
Esri Regular Contributor

Hi All

I am trying to create a graphic element with code and then to get the element by the name.

Code example below.

The FindElement does not find anything.

Open the element in Pro show that the name is still the default.

In all the samples of FindElement it looks by default names (Point, Point 1, etc).

Is it a bug??

Thanks

Mody

QueuedTask.Run(() =>
{
//Place symbol in the center of the map
var extent = MapView.Active.Extent;
var location = extent.Center;

//specify a symbol
var pt_symbol = SymbolFactory.Instance.ConstructPointSymbol(
ColorFactory.Instance.GreenRGB);

//create a CIMGraphic
var graphic = new CIMPointGraphic()
{
Symbol = pt_symbol.MakeSymbolReference(),
Location = location, //center of map
};
graphic.Name = "Mody";
graphicsLayer.AddElement(graphic);
var elems = graphicsLayer.FindElements(new List<string>() { "Mody"})[0];

});

0 Kudos
1 Solution

Accepted Solutions
UmaHarano
Esri Regular Contributor

Hi Mody,

This does look like a bug. Thanks for reporting it.

As a workaround, can you please try naming the element after creating it? That works for me.

      var graphicsLayer = MapView.Active?.Map.TargetGraphicsLayer;
      if (graphicsLayer == null) return;
      QueuedTask.Run(() =>
      {
        //Place symbol in the center of the map
        var extent = MapView.Active.Extent;
        var location = extent.Center;
        //specify a symbol
        var pt_symbol = SymbolFactory.Instance.ConstructPointSymbol(
        ColorFactory.Instance.GreenRGB);
        //create a CIMGraphic 
        var graphic = new CIMPointGraphic()
        {
          Symbol = pt_symbol.MakeSymbolReference(),
          Location = location, //center of map
        };
        graphic.Name = "MyCoolPoint";
        graphicsLayer.AddElement(graphic);
        var listOfElemToFind = new List<string>
        {
          "MyCoolPoint"
        };
        //This returns null
        var elems = graphicsLayer.FindElements(listOfElemToFind);
        //And so does this one.
        var elem = graphicsLayer.FindElement("MyCoolPoint");
        //The trick is to find the element you just added.
        var ptElem = graphicsLayer.GetElements().FirstOrDefault();
        //Rename now
        ptElem.SetName("MyCoolPoint");
        //This works now
        elems = graphicsLayer.FindElements(listOfElemToFind);
        //This works now
        elem = graphicsLayer.FindElement("MyCoolPoint");‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

0 Kudos
4 Replies
UmaHarano
Esri Regular Contributor

Hi Mody,

This does look like a bug. Thanks for reporting it.

As a workaround, can you please try naming the element after creating it? That works for me.

      var graphicsLayer = MapView.Active?.Map.TargetGraphicsLayer;
      if (graphicsLayer == null) return;
      QueuedTask.Run(() =>
      {
        //Place symbol in the center of the map
        var extent = MapView.Active.Extent;
        var location = extent.Center;
        //specify a symbol
        var pt_symbol = SymbolFactory.Instance.ConstructPointSymbol(
        ColorFactory.Instance.GreenRGB);
        //create a CIMGraphic 
        var graphic = new CIMPointGraphic()
        {
          Symbol = pt_symbol.MakeSymbolReference(),
          Location = location, //center of map
        };
        graphic.Name = "MyCoolPoint";
        graphicsLayer.AddElement(graphic);
        var listOfElemToFind = new List<string>
        {
          "MyCoolPoint"
        };
        //This returns null
        var elems = graphicsLayer.FindElements(listOfElemToFind);
        //And so does this one.
        var elem = graphicsLayer.FindElement("MyCoolPoint");
        //The trick is to find the element you just added.
        var ptElem = graphicsLayer.GetElements().FirstOrDefault();
        //Rename now
        ptElem.SetName("MyCoolPoint");
        //This works now
        elems = graphicsLayer.FindElements(listOfElemToFind);
        //This works now
        elem = graphicsLayer.FindElement("MyCoolPoint");‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
ModyBuchbinder
Esri Regular Contributor

Hi Uma

Thanks for the answer and code. I will try it.

What will happened if I add a few elements one by one?

Will GetElements().FirstOrDefault() always return the last element added?

If it is true then I need to do a loop with AddElement, GetElements().FirstOrDefault and SetName - for each element I would like to add - correct

Thanks

Mody

0 Kudos
UmaHarano
Esri Regular Contributor

Hi,

Here is the screenshot of the items in my GraphicsLayer:

The order I added the points are like this:

1. Point was added first

2. MyCoolPoint was added second.

Thanks!

Uma

0 Kudos
UmaHarano
Esri Regular Contributor

Hi Mody,

This issue is now fixed in ArcGIS Pro 2.7, which is the next release of Pro that will be out.

Thanks again for reporting this issue.

Uma

0 Kudos