modifying pcbnew layout from python

I my previous post, I talked about querying for information about your layout. In this one I’ll show you how to create your own wires/vias from python. I also cover moving modules. Most of the code is self-explanatory but I find it helpful to have sample “cookbook” code.

The main thing to keep in mind when creating new objects is that even though you have to pass a board pointer to the constructors, you still have to call board.Add(obj). Also, you have to add it before setting the net. If you try to set the net before, it’ll do nothing.

Remember that the units are 1E-6mm. So if you have a mm value multiply it by a million.


First, let’s generate our layer mapping

layertable = {}

numlayers = pcbnew.LAYER_ID_COUNT
for i in range(numlayers):
 layertable[i] = board.GetLayerName(i)
 print("{} {}".format(i, board.GetLayerName(i)))

Add a track

track = pcbnew.TRACK(board)
track.SetStart(pcbnew.wxPoint(136144000, 95504000))
track.SetEnd(pcbnew.wxPoint(176144000, 95504000))


Add a via

In this case, I’m going to copy an existing via. Note that there is also the clone method, but doing it this way you’ll know how to generate a via from scratch.

There isn’t yet a direct way to query a via for its layers. The way I work around this is by looping through all layers and calling IsOnLayer This is one of the reasons I’m showing how to copy an existing via.

The via types will be one of these:

  • pcbnew.VIA_THROUGH
  • pcbnew.VIA_MICROVIA

The width if the via is the diameter. If you forget to set this, you’ll get funny behavior where via disappears from the display when you zoom in.

newvia = pcbnew.VIA(board)
# need to add before SetNet will work, 
# so just doing it first

for l in range(pcbnew.LAYER_ID_COUNT):
   if not track.IsOnLayer(l):
   toplayer = max(toplayer, l)
   bottomlayer = min(bottomlayer, l)

# now that I have the top and bottom layers, I tell the new
# via
newvia.SetLayerPair(toplayer, bottomlayer)

Moving a module

I haven’t tried creating a new module yet. I prefer to let the netlist importer do this for me. I do, however, find it useful to be able to move modules. They all come in on top of each other. There are a variety of placement algorithms one might want to implement. 1

Note that the orientation is degrees*10.0

peer.SetPosition(pcbnew.wxPoint(newx, newy))

Class diagram

I’ve created a class diagram to help me remember. Click to enlarge. Also available in my github

  1. In my previous professional life, one of the more interesting ones I saw was using linear programming. You start with everything in the middle. You create a set of equations representing net connectivity as well as cell overlaps. Solve the equations. Repeat. It was good for a couple hundred thousand cells. Much more than what PCB requires. Simulated Annealing would likely be easier here.

The basics of scripting in pcbnew

I’ve found that, so far, 1 I’m able to do all of the layout queries and manipulations I’ve wanted to do.

Note that these examples don’t work on the latest release as of Feb 2017 (4.0.5). You want one of the nightly builds, also in the kicad download area. Or you can build it

The interface is lacking some consistency but it’s fine if you have a map of the classes (click for a larger version or download it from here):


In this post, I’ll focus on querying a board for information about it’s contents. The code can be found in this github repo.

Getting started

To invoke any of these examples, you’ll want pcbnew’s scripting window. Tools->scripting console

You’ll probably want these in your scripts

import pcbnew

# most queries start with a board
board = pcbnew.GetBoard()


Want to know all of the nets in your board?
Nets can be looked up in two ways:

  • by name
  • by netcode – a unique integer identifier for your net.

If you run this code:

# 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()))

# here's another way of doing the same thing.
print("here's the other way to do it")
nets = board.GetNetsByName()
for netname, net in nets.items():
    print("method2 netcode {}, name{}".format(net.GetNet(), netname))

# maybe you just want a single net
# the find method returns an iterator to all matching nets.
# the value of an iterator is a tuple: name, netinfo
clknet = nets.find("/clk").value()[1]
clkclass = clknet.GetNetClass()

