Select to view content in your preferred language

Converting from GraphicsContainer to Polygon Feature

1212
1
10-19-2016 06:40 AM
ScottHall1
New Contributor

I have a problem with ArcObjects trying to convert a graphics container filled with segments into a polygon to insert into a feature class. In order to make the jump from a graphics element to a polygon, I believe I have to go from an element to a segment collection that can be inserted into a feature class.

My routine can insert the segments, but the feature class creates a polygon that is missing segments from the beginning or end of the object chain.

        public void FinalizeObject(IFeatureLayer layer, IGraphicsContainer graphicsContainer)
        {
            ISegmentCollection segmentCollection = new PolygonClass() as ISegmentCollection;

            try
            {
                graphicsContainer.Reset();
                IElement element = graphicsContainer.Next();
                ISegmentCollection tmp = new PolygonClass();

                while (element != null)
                {
                    IGeometry geometry = element.Geometry;
                    ISegmentCollection tempClass = new PolygonClass() as ISegmentCollection;
                    tempClass = geometry as ISegmentCollection;

                    if (tempClass != null && tempClass.SegmentCount > 0)
                    {
                        for (int x = 0; x < tempClass.SegmentCount; x++)
                        {
                            ISegment segment = tempClass.Segment[x];
                            segmentCollection.AddSegment(segment);
                            segmentCollection.SegmentsChanged();
                        }
                    }
                    element = graphicsContainer.Next();
                }

                IFeatureClass featureClass = layer.FeatureClass;
                IFeature feature = featureClass.CreateFeature();
                feature.Shape = (IPolygon)segmentCollection;
                feature.Store();
            }
            catch (NullReferenceException nre)
            {
                MessageBox.Show(nre.ToString());
            }
        }

I am breaking it down and piecing it back together because I don't know if there is a way to take the entirety of a graphics container as a single usable object, hence having to loop through the container and piecing it into a segment collection.

What am I missing? Thank you for any help.

0 Kudos
1 Reply
DuncanHornby
MVP Notable Contributor

Can you amend your question and show some example images of the sorts of graphics you are trying to convert into polygons in a featureclass.

0 Kudos