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?