Add point to created layer C#

5292
2
Jump to solution
12-01-2013 11:44 AM
BenasGircys
New Contributor
Hi, I developing a tool to create new layer and save points in this layer when clicked to map. Now I can create new layer or add point, both not working and I am wondering, maybe someone could help me.
My code:
here I get coordinates and draw point.
  ESRI.ArcGIS.Display.IDisplayTransformation displayTransformation = screenDisplay.DisplayTransformation;             ESRI.ArcGIS.Geometry.IPoint point = displayTransformation.ToMapPoint(x, y);             screenDisplay.DrawPoint(point);

then I create element, layer, add element to layer and add layer to map.
        IElement elem = null;         elem = point as IElement;         IMap map = activeView.FocusMap;          IGraphicsLayer graphicsLayer;         graphicsLayer = new CompositeGraphicsLayerClass();         ((ILayer)graphicsLayer).Name = "New Layer";         (graphicsLayer as IGraphicsContainer).AddElement(elem, 0);         map.AddLayer(graphicsLayer as ILayer);


I have example code which do the same but create polygon.
ESRI.ArcGIS.Display.IRubberBand rubberBand = new ESRI.ArcGIS.Display.RubberPolygonClass();             ESRI.ArcGIS.Geometry.IGeometry geometry = rubberBand.TrackNew(screenDisplay, symbol);             screenDisplay.SetSymbol(symbol);             screenDisplay.DrawPolygon(geometry);             screenDisplay.FinishDrawing();              IElement elem = null;             IPolygonElement polygonElement = new PolygonElementClass();             elem = polygonElement as IElement;             elem.Geometry = geometry as ESRI.ArcGIS.Geometry.IGeometry;              IMap map = activeView.FocusMap;              IGraphicsLayer graphicsLayer;             if (map.LayerCount == 0)             {                 graphicsLayer = new CompositeGraphicsLayerClass();                 ((ILayer)graphicsLayer).Name = "New Layer";                 (graphicsLayer as IGraphicsContainer).AddElement(elem, 0);                 map.AddLayer(graphicsLayer as ILayer);             }             else             {                 ILayer layer = map.get_Layer(0);                 graphicsLayer = layer as IGraphicsLayer;                 (graphicsLayer as IGraphicsContainer).AddElement(elem, 0);             }              activeView.Refresh();


So maybe someone knows how to solve my problem?
0 Kudos
1 Solution

Accepted Solutions
ModyBuchbinder
Esri Regular Contributor
Hi

If you need to save it into a feature class you must create a feature class (there is a snippet for this) and use cursor to add the point.
If you need a graphic I do not think the line: "elem = point as IElement;" works (elem will be null).
It should be
IElement elem = new MarkerElementClass();
elem.geometry = point;

ArcMap have a default symbol but you better set a marker symbol too for the element.
Have Fun
Mody

View solution in original post

0 Kudos
2 Replies
ModyBuchbinder
Esri Regular Contributor
Hi

If you need to save it into a feature class you must create a feature class (there is a snippet for this) and use cursor to add the point.
If you need a graphic I do not think the line: "elem = point as IElement;" works (elem will be null).
It should be
IElement elem = new MarkerElementClass();
elem.geometry = point;

ArcMap have a default symbol but you better set a marker symbol too for the element.
Have Fun
Mody
0 Kudos
BenasGircys
New Contributor
Hi

If you need to save it into a feature class you must create a feature class (there is a snippet for this) and use cursor to add the point.
If you need a graphic I do not think the line: "elem = point as IElement;" works (elem will be null).
It should be
IElement elem = new MarkerElementClass();
elem.geometry = point;

ArcMap have a default symbol but you better set a marker symbol too for the element.
Have Fun
Mody


Thanks a lot! It works 🙂
0 Kudos