How do I convert a Map or FeatureLayer to graphics on ArcObjects 10.1?
Problem Solved
this is the working code:
private void Convert_to_graphics( IMapDocument doc)
{
IMap pmap;
IFeature pFeature;
IElement pElement = null;
IMarkerElement pp = new MarkerElementClass();
IGraphicsContainer pGraphicsContainer;
IActiveView pActiveView;
pmap = doc.get_Map(0);
pActiveView = doc.ActiveView;
//pGraphicsContainer = doc.ActiveView.GraphicsContainer;
int[] array = {8 , 7 , 6 , 5 , 1}; //layers indexes ,
pGraphicsContainer = (IGraphicsContainer)pmap;
foreach (var i in array)
{
IGeoFeatureLayer fl = (IGeoFeatureLayer)pmap.get_Layer(i);
IFeatureClass fc = fl.FeatureClass;
IFeatureCursor fcur = fc.Search(null, false);
IFeatureRenderer fr = fl.Renderer;
pFeature = fcur.NextFeature() ;
while (pFeature != null)
{
if (pFeature.Shape.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)
{
IMarkerElement pmkElement = new MarkerElementClass();
ISymbol sym = fr.get_SymbolByFeature(pFeature);
pmkElement.Symbol = (IMarkerSymbol)sym;
pElement = (IElement)pmkElement;
}
if (pFeature.Shape.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline)
{
ILineElement pmkElement = new LineElementClass();
pmkElement.Symbol = (ILineSymbol)fr.get_SymbolByFeature(pFeature);
pElement = (IElement)pmkElement;
}
if (pFeature.Shape.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
{
IFillShapeElement pmkElement = new PolygonElementClass();
pmkElement.Symbol = (IFillSymbol)fr.get_SymbolByFeature(pFeature);
pElement = (IElement)pmkElement;
}
if (pElement != null)
{
pElement.Geometry = pFeature.Shape;
pGraphicsContainer.AddElement(pElement, 0);
}
pFeature = fcur.NextFeature();
}
}
pActiveView.Refresh();
}
Best Answer
Here's a Convert Features to Graphic Elements VBA code sample. I would modify it so that the element's symbol gets set to whatever is returned by IFeatureRenderer.SymbolByFeature. The interface used to set the symbol will depend on the underlying coclass (for PolygonElement use IFillShapeElement).