Many circuit designs can have repeated structures. Sadly, pcbnew doesn’t have any features to make the placement and routing any more productive.
Here is a demo of a script I wrote to help:

If you have X copies of the same sheet in eeschema, the script will allow you to place and route one of them and them apply that to the other sheets.

How’s it work?

If you call GetPath() on the modules in your design, you’ll get values like these (note the the path is only the /number/number part):

mod Q1 path /587DA765/58758821
mod Q2 path /5875D13C/58758821
mod Q3 path /5875D13D/58758821
mod Q4 path /5875D13E/58758821
mod R1 path /587DA765/5875882F
mod R2 path /587DA765/5875883D
mod R3 path /5875D13C/5875882F
mod R4 path /5875D13C/5875883D

The first number tells you which sheet instance the module belongs to. The second is an identifier for the module. So from the information above we can tell that these are all in the same sheet:

mod Q1 path /587DA765/58758821
mod R1 path /587DA765/5875882F
mod R2 path /587DA765/5875883D

Similarly, we know that these are the same transistor, just in different instances of the sheet:

mod Q1 path /587DA765/58758821
mod Q2 path /5875D13C/58758821
mod Q3 path /5875D13D/58758821
mod Q4 path /5875D13E/58758821

So if I have placed and routed sheet 587DA765, the script just needs to find all of the members of 5875D13C and simply adjust their positions by some constant increment.
The script can be found here in my github repository for kicad

Replicating pcbnew new for arrayed sheets

11 thoughts on “Replicating pcbnew new for arrayed sheets

  • Pingback:Pan, Zoom, Refresh from python in pcbnew – Kicad Scripting blog

  • May 19, 2017 at 9:33 pm
    Permalink

    I am trying to get the script on github to work and it seems to stop after it prints the index of my part. Do you know if there are any limitations to the script? Two things that might be causing this issue is that I am using several multipart components as well as nested sub sheets.

    Reply
    • May 21, 2017 at 3:35 pm
      Permalink

      Hi Stephen,
      Thank you for your comment.
      I haven’t tried nested sheets, though I sort of thought about how to approach them when I wrote this. I believe (and I’m not sure on this) that my script should just look at the top level.
      If you are willing to send me a testcase, I’d be happy to look at it. This type of functionality seems to have a demand in the kicad community and would surely be worth the effort.

      Reply
  • July 14, 2017 at 4:23 pm
    Permalink

    Hi script fails with this:
    Traceback (most recent call last):
    File “”, line 1, in
    File “/home/heyyo/replicatelayout.py”, line 142, in
    SheetInstance.RegisterModulesAndNets(board)
    File “/home/heyyo/replicatelayout.py”, line 88, in RegisterModulesAndNets
    common = SheetInstance.NetIsSheetInternal(net)
    File “/home/heyyo/replicatelayout.py”, line 63, in NetIsSheetInternal
    for pad in net.Pads():
    File “/usr/lib/python2.7/dist-packages/pcbnew.py”, line 14991, in
    __getattr__ = lambda self, name: _swig_getattr(self, NETINFO_ITEM, name)
    File “/usr/lib/python2.7/dist-packages/pcbnew.py”, line 74, in _swig_getattr
    return _swig_getattr_nondynamic(self, class_type, name, 0)
    File “/usr/lib/python2.7/dist-packages/pcbnew.py”, line 69, in _swig_getattr_nondynamic
    return object.__getattr__(self, name)
    AttributeError: type object ‘object’ has no attribute ‘__getattr__’
    I think net.Pads() function is removed.

    Reply
    • December 29, 2017 at 2:18 pm
      Permalink

      The most recent version of replicate should fix this.

      Reply
  • February 11, 2018 at 11:55 am
    Permalink

    Hi Miles,
    thanks for all the hard work you put into this.
    The code fails for me with a similar error to what hyOzd got:
    Traceback (most recent call last):
    File “”, line 1, in
    File “/home/hal9000/Projekty/UHTSControl/kicad_mmccoo/replicatelayout/replicatelayout.py”, line 151, in
    SheetInstance.RegisterModulesAndNets(board)
    File “/home/hal9000/Projekty/UHTSControl/kicad_mmccoo/replicatelayout/replicatelayout.py”, line 97, in RegisterModulesAndNets
    for net in board.GetNetsByNetcode().values():
    File “/usr/lib/python2.7/dist-packages/pcbnew.py”, line 5022, in
    __getattr__ = lambda self, name: _swig_getattr(self, BOARD, name)
    File “/usr/lib/python2.7/dist-packages/pcbnew.py”, line 74, in _swig_getattr
    return _swig_getattr_nondynamic(self, class_type, name, 0)
    File “/usr/lib/python2.7/dist-packages/pcbnew.py”, line 69, in _swig_getattr_nondynamic
    return object.__getattr__(self, name)
    AttributeError: type object ‘object’ has no attribute ‘__getattr__’
    I get similar issues when I try to run scripts from your “Basics” post:
    # returns a dictionary netcode:netinfo_item
    netcodes = board.GetNetsByNetcode()
    # list off all of the nets in the board.
    for netcode, net in netcodes.items():
    print(“netcode {}, name {}”.format(netcode, net.GetNetname()))
    Traceback (most recent call last):
    File “”, line 2, in
    File “/usr/lib/python2.7/dist-packages/pcbnew.py”, line 5022, in
    __getattr__ = lambda self, name: _swig_getattr(self, BOARD, name)
    File “/usr/lib/python2.7/dist-packages/pcbnew.py”, line 74, in _swig_getattr
    return _swig_getattr_nondynamic(self, class_type, name, 0)
    File “/usr/lib/python2.7/dist-packages/pcbnew.py”, line 69, in _swig_getattr_nondynamic
    return object.__getattr__(self, name)
    AttributeError: type object ‘object’ has no attribute ‘__getattr__’
    This is on the Linux Mint repository version of KiCad (4.0.7). Is it because it’s outdated?

    Reply
    • April 3, 2018 at 11:24 am
      Permalink

      Hello Marcin
      When I look at the pcbnew source code, I see that GetNetsByNetcode was added in Sep-2016. Kicad 4.0.7 was released a year later, but surprisingly to me, that release is missing a boatload of python related stuff.
      I don’t know how I managed to miss that. I guess since I had to read the c sources to decypher the python stuff, I switched to my own builds pretty early on.
      Kicad 5.0 should be coming out soon (I think. The developer list has a bit of traffic on that.) In the meantime, I’ve had good luck with the latest code. The Kicad folks are good about keeping things functional.
      The download page has a blurb about how to get a nightly build. I suggest you try that.
      Either way, thank you for asking the question that prompted me to know this important detail. I will add a prominent note here somewhere.
      Miles

      Reply
    • September 7, 2018 at 8:18 am
      Permalink

      Thank you for taking the time to comment 🙂

      Reply
  • July 7, 2019 at 9:31 pm
    Permalink

    Thanks! A HUGE time saver. Have been looking for this a long time.
    On the wishlist; Maybe the footprint texts location/orientation can be copied too?

    Reply
  • July 9, 2019 at 9:21 pm
    Permalink

    Wow! Just what I was looking for. A huge time saver!
    Extra plus if maybe also reference text position/orientation can be copied?

    Reply

Leave a Reply to Markus Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.