Knowledge Base - Technical Articles


Technical Article   HowTo:  Split a polyline at the vertices using ArcObjects

Article ID: 24483
Software:  ArcGIS - ArcEditor 8.3, 8.1, 8.1.2, 8.2, 9.0, 9.1, 9.2, 9.3, 9.3.1 ArcGIS - ArcInfo 8.2, 9.0, 8.1, 8.1.2, 8.3, 9.1, 9.2, 9.3, 9.3.1 ArcGIS - ArcView 8.3, 8.1, 8.1.2, 8.2, 9.0, 9.1, 9.2, 9.3, 9.3.1
Platforms: N/A

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. -show me-

    A. Select Tools > Customize to open the Customize dialog box.
    B. Click the Commands tab.
    C. Select UIControls from the Categories list box.
    D. Select Untitled from the Save In dropdown list to save the button to this map document. Select Normal to save the button to all ArcMap documents on your machine.
    E. Click New UIControl.
    F. Select UIButtonControl and Create.
    G. Drag the new UIButtonControl to the toolbar of your choice.
    H. Close the Customize dialog box.


      For more information on creating a UIControl, see the ArcGIS Desktop Help topic 'How to create custom commands with VBA.'

  3. Right-click the UIButtonControl and select View Source.
  4. Copy and paste the following code into the UIButtonControl's click event.

       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.

       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.

Created: 2/7/2003
Last Modified: 5/3/2011

Article Rating: (3)
If you would like to post a comment, please login

Comments

By Anonymous - 11/22/2006 11:50 AM

I followed the article’s instructions, but experienced another problem. I’ve provided details below.

You should include in your instructions that the user should leave out the first Sub declaration, and the final End Sub declaration, since (at least in 9.1) when you click "View Source" and get to the VBA editor, it has already defined a UIControl_click() sub statement. Also, the _ did not work to extend lines. I debugged the code and it is at http://64.202.170.231/carto/breakvertices.txt You should also mention which layers it runs on. Is it the whole document? I brought up a roads layer,

Rating:

By Anonymous - 10/28/2005 8:55 AM

When splitting the lines, this code transforms arcs into their chord bearing. It completely changes the nature of the original data, and has caused me trouble. You should say this in the article

By Anonymous - 11/02/2004 12:07 PM

Error in code; Expect End Sub message. Since I don't know VB, I was lost. Someone else told me to take out the Sub SplitALVertex and the End Sub at the end. Then the directions worked. PS, I using version ArcInfo 9, SP 1.

Rating:

By Anonymous - 06/24/2003 2:50 AM

I think you should include that the layer you want to split should be in an edit session, otherwise you get a runtime VB error saying you don't have permission to do what you're doing. Thanks for the code!

Rating: