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.
<SPAN class="keyword token">public</SPAN> <SPAN class="keyword token">void</SPAN> <SPAN class="token function">FinalizeObject</SPAN><SPAN class="punctuation token">(</SPAN>IFeatureLayer layer<SPAN class="punctuation token">,</SPAN> IGraphicsContainer graphicsContainer<SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
ISegmentCollection segmentCollection <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">PolygonClass</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> ISegmentCollection<SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">try</SPAN>
<SPAN class="punctuation token">{</SPAN>
graphicsContainer<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Reset</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
IElement element <SPAN class="operator token">=</SPAN> graphicsContainer<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Next</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
ISegmentCollection tmp <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">PolygonClass</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">while</SPAN> <SPAN class="punctuation token">(</SPAN>element <SPAN class="operator token">!=</SPAN> <SPAN class="keyword token">null</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
IGeometry geometry <SPAN class="operator token">=</SPAN> element<SPAN class="punctuation token">.</SPAN>Geometry<SPAN class="punctuation token">;</SPAN>
ISegmentCollection tempClass <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">PolygonClass</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> ISegmentCollection<SPAN class="punctuation token">;</SPAN>
tempClass <SPAN class="operator token">=</SPAN> geometry <SPAN class="keyword token">as</SPAN> ISegmentCollection<SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>tempClass <SPAN class="operator token">!=</SPAN> <SPAN class="keyword token">null</SPAN> <SPAN class="operator token">&&</SPAN> tempClass<SPAN class="punctuation token">.</SPAN>SegmentCount <SPAN class="operator token">></SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">for</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">int</SPAN> x <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">;</SPAN> x <SPAN class="operator token"><</SPAN> tempClass<SPAN class="punctuation token">.</SPAN>SegmentCount<SPAN class="punctuation token">;</SPAN> x<SPAN class="operator token">++</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
ISegment segment <SPAN class="operator token">=</SPAN> tempClass<SPAN class="punctuation token">.</SPAN>Segment<SPAN class="punctuation token">[</SPAN>x<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">;</SPAN>
segmentCollection<SPAN class="punctuation token">.</SPAN><SPAN class="token function">AddSegment</SPAN><SPAN class="punctuation token">(</SPAN>segment<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
segmentCollection<SPAN class="punctuation token">.</SPAN><SPAN class="token function">SegmentsChanged</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">}</SPAN>
element <SPAN class="operator token">=</SPAN> graphicsContainer<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Next</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
IFeatureClass featureClass <SPAN class="operator token">=</SPAN> layer<SPAN class="punctuation token">.</SPAN>FeatureClass<SPAN class="punctuation token">;</SPAN>
IFeature feature <SPAN class="operator token">=</SPAN> featureClass<SPAN class="punctuation token">.</SPAN><SPAN class="token function">CreateFeature</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
feature<SPAN class="punctuation token">.</SPAN>Shape <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>IPolygon<SPAN class="punctuation token">)</SPAN>segmentCollection<SPAN class="punctuation token">;</SPAN>
feature<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Store</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">catch</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="token class-name">NullReferenceException</SPAN> nre<SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
MessageBox<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Show</SPAN><SPAN class="punctuation token">(</SPAN>nre<SPAN class="punctuation token">.</SPAN><SPAN class="token function">ToString</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> 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.