4 Replies Latest reply on Dec 5, 2017 10:31 PM by Hiyel Kar

    How to exit/close a sketch without saving changes

    Hiyel Kar

      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!

        • Re: How to exit/close a sketch without saving changes
          Robert H.

          I believe the only way to do this is to have the macro record/keep track of all the operations it applies and just run the undo command on all operations if it encounters an inconsistency.

          • Re: How to exit/close a sketch without saving changes
            Alex Burnett

            When recording a macro, the only difference appears to be that after exiting the sketch and clicking discard changes it calls Part.EditUndo2 2. I suppose this is the same as editing a sketch and exiting and then clicking the undo arrow.

              • Re: How to exit/close a sketch without saving changes
                Alex Burnett

                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.

                 

                Dim swApp As Object
                
                
                Dim Part As ModelDoc2
                Dim swModelDocExt As SldWorks.ModelDocExtension
                Dim boolstatus As Boolean
                Dim longstatus As Long, longwarnings As Long
                
                
                Sub main()
                
                
                Set swApp = _
                Application.SldWorks
                
                
                Set Part = swApp.ActiveDoc
                Set swModelDocExt = Part.Extension
                
                
                swModelDocExt.StartRecordingUndoObject
                
                
                boolstatus = Part.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
                Part.EditSketch
                Part.ClearSelection2 True
                Dim skSegment As Object
                Set skSegment = Part.SketchManager.CreateCenterLine(-0.317978, 0.108185, 0#, -0.317978, 0#, 0#)
                Set skSegment = Part.SketchManager.CreateCenterLine(-0.317978, 0#, 0#, -0.238348, -0.013286, 0#)
                Set skSegment = Part.SketchManager.CreateCenterLine(-0.238348, -0.013286, 0#, -0.231679, 0.087299, 0#)
                Set skSegment = Part.SketchManager.CreateCenterLine(-0.231679, 0.087299, 0#, -0.317978, 0.108185, 0#)
                Set skSegment = Part.SketchManager.CreateCenterLine(-0.317978, 0.108185, 0#, -0.246056, 0.142338, 0#)
                Set skSegment = Part.SketchManager.CreateCenterLine(-0.215065, 0.212119, 0#, -0.131939, 0.215621, 0#)
                Set skSegment = Part.SketchManager.CreateCenterLine(-0.131939, 0.215621, 0#, -0.123899, 0.182829, 0#)
                Set skSegment = Part.SketchManager.CreateCenterLine(-0.123899, 0.182829, 0#, -0.206686, 0.130666, 0#)
                Set skSegment = Part.SketchManager.CreateCenterLine(-0.206686, 0.130666, 0#, -0.215065, 0.212119, 0#)
                Set skSegment = Part.SketchManager.CreateCenterLine(-0.215065, 0.212119, 0#, -0.25365, 0.219471, 0#)
                Set skSegment = Part.SketchManager.CreateCenterLine(0.016239, 0.199447, 0#, -0.067403, -0.005671, 0#)
                Set skSegment = Part.SketchManager.CreateCenterLine(-0.067403, -0.005671, 0#, 0.077687, -0.038204, 0#)
                Set skSegment = Part.SketchManager.CreateCenterLine(0.077687, -0.038204, 0#, 0.016239, 0.199447, 0#)
                Set skSegment = Part.SketchManager.CreateCenterLine(0.016239, 0.199447, 0#, 0.077687, 0.182829, 0#)
                boolstatus = Part.Extension.SelectByID2("Line7", "SKETCHSEGMENT", -0.153371883278697, 0.162090740985598, 6.87965146996077E-02, False, 0, Nothing, 0)
                Dim myDisplayDim As Object
                Set myDisplayDim = Part.AddDimension2(-0.349659046358454, 0, -8.97750582723233E-02)
                Part.ClearSelection2 True
                Dim myDimension As Object
                Set myDimension = Part.Parameter("D3@Sketch1")
                myDimension.SystemValue = 0.108185245737
                Part.ClearSelection2 True
                Part.SketchManager.InsertSketch True
                
                
                swModelDocExt.FinishRecordingUndoObject2 "API Undo", False
                Part.EditUndo2 1
                
                
                End Sub