varmap=MapView.Active.Map;if(map.MapType!=MapType.Map)return;// not 2Dvargl_param=newGraphicsLayerCreationParams{Name="Graphics Layer"};QueuedTask.Run(()=>{//By default will be added to the top of the TOCvargraphicsLayer=LayerFactory.Instance.CreateLayer<ArcGIS.Desktop.Mapping.GraphicsLayer>(gl_param,map);//Add to the bottom of the TOCLayerFactory.Instance.CreateLayer<ArcGIS.Desktop.Mapping.GraphicsLayer>(gl_param,map,LayerPosition.AddToBottom);//Add a graphics layer to a group layer...vargroup_layer=map.GetLayersAsFlattenedList().OfType<GroupLayer>().First();LayerFactory.Instance.CreateLayer<ArcGIS.Desktop.Mapping.GraphicsLayer>(gl_param,group_layer);//TODO...use the graphics layer });
Accessing GraphicsLayer
//get the first graphics layer in the map's collection of graphics layersvargraphicsLayer=map.GetLayersAsFlattenedList().OfType<ArcGIS.Desktop.Mapping.GraphicsLayer>().FirstOrDefault();if(graphicsLayer!=null){//TODO...use the graphics layer }
Copy Graphic elements
//on the QueuedTaskvarelems=sourceGraphicsLayer.FindElements(newList<string>(){"Point 1","Line 3","Text 1"});varcopiedElements=targetGraphicsLayer.CopyElements(elems);
Remove Graphic elements
//on the QueuedTask graphicsLayer.RemoveElements(graphicsLayer.GetSelectedElements());
Create Graphic Elements
Graphic Element using CIMGraphic
vargraphicsLayer=MapView.Active.Map.GetLayersAsFlattenedList().OfType<ArcGIS.Desktop.Mapping.GraphicsLayer>().FirstOrDefault();if(graphicsLayer==null)return;QueuedTask.Run(()=>{//Place symbol in the center of the mapvarextent=MapView.Active.Extent;varlocation=extent.Center;//specify a symbolvarpt_symbol=SymbolFactory.Instance.ConstructPointSymbol(ColorFactory.Instance.GreenRGB);//create a CIMGraphic vargraphic=newCIMPointGraphic(){Symbol=pt_symbol.MakeSymbolReference(),Location=location//center of map};graphicsLayer.AddElement(graphic);});
Graphic Element using CIMSymbol
vargraphicsLayer=MapView.Active.Map.GetLayersAsFlattenedList().OfType<ArcGIS.Desktop.Mapping.GraphicsLayer>().FirstOrDefault();if(graphicsLayer==null)return;QueuedTask.Run(()=>{//Place symbol in the center of the mapvarextent=MapView.Active.Extent;varlocation=extent.Center;//specify a symbolvarpt_symbol=SymbolFactory.Instance.ConstructPointSymbol(ColorFactory.Instance.GreenRGB);graphicsLayer.AddElement(location,pt_symbol);});
Text Graphic Element
vargraphicsLayer=MapView.Active.Map.GetLayersAsFlattenedList().OfType<ArcGIS.Desktop.Mapping.GraphicsLayer>().FirstOrDefault();if(graphicsLayer==null)return;QueuedTask.Run(()=>{//Place symbol in the center of the mapvarextent=MapView.Active.Extent;varlocation=extent.Center;//specify a text symbolvartext_symbol=SymbolFactory.Instance.ConstructTextSymbol(ColorFactory.Instance.BlackRGB,8.5,"Corbel","Regular");graphicsLayer.AddElement(location,"Text Example",text_symbol);});
Bulk Graphics creation
//Point Feature layer to convert into graphicsvarlyr=MapView.Active?.Map?.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault();//Graphics layer to store the graphics tovargl=MapView.Active.Map.GetLayersAsFlattenedList().OfType<ArcGIS.Desktop.Mapping.GraphicsLayer>().FirstOrDefault();if(lyr==null)return;QueuedTask.Run(()=>{//Point symbol for graphicsvarpointSymbol=SymbolFactory.Instance.ConstructPointSymbol(CIMColor.CreateRGBColor(100,255,40),10,SimpleMarkerStyle.Circle);//Collection to hold the point graphicsvarlistGraphicElements=newList<CIMGraphic>();//Iterate through each point feature in the feature layerusing(RowCursorrows=lyr.Search())//execute{inti=0;while(rows.MoveNext()){using(varfeature=rows.CurrentasFeature){//Create a point graphic for the featurevarcrimePt=feature.GetShape()asMapPoint;if(crimePt!=null){varcimGraphicElement=newCIMPointGraphic{Location=crimePt,//MapPointSymbol=pointSymbol.MakeSymbolReference()};//Add the point feature to the collectionlistGraphicElements.Add(cimGraphicElement);i++;}}}}//Magic happens...Add all the features to the Graphics layer gl.AddElements(listGraphicElements);});
Graphic Elements Selection
Select Graphic Elements
vargraphicsLayer=MapView.Active.Map.GetLayersAsFlattenedList().OfType<ArcGIS.Desktop.Mapping.GraphicsLayer>().FirstOrDefault();if(graphicsLayer==null)return;varelements=graphicsLayer.GetElementsAsFlattenedList().Where(e =>e.Name.StartsWith("Text"));QueuedTask.Run(()=>{graphicsLayer.SelectElements(elements);//or select one elementgraphicsLayer.SelectElement(elements.FirstOrDefault());});
Find Graphic elements
//on the QueuedTask//Find elements by namevarelems=graphicsLayer.FindElements(newList<string>(){"Point 1","Line 3","Text 1"});//Find elements by type//Find all point graphics in the Graphics LayervarpointGraphics=graphicsLayer.GetElementsAsFlattenedList().Where(elem =>elem.GetGraphic()isCIMPointGraphic);//Find all line graphics in the Graphics LayervarlineGraphics=graphicsLayer.GetElementsAsFlattenedList().Where(elem =>elem.GetGraphic()isCIMLineGraphic);//Find all polygon graphics in the Graphics LayervarpolygonGraphics=graphicsLayer.GetElementsAsFlattenedList().Where(elem =>elem.GetGraphic()isCIMPolygonGraphic);//Find all text graphics in the Graphics LayervartextGraphics=graphicsLayer.GetElementsAsFlattenedList().Where(elem =>elem.GetGraphic()isCIMTextGraphic);//Find all picture graphics in the Graphics LayervarpictureGraphic=graphicsLayer.GetElementsAsFlattenedList().Where(elem =>elem.GetGraphic()isCIMPictureGraphic);
Spatial selection of elements in all Graphics Layers
//Map Tool is used to perform Spatial selection.//Graphic selection uses the selection geometry //to intersect the geometries of those elements (graphic or group) //that will be selected and then highlights them. protectedoverrideasyncTask<bool>OnSketchCompleteAsync(Geometrygeometry){varselPoly=geometryasPolygon;returnawaitQueuedTask.Run(()=>{//note: the selected elements may belong to more than one layer...varelems=MapView.Active.SelectElements(selPoly,SelectionCombinationMethod.New);returntrue;});}
Spatial selection of elements in one graphics layer
//on the QueuedTask//Create an extent to use for the spatial selectionvarextent=MapView.Active.Extent;varselectionExtent=extent.Expand(0.5,0.5,true);//Select elements in specified graphics layer using the selection extent.varselectedElements=MapView.Active.SelectElements(graphicsLayer,selectionExtent,SelectionCombinationMethod.Add);
vargraphicsLayer=MapView.Active.Map.GetLayersAsFlattenedList().OfType<ArcGIS.Desktop.Mapping.GraphicsLayer>().FirstOrDefault();if(graphicsLayer==null)return;//unselect the first element in the currently selected elementsvarelem=graphicsLayer.GetSelectedElements().FirstOrDefault();QueuedTask.Run(()=>{if(elem!=null)//Unselect one elementgraphicsLayer.UnSelectElement(elem);//unselect all elementsgraphicsLayer.UnSelectElements();//equivalent tographicsLayer.ClearSelection();});
Graphic Element Events
Subscribe to ElementSelectionChangedEvent
ArcGIS.Desktop.Mapping.Events.ElementSelectionChangedEvent.Subscribe((args)=>{//check if the container is a graphics layer - could be a Layout (or even map view)if(args.ElementContainerisArcGIS.Desktop.Mapping.GraphicsLayergraphicsLayer){//get the total selection count for the containervarcount=args.SelectedElementCount;//Check count - could have been an unselect or clearselectif(count>0){//this is a selection or add to selectionvarelems=graphicsLayer.GetSelectedElements();//TODO process the selection...}else{//This is an unselect or clear selection//TODO process the unselect or clear select}}});
Grouping and Ordering Graphic Elements
Group Graphic Elements
vargraphicsLayer=MapView.Active.Map.GetLayersAsFlattenedList().OfType<ArcGIS.Desktop.Mapping.GraphicsLayer>().FirstOrDefault();if(graphicsLayer==null)return;varelemsToGroup=graphicsLayer.GetSelectedElements();//Note: run within the QueuedTask//group elementsvargroupElement=graphicsLayer.GroupElements(elemsToGroup);
Un-Group Graphic Elements
varselectedElements=graphicsLayer.GetSelectedElements().ToList();;if(selectedElements?.Any()==false)//must be at least 1.return;varelementsToUnGroup=newList<GroupElement>();//All selected elements should be grouped.if(selectedElements.Count()==selectedElements.OfType<GroupElement>().Count()){//Convert to a GroupElement list.elementsToUnGroup=selectedElements.ConvertAll(x =>(GroupElement)x);}if(elementsToUnGroup.Count()==0)return;//UnGroupgraphicsLayer.UnGroupElements(elementsToUnGroup);
Parent of GroupElement
//check the parentvarparent=groupElement.Elements.First().GetParent();//will be the group element//top-most parentvartop_most=groupElement.Elements.First().GetParent(true);//will be the GraphicsLayer
Ordering: Send backward and Bring forward
//On the QueuedTask//get the current selection setvarsel_elems=graphicsLayer.GetSelectedElements();//can they be brought forward? This will also check that all elements have the same parentif(graphicsLayer.CanBringForward(sel_elems)){//bring forwardgraphicsLayer.BringForward(sel_elems);//bring to front (of parent)//graphicsLayer.BringToFront(sel_elems);}elseif(graphicsLayer.CanSendBackward(sel_elems)){//send backgraphicsLayer.SendBackward(sel_elems);//send to the back (of parent)//graphicsLayer.SendToBack(sel_elems);}
Get Z-Order
varselElementsZOrder=graphicsLayer.GetSelectedElements();//list out the z orderforeach(vareleminselElementsZOrder)System.Diagnostics.Debug.WriteLine($"{elem.Name}: z-order {elem.GetZOrder()}");
Modifying Graphic Elements
Move Graphic Elements
//Each selected element will move to a set distance to the upper right.varselElements=graphicsLayer.GetSelectedElements();if(selElements.Count==0)return;//Move the element upforeach(varselElementinselElements){//Get the element's boundsvarelementPoly=PolygonBuilder.CreatePolygon(selElement.GetBounds());//get the coordinates of the element bounding envelope.varpointsList=elementPoly.Copy2DCoordinatesToList();//Move the element's Anchor point to the upper right.selElement.SetAnchorPoint(pointsList[1]);}
Modify symbology of a Graphic Element
//within a queued Task//get the first line element in the layervarge=graphicsLayer.FindElement("Line 10")asGraphicElement;vargraphic=ge.GetGraphic();if(graphicisCIMLineGraphiclineGraphic){//change its symbollineGraphic.Symbol=SymbolFactory.Instance.ConstructLineSymbol(SymbolFactory.Instance.ConstructStroke(ColorFactory.Instance.BlueRGB,2,SimpleLineStyle.DashDot)).MakeSymbolReference();//apply the changege.SetGraphic(lineGraphic);}