ArcMap add-in: How to add button operation to undo/redo stack

738
2
05-16-2019 05:12 PM
CoryDavis
New Contributor III

I've built an add-in with several buttons which initiate an editing session to perform their actions. I would like to push the operation to ArcMap's operation stack so the undo/redo buttons are enabled, but I can't figure out how.

Here is a snippet of the action of one button. This one reduces the font size of all selected features. Is it possible to add this operation to the stack after it is used?

IFeatureLayer currLayer = currMap.Layer[j] as IFeatureLayer;
ISelectionSet ss = ((IFeatureSelection)currLayer).SelectionSet;
if(ss.Count > 0 && currLayer.FeatureClass.FindField(attributeName) > -1)
{
    IWorkspaceEdit wse = ((IDataset)currLayer.FeatureClass).Workspace as IWorkspaceEdit;
    wse.StartEditing(true);
    wse.StartEditOperation();

    //Update all annotations in the selection set
    IFeature currFeature;
    int attributeIndex;
    IEnumIDs ids = ss.IDs;
    ids.Reset();
    int id = ids.Next();
    while(id > -1)
    {
        currFeature = currLayer.FeatureClass.GetFeature(id);
        attributeIndex = currFeature.Class.FindField(attributeName);
        currFeature.Value[attributeIndex] = currFeature.Value[attributeIndex] - 0.5;
        currFeature.Store();
        id = ids.Next();
    }
    mxd.ActiveView.Refresh();
    wse.StopEditOperation();
    wse.StopEditing(true);
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
2 Replies
DuncanHornby
MVP Notable Contributor

Not sure if this is a red herring but if you look at the remarks on this IOperationStack it says:

If you are outside an editsession and updating a geodatabase directly, calls to IWorkspaceEdit::StartEditOperation and StopEditOperation will not automatically add anything to ArcMap’s OperationStack since these interfaces are independent of ArcMap, and these changes will be made directly to the geodatabase.

Your code is making direct changes to the FeatureClass and not through the main UI, so may be not possible to do an undo?  Some discussion here but not sure if that helps.

May be you can create your own stack and have the ability to reverse your calculations but I would imagine that would be significant coding for all but the simplest scenarios?

0 Kudos
by Anonymous User
Not applicable

You'll want to use IEditor startediting/stopediting and startoperation/stopoperation

0 Kudos