This document is archived and information here might be outdated.  Recommended version.


How to modify a specific segment of a polyline (ArcObjects .NET 10.5 SDK)

How to modify a specific segment of a polyline


Summary
A polyline is composed of one or more paths (parts), and a path (part) is formed of one or more segments.

Modifying a specific segment of a polyline

To modify a polyline segment, follows these steps:
  1. Cast IGeometryCollection to the polyline.
  2. Cast the polyline to IHitTest and use the HitTest method (esriGeometryPartBoundary option).
  3. Use the geometry property with the part index returned by the HitTest method to get the path containing the point to modify.
  4. Cast the path to ISegmentCollection.
  5. Use the segment property with the segment index returned by HitTest (get a reference to the segment).
  6. Modify the segment.
In the following code example, the HitTest method of the IHitTest interface is used to locate the indexes of the part and the segment that need to be modified. When found, ITransform2D is used to move the segment.
[VB.NET]
Public Function ModifyFirstSegmentOfAPolyline(ByVal geometryCollection_Polyline As ESRI.ArcGIS.Geometry.IGeometryCollection, ByVal searchRadius As System.Double, ByVal offsetX As System.Double, ByVal offsetY As System.Double) As ESRI.ArcGIS.Geometry.ISegmentCollection
    
    Dim polyline As ESRI.ArcGIS.Geometry.IPolyline=CType(geometryCollection_Polyline, ESRI.ArcGIS.Geometry.IPolyline)
    Dim queryPoint As ESRI.ArcGIS.Geometry.IPoint=polyline.FromPoint
    
    Dim hitPoint As ESRI.ArcGIS.Geometry.IPoint=New ESRI.ArcGIS.Geometry.PointClass
    
    'Define and initialize the variables that will get populated from the .HitTest() method.
    Dim hitDistance As System.Double=0
    Dim hitPartIndex As System.Int32=0
    Dim hitSegmentIndex As System.Int32=0
    Dim rightSide As System.Boolean=False
    
    Dim hitTest As ESRI.ArcGIS.Geometry.IHitTest=CType(geometryCollection_Polyline, ESRI.ArcGIS.Geometry.IHitTest)
    Dim foundGeometry As System.Boolean=hitTest.HitTest(queryPoint, searchRadius, ESRI.ArcGIS.Geometry.esriGeometryHitPartType.esriGeometryPartVertex, hitPoint, hitDistance, hitPartIndex, hitSegmentIndex, rightSide)
    
    If foundGeometry=True Then
        Dim geometry As ESRI.ArcGIS.Geometry.IGeometry=geometryCollection_Polyline.Geometry(hitPartIndex)
        Dim segmentCollection As ESRI.ArcGIS.Geometry.ISegmentCollection=CType(geometry, ESRI.ArcGIS.Geometry.ISegmentCollection)
        Dim segment As ESRI.ArcGIS.Geometry.ISegment=segmentCollection.Segment(hitSegmentIndex)
        
        Dim transform2D As ESRI.ArcGIS.Geometry.ITransform2D=CType(segment, ESRI.ArcGIS.Geometry.ITransform2D)
        transform2D.Move(offsetX, offsetY)
        
        'Call the next line to update the polyline.
        segmentCollection.SegmentsChanged()
        
        Return segmentCollection
        
    End If
    
    Return Nothing
    
End Function
[C#]
public ESRI.ArcGIS.Geometry.ISegmentCollection ModifyFirstSegmentOfAPolyline
    (ESRI.ArcGIS.Geometry.IGeometryCollection geometryCollection_Polyline,
    System.Double searchRadius, System.Double offsetX, System.Double offsetY)
{
    ESRI.ArcGIS.Geometry.IPolyline polyline=(ESRI.ArcGIS.Geometry.IPolyline)
        geometryCollection_Polyline;
    ESRI.ArcGIS.Geometry.IPoint queryPoint=polyline.FromPoint;
    ESRI.ArcGIS.Geometry.IPoint hitPoint=new ESRI.ArcGIS.Geometry.PointClass();

    //Define and initialize the variables that will get populated from the .HitTest() method.
    System.Double hitDistance=0;
    System.Int32 hitPartIndex=0;
    System.Int32 hitSegmentIndex=0;
    System.Boolean rightSide=false;

    ESRI.ArcGIS.Geometry.IHitTest hitTest=(ESRI.ArcGIS.Geometry.IHitTest)
        geometryCollection_Polyline;
    System.Boolean foundGeometry=hitTest.HitTest(queryPoint, searchRadius,
        ESRI.ArcGIS.Geometry.esriGeometryHitPartType.esriGeometryPartVertex,
        hitPoint, ref hitDistance, ref hitPartIndex, ref hitSegmentIndex, ref
        rightSide);

    if (foundGeometry == true)
    {
        ESRI.ArcGIS.Geometry.IGeometry geometry =
            geometryCollection_Polyline.get_Geometry(hitPartIndex);
        ESRI.ArcGIS.Geometry.ISegmentCollection segmentCollection=
            (ESRI.ArcGIS.Geometry.ISegmentCollection)geometry;
        ESRI.ArcGIS.Geometry.ISegment segment=segmentCollection.get_Segment
            (hitSegmentIndex);

        ESRI.ArcGIS.Geometry.ITransform2D transform2D=
            (ESRI.ArcGIS.Geometry.ITransform2D)segment;
        transform2D.Move(offsetX, offsetY);

        //Call the next line to update the polyline.
        segmentCollection.SegmentsChanged();

        return segmentCollection;

    }

    return null;

}