print("net {} is on netclass {}".format(clknet.GetNetname(),

You’ll get something like this:

netcode 49, name /ihg
netcode 50, name /ihh
netcode 51, name /data_in
netcode 52, name /data_out
netcode 53, name /clk
here's the other way to do it
method2 netcode 0, name
method2 netcode 23, name+5V
method2 netcode 53, name/clk
method2 netcode 55, name/data_contd
method2 netcode 51, name/data_in

Physical dimensions

The coordinate space of kicad_pcb is in mm. At the beginning of this wiki about Kicad’s Board_File_Format

“All physical units are in mils (1/1000th inch) unless otherwise noted.”

Then later in historical notes, it says,

“As of 2013, the PCBnew application creates ‘.kicad_pcb’ files that begin with (kicad_pcb (version 3)”. All distances are in millimeters.

In short, for the data that I’ve recently created, the internal coordinate space of pcbnew is 10E-6 mm. (a millionth of a mm)
For example, the coordinate 121550000 corresponds to 121.550000mm

SCALE = 1000000.0

boardbbox = board.ComputeBoundingBox()
boardxl = boardbbox.GetX()
boardyl = boardbbox.GetY()
boardwidth = boardbbox.GetWidth()
boardheight = boardbbox.GetHeight()

print("this board is at position {},{} {} wide and {} high".format(boardxl,


Each of your placed modules can be found with its reference name. The module connection points are pads, of course.

# generate a LUT with shape integers to a string
padshapes = {
# new in the most recent kicad code
if hasattr(pcbnew, 'PAD_SHAPE_ROUNDRECT'):

modref = "U1"
mod = board.FindModuleByReference(modref)
for pad in mod.Pads():
    print("pad {}({}) on {}({}) at {},{} shape {} size {},{}"
                pad.GetPosition().x, pad.GetPosition().y,
                pad.GetSize().x, pad.GetSize().y

Gives you this:

pad 1(/ilb) on U1(74HC595) at 127635000,106520000 shape PAD_SHAPE_RECT size 1500000,600000
pad 2(/ilc) on U1(74HC595) at 126365000,106520000 shape PAD_SHAPE_RECT size 1500000,600000
pad 3(/ild) on U1(74HC595) at 125095000,106520000 shape PAD_SHAPE_RECT size 1500000,600000


Most of the pcb data is on a layer. pcbnew stores layers as numbers. Here we can print them all out

layertable = {}

numlayers = pcbnew.LAYER_ID_COUNT
   for i in range(numlayers):
       layertable[i] = board.GetLayerName(i)
       print("{} {}".format(i, board.GetLayerName(i)))

Which gives you this:

0 F.Cu
1 In1.Cu
2 In2.Cu
3 In3.Cu

Tracks (wires and vias)

A wire is stored in a TRACK object. Vias are in a class derived from TRACK. Let’s list some of them.

# clk net was defined above as was SCALE
clktracks = board.TracksInNet(clknet.GetNet())
   for track in clktracks:
       print("{},{}->{},{} width {} layer {}".format(track.GetStart().x/SCALE,

And here are the wires

121.92,97.79->126.492,97.79 width 0.25 layer F.Cu
127.762,100.33->125.984,100.33 width 0.25 layer F.Cu
127.762,99.314->127.762,100.33 width 0.25 layer B.Cu

So that’s how to query data. In my next post, I’ll talk about making changes

  1. thanks to recent work by Kicad contributor/developer Dick Hollenbeck

Compiling Kicad on Ubuntu

In my last post, I mentioned that compiling Kicad is something most would not be willing to do.

Most of the scripting in this blog will not work in version 4.0.5 which the most recent as of this writing. I recommend to install either the nightly or build yourself. If you just want the latest verion of Kicad, then you can find nightly builds in the Kicad Downloads Area

Well, this post is about the steps needed to run your own compiled version on Ubuntu 1. These steps worked for me on a freshly installed VirtualBox Ubuntu install.

The scripting support in the latest source based kicad has progressed a bit beyond the released Kicad. There are one or two APIs that have appeared and the scripting window has some IDE’ish features added 2. I do want to make some C++ changes, so I have the need to compile. I was also helpful to step through execution to understand some of the APIs

VirtualBox Ubuntu install

These are my notes from installing Ubuntu VirtualBox.

  • create virtual machine
  • linux – ubuntu 64
  • 4 GB ram
  • virtual disk image
  • file location and size to 32GB


virtualbox settings->storage->controller IDE->empty = set to ubuntu-16.04.1-desktop-amd64.iso

start machine

install ubuntu
no install updates
erase and install
write the changes to disks – continue
set time zone
set keyboard

put in login details


it will want to reboot. do that now.
it will say to remove the install medium. VirtualBox seems to do that automagically

At this point you’ll have a clean install of ubuntu.

Take a snapshot so you’ll have it later.

in VirtualBox manager in the upper right. click snapshots
click on the blue camera and take a snapshot

start the machine again
go the the devices menu at the top-> shared clipboard -> bidirectional
devices menu -> insert guest additions
click run on the popup
give your password

start an xterm

Build Kicad

Based on a list here: with some additions:

sudo apt-get -y install libwxgtk3.0-0v5 libglew-dev libcairo2-dev libbz2-dev doxygen libssl-dev libboost-dev libboost-thread-dev libboost-context-dev libboost-filesystem-dev libboost-iostreams-dev libboost-locale-dev libboost-program-options-dev swig python-wxgtk3.0* git cmake libwxgtk3.0 libglm-dev libcurl3 libcurl3-dev python-dev

mkdir kicad
cd kicad
mkdir build
mkdir install

git clone -b master
cd build

make install


If you install into something other than /usr/lib; if you use prefix as is shown above, you’ll need to do this:

export LD_LIBRARY_PATH=`realpath ../install/lib/`

This is for Ubuntu 16.04.1. If these directions stop working, please leave a comment below.

  1. I have basically stopped using the Windows platform. Adobe Lightroom and Autodesk Fusion are the only reasons I bother with it at all. Sorry.

  2. it’s very possible that the IDE stuff is a freebie due to using a more recent Python component.

Real Scripting. The most important feature a tool can have.

One of the dreams of Free and Open Source Software (FOSS) is that if a program doesn’t do what you want, you can add it. This is frequently said, but I don’t think it’s as true as we’d like it to be or at least not as true as it could be. Kicad is open source and used by many. It’s also missing a lot of features, but what would it take for someone to contribute?

First, it’s written in C++ which is intimidating to many. I’d guess that for many (most?) folks who want to design a PCB, C++ means Kicad is effectively closed source.

Second, even if one is comfortable with C++, many (most?) compiled projects are not so easy to replicate. Just building and running the unmodified code is an obstacle.

So most potential contributors don’t even make it out of the gate. Scripting capabilities lower the bar. Few will compile code, many might consider trying a simple hello world script.

There are lots of examples of tools with scripting out there. Adobe products, for example, have scripting. Photoshop has a macro feature that enables user to automate many tasks. Lightroom goes further. 1. Even artsy people who are completely terrified by the idea of programming can use these.

A better example is the Firefox and later Chrome browsers. To write an addon/extension doesn’t require one to know C++ or even compile anything. Javascript is all that’s needed. In the case of Chrome, the debugger is always right there. 2. These browsers are successful largely (entirely?) because of the wealth of extensions available.

Yet another example is Blender. Everything is exposed to the scripting language. Everything. Menus, bindings, direct node/edge/face manipulation. There’s nothing you do from C++ that isn’t available in the scripting language.

Kicad’s pcbnew has had python support for some time now; I wrote a message on kicad-users about it almost four years ago. Things have progressed a little, but not much. You no longer have to recompile pcbnew to use it, which is a big step.

The purpose of this blog is to help things along. I hope to better document the existing scripting features to help new potential user make real changes to pcbnew 3

At this point in the post, if you just want to get into scripting, continue to my next posts. The rest of this one is just commentary on why I think this is an important topic.

Who am I. Who is this guy?

Up til two years ago 4, I worked for a well know semiconductor manufacturer. You’ve heard of the company and it’s almost guaranteed that you’ve owned one or more of their products. I worked there for a bit over 20 years in the CAD/EDA 5 department. For 14 of those years, I worked on and supported the VLSI 6 layout editing tools used to design the most well know of their products.

The earliest of the editors didn’t have scripting. They weren’t even written in C++ which was a young/new language at the time. 7. When I took from being a tool developer to a year and a half of active floor support, I made an important niche for myself by writing simple editing macros. In meetings, when someone reported a simple/repetitive task that’s too difficult on the tools, a frequent answer was, “let’s get Miles to write a macro”. It was a nice position to be in. Some of those macros were hard to write, but most were pretty trivial… Trivial if you knew how to do it.

Eventually that generation of tools wasn’t good enough and a new one was written. 8 The new one was written in C++ with a comprehensive TCL interface 9. You can do anything through the TCL interface. The only reason to move to C++ is runtime performance. 10

The result of this scripting capability is that great new features came from some unlikely places. This company has an army of polygon pushers with no training in software. Many of these came up with some great stuff that tool developers later supported. I’ve venture to say that today, the majority of the tools’ capabilities originated this way.

There’s a small handful of kicad developers 11. There are lots of kicad users.

How many of them would try to dig through the C++ code?

How many of them would pull up the scripting window and enter a couple python commands?

That’s the power I’d like to harness.

  1. I personally, think their “scripting” is a joke. They only enable you to invoke existing functionality. You can’t really create your own new functions. I would love to add a levels feature to Lightroom. I’ve had another idea for dealing with white balance. Alas,… can’t do it.

  2. right click->inspect

  3. I would love to see the same thing happen with eeschema

  4. in other words, I don’t work there now and I don’t represent them in any way

  5. Computer Aided Design is what most people think of it, but Electronic Design Automation is the term used by industry

  6. although I worked on chip design tools, which are not the same as PCB design tools, I think these lessons still apply

  7. Mainsail was the language that was used. I liked using it.

  8. This was all internal. Only a handful of companies do this kind of stuff and no one does nearly the amount of hand design. It made sense to develop these tools in-house and that continues to this day.

  9. Like with MAINSAIL, people hate on TCL. I think it’s a fine language that can do most things I want. A notable exception is closures/lambdas, but C++ doesn’t really have those either. C++ lambdas are very limited.

  10. There was one guy switched his code to C++ for the sole reason of preventing his users from messing with it. He claimed that too often, users would modify it poorly/incorrectly and then complain to him.  It was a cop out.

  11. I don’t 100% know that this is true; I’m new to the kicad world, but I’ll bet you a dollar.