7 Replies Latest reply on May 8, 2018 3:24 AM by martinthompson

    Adding vias with python


      Hi all,


      First, for background: I'm a proficient Python programmer, and have used Python to script modifications to my libraries successfully.


      I'm new to PCB automation, and tried to writer a quick script to add a via today, to no avail.


      Here's the bare bones:

         app = win32com.client.Dispatch('MGCPCB.ExpeditionPCBApplication')

          doc = app.ActiveDocument


          vianame = "RFEVia_0.15mm hole, 0.45mm pad"


          viastack = None

          for padstack in doc.Padstacks:

              if padstack.Name == vianame:

                  viastack = padstack

                  print "This is the padstack"


          if viastack is None:

              print "Can't find  a padstack called", vianame


          via = doc.PutVia(10,10, viastack)


      which fails as viastack is apparently not a COM object - which surprised me!

      Traceback (most recent call last):

        File "test1.py", line 79, in <module>


        File "test1.py", line 68, in test


        File "<COMObject <unknown>>", line 5, in PutVia

      TypeError: The Python instance can not be converted to a COM object


      Can anyone suggest how I do this please?


        • 1. Re: Adding vias with python

          In Xpedition you have to have a call to get the Automation license.  It also looks like you are missing arguments for the putvia command.


          Please see the Python examples in AATK/Examples/Python



          • 2. Re: Adding vias with python

            Thanks Kendall -


            the license is taken in ValidateServer() - a function call name that I took from an example somewhere - it could do with a better name


            As far as I can tell, putvia only has 3 mandatory arguments:

            Document.PutVia(ByVal dX As Double,

            ByVal dY As Double,

            ByVal pPadstack As IMGCPCBPadstack,

            [ByVal pNet As IMGCPCBNet],

            [ByVal pComponent As IMGCPCBComponent],

            [ByVal eFixed As EPcbAnchorType = epcbAnchorNone],

            [ByVal eUnit As EPcbUnit = epcbUnitCurrent]) As IMGCPCBVia


            Am I reading this wrong?


            Thanks for the pointer to the examples.  Forgive my naivety, but I cannot find an examples\python folder anywhere - can you point me more directly?  Should I have installed them?



            • 3. Re: Adding vias with python

              You are correct, I would have swore the pNet was required,  if no net, it adds it a (Net0).


              this works in VB:


                  For Each Padstack In pcbdoc.Padstacks

                      If Padstack.Name = vianame Then

                          Set viastack = Padstack

                          'Debug.Print "This is the padstack"
                          pcbdoc.TransactionStart (epcbDRCModeNone)
                          Via = pcbdoc.PutVia(1200, 10, viastack)
                      End If




              • 4. Re: Adding vias with python

                Thanks Kendall - it must be something Pythony then... could you give me any more details about where I find the python examples folder please?




                • 5. Re: Adding vias with python

                  I think two problems were at the root of this:

                  1) I was not using the gencache module to create the datastructures which Python required, and also give access to the constants.epcbUnitCurrent required in step 2:

                  app = win32com.client.gencache.EnsureDispatch('MGCPCB.ExpeditionPCBApplication')

                  2) The optional parameters are not optional in Python it seems, so I had to use this:

                  via = doc.PutVia(10,10, viastack, vianet, None, constants.epcbAnchorNone, constants.epcbUnitCurrent)

                  Now it works

                  • 6. Re: Adding vias with python

                    martinthompson wrote:


                    [ ...snipped ... ]


                    Thanks for the pointer to the examples. Forgive my naivety, but I cannot find an examples\python folder anywhere - can you point me more directly? Should I have installed them?



                    The examples Kendall mentioned, and there are only a couple of them and they are very old, are in AATK which you can download from SourceForge.  The Python examples are in the AATK/Examples/Python directory.


                    There is a much more robust example / application which I wrote a couple years ago to import GDS data into Xpedition.  It was done for a very specific use case we had a while back and while limited, it does have lots of useful examples of using Python make Xpedition API automation calls.  You can find this utility on GitHub, there is some documentation included with it and the code is commented.  One of the challenges in working with languages like Python or Tcl is the need to convert arrays or tuples into Points Arrays.  There are some examples in the GDS import implementation which do this.


                    While I like Python quite a bit, the lack of a Linux solution which will work with Xpedition keeps me from doing much with it.  Python, much like using Tcl with Xpedition, requires arguments to be more specific than VBscript does.  Many of the things which are optional in VBscript aren't when using Python or Tcl.  I can't tell you exactly why it is necessary, but that has been my experience.

                    • 7. Re: Adding vias with python

                      Many thanks Mike - I happened upon your GDS utility before I saw your reply, and found it very helpful, especially on the "lists of coordinates"