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


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

How to modify a specific vertex 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. Each segment has a from point and a to point.

Modifying a specific vertex of a polyline

To modify a vertex of a polyline, follow these steps:
  1. Cast IGeometryCollection to IPolyline.
  2. Cast the polyline to IHitTest and use the HitTest method (esriGeometryPartVertex 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 IPointCollection.
  5. Use the point property with the vertex (segment) index returned by HitTest (get a copy of the point).
  6. Modify the point using any method.
  7. Update the point using UpdatePoint with the vertex index from HitTest.
In the following code example, the HitTest method of the IHitTest interface is used to locate the indexes of the part and the vertex that need to be modified. When found, ITransform2D is used to modify the coordinate of the vertex.
[VB.NET]
Public Function modifyFirstVertexOfAPolyline(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.IPointCollection4
    
    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 pointCollection As ESRI.ArcGIS.Geometry.IPointCollection4=CType(geometry, ESRI.ArcGIS.Geometry.IPointCollection4)
        Dim transformPoint As ESRI.ArcGIS.Geometry.IPoint=pointCollection.Point(hitSegmentIndex)
        
        Dim transform2D As ESRI.ArcGIS.Geometry.ITransform2D=CType(transformPoint, ESRI.ArcGIS.Geometry.ITransform2D)
        transform2D.Move(offsetX, offsetY)
        
        Dim afterMovePoint As ESRI.ArcGIS.Geometry.IPoint=CType(transform2D, ESRI.ArcGIS.Geometry.IPoint)
        
        'The point is not updated in the polyline until the next line is called.
        pointCollection.UpdatePoint(hitSegmentIndex, CType(transform2D, ESRI.ArcGIS.Geometry.IPoint))
        
        Return pointCollection
    End If
    
    Return Nothing
    
End Function
[C#]
public ESRI.ArcGIS.Geometry.IPointCollection4 modifyFirstVertexOfAPolyline
    (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.IPointCollection4 pointCollection=
            (ESRI.ArcGIS.Geometry.IPointCollection4)geometry;
        ESRI.ArcGIS.Geometry.IPoint transformPoint=pointCollection.get_Point
            (hitSegmentIndex);

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

        ESRI.ArcGIS.Geometry.IPoint afterMovePoint=(ESRI.ArcGIS.Geometry.IPoint)
            transform2D;

        //The point is not updated in the polyline until the next line is called.
        pointCollection.UpdatePoint(hitSegmentIndex, (ESRI.ArcGIS.Geometry.IPoint)
            transform2D);

        return pointCollection;
    }

    return null;

}