How to create a sketch operation


Summary
Enclose edits made to an edit sketch inside a sketch operation to allow the sketch operation to be added to the operation stack, thereby providing undo or redo capabilities.


About sketch operations

Sketch operations are operations that work with the edit sketch. Make changes to the edit sketch within sketch operations, so they can be added to the operation stack, thereby providing undo or redo capabilities.
Within the lifetime of the edit session, sketch operations added to the operation stack are temporary. When the sketch is finished, sketch operations are removed and replaced with a single edit operation. For example, when digitizing a new polyline with five vertices, five individual sketch operations (each called add vertex) are added to the operation stack. By finishing the sketch, these sketch operations are replaced by a single (called create) edit operation. 

Creating sketch operations

IEngineSketchOperation.Start marks the beginning of a sketch operation. Use the IEngineSketchOperation.SetMenuString method to name the operation so that it can be identified on the operation stack and used as a ToolTip for the Undo and Redo commands.
 
IEngineSketchOperation.Finish marks the end of a sketch operation, at which point it is added to the operation stack.
There are a number of setup steps required to ensure that sketch operations are correctly added to the operation stack. For more information, see How to work with the operation stack.
The following code example shows how to create a sketch operation that deletes the last vertex in the edit sketch:
[C#]
IEngineSketchOperation sketchOp = new EngineSketchOperationClass();
IEngineEditor engineEditor = new EngineEditorClass();
IEngineEditSketch editSketch = (IEngineEditSketch)engineEditor;

//Clone the original edit sketch envelope. Used to invalidate the display.
IEnvelope invalidateEnv = ((IClone)editSketch.Geometry.Envelope).Clone()as IEnvelope;

//Start the edit sketch operation.
sketchOp.Start(engineEditor);

//Set the MenuString to identify the edit sketch operation.
sketchOp.SetMenuString("Delete Vertex");

//Get the point collection from the edit sketch.
IPointCollection pointCol = (IPointCollection)editSketch.Geometry;

//Clone the vertex to be removed.
IClone vertexToRemove = (IClone)pointCol.get_Point(pointCol.PointCount - 1);
vertexToRemove.Clone();

//Remove the last vertex and refresh the sketch.
pointCol.RemovePoints(pointCol.PointCount - 1, 1);
editSketch.Geometry = (IGeometry)pointCol;
editSketch.RefreshSketch();

//Finish the sketch operation.
sketchOp.Finish(invalidateEnv,
    esriEngineSketchOperationType.esriEngineSketchOperationVertexDeleted,
    vertexToRemove as System.Object);
[VB.NET]
Dim sketchOp As IEngineSketchOperation = New EngineSketchOperationClass()
Dim engineEditor As IEngineEditor = New EngineEditorClass()
Dim editSketch As IEngineEditSketch = CType(engineEditor, IEngineEditSketch)

'Clone the original edit sketch envelope. Used to invalidate the display.
Dim clonedEnv As IClone = (CType(editSketch.Geometry.Envelope, IClone)).Clone()
Dim invalidateEnv As IEnvelope = CType(clonedEnv, IEnvelope)

'Start the edit sketch operation.
sketchOp.Start(engineEditor)

'Set the MenuString to identify the edit sketch operation.
sketchOp.SetMenuString("Delete Vertex")

'Get the point collection from the edit sketch.
Dim pointCol As IPointCollection = CType(editSketch.GeomeTry, IPointCollection)

'Clone the vertex to be removed.
Dim vertexToRemove As IClone = CType(pointCol.Point(pointCol.PointCount - 1), IClone)
vertexToRemove.Clone()

'Remove the last vertex and refresh the sketch.
pointCol.RemovePoints(pointCol.PointCount - 1, 1)
editSketch.GeomeTry = CType(pointCol, IGeomeTry)
editSketch.RefreshSketch()

'Finish the sketch operation.
sketchOp.Finish(editSketch.Geometry.Envelope, esriEngineSketchOperationType.esriEngineSketchOperationVertexDeleted, CType(vertexToRemove, System.Object))
The following arguments supplied to the IEngineSketchOperation.Finish method are important:
  • invalEnv—The envelope to be invalidated. In the previous code example, the pre-modified envelope of the edit sketch is used to avoid potentially leaving artifacts on the screen after deleting the last vertex.
  • opType—An esriEngineSketchOperationType constant that ensures the appropriate listener will be notified. In the previous code example, specifying esriEngineSketchOperationVertexDeleted ensures that the IEngineEditEvents.OnVertexDeleted event is fired.
  • Data—The vertex on which the action was performed and passed to the appropriate events. In the previous code example, the deleted vertex is passed to the Point parameter of the IEngineEditEvents.OnVertexDeleted event. 

Undo or redo sketch operations

To undo or redo an individual sketch operation, use the IEngineSketchOperation.Undo (derived from IOperation.Undo) or IEngineSketchOperation.Redo (derived from IOperation.Redo) method.
To undo or redo multiple sketch operations, access the operation stack using IToolbarControl2.OperationStack, then call the IOperationStack.Undo or IOperationStack.Redo method, respectively.

Sketch operation events

The IEngineEditEvents interface exposes the following events that allow the application to respond to sketch operations:


See Also:

How to create an edit session
How to listen to edit events
How to work with the operation stack




To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
Engine Developer Kit Engine
ArcGIS for Desktop Basic
ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced