How to use the IEnumSegment methods


Using the IEnumSegment methods

Do the following steps to use the IEnumSegment methods:
  1. Create a polyline that contains two parts. Each part has four segments.
  2. Use ISegmentCollection to represent the multipart polyline.
  3. Set the enumerator of the segment collection.
  4. Call different methods on the segment enumerator to display the usage.
See the following code example:
[C#]
public void IEnumSegment_Example()
{
    ISegmentCollection segmentCollection = CreateMultipartPolyline()as
        ISegmentCollection;
    IEnumSegment enumSegment = segmentCollection.EnumSegments;
    //Reset the enumerator.
    enumSegment.Reset();
    //Get the first segment in the enumerator.
    ISegment segment;
    int partIndex = 0;
    int segmentIndex = 0;
    enumSegment.Next(out segment, ref partIndex, ref segmentIndex);
    //Clone the enumerator. This preserves the current location.
    IEnumSegment clonedEnumSegment;
    enumSegment.Clone(out clonedEnumSegment);
    //Move the original enumerator by one position and print the info about that segment.
    enumSegment.Next(out segment, ref partIndex, ref segmentIndex);
    System.Windows.Forms.MessageBox.Show("Segment type: " + segment.GeometryType + 
        " , Part Index: " + partIndex + " , Segment Index: " + segmentIndex);
    clonedEnumSegment.Next(out segment, ref partIndex, ref segmentIndex);
    //Move the cloned enumerator by one position and print the info about that segment.
    System.Windows.Forms.MessageBox.Show("Cloned Segment type: " +
        segment.GeometryType + " , Part Index: " + partIndex + " , Segment Index: " 
        + segmentIndex);
    //Check if recycling.
    System.Windows.Forms.MessageBox.Show("IsRecycling: " + enumSegment.IsRecycling);
    //Set the enumerator to segment three in part one.
    enumSegment.SetAt(1, 3);
    //Check that the enumerator is now located at the last segment of a part.
    System.Windows.Forms.MessageBox.Show("IsLastInPart : " +
        enumSegment.IsLastInPart());
    //Use the NextEx method to get more info about the current segment in the enumerator.
    String report = "***** NextEx ***** \n";
    esriSegmentInfo segmentInfo;
    enumSegment.NextEx(out segmentInfo);
    report = report + "Segment info - LastInPart: " + segmentInfo.bLastInPart + "\n"
        + "Segment info - Absolute Segment Index: " + segmentInfo.iAbsSegment + "\n"
        + "Segment info - Part Index: " + segmentInfo.iPart + "\n" + 
        "Segment info - Segment Index in current part (Relative) : " +
        segmentInfo.iRelSegment + "\n" + 
        "Segment info - Get the length of the segment: " +
        segmentInfo.pSegment.Length + "\n";
    enumSegment.Reset();
    //Set the enumerator to the last segment of the first part.
    enumSegment.NextInPart(out segment, ref segmentIndex);
    //Set the enumerator to the previous location.
    report = report + "***** Previous *****" + "\n";
    enumSegment.Next(out segment, ref partIndex, ref segmentIndex);
    report = report + "Segment type: " + segment.GeometryType + " , Part Index: " +
        partIndex + " , Segment Index: " + segmentIndex + "\n";
    enumSegment.Previous(out segment, ref partIndex, ref segmentIndex);
    report = report + "Segment type: " + segment.GeometryType + " , Part Index: " +
        partIndex + " , Segment Index: " + segmentIndex + "\n" + 
        "***** NextInPartEx *****" + "\n";
    enumSegment.NextInPartEx(out segmentInfo);
    report = report + "Segment info - LastInPart: " + segmentInfo.bLastInPart + "\n"
        + "Segment info - Absolute Segment Index: " + segmentInfo.iAbsSegment + "\n"
        + "Segment info - Part Index: " + segmentInfo.iPart + "\n" + 
        "Segment info - Segment Index in current part (Relative) : " +
        segmentInfo.iRelSegment + "\n" + 
        "Segment info - Get the length of the segment: " +
        segmentInfo.pSegment.Length + "\n";
    //Reset the enumerator to the end.
    enumSegment.ResetToEnd();
    enumSegment.Previous(out segment, ref partIndex, ref segmentIndex);
    report = report + "Segment type: " + segment.GeometryType + " , Part Index: " +
        partIndex + " , Segment Index: " + segmentIndex + "\n";
    //Reset the enumerator.
    enumSegment.Reset();
    //Skip.
    report = report + "***** Skip *****" + "\n";
    enumSegment.Skip(2);
    enumSegment.Next(out segment, ref partIndex, ref segmentIndex);
    report = report + "Segment type: " + segment.GeometryType + " , Part Index: " +
        partIndex + " , Segment Index: " + segmentIndex + "\n";
    report = report + "***** Skip backward *****" + "\n";
    enumSegment.Skip( - 2);
    enumSegment.Next(out segment, ref partIndex, ref segmentIndex);
    report = report + "Segment type: " + segment.GeometryType + " , Part Index: " +
        partIndex + " , Segment Index: " + segmentIndex + "\n";
    System.Windows.Forms.MessageBox.Show(report);
}

//Create a polyline with two parts of four segments each.
//Each part contains one line, one circular arc, one elliptic arc, one Bezier curve.
private IPolyline CreateMultipartPolyline()
{
    //Create the end points for all the segments.
    IPoint[] points = new IPoint[5];
    for (int i = 0; i < 5; i++)
    {
        points[i] = new PointClass();
    }
    points[0].PutCoords(10, 10);
    points[1].PutCoords(20, 10);
    points[2].PutCoords(30, 10);
    points[3].PutCoords(40, 10);
    points[4].PutCoords(50, 10);
    //Create four segments.
    //One line, one circular arc, one elliptic arc, one Bezier curve.
    ILine line = new LineClass();
    line.PutCoords(points[0], points[1]);
    ICircularArc circularArc = new CircularArcClass();
    IPoint centerPoint = new PointClass();
    centerPoint.PutCoords(25, 10);
    circularArc.PutCoords(centerPoint, points[1], points[2],
        esriArcOrientation.esriArcClockwise);
    IEllipticArc ellipticArc = new EllipticArcClass();
    IPoint ellipticArcPoint = new PointClass();
    ellipticArcPoint.PutCoords(35, 10);
    ellipticArc.PutCoordsByAngle(false, ellipticArcPoint,  - Math.PI,  - Math.PI, 0,
        5, 0.2);
    IPoint[] bezierControlPoints = new IPoint[4];
    bezierControlPoints[0] = points[3];
    bezierControlPoints[1] = new PointClass();
    bezierControlPoints[1].PutCoords(43.33, 20);
    bezierControlPoints[2] = new PointClass();
    bezierControlPoints[2].PutCoords(46.66, 0);
    bezierControlPoints[3] = points[4];
    IBezierCurveGEN bezier = new BezierCurveClass();
    bezier.PutCoords(ref bezierControlPoints);
    //Create two paths.
    ISegmentCollection segmentCollection1 = new PathClass();
    ISegmentCollection segmentCollection2 = new PathClass();
    //Add segments to the path.
    object Missing = Type.Missing;
    segmentCollection1.AddSegment(line as ISegment, ref Missing, ref Missing);
    segmentCollection1.AddSegment(circularArc as ISegment, ref Missing, ref Missing);
    segmentCollection1.AddSegment(ellipticArc as ISegment, ref Missing, ref Missing);
    segmentCollection1.AddSegment(bezier as ISegment, ref Missing, ref Missing);
    //Clone the first path and move it to the right by 10.
    IClone clone = segmentCollection1 as IClone;
    segmentCollection2 = clone.Clone()as ISegmentCollection;
    ITransform2D transfrom2D = segmentCollection2 as ITransform2D;
    transfrom2D.Move(50, 0);
    //Create the polyline object.
    IGeometryCollection geometryCollection = new PolylineClass();
    geometryCollection.AddGeometry(segmentCollection1 as IGeometry, ref Missing, ref
        Missing);
    geometryCollection.AddGeometry(transfrom2D as IGeometry, ref Missing, ref
        Missing);
    return geometryCollection as IPolyline;
}
[VB.NET]
Sub IEnumSegment_Example()
    Dim pSegmentCollection As ISegmentCollection
    Dim pEnumSegment As IEnumSegment
    Dim pClonedEnumSegment As IEnumSegment
    Dim pSegment As ISegment, lPartIndex As Long, lSegmentIndex As Long
    Dim pSegInfo As esriSegmentInfo 'Segment info structure.
    pSegmentCollection = CreateMultipartPolyline
    pEnumSegment = pSegmentCollection.EnumSegments
    'Reset the enumerator.
    pEnumSegment.Reset()
    'Get the firt segment in the enumerator.
    pEnumSegment.Next(pSegment, lPartIndex, lSegmentIndex)
    'Clone the enumerator. This preserves the current location.
    pEnumSegment.Clone(pClonedEnumSegment)
    'Move the original enumerator by one position and print the info about that segment.
    pEnumSegment.Next(pSegment, lPartIndex, lSegmentIndex)
    Debug.Print("Segment type: " & pSegment.GeometryType & " , Part Index: " & lPartIndex & " , Segment Index: " & lSegmentIndex)
    pClonedEnumSegment.Next(pSegment, lPartIndex, lSegmentIndex)
    'Move the cloned enumerator by one position and print the info about that segment.
    Debug.Print("Segment type: " & pSegment.GeometryType & " , Part Index: " & lPartIndex & " , Segment Index: " & lSegmentIndex)
    'Check if recycling.
    Debug.Print("IsRecycling: " & pEnumSegment.IsRecycling)
    'Set the enumerator to segment three in part one.
    pEnumSegment.SetAt(1, 3)
    'Check that the enumerator is now located at the last segment of a part.
    Debug.Print("IsLastInPart : " & pEnumSegment.IsLastInPart)
    'Use the NextEx method to get more info about the current segment in the enumerator.
    Debug.Print("***** NextEx *****")
    pEnumSegment.NextEx(pSegInfo)
    Debug.Print("Segment info - LastInPart: " & pSegInfo.bLastInPart)
    Debug.Print("Segment info - Absolute Segment Index: " & pSegInfo.iAbsSegment)
    Debug.Print("Segment info - Part Index: " & pSegInfo.iPart)
    Debug.Print("Segment info - Segment Index in current part (Relative) : " & pSegInfo.iRelSegment)
    Debug.Print("Segment info - Get the length of the segment: " & pSegInfo.pSegment.Length)
    pEnumSegment.Reset()
    'Set the enumerator to the last segment of the first part.
    pEnumSegment.NextInPart(pSegment, lSegmentIndex)
    'Set the enumerator to the previous location.
    Debug.Print("***** Previous *****")
    pEnumSegment.Next(pSegment, lPartIndex, lSegmentIndex)
    Debug.Print("Segment type: " & pSegment.GeometryType & " , Part Index: " & lPartIndex & " , Segment Index: " & lSegmentIndex)
    pEnumSegment.Previous(pSegment, lPartIndex, lSegmentIndex)
    Debug.Print("Segment type: " & pSegment.GeometryType & " , Part Index: " & lPartIndex & " , Segment Index: " & lSegmentIndex)
    Debug.Print("***** NextInPartEx *****")
    pEnumSegment.NextInPartEx(pSegInfo)
    Debug.Print("Segment info - LastInPart: " & pSegInfo.bLastInPart)
    Debug.Print("Segment info - Absolute Segment Index: " & pSegInfo.iAbsSegment)
    Debug.Print("Segment info - Part Index: " & pSegInfo.iPart)
    Debug.Print("Segment info - Segment Index in current part (Relative) : " & pSegInfo.iRelSegment)
    Debug.Print("Segment info - Get the length of the segment: " & pSegInfo.pSegment.Length)
    'Reset the enumerator to the end.
    pEnumSegment.ResetToEnd()
    pEnumSegment.Previous(pSegment, lPartIndex, lSegmentIndex)
    Debug.Print("Segment type: " & pSegment.GeometryType & " , Part Index: " & lPartIndex & " , Segment Index: " & lSegmentIndex)
    'Reset the enumerator.
    pEnumSegment.Reset()
    'Skip.
    Debug.Print("***** Skip *****")
    pEnumSegment.Skip(2)
    pEnumSegment.Next(pSegment, lPartIndex, lSegmentIndex)
    Debug.Print("Segment type: " & pSegment.GeometryType & " , Part Index: " & lPartIndex & " , Segment Index: " & lSegmentIndex)
    Debug.Print("***** Skip backward *****")
    pEnumSegment.Skip( -2)
    pEnumSegment.Next(pSegment, lPartIndex, lSegmentIndex)
    Debug.Print("Segment type: " & pSegment.GeometryType & " , Part Index: " & lPartIndex & " , Segment Index: " & lSegmentIndex)
    
End Sub

'Create a polyline with two parts of four segments each.
'Each part contains one line, one circular arc, one elliptic arc, one Bezier curve.

Private Function CreateMultipartPolyline() As IPolyline
    Dim pSegColl0 As ISegmentCollection, pSegColl1 As ISegmentCollection
    Dim pLine As ILine, pPoints(4) As IPoint, i As Long, dPi As Double
    Dim pCArc As ICircularArc, pCenterCArc As IPoint
    Dim pEArc As IEllipticArc, pCenterEArc As IPoint
    Dim pBezier As IBezierCurve, pBezControlPoints(3) As IPoint
    Dim pGeoColl As IGeometryCollection, pClone As IClone
    Dim pTrans2D As ITransform2D
    'Create the polyline object.
    pGeoColl = New Polyline
    'Create two paths.
    pSegColl0 = New Path
    'PI value.
    dPi = Math.Atan(1) * 4
    'Create the end points for all the segments.
    For i = 0 To 4
        pPoints(i) = New Point
    Next
    pPoints(0).PutCoords(10, 10)
    pPoints(1).PutCoords(20, 10)
    pPoints(2).PutCoords(30, 10)
    pPoints(3).PutCoords(40, 10)
    pPoints(4).PutCoords(50, 10)
    'Create four segments.
    'One line, one circular arc, one elliptic arc, one Bezier curve.
    pLine = New Line
    pLine.PutCoords(pPoints(0), pPoints(1))
    pCArc = New CircularArc
    pCenterCArc = New Point
    pCenterCArc.PutCoords(25, 10)
    pCArc.PutCoords(pCenterCArc, pPoints(1), pPoints(2), esriArcOrientation.esriArcClockwise)
    pEArc = New EllipticArc
    pCenterEArc = New Point
    pCenterEArc.PutCoords(35, 10)
    pEArc.PutCoordsByAngle(False, pCenterEArc, - dPi, - dPi, 0, 5, 0.2)
    pBezier = New BezierCurve
    pBezControlPoints(0) = pPoints(3)
    pBezControlPoints(1) = New Point
    pBezControlPoints(1).PutCoords(43.33, 20)
    pBezControlPoints(2) = New Point
    pBezControlPoints(2).PutCoords(46.66, 0)
    pBezControlPoints(3) = pPoints(4)
    pBezier.PutCoords(4, pBezControlPoints(0))
    'Add the segments to the path.
    pSegColl0.AddSegment(pLine)
    pSegColl0.AddSegment(pCArc)
    pSegColl0.AddSegment(pEArc)
    pSegColl0.AddSegment(pBezier)
    'Clone the first path and move it to the right by 10.
    pClone = pSegColl0
    pSegColl1 = pClone.Clone
    pTrans2D = pSegColl1
    pTrans2D.Move(50, 0)
    pGeoColl.AddGeometry(pSegColl0)
    pGeoColl.AddGeometry(pTrans2D)
    CreateMultipartPolyline = pGeoColl
End Function






To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
ArcGIS for Desktop Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced
Engine Developer Kit Engine