8 Replies Latest reply on Nov 7, 2012 10:13 AM by volker.hetzer

    Expedition keyins - how to extend?

    volker.hetzer

      Hi!

      I'd like to write an addistional KeyIn for ExpeditionPCB, that is, a command with parameters.

       

      In DxDesigner it's relatively easy, DxD has a method "ProcessCommand" which expects a commandline and calls a VBScript function that has been registered using RegisterOLECommand.

       

      The closest ExpeditionPCB equivalent I've found is the "keyin" (like "fr D100" for finding a component with the reference designator D100). But how do I register a new keyin in ExpeditionPCB?

       

      Lots of Greetings!

      Volker

       

      spelling fixed

        • 1. Re: Expedition keyins - how to extend?
          Patrick.Cashman

          There is a bit of a process.

           

          1. In your WDIR directory, there is a scripts.ini file.  In that file, you need a line like this:

           

          [Expedition PCB]

          Script#0=<path to key bindings vbs file>\my_key_bindings.vbs

           

          2.  The my_key_bindings.vbs file (it can be named whatever you want; just reference it correctly in scripts.ini) should look something like this:

           

          Option Explicit

           

          Dim My_App       ' The application

          Dim My_Board     ' The Board

          Dim Gui

          Dim BindMenu

          Dim BindAccelerator

          Dim BindCommand

          Dim keyBindTables

           

          Set My_App = Application

          Set My_Board = My_App.ActiveDocument

          Set Gui = My_App.Gui

           

           

          BindMenu = 0

          BindAccelerator = 1

          BindCommand = 1

           

          Set keyBindTables = Gui.Bindings("Document")

          ' Bind the execution of a user script to a key

           

          'select entire net and lock it

          keyBindTables.AddKeyBinding "Ctrl+L", "run D:\_software_development\vb_apps\Mentor_Expedition\Hotkeys\select_entire_net_and_lock.vbs", BindCommand, BindAccelerator

           

          'select entire net and unlock it

          keyBindTables.AddKeyBinding "Ctrl+U", "run D:\_software_development\vb_apps\Mentor_Expedition\Hotkeys\select_entire_net_and_unlock.vbs", BindCommand, BindAccelerator

           

          'run the stretch trace command

          keyBindTables.AddKeyBinding "Ctrl+Q", "run D:\_software_development\vb_apps\Mentor_Expedition\Hotkeys\run_stretch_trace_command.vbs", BindCommand, BindAccelerator

           

           

          3. Each of the vbs files referenced in the my_key_bindings.vbs file should look something like this: (take note of the use of ProcessCommand here)

           

          For example, run_stretch_trace_command.vbs

           

          Option explicit

           

          dim app

          dim doc

          dim gui

           

          Set app = GetObject(, "MGCPCB.Application")

          set gui = app.gui

          Call Scripting.AddTypeLibrary("MGCPCB.Application")

           

          ' get the (licenced) active document

          Set doc = GetLicensedDoc(app)

           

          If not doc is nothing then

              'msgbox("in the doc")

           

              gui.processcommand("Smart Utilities->Design Editing Aids->Drafting Trace Stretch")

           

          End if

           

          Set app = Nothing

          Set doc = Nothing

           


           

          4. Each of the vbs files with the actual commands has a line that calls the GetLicencedDoc function, so it needs to be included in all of them:  (just put it at the bottom)


          '-------------------------------------------------------------------------

          ' Function that returns a licensed doc object

          '

          ' Returns:

          '   - 'Nothing' if licensing failed .

          '   - A reference to the active document of the application if

          '     licensing succeeded.

          '-------------------------------------------------------------------------

          Public Function GetLicensedDoc(appObj)

              On Error Resume next

              Dim key,licenseServer,licenseToken,docObj

           

              Set GetLicensedDoc = Nothing

           

              ' collect the active document

              Set docObj = appObj.ActiveDocument

              If (Err) Then

                  Call appObj.Gui.StatusBarText("No active document: " + _

                                           Err.Description,epcbStatusFieldError)

                  Exit Function

              End If

           

              ' Ask Expedition’s 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 appObj.Gui.StatusBarText("No active document license: " + _

                                                Err.Description,epcbStatusFieldError)

                  Exit Function

              End If

           

              ' everything is OK, return document

              Set GetLicensedDoc = docObj

          End Function

           

           

           

          That is all you need.  There may be some simpler method that doesn't require a series of 3 different files, but this method works and has been quite reliable for me.  Also it's not slow.  The commands execute very quickly, so I don't think the separte files slows it down much.

          2 of 2 people found this helpful
          • 2. Re: Expedition keyins - how to extend?
            volker.hetzer

            Hi!

            That's great, but I'm not looking for a Keybinding, I'm looking for a keyin:

            Clipboard01.bmp

             

            Lots of Greetings!

            Volker

            • 3. Re: Expedition keyins - how to extend?
              Patrick.Cashman

              I'm sorry, I'm lost on what you mean.  You mention "a command with parameters".  You can execute any command with the methods above, and with the scripting they include, add parameters, pass values, get info from the design, or whatever.

               

              Perhaps if you could tell how you want to execute these commands - do you want to click on something, type in something, execute a string of commands in some order programmatically, or what?  That would help.  As it is I don't get how either:

               

              - adding menu items

              - using an external executable or efm

              - setting up hotkeys

               

              would not adequately satisfy the need to interact with the tool. All automation functions I know of can be accessed using one or more of these three methods.

              • 4. Re: Expedition keyins - how to extend?
                matthias.cosaert

                The Application object has an OnProcessKeyin event that can be used to make custom keyins.

                Usage

                Function Application_OnProcessKeyin(ByVal keyinText As String) As Boolean

                Description

                Fired when keyin data is processed. The client gets the first chance to process it.

                Return False to indicate the system should process. Returns True to keep it from being processed.

                Arguments

                • keyinText

                A string that contains the keyin text.

                 

                Here's an example:

                 

                '-------------------------------------------------------------------------
                ' Add any type libraries to be used.
                Scripting.AddTypeLibrary("MGCPCB.ExpeditionPCBApplication")
                Dim pcbApp  'Application object
                Dim pcbDoc  'Document object
                ' Get the application object.
                Set pcbApp = Application
                ' Attach events
                Call Scripting.AttachEvents(pcbApp, "Application")
                '-------------------------------------------------------------------------
                
                Scripting.dontexit = true
                
                Function Application_OnProcessKeyin(keyinText)
                
                 Application_OnProcessKeyin = false
                
                 ' Get the active document
                 Set pcbDoc = pcbApp.ActiveDocument
                
                 if NOT(pcbDoc is nothing) then
                  ' License the document
                  ValidateServer(pcbDoc)
                
                  if pcbDoc.isvalid = true then
                   'msgbox keyinText
                   if LCASE(keyinText) = LCASE("MyCommand") then
                    Msgbox "Add command actions here"
                    Application_OnProcessKeyin = true 'Do not let Expedition process command
                   end if
                  end if
                 end if
                end function
                
                '=============================================================
                ' Validate server function
                '=============================================================
                Private Function ValidateServer(doc)
                
                 Dim key, licenseServer, licenseToken
                key = doc.Validate(0)
                ' Get license server
                 Set licenseServer = CreateObject("MGCPCBAutomationLicensing.Application")
                ' Ask the license server for the license token
                 licenseToken = licenseServer.GetToken(key)
                ' Release license server
                 Set licenseServer = nothing
                ' Turn off Error messages.  Validate may fail if the token is incorrect
                 On Error Resume Next
                 Err.Clear
                ' Ask the document to validate the license token
                 doc.Validate(licenseToken)
                 If Err Then
                  ValidateServer = 0
                 Else
                  ValidateServer = 1
                 End If
                End Function
                '=============================================================
                
                • 5. Re: Expedition keyins - how to extend?
                  volker.hetzer

                  Hi!

                  What I want is something comparable to the DxDesigner "Application.ProcessCommand" call. Unlike its ExpeditionPCB equivalent, the DxDesigner call allows the passing of parameters:

                  Application.ProcessCommand("generateData c:\temp\x.dat")

                   

                  At that point, DxDesigner calls a VBScript procedure that has been registered before and which has the signature:

                  sub MyFunctionName(Rest_of_line)

                  end sub

                   

                  where Rest_of_Line gets filled with, in this case c:\temp\x.dat by DxDesigner.

                   

                  What the user can do (although here it's not what I want) is, opening the Command bar in DxDesigner and typing in "generateData c:\temp\x.dat", at which point my VBScript function MyFunctionName (that is, not the script, the function in the script) gets called with the parameter.

                   

                  ExpeditionPCB apparently allows something similar, for the user, in the form of Keyins, see the screenshot of the posting you replied to.

                  What I wanted to know (and what Matthias has answered) is how I use this programmatically.

                   

                  I'm going to call my Keyins by calling Application.Gui.ProcessKeyin("generateData c:\temp\x.dat").

                   

                  Lots of Greetings!

                  Volker

                  • 6. Re: Expedition keyins - how to extend?
                    volker.hetzer

                    That's it.

                    Thanks a lot!

                    Volker

                    • 7. Re: Expedition keyins - how to extend?
                      Patrick.Cashman

                      OK, now I understand.  Looks like there's a solution

                       

                      I guess I'm geared toward avoiding the need to type to execute a function as much as possible, so I tend to employ solutions that use other methods.  Such as the file open dialog, file save dialog, consistent filename conventions throughout the design and the design community, and so on.  And if I need input from a user, I provide an inputbox or some other place to enter the parameter into the tool.

                       

                      I get it that you are using a different method, and that's cool.  It just didn't occur to me to think about it like that.

                       

                      Glad you got it sorted.

                      • 8. Re: Expedition keyins - how to extend?
                        volker.hetzer

                        You're right, normally we don't have the user do commandlines either.

                         

                        The problem we're trying to solve here is the huge speed penalty one incurs when connecting to ExpeditionPCB via an external COM client.

                         

                        So, what we do is, we implement the COM intensive stuff in such a keyin, which runs inside Expedition and therefore is fast, and we then get the data in one lump, like as a file or via named pipes, into the external application.

                         

                        One possible usage model is:

                        • start excel with a bunch of macros
                        • one of the macros connects to Expedition
                        • the macro calls the "getData" keyin with a file name as parameter
                        • the keyin gets the data really quickly and puts it into the file or the pipe
                        • Our excel Macros read the data and present it to the users, who do things to it.
                        • While the user edits the data in excel, we write the changes back to ExpeditionPCB but this time we use COM because single values are quick enough not to need any speedups.

                         

                         

                        Lots of Greetings!