In this section:
Example Code Click here.
Description This project provides a custom editor command that will perform a difference operation on the two features currently selected in a layer being edited. The result is applied to the geometry of the first feature, and the second feature is deleted. The command will appear on the Editor menubar.
Design Coclass DifferenceCommand is a subtype of the Command abstract class and also sinks the IEditEvents interface.
License required ArcEditor or above
Libraries Carto, Editor, Framework, Geodatabase, Geometry, System, and SystemUI
Languages Visual Basic
Categories ESRI Mx Commands
Interfaces ICommand, IEditEvents
How to use
The two features you selected will be combined into one feature with the overlapping area removed.
The standard Editor menu provides a number of spatial operations that can be applied to multiple features, for example, Union, Intersect, and Clip. However, if you want to merge two features into one feature, but remove the overlapping area (the area that intersects), there is no command available to allow you to do this operation in one step.
As no editor command provides the operation you require, you will create a custom editor command to meet your requirements by performing a difference operation.
This example demonstrates how to create an editor command to perform a difference operation on two features.
The previous topic, Editor Commands and Tools discussed how most Editor commands are implemented as a subtype of Command by implementing the ICommand interface. Most editor commands also sink the IEditEvents outbound interface from the Editor coclass, to respond to editing events.
You can easily solve your requirements with a custom command that is similar to the Union, Intersect, and Clip commands. A custom command is appropriate here because you can rely on another tool like the Edit tool to make the selection beforehand, and your command will deal strictly with the task of performing the difference operation and setting the geometry of the feature to be the result of the operation.
You will create a subtype of Command called DifferenceCommand by implementing ICommand. and sinking the IEditEvents outbound interface from the Editor coclass.
Begin by implementing OnCreate, where you will store references to the Application and Editor objects in the ArcMap application.
Privatem_pAppAsesriFramework.IApplicationPrivatem_pEditorAsesriEditor.IEditorPrivatem_pEditLayersAsesriEditor.IEditLayersPrivate SubICommand_OnCreate(ByValhookAsObject)DimpIDAs NewesriSystem.UID pID = "esriEditor.Editor"Setm_pApp = hookSetm_pEditor = m_pApp.FindExtensionByCLSID(pID)If Notm_pEditorIs Nothing ThenSetm_pEditLayers = m_pEditorEnd If End Sub
To complete the enabled property, add a member variable m_bEnabled and return its value; you will set this value later when implementing IEditEvents.
Privatem_bEnabledAs BooleanPrivate Property GetICommand_Enabled()As BooleanICommand_Enabled = m_bEnabled' Check private memberEnd Property
Now you can perform the difference operation in the OnClick member using the variables you stored in OnCreate.
Private SubICommand_OnClick()On Error GoToErrorHandler:DimpTopoOpAsITopologicalOperatorDimpGeoResultAsIGeometryDimpActiveViewAsIActiveView' Start an edit operationm_pEditor.StartOperation' Do the differenceSetpTopoOp = m_pFeature1.ShapeSetpGeoResult = pTopoOp.SymmetricDifference(m_pFeature2.Shape)IfpGeoResultIs Nothing Then GoToErrorHandler' Delete the second feature and reset feature_1's shapem_pFeature2.DeleteSetm_pFeature1.Shape = pGeoResult m_pFeature1.Store' Complete the operation and redraw new feature and selectionm_pEditor.StopOperation "Difference" m_pEditor.Display.Invalidate m_pFeature1.Extent,True, 0SetpActiveView = m_pEditor.Map pActiveView.PartialRefresh esriViewGeoSelection,Nothing,Nothing Exit Sub' Exit sub to avoid error handlerErrorHandler:'In the event of an error, abort the operationm_pEditor.AbortOperation MsgBox "Difference command failed."End Sub
Editor commands are usually displayed as a caption only, so you do not need to return anything from the Bitmap member.
By listening to the events on the IEditEvents interface, you can control access to your command. Commands are often not enabled until a specific set of criteria is met; in this case, you can write the command so that it is only enabled when the end user has selected two polygon features and set the target layer to a layer containing polygon features.
The best method for enabling a command based on selection criteria is to establish the command as an edit events client responding specifically to IEditEvents::OnSelectionChanged and IEditEvents::OnCurrentLayerChanged.
Private WithEventsEditorEventsAsesriEditor.Editor
Private SubICommand_OnCreate(ByValhookAsObject) ...If Notm_pEditorIs Nothing ThenSetm_pEditLayers = m_pEditor' Respond to IEditEvents interface.SetEditorEvents = m_pEditorEnd If End Sub
Private SubSetEnabledStatus()DimpEnumFeatAsesriGeodatabase.IEnumFeature' Assume command should not be enabledm_bEnabled =False' Make sure the current layer exists. When ArcMap is shutting down,' the Editor fires various events, some of which we are listening to,' but the Editor's properties have likely been emptied at this point.Ifm_pEditLayers.CurrentLayerIs Nothing Then Exit Sub' Check the target layer geometry typeIfm_pEditLayers.CurrentLayer.FeatureClass.ShapeType = esriGeometryPolygonThen' Analyze the Editor's selectionIfm_pEditor.SelectionCount = 2Then SetpEnumFeat = m_pEditor.EditSelection pEnumFeat.ResetSetm_pFeature1 = pEnumFeat.NextIfm_pFeature1.Shape.IsEmptyThen Exit Sub Setm_pFeature2 = pEnumFeat.NextIfm_pFeature2.Shape.IsEmptyThen Exit Sub' If both features are polygons, enable the commandIfm_pFeature1.Shape.GeometryType = esriGeometryPolygonAnd_ m_pFeature2.Shape.GeometryType = esriGeometryPolygonThenm_bEnabled =True Exit Sub End If End If End If End Sub
Private SubEditorEvents_OnCurrentLayerChanged() SetEnabledStatusEnd Sub Private SubEditorEvents_OnSelectionChanged() SetEnabledStatusEnd Sub
Now you can compile the DifferenceCommand project and use it in an ArcMap edit session.
Go to example code.
See Also Editor Commands And Tools, and Split At Intersection Tool Example.