Undo a Delete

331
3
12-14-2011 08:46 AM
GregRieck
Occasional Contributor III
Hello Forum,

Is it okay to call IMxDocument.OperationStack.Undo() inside an OnStopOperation() event?

The only way I've gotten the below to work is by calling IMxDocument.OperationStack.Undo() and then executing an esriEditor.SaveEditCommand inside the OnStopOperation().


I'm using a class extension with IObjectClassEvents. When a feature is deleted on the map I am successful in trapping the delete event. However, I ask the user some verifying questions prior to permitting the delete to finish.

The problem I'm having is I can't seem to get the delete to "undo", and here's the key, while keeping the feature still visible on the screen and not having anything in the undo redo cache.

I want the undo to:
1) Keep the feature on the map unchanged, no refresh, start and stop editing etc.
2) Not have any undo or redo cache related to the event.

I've tried several variations of IEditor3 and IWorkSpaceEdit2 start and stop editing and undo's but nothing seems to get this exact solution. I can get it where the feature remains unchanged on the map without having to do a manual start stop edit but then I'm left with a redo option. Also, I wouldn't want to completely clear the undo redo cache but possibly just remove the feature delete and that's it.

G
0 Kudos
3 Replies
AlexanderGray
Occasional Contributor III
if you use the IObjectClassEvents OnDelete event instead of the editor extension, you can raise an unhandled exception.  ArcGIS will pick up on the exception, abort the operation and show the user the message of the exception.  That way the operation is aborted, map and database stay the same and the operation is not in the stack.
0 Kudos
GregRieck
Occasional Contributor III
Hello Alexander,

Yes, I am using IObjectClassEvents OnDelete. What I'm trying to do is gracefully undo the delete. If a user deletes an IFeature object I do a couple of things. I check if the feature has related data that will be impacted by the deletion of the feature and ask the user if they want to continue. If they choose not to I need to undo the delete. If they continue I ask the user if they are sure they want to delete the feature. Again if they choose not to I would need to undo the delete.

So they are confirming that they want to delete the related data and the actual object itself. Of course if they choose not to delete the feature object then the related data would not be deleted either.

I don't think raising an exception is the answer here. The user won't be able to respond to an exception in a "YES" or "NO" fashion.

Greg

Note: I tried wrapping the delete event around edit operations without any success. The only way I got this to work was by inserting a boolean check inside the OnStopOperation that performs an MxDocument.OperationStack.Undo() and then executing a call to esriEditor.SaveEditCommand. The only problem with this approach is it forces a save on the entire operation stack, thus clearing it.
0 Kudos
AlexanderGray
Occasional Contributor III
Well what I would do, is in the event handler, I would prompt the user to continue or not to continue.  If the user chooses to continue, the event handler does nothing else.  If the user chooses not to continue I would raise an exception with the message that the user chose to abort the edit.  Unfortunately, that will show a second window saying the user aborted the edit.  However, the operation will be aborted gracefully and the undo stack will be unaffected.
0 Kudos