ArcObjects Library Reference  (Geometry)    

SplitAtPoint_Example

'This example demonstrates how to use SplitAtPoint

Sub exampleSplitAtPoint()
Dim ptc As IPointCollection, pt(2) As IPoint, i As Long, pSplitPoint As IPoint, ppol As IPolycurve
Dim bProject As Boolean, bCreatePart As Boolean, bSplitted As Boolean, lNewPart As Long, lNewSeg As Long
Dim pc As IClone
Set ptc = New Polyline
'Should define the spatial reference on the polyline using IGeometry::SpatialReference here (Code skipped)
For i = 0 To 2
   Set pt(i) = New Point
Next
pt(0).PutCoords 0, 0
pt(1).PutCoords 20, 0
pt(2).PutCoords 40, 0
ptc.AddPoints 3, pt(0)
Set pc = ptc
Set pSplitPoint = New Point
'Should define the spatial reference on the point using IGeometry::SpatialReference here (Code skipped)

'Example one, do not create part, point projected
Debug.Print "**************** Example 1 ****************"
pSplitPoint.PutCoords 30, 10
bProject = True 'The point will be projected on the curve
bCreatePart = False
Set ppol = pc.Clone
ppol.SplitAtPoint pSplitPoint, bProject, bCreatePart, bSplitted, lNewPart, lNewSeg
Debug.Print "Polyline split : " & bSplitted
Debug.Print "New part index : " & lNewPart
Debug.Print "New segment index : " & lNewSeg
printSegmentCoordinates ppol
'**************** Example 1 ****************
'Polyline split : True
'New part index : 0
'New segment index : 2
'Part : 0
'Segment : 0
'0 , 0
'20 , 0
'Segment : 1
'20 , 0
'30 , 0
'Segment : 2
'30 , 0
'40 , 0

'Example two : do not create part, point not projected
Debug.Print "**************** Example 2 ****************"
pSplitPoint.PutCoords 30, 10
bProject = False 'The point will not be projected on the curve
bCreatePart = False
Set ppol = pc.Clone
ppol.SplitAtPoint pSplitPoint, bProject, bCreatePart, bSplitted, lNewPart, lNewSeg
Debug.Print "Polyline split : " & bSplitted
Debug.Print "New part index : " & lNewPart
Debug.Print "New segment index : " & lNewSeg
printSegmentCoordinates ppol
'**************** Example 2 ****************
'Polyline split : True
'New part index : 0
'New segment index : 2
'Part : 0
'Segment : 0
'0 , 0
'20 , 0
'Segment : 1
'20 , 0
'30 , 10
'Segment : 2
'30 , 10
'40 , 0
'Example three : create part, point projected
Debug.Print "**************** Example 3 ****************"
pSplitPoint.PutCoords 30, 10
bProject = True 'The point will be projected on the curve
bCreatePart = True
Set ppol = pc.Clone
ppol.SplitAtPoint pSplitPoint, bProject, bCreatePart, bSplitted, lNewPart, lNewSeg
Debug.Print "Polyline split : " & bSplitted
Debug.Print "New part index : " & lNewPart
Debug.Print "New segment index : " & lNewSeg
printSegmentCoordinates ppol
'**************** Example 3 ****************
'Polyline split : True
'New part index : 1
'New segment index : 0
'Part : 0
'Segment : 0
'0 , 0
'20 , 0
'Segment : 1
'20 , 0
'30 , 0
'Part : 1
'Segment : 0
'30 , 0
'40 , 0
End Sub


Sub printSegmentCoordinates(pgc As IGeometryCollection)
Dim i As Long, j As Long
Dim psc As ISegmentCollection
Dim ps As ISegment
For i = 0 To pgc.GeometryCount - 1
   Debug.Print "Part : " & i
   Set psc = pgc.Geometry(i)
   For j = 0 To psc.SegmentCount - 1
      Debug.Print "Segment : " & j
      Set ps = psc.Segment(j)
      Debug.Print ps.FromPoint.X & " , " & ps.FromPoint.Y
      Debug.Print ps.ToPoint.X & " , " & ps.ToPoint.Y
   Next
Next

End Sub


[C#]
public void SplitAtPoint()
{
  IPointCollection pointCollection = new PolylineClass();
  IPoint point0 = new PointClass();
  point0.PutCoords(0,0);
  IPoint point1 = new PointClass();
  point1.PutCoords(20,0);
  IPoint point2 = new PointClass();
  point2.PutCoords(30,0);
  object missing = Type.Missing;
  pointCollection.AddPoint(point0, ref missing, ref missing);
  pointCollection.AddPoint(point1, ref missing, ref missing);
  pointCollection.AddPoint(point2, ref missing, ref missing);
  IPoint splitPoint = new PointClass();
  splitPoint.PutCoords(30, 10);
  //Example one, do not create part, point projected
  //The point will be projected on the curve
  bool projectPoint = true; 
  bool createPart = false;
  IClone clone = pointCollection as IClone;
  IPolycurve polycurve = clone.Clone() as IPolycurve;
  bool isSplitted;
  int newPartIndex;
  int newSegmentIndex; 
  polycurve.SplitAtPoint(splitPoint, projectPoint, createPart, out isSplitted, out newPartIndex, out newSegmentIndex);
  PrintSegmentCoordinates("Example1", polycurve, isSplitted, newPartIndex, newSegmentIndex);

  //Example two : do not create part, point not projected
  projectPoint = false; 
  createPart = false;
  polycurve = clone.Clone() as IPolycurve;
  polycurve.SplitAtPoint(splitPoint, projectPoint, createPart, out isSplitted, out newPartIndex, out newSegmentIndex);
  PrintSegmentCoordinates("Example2", polycurve, isSplitted, newPartIndex, newSegmentIndex);
  //Example three : create part, point projected
  projectPoint = true; 
  createPart = true;
  polycurve = clone.Clone() as IPolycurve;
  polycurve.SplitAtPoint(splitPoint, projectPoint, createPart, out isSplitted, out newPartIndex, out newSegmentIndex);
  PrintSegmentCoordinates("Example3", polycurve, isSplitted, newPartIndex, newSegmentIndex);
}
private void PrintSegmentCoordinates(object header, IPolycurve polycurve, bool isSplitted, int newPartIndex, int newSegmentIndex)
{
IGeometryCollection geometryCollection = polycurve as IGeometryCollection;
String report = header + "\n";
  for (int i = 0; i < geometryCollection.GeometryCount; i++)
    {
     report = report +  "Part : " + i;
     ISegmentCollection segmnetCollection = geometryCollection.get_Geometry(i) as ISegmentCollection;
     for (int j = 0; j < segmnetCollection.SegmentCount; j++)
     {
        report = report + " Segment : " + j;
        ISegment segment = segmnetCollection.get_Segment(j);
        report = report + "From Point X, Y = " + segment.FromPoint.X + " , " + segment.FromPoint.Y + "\n" +
        "From Point X, Y = " + segment.ToPoint.X + " , " + segment.ToPoint.Y + "\n";
     }
  }
  
  System.Windows.Forms.MessageBox.Show(report);
}

[Visual Basic .NET, C++]
No example is available for Visual Basic .NET or C++. To view a Visual Basic 6.0 or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.