How To: Split a polyline at the vertices using ArcObjects

Summary

This article contains an ArcObjects code sample that demonstrates how to split a polyline at the vertices, creating separate lines from each line segment.

Procedure

 

  1. Start ArcMap.
  2. Create a new UIButtonControl: How To: Create a new UIButtonControl​
    Note:
    For more information on creating a UIControl, see the ArcGIS Desktop Help topic: 'Creating custom commands with VBA and UI Controls'
  3. Right-click the UIButtonControl and select View Source.
  4. Copy and paste the following code into the UIButtonControl's click event.
     
    Code:
       Dim pMxDoc As IMxDocument
       Dim pFeatureClass As IFeatureClass
       Dim pFeatureLayer As IFeatureLayer
       Dim pFeatureCursor As IFeatureCursor
       Dim pOutFeatureCursor As IFeatureCursor
       Dim pFeature As IFeature
       Dim pOutFeatureBuffer As IFeatureBuffer
       Dim pSegmentCollection As ISegmentCollection
       Dim pSegment As ISegment
       Dim pPointCollection As IPointCollection
       Dim i As Integer
       Dim index As Integer
       Dim numFeatures As Integer
    
       'Split lines for selected layer
       Set pMxDoc = ThisDocument
    
       If Not pMxDoc.SelectedLayer Is Nothing Then
         Set pFeatureLayer = pMxDoc.SelectedLayer
       Else
          MsgBox "Please select layer to split"
          Exit Sub
       End If
    
       Set pFeatureClass = pFeatureLayer.FeatureClass
       Set pFeatureCursor = pFeatureClass.Update(Nothing, False)
       Set pOutFeatureCursor = pFeatureClass.Insert(True)
       Set pFeature = pFeatureCursor.NextFeature
       numFeatures = pFeatureClass.FeatureCount(Nothing)
    
       'Loop through the features and split each feature at
       'it's vertices then copy attributes and shape to new feature
       For index = 0 To numFeatures - 1
          Set pSegmentCollection = pFeature.Shape
    
          For i = 0 To pSegmentCollection.SegmentCount - 1
             Set pSegment = pSegmentCollection.Segment(i)
             Set pOutFeatureBuffer = pFeatureClass.CreateFeatureBuffer
    
             AddFields pOutFeatureBuffer, pFeature
    
             Set pPointCollection = New Polyline
             pPointCollection.AddPoint pSegment.FromPoint
             pPointCollection.AddPoint pSegment.ToPoint
    
             Set pOutFeatureBuffer.Shape = pPointCollection
             pOutFeatureCursor.InsertFeature pOutFeatureBuffer
          Next i
    
          pFeatureCursor.DeleteFeature
          
          Set pFeature = pFeatureCursor.NextFeature
    
          pOutFeatureCursor.Flush
        Next index
    
       pFeatureCursor.Flush
    
       'Refresh
    
       pMxDoc.ActiveView.Refresh
       
       MsgBox ("Completed!")
    End Sub
    
    
  5. The code above calls a routine called "AddFields". To support this, copy and paste this code so it follows the "End Sub" of the Click event handler above.
     
    Code:
       Private Sub AddFields(pFeatureBuffer As IFeatureBuffer, pFeature As IFeature)
       'Copy the attributes from the original feature to the new one
       Dim pRowBuffer As IRowBuffer
       Dim pNewFields As IFields
       Dim pNewField As IField
       Dim pFields As IFields
       Dim pField As IField
       Dim i As Integer
       Dim NewFieldIndex As Long
    
       Set pRowBuffer = pFeatureBuffer
       Set pNewFields = pRowBuffer.Fields
       Set pFields = pFeature.Fields
    
       For i = 0 To pFields.FieldCount - 1
          Set pField = pFields.Field(i)
    
          If Not pField.Type = esriFieldTypeGeometry And Not pField.Type = esriFieldTypeOID And pField.Editable Then
            NewFieldIndex = pNewFields.FindField(pField.Name)
    
            If Not NewFieldIndex = -1 Then
               pFeatureBuffer.Value(NewFieldIndex) = pFeature.Value(i)
            End If
          End If
       Next
    End Sub
    
  6. In ArcMap, select the polyline layer whose features you want to split. Click the newly created button to split all the lines in this layer.