Adding your own command buttons to the pcbnew GUI

UPDATE April 14, 2017: You no longer need to use the hardcoded numbers like 6038 mentioned below. You can use pcbnew.ID_H_TOOLBAR.
Kicad has three rows of command buttons with predefined functionality. This post is about how to add your own button.
The sample code can be found here.


Before getting into the code, there are a couple things that are weird about kicad/wxPython’s behavior

Getting proper aui pointers

The first thing is that if you simply open the python scripting window in pcbnew, find the top level window, and look for the command bars, you’ll likely get a pointer of type wxControl. That type doesn’t have the APIs needed to look at, amend, and change the command buttons. The real type of these is wx.aui.AuiToolBar but unless the pointer is cast correctly, you’re out of luck.
After a bunch of poking around, running in the debugger, trying to recompile (unsuccessfully) wxPython with debug symbols turned on, I stumbled upon the easy answer. You have to do this before you run any of the wx scripting apis.
import wx
import wx.aui # this is the key one
After running these, you’ll get the correct pointer types.…

How to add mounting holes

Most of the time, the modules in your design will be introduced via netlist import from eeschema. An important exception to this is mounting holes. Your design may need some holes to enable you to screw the resulting board onto some sort of case/enclosure. In the GUI, you’d do this via the “Add footprints” command. This works, but what if you want to ensure that the resulting holes end up in specific locations? Script it!
Compared to other scripting tasks in pcbnew, figuring out how to add footprints to a design was a pain. In the end, it’s pretty easy
My designs are pretty simple, amounting to rectangular boards. My “enclosures” tend to be a piece of wood to which I mount a board with some drywall screws. Because of this simplicity, it doesn’t really matter where my mounting holes are. Still, I would like for them to at least be in a regular pattern. So I put together a script to put one in each corner of the board’s boundary.
First off, while pcbnew’s add footprint command can access kicad’s footprint libraries on GITHUB, this is not something I’ve achieved yet. For the code in this post to work, you’ll need to clone at least one of kicad’s footprint repos.…

adding GUI elements in pcbnew

In my last post, I talked about how you can run pcbnew headless. In many cases you actually want more GUI. For example, in my code sample for replicating module placement across multiple sheet instances, I have the pivot instances hard coded in the script. Why not do it in a GUI?
Adding GUI elements to pcbnew is what this post is about.. The code I’m talking about can be found here.
The pcbnew GUI is written using wxWidgets/wxPython. If you’re not familiar with wxPython, here is my favorite tutorial so far. All of its python APIs work fine for me in pcbnew so far. I’ve found there are two important things to keep in mind:

  • You don’t need to do the normal app = MyApp(0); app.MainLoop()
  • If you do print from callback (for debugging) the text will not be in the python window. Instead, you can find it in the terminal where you invoked kicad. Keep this in mind if you usually invoke from the kicad project manager or from you OS task launcher.

At the end of this post, we’ll have a new, not particularly attractive, window in pcbnew like this:

As usual, you’ll need some imports:

import wx
import pcbnew

To create a new window, you’ll want to create a new class derived from wx.Frame.…

pcbnew scripting doesn't require the GUI

If you just want to process something in the pcbnew data model, you don’t have to bring up the GUI. You can just start a python job directly from the commandline.
Why would you do this?
In my previous life working for a large semiconductor company, we had many, many quality checks and progress trackers. Now that experience was in VLSI, but surely there are parallels in the PCB world.
Management needs a way to track the progress of the project. Engineering needs a way to enforce layout standards. An obvious example of this is design rule checking. How many opens, shorts and spacing violations are there currently? There are many other things that one could check. Maybe you have rules about the clock network. Maybe you want to check that the power grid is strong enough to handle the current its being asked to carry. Maybe the sub-design you’re working on has some restrictions on it to ensure it’ll fit into the larger design.
At the beginning of a project, we had many millions of such violations. For a while the number wouldn’t change since we were still deciding on the bigger picture stuff. Eventually, we’d change gears and want to move towards tapeout.…

Zones, boundaries, and silkscreen

UPDATE April 14, 2017 two things have changed:

  • LAYER_ID_COUNT has been renamed to PCB_LAYER_ID_COUNT
  • Zones have been changed to use the same data structure as other stuff in kicad. As a result, some of the APIs have changed
    • instead of AppendCorner, you’ll need just Append
    • The Hatch method is on the Area, not the outline. I have not updated the text below, but the code in the github will work on the latest Kicad codebase soon.

In previous posts, I’ve talked about the main layout components. Tracks, modules, pads. In this one, I’m focusing on lines. The board boundary, zones, and silkscreens.
At the end of this post, I’ll have a script (you can find the full script here) that redraws the board boundary to shrink wrap around all of the existing components. It’ll also generate a zone on the clk net (of course, it’s more common to use power/gnd). The zone will have the same outline as the board.
Most of the code is pretty straightforward, but I do recommend looking closely at the notes about zones at the end of this post. There are a couple things that are not immediately obvious.
As a reminder, I have a uml diagram in my github area of useful, pcbnew python APIs (click for a bigger view)

Some preliminaries

First, it’s helpful to have a layername->layernumber lookup table.…

Compiling Kicad on Ubuntu

Edit Dec 29, 2017: added cmake flag enabling external plugins
Edit Aug 28,2018: added cmake flags for skipping more recent stuff like spice. Also added libboost-all-dev to list of packages installed
These instructions don’t work on ubuntu 18.0 or the recents linux mints (since they’re based on ubuntu 18). This is due to wxpython gtk2/3 issues.
end edit Aug 28, 2018
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 . 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 . I do want to make some C++ changes, so I have the need to compile.…

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. . Even artsy people who are completely terrified by the idea of programming can use these.…