0

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

This is code I have worked up so far:

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

        try
        {
            graphicsContainer.Reset();
            IElement element = graphicsContainer.Next();

            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;
        }
        catch (NullReferenceException nre)
        {
            MessageBox.Show(nre.ToString());
        }
    }

What am I missing?

  • If your element has a valid Geometry, why break it down and build it back up? Have you tried: IPolygon thePolygon = element.Geometry as IPolygon ? Then just create your feature and do feature.shape = thePolygon – Brad Oct 18 '16 at 15:28
  • Also, I think you need to call feature.Store() after you set feature.Shape. – Dan Jurgella Oct 18 '16 at 19:19
  • @Brad, 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. – user66338 Oct 19 '16 at 13:33
1

Try this:

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

    try
    {
        graphicsContainer.Reset();
        IElement element = graphicsContainer.Next();
        IFeatureClass featureClass = layer.FeatureClass;

        IGeometry geometry = null;
        while (element != null)
        {
            geometry = element.Geometry;
            IFeature feature = featureClass.CreateFeature();
            feature.Shape = geometry;
            feature.Store();
            element = graphicsContainer.Next();
        }
    }
    catch (NullReferenceException nre)
    {
        MessageBox.Show(nre.ToString());
    }
}
  • This results in a "No support for this geometry type" error at the line feature.Shape = geometry;. – user66338 Oct 19 '16 at 16:47
  • @shall71 is your feature class point, polyline or polygon? Is the graphic point, polyline or polygon? – Brad Oct 19 '16 at 17:49
  • It is a polygon feature class and the graphics container is polyline. – user66338 Oct 19 '16 at 18:13
  • So you cannot store a polyline geometry in a polygon feature class. I can see why you were trying to re-create it. If your polyline's To and From points are the same, you could break it down into its segments and build a polygon from that. If not, you can look into IPolygon.Close to add the final segment for you. – Brad Oct 19 '16 at 18:23
1

you are not validating that your segments are indeed connected to each others, in the order that you read them. Moreover you are not considering digitization direction. Depending on your data / storage, you may also want to check that the resulting polygon is not multi part.

The doc says "Adding segments to a polygon defined as a SegmentCollection [..] assumes that the polygon is a single ring of connected segments."

You may want to read the graphic container, save each segment in a list. Starting with a random segment (the 1st one to make it easy), you take the TO point coordinates. Then you search in your list the segment having a FROM coordinate equal to the value you just recorded, then read its TO coordinate and continue. If no segment is found, you then check if one has the same TO coordinate (so segment 1 TO = segment X TO). When found, you then reverse its direction and proceed as described above. Make sure that any - and every - segment is used only once (hence the use of a list, allowing you to remove items as they are consumed)

Your Answer

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