6 Replies Latest reply on Mar 4, 2013 1:49 PM by andrew_french

    Event Handler for Expedition

    andrew_french

      I want to add an event handler for Expedition so that when it closes it will set the default license to be used on next instantiation to Ascent.  I found the example for event handling in the documentation, copied it and made the changes for what I needed accomplished.  My problem is arising from code already populated in the example.

       

      "Set docPCB = GetLicensedDoc(appPCB)" is where the script dies when I am opening Expedition.

       

      And the error is:

      Type mismatch: 'GetLicensedDoc'

       

      Any help on fixing the error would be appreciated. Thanks.

       

      '

      ' The example below shows the event handler framework for an

      ' Expedition application object.

      '

      Dim docPCB

      Dim doNotExit

      doNotExit = True

       

      ' ==================================

      ' The application is quitting

      ' ==================================

      Sub appEvents_Quit()

       

          'Sets the default license back to Ascent

          Dim Reg, Path

          set reg = CreateObject("WScript.Shell")

          Path = "HKCU\Software\Mentor Graphics\ExpeditionPCB\Expedition PCB\2007.00\Licenses\"

          reg.RegWrite Path & "Pinnacle","0", "REG_DWORD"

          reg.RegWrite Path & "Ascent LX","0", "REG_DWORD"

          reg.RegWrite Path & "Ascent","1", "REG_DWORD"

       

         'MsgBox "Quitting the Application"

         'Exit the infinite loop

         doNotExit = False

         ' Make sure all objects are released "

         Set docPCB = Nothing

         Set appPCB = Nothing

      End Sub

       

      '

      ' Testing code for the sample

      '

      Dim appPCB

      Set appPCB = GetObject(,"MGCPCB.ExpeditionPCBApplication")

      Call Scripting.AttachEvents(appPCB, "appEvents")

      ' collect document object

      Set docPCB = GetLicensedDoc(appPCB)

      ' An infinite loop to prevent script from exiting

      Do While doNotExit

         Scripting.Sleep(300)

      Loop

        • 1. Re: Event Handler for Expedition
          andrew_french

          A friendly Mentor employee pointed out that I needed to include the code for GetLicensedDoc function which was provided earlier in the documentation that I had skipped. There is still an issue with launching Expediton with no errors running this.  I'll post the answer as I find the solutions.

           

          Public Function GetLicensedDoc(app)

             On Error Resume next

             Dim key,licenseServer,licenseToken,docObj

             Set GetLicensedDoc = Nothing

             ' collect the active document

             Set docObj = app.ActiveDocument

             If (Err) Then

                Call app.Gui.StatusBarText("No active document: " & Err.Description,epcbStatusFieldError)

                Exit Function

             End If

             ' Ask document for the key

             key = docObj.Validate(0)

             ' Get token from license server

             Set licenseServer = CreateObject("MGCPCBAutomationLicensing.Application")

             licenseToken = licenseServer.GetToken(key)

             Set licenseServer = Nothing

             ' Ask the document to validate the license token

             Err.Clear

             docObj.Validate(licenseToken)

             If (Err) Then

                Call app.Gui.StatusBarText("No active document license: " & Err.Description,epcbStatusFieldError)

                Exit Function

             End If

             ' everything is OK, return document

             Set GetLicensedDoc = docObj

          End Function

          • 2. Re: Event Handler for Expedition
            john_dube

            Hi Andrew,

             

            Yes, the GetLicensedDoc function is included in the PCB Automation Manual.

             

            You can use the Scripting.DontExit property to allow the script to handle events without looping and calling Sleep.

             

            John

            1 of 1 people found this helpful
            • 3. Re: Event Handler for Expedition
              john_dube

              I just noticed that you are only handling the Application Quit event, so you do not need to get and validate the document object.

               

              John

              • 4. Re: Event Handler for Expedition
                andrew_french

                so simply replace:

                ' An infinite loop to prevent script from exiting

                Do While doNotExit

                   Scripting.Sleep(300)

                Loop

                 

                with:

                Scripting.DontExit

                 

                ?

                • 5. Re: Event Handler for Expedition
                  andrew_french

                  When I do that the board still doesn't want to open when I double click a pcb file.

                   

                  Al suggested that I use [Expedition PCB - Document] in my scripts.ini file for my event handler which appears to work pretty nicely so far.  I was just using [Expedition PCB] previously.

                  • 6. Re: Event Handler for Expedition
                    andrew_french

                    its

                    Scripting.DontExit = True

                     

                    I was getting an error thrown when I exited Expedition and changing my event handler to use the Scripting.DontExit = True instead of the loop got rid of the error.

                     

                    Thanks for the help John!