The best way to move features is to use a Set and call MoveSet. Sets work well because it takes into account connected features such as those found in a network. For example, if a selected polyline is a network edge feature and it has connected features, MoveSet will move all of the features even though only one has been selected.
How to use
- Paste this code into your VBA Application.
- This maco moves features 50 map units to the right, change as desired.
- Select some features to move, then run this macro.
Public Sub MoveFeatures()
Dim pEditor As IEditor
Dim pEndPoint As IPoint
Dim pEnumFeature As IEnumFeature
Dim pFeature As IFeature
Dim pFeatureEdit As IFeatureEdit
Dim pID As New UID
Dim pInvalidArea As IInvalidArea
Dim pLine As ILine
Dim pMoveSet As ISet
Dim pSpatialReference As ISpatialReference
Dim pStartPoint As IPoint
Dim origX As Double
Dim origY As Double
Dim bInOperation As Boolean
On Error GoTo ErrorHandler
'Get a reference to the editor extension
pID = "esriEditor.Editor"
Set pEditor = Application.FindExtensionByCLSID(pID)
'Create an edit operation enabling undo for the operation
pEditor.StartOperation
bInOperation = True
'Make sure something has been selected
If pEditor.SelectionCount = 0 Then Exit Sub
'Add all the editor's selected features to a new set
Set pEnumFeature = pEditor.EditSelection
'Flag those areas of the display that need refreshing
Set pInvalidArea = New InvalidArea
Set pInvalidArea.Display = pEditor.Display
pInvalidArea.Add pEnumFeature
Set pMoveSet = New esriSystem.Set
pEnumFeature.Reset
Set pFeature = pEnumFeature.Next
Do While Not pFeature Is Nothing
pMoveSet.Add pFeature
Set pFeature = pEnumFeature.Next
Loop
'Reset the Set
pMoveSet.Reset
'MoveSet requires a line to specify the new location'Use the selection anchor as a starting point for the line
Set pStartPoint = pEditor.SelectionAnchor.Point
Set pLine = New Line
pStartPoint.QueryCoords origX, origY
Set pEndPoint = New Point
pEndPoint.PutCoords (origX + 50), (origY + 0) 'offset the selection by 50 units in the x direction
pLine.PutCoords pStartPoint, pEndPoint
'Get the spatial reference from the map and assign it to the new line
Set pSpatialReference = pEditor.Map.SpatialReference
Set pLine.SpatialReference = pSpatialReference 'Set the spatial reference of the new line'Do the move while looping through the set
Set pFeatureEdit = pMoveSet.Next
Do While Not pFeatureEdit Is Nothing
pFeatureEdit.MoveSet pMoveSet, pLine 'Move all the selected features 50 units to the right
Set pFeatureEdit = pMoveSet.Next
Loop
'Stop the Edit Operation
pEditor.StopOperation "Move Selection"
bInOperation = False
pInvalidArea.Invalidate esriAllScreenCaches
'Additionally move the selection anchor
pEditor.SelectionAnchor.MoveTo pEndPoint, pEditor.Display
Exit Sub
ErrorHandler:
If bInOperation Then
pEditor.AbortOperation
MsgBox "Error moving features. Check selected features for topological associations."
End If
End Sub