18 Replies Latest reply on Dec 22, 2017 11:11 AM by Ulf Stockburger

    Exit macro with escape?

    John Boire

      Background:
      I am attempting to write a macro that will allow users to select lines on their drawings and then the lines will become bolded.
      That part of the code is working great, however it just wont stop.  The macro keeps running and I cannot end it. I have included the text for users to check.

       

      I do not know exactly what is happening here as this is my first time dealing with classes

       

      Code text:

       

      Dim swApp As SldWorks.SldWorks

       

      Dim swModel As SldWorks.ModelDoc2

      Dim swDraw As SldWorks.drawingDoc

      Dim selMgr As SldWorks.SelectionMgr

       

      Dim boolstatus As Boolean

      Dim drawingDoc As New Class1

       

      Sub main()

       

      Set swApp = Application.SldWorks

      Set swModel = swApp.ActiveDoc

       

      drawingDoc.init swModel

       

      End Sub

       

      Class1:

      Public WithEvents doc As drawingDoc

       

      Public Function init(ByRef docin As Object)

          Set doc = docin

      End Function

       

      Private Function doc_UserSelectionPostNotify() As Long

              doc.SetLineWidth 3

      End Function

          • Re: Exit macro with escape?
            John Boire

            Thank you, this isn't quite what I wanted but is definitely helping, my new code is below.

            Now unless the shift key is pressed the selection wont turn bold

             

            Dim swApp As SldWorks.SldWorks

             

            Dim swModel As SldWorks.ModelDoc2

            Dim swDraw As SldWorks.drawingDoc

            Dim selMgr As SldWorks.SelectionMgr

             

            Dim boolstatus As Boolean

             

            Dim drawingDoc As New Class1

             

            Public Const SHIFT_KEY = &H10    'Shift

            Public Const ESC_KEY = &H1B

            Declare PtrSafe Function GetKeyState Lib "USER32" (ByVal vKey As Long) As Integer

             

            Sub main()

             

            Set swApp = Application.SldWorks

            Set swModel = swApp.ActiveDoc

             

            drawingDoc.init swModel

            End Sub

             

            Class1

            Public WithEvents doc As drawingDoc

             

            Public Function init(ByRef docin As Object)

                Set doc = docin

            End Function

             

            Private Function doc_UserSelectionPostNotify() As Long

             

                If GetKeyState(SHIFT_KEY) < 0 Then

                    doc.SetLineWidth 3

                ElseIf GetKeyState(ESC_KEY) < 0 Then

                      'IDK what to put here, end Function causes syntax error same with end sub

                      'I want to have the macro end here

                End If

                   

            End Function

            • Re: Exit macro with escape?
              John Boire

              Ulf Stockburger wrote:

               

              Hi John

               

              Take a look here: How to detect keyboard key presses at the start of a macro run

               

              BiI SAP-PDM

              After working a little I tried the code below:


              This just causes solidworks to crash so I believe it is a step backwards, however I can't figure out where I am going wrong.

               

              Option Explicit

               

              Dim swApp As SldWorks.SldWorks

               

              Dim swModel As SldWorks.ModelDoc2

              Dim swDraw As SldWorks.drawingDoc

              Dim selMgr As SldWorks.SelectionMgr

               

              Dim boolstatus As Boolean

               

              Dim drawingDoc As New Class1

               

              Public Const SHIFT_KEY = &H10    'Shift

              Public Const ESC_KEY = &H1B

              Declare PtrSafe Function GetKeyState Lib "USER32" (ByVal vKey As Long) As Integer

               

              Sub main()

               

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

              Dim i As Integer

              i = 1

                 

              Do While i <> 0

                 

                  drawingDoc.init swModel

                 

                  If GetKeyState(ESC_KEY) < 0 Then

                      i = 0

                  Else

                  End If

              Loop

                 

              End Sub

               

              Class1:

              Public WithEvents doc As drawingDoc

               

               

              Public Function init(ByRef docin As Object)

                  Set doc = docin

              End Function

               

               

              Private Function doc_UserSelectionPostNotify() As Long

                      doc.SetLineWidth 3

              End Function

                • Re: Exit macro with escape?
                  Ulf Stockburger

                  Hi John

                   

                  I'm very busy now, but I have time again tomorrow.

                   

                  BiI SAP-PDM

                  • Re: Exit macro with escape?
                    Ulf Stockburger

                    Hi John

                     

                    Yesterday I understood that you want to exit the macro by pressing the ESC key. That's why I pointed to this article. Below is my suggestion for this case.

                     

                    Class 1:

                    Public WithEvents doc As drawingDoc

                    Private Declare PtrSafe Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Long) As Long

                    Public Function init(ByRef docin As Object)

                        Set doc = docin

                    End Function

                    Private Function doc_UserSelectionPostNotify() As Long

                        Dim nRetVal As Long

                        nRetVal = GetAsyncKeyState(&H1B)

                        If nRetVal = -32767 Then

                            If MsgBox("Exit?", vbYesNo) = vbYes Then

                                End

                            End If

                        End If

                        doc.SetLineWidth 3

                    End Function

                     

                    I often use this in my macros to assign different functions to the mouse by keyboard. In this case, it could be different line thicknesses, for example.

                     

                    BiI SAP-PDM

                      • Re: Exit macro with escape?
                        John Boire

                        Thank you, maybe you could explain something to me:
                        I got the macro working to exit on escape using the method you showed me through that article, however it only worked when I held escape and clicked at the same time. Do the events within "WithEvents" only fire on a mouse command?

                          • Re: Exit macro with escape?
                            Josh Brady

                            The only "event" that you have told SW that you want to get notified for is when the user makes a selection.  That's what

                             

                            doc_UserSelectionPostNotify

                             

                            does.  It says "Please tell me every time the user selects something in doc".  That's when the code runs - when the user selects something.  So using the original method that Ulf posted, your code only checks the key state at that instant when the user makes a selection.  If you're not holding the key when you select something, the code doesn't see it.

                             

                            The second method that Ulf posted still only runs when the user makes a selection.  But using the GetAsyncKeyState basically asks Windows "Hey, has somebody pressed this key since the last time I asked?" instead of asking "Is somebody pressing the key right now?"

                             

                            So the macro doesn't actually quit when you hit the "Esc" key.  It only quits if you hit Esc and then make another selection.

                            • Re: Exit macro with escape?
                              Ulf Stockburger

                              Hi John

                               

                              The check for ESC (GetAsyncKeyState) is triggered in the event that is fired when a drawing is clicked. That's why you have to do both. If you want to exit at any time with ESC, you can do this.

                               

                              Code text:

                              Option Explicit

                              Private Declare PtrSafe Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Long) As Long

                              Dim swApp As SldWorks.SldWorks

                              Dim swModel As SldWorks.ModelDoc2

                              Dim swDraw As SldWorks.drawingDoc

                              Dim selMgr As SldWorks.SelectionMgr

                              Dim boolstatus As Boolean

                              Dim breakThis As Boolean

                              Dim drawingDoc As New Class1

                              Sub main()

                                  Dim nRetVal As Long

                                  Dim key As Boolean

                                  Set swApp = Application.SldWorks

                                  Set swModel = swApp.ActiveDoc

                                  drawingDoc.init swModel

                                      Dim stopMacro As Boolean

                                  Do

                                      nRetVal = GetAsyncKeyState(&H1B)

                                      If nRetVal = -32767 Then

                                          If MsgBox("Exit?", vbYesNo) = vbYes Then

                                              drawingDoc.stopMacro = True

                                          End If

                                      End If

                                      DoEvents

                                  Loop Until drawingDoc.stopMacro

                                  End Sub

                               

                              Class 1:

                              Option Explicit

                              Public WithEvents doc As drawingDoc

                              Public stopMacro As Boolean

                              Public Function init(ByRef docin As Object)

                                  Set doc = docin

                              End Function

                              Private Function doc_UserSelectionPostNotify() As Long

                                  If stopMacro Then

                                      End

                                  Else

                                      doc.SetLineWidth 3

                                  End If

                              End Function

                               

                              Be careful, the ESC check is global. If SolidWorks runs in the background and ESC is pushed in another application, the prompt for exit is displayed in SolidWorks. I don't like it, but it works.

                               

                              BiI SAP-PDM

                      • Re: Exit macro with escape?
                        Josh Brady

                        With reference to your initial macro posted (not any of the replies, but the original)

                        Replace

                         

                        Private Function doc_UserSelectionPostNotify() As Long

                                doc.SetLineWidth 3

                        End Function

                         

                        with

                         

                        Private Function doc_UserSelectionPostNotify() As Long
                                If doc.SelectionManager.GetSelectedObjectType3(1, -1) <> 1 Then
                                    If MsgBox("You selected item with type: " & _ 
                                        doc.SelectionManager.GetSelectedObjectType3(1, -1) & _ 
                                        vbCrLf & "Terminate macro?", vbYesNo) = vbYes Then End
                                Else
                                    doc.SetLineWidth 3
                                End If
                        End Function
                        

                         

                        If your user selects something that is not a line, they will be asked if they want to terminate the macro.

                        • Re: Exit macro with escape?
                          John Boire

                          Almost positive. Let me do a clean copy and paste into a new macro file and restart solidworks to double check.