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
- Start ArcMap.
- Create a new UIButtonControl.
- Right-click the UIButtonControl and select View Source.
- Copy and paste the following code into the UIButtonControl's click event.
Private Sub Command3_Click()
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
- 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 - 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.
-----------------------------------------------------------
佛对我说:你心里有尘。我用力的拭擦。