.NET Modify Z or M without modifying curves

Discussion created by rfairhur24 on Oct 28, 2011
Latest reply on Oct 30, 2011 by rfairhur24
I am trying to modify Z or M values of a polyline that has nonlinear segments, such as circular arcs, eliptical arcs, and bezier curves, without converting the line to linear segments.  Densifying the curve into line segments is not an option.  My code below seems to work until I try to update the feature shape with the modified geometry.  At that point ArcMap crashes (I know bad error handling, but an error clearly is occurring the first time that code section runs and I need to overcome it).  The feature I am trying to modify is an ellipse with one part and one segment, just to see if I can get this to work at all. I'll worry about multiple parts and segments later.  Any ideas on how to properly use the ISegmentCollection or possibly the IGeometryCollection objects to achieve what I want?  There is very little documentation of these objects.

' Assume I already have a valid Polyline Featurelayer that is Z and M Aware called pFLayer
        Dim pFC As ESRI.ArcGIS.geodatabase.IFeatureClass
        pFC = pFLayer.FeatureClass ' Get FeatureClass of FeatureLayer
        Dim pFCursor As ESRI.ArcGIS.Geodatabase.IFeatureCursor
        pFCursor = pFC.Update(Nothing, False) ' Create an UpdateCursor on FeatureClass
        Dim pFeat As ESRI.ArcGIS.Geodatabase.IFeature
        pFeat = pFCursor.NextFeature ' Get First Feature
        Dim pPolyline As IPolyline = pFeat.ShapeCopy
        Dim pSegColl As ESRI.ArcGIS.Geometry.ISegmentCollection
        Dim Nonlinear As Boolean = False
        Dim SegCount As Long = 0
        Dim pInPoint As IPoint ' Current Point to process
        Dim distAlong As Double ' 3D Distance along the curve
        Dim j As Long = 0
        Dim pSegment As ISegment
        Dim NewSegments As ESRI.ArcGIS.Geometry.ISegmentCollection = New Polyline

        Do While Not pFeat Is Nothing
            pPolyline = New Polyline
            pPolyline.SpatialReference = pFeat.Shape.SpatialReference ' Make sure Polyline has Spatial Reference
            pPolyline = pFeat.Shape ' Assign geometry to Polyline
            pSegColl = pPolyline
            If Not Nonlinear Then
                ' All Segments are linear and code works without a problem so it is not included
                ' One or more segments of the polyline is Nonlinear
                SegCount = pSegColl.SegmentCount
                NewSegments = New Polyline
                distAlong = 0
                For j = 0 To SegCount - 1 ' read all segments
                    pSegment = pSegColl.Segment(j)
                    pInPoint = New Point
                    pInPoint.SpatialReference = pFeat.Shape.SpatialReference
                    pInPoint = pSegment.FromPoint
                    pInPoint.M = distAlong
                    pSegment.FromPoint = pInPoint
                    distAlong = Math.Sqrt(pSegment.Length ^ 2 + (pSegment.FromPoint.Z - pSegment.ToPoint.Z) ^ 2) + distAlong
                    pInPoint = New Point
                    pInPoint.SpatialReference = pFeat.Shape.SpatialReference
                    pInPoint = pSegment.ToPoint
                    pInPoint.M = distAlong
                    pSegment.ToPoint = pInPoint ' Modify ToPoint to add M value
                    MsgBox("Segment To Point M Value is " & pSegment.ToPoint.M & " pInPoint M Value is " & pInPoint.M & " and Distance Along is " & distAlong) ' Verify everything updated
                    NewSegments.AddSegment(pSegment) ' Append segment to another segmentCollection
                Next j
                pPolyline.SpatialReference = pFeat.Shape.SpatialReference
                pPolyline = CType(NewSegments, ESRI.ArcGIS.Geometry.Polyline)
                If pPolyline Is Nothing Then
                    MsgBox("Bad Segments to Polyline")
                    MsgBox("To Point M Value is " & NewSegments.Segment(0).ToPoint.M & " Geometrytype is " & NewSegments.Segment(0).GeometryType) ' Verify that NewSegment is a curve GeometryType still. It says it is an Ellipse Geometry with updated M values and everything seems to be working.
                End If
                pFeat.Shape = NewSegments ' Code causes ArcMap to crash here on the first nonlinear feature.  Also tried "pFeat.Shape = pPolyline" with same result
                pFCursor.UpdateFeature(pFeat) ' Update the cursor
            End If
            pFeat = pFCursor.NextFeature
            pPolyline = Nothing