Kicad Scripting Table of Contents

This blog is about how to write scripts for pcbnew.1

This page is an effort to summarize all of the available posts in this blog. Let me know if there’s a particular topic you’d like to see covered. I’ll update this page as I go along.

GITHUB

Many/most of my posts in this blog will have associated sample code in my github. Please let me know if you find a bug.

In particular, I’d like to draw your attention to a UML diagram I’ve put together summarizing the more useful functions available in the python interface. Click for a larger image, or you can find it in the github.

Note that there is more complete documentation of pcbnew’s python APIs on the Kicad website.

Soapbox

I begin the blog on a bit of a soapbox. I try to explain why I write this blog.

Running a recent version

As of February 2017, the latest Kicad release is missing some important Python capabilities. My second post was about how to compile Kicad in the Ubuntu linux environment. Alternately, it also talks about getting one of the nightlies.

Basics of scripting in pcbnew

I then continue onto some of the basics. Things like iterating over net, modules and pads and getting their attributes. Location, names,…

Simple layout modifications

Now that you know how to gather information about your pcb design, you’ll want start making changes. Add a track or a via. Move a module.

More comprehensive queries

My post on the basics showed how to get some of the layout attributes. As a way to convince myself that I’m able to query a useful amount of information, I put together a script to generate an SVG file displaying the design. A writeup of it can be found here.

Multiple instances of the same sheet

If you have arrays of stuff in your design, pcbnew doesn’t really enable you to take advantage of that regularity. If you have X copies of the same subcircuit, you still have to place every module. My replicate post talks about a way to place one of the copies and then apply that placement and internal routing to the other copies. Maybe it doesn’t yield final layout, but it’s better than having all of the modules sitting on top of each other.

Manipulating more symbolic information

Most of the APIs I’ve covered so far focused on the actual layout. Modules, wires,… My post about zones and boundaries tries to go beyond that. I gather information about everything in the design and then adjust the board boundary to shrink wrap (albeit a rectangle) around it.

Running scripts from the command line

When thinking about scripting in pcbnew, most would probably think about augmenting the editing commands. There’s a whole other universe of stuff you might want to script, just to extract information about your design. I have a post on running pcbnew APIs within normal command line python scripts.

Or you can add GUI elements

While we’re on topic of running a script outside of the pcbnew application, what if you want to go in the other direction? Here’s some information about adding some interactivity to your scripts.

Adding additional modules in pcbnew

Usually, modules are introduced via netlist import. Sometimes you want to add more in pcbnew. Here’s a post on how to add mounting holes via a python script.

Zoom, Pan, Refresh

When manipulating your layout from python, you’ll want a way to refresh the display; to trigger a redraw. Additionally, you may want to zoom into the change2. Here’s how.


  1. Over time, I expect I’ll start covering some of the C++ code as well. I’ve had to spend a lot of time with it basically reverse engineering it to learn how to get the scripting interface to do what I want.

  2. I can imagine reasons to change zoom, even without making a change