2 Replies Latest reply on Oct 29, 2012 2:22 PM by robert.berrington

    Changing the number of PADS Layout Electrical Layers using the COM interface and Python


      I'm looking to automate the creation of new board files starting from a 2-layer board file (including CAM and Create PDF settings). We do many different kinds of board designs, so having multiple start-up files is impractical.


      It appears I can get the number of electrical layers through "Document.ElectricalLayerCount", but it isn't clear how I can change it.


      We're currently on PADS 9.3.1 update 3.

        • 1. Re: Changing the number of PADS Layout Electrical Layers using the COM interface and Python

          So here's the explination and the work-around.


          You can record macros from inside PADS Layout and grab commands/syntax for all sorts of things. You can then run these from the COM interface by using the "PowerPCBApp.RunMacro("", [command])", where [command] is the line you recorded in the macro.


          You can also just grab all the recorded macro pieces and put them in a file and call the file from you automation code using the same "PowerPCBApp.RunMacro([macro.mcr], "")". The kicker here is that the .RunMacro documentation suggests you can pick which macro "Sub" to run, but it really just operates on what's in-line, just as if you had copied the contents into the PADS Layout macro engine and pressed "play". So, it works, but you'll need separate files for each macro function you want to automate. Oh, and good luck passing it variables.


          Next, there's a different between Modal and Modeless dialog boxes when running from the COM interface. You can operate a bunch of Modeless dialog commands as individual calls to the COM interface and it works just fine. Modal dialog boxes are complicated. As soon as you open it, user interaction is required to handle the dialog box, and any settings you want to modify are lost. However, you can call a macro that has a sequence of actions that operates on a Modal dialog box, and that works fine.


          The work-around for commands to Modal dialog boxes is this: Write your commands to a temporary macro file, then call the file.


          My guess is that when you call Modal dialog box in-line, the code connects to the COM interface to open the dialog box, then disconnects, ending the session with that box. However if you pass all your commands in through a macro (like a Trojen Horse), the PADS Layout macro engine will complete all the commands before the code disconnects from the COM interface.

          • 2. Re: Changing the number of PADS Layout Electrical Layers using the COM interface and Python

            Oh, the solution looks like this (in Python):


            def main():
                global powerPCBapp


                #  Try and launch PADS Layout, it may already be open
                    powerPCBapp = win32com.client.Dispatch("PowerPCB.Application")
                    powerPCBapp.Visible = 1
                except pythoncom.com_error, (hr, msg, exc, argv):
                    print "Can't invoke PADS Layout"
                    sys.exit("Terminating script.")


                # Construct the commands for the modal dialog box for setting the electrical layer count, these are copied from the "Record macro" operation in PADS Layout
                commands = [
                           "Application.ExecuteCommand(\"Layer Setup\")", # Open the dialog box
                           "LayersSetupDlg.Modify.Click()", # Modify the number of electrical layers
                           "ModifyElectricalLayerCountDlg.EnterNewNumberOfElectricalLayersBetween2And20 = " + str(ElectricalLayerCount),
                           "ReassignElectricalLayersDlg.Ok.Click()", # Accept the default action that maps Layer_1 -> Layer_1 and Layer_2 -> Layer_nLayerCount
                HandlePowerPCBModalCommands(commands, "Setting the number of Electrical layers...")

            def HandlePowerPCBModalCommands(commands, message):
                macro_path = "c:\\temp"
                macro_filename = "temp.mcr"
                # Construct the full path\filename and open the file
                macro_path_filename = macro_path + "\\" + macro_filename
                # Add the \n character to each entry
                for n in range(len(commands)):
                    commands[n] = commands[n] + "\n"
                # Write the statements to a temp file
                with open(macro_path_filename, 'w') as pFile:
                # Execute the commands as a nested macro
                powerPCBapp.RunMacro(macro_path_filename, "")
                StatusBar(message + " Complete!")
                # Delete the file to make room for the next one