1

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();


    }
  • This is a very old question but I think it would still benefit from having the "answer in question" extracted out to be a true answer. Note that the question body left as a result would be too brief by today's GIS SE standards so maybe that could be expanded a little too. – PolyGeo Jul 26 '15 at 21:32
2

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.