Hello,
I edit an existing sketch with a macro in order to manipulate certain things given certain input. During the manipulation, the macro may find an inconsistency in the input given, at which point I would like to exit the sketch, revert all the changes done, and end the macro. It seems like InsertSketch Method (ISketchManager) is the only way to close/exit a sketch, but there doesn't seem to be a way to discard the changes. Is there any way to achieve this?
Thanks in advance!
I suppose that you would have to count the number of changes done to the sketch so that you know how many steps to undo if that makes sense. My sample part was 2 steps because I deleted 1 relation from the base sketch. I tested again and deleted several and there were obviously more steps to undo so the macro recorded Part.EditUndo2 7.
Edit: After a bit of looking, there is a method to record an undo object which I assume wraps several calls into one object that can be added to the undo list. I think this is a good route to go. In that event, you can call Part.EditUndo2 2 to undo your edit sketch (1 undo step) and the group of changes (1 recorded UndoObject). You may be able to put them all into one object including the sketch edit and exit before ending your undo object recording requiring only one undo step.
2016 SOLIDWORKS API Help - StartRecordingUndoObject Method (IModelDocExtension)
2016 SOLIDWORKS API Help - FinishRecordingUndoObject2 Method (IModelDocExtension)
Edit 2:
Simple Example to show concept - This adds a bunch of construction lines to a sketch and dimensions one of them before exiting. I call undo one step and it completes as desired.
Also, the undo menu has only one undo object as shown here. This undoes everything between the StartRecordingUndoObject and FinishRecordingUndoObject.