Electronics, programming, VHDL, C++

PCB back annotation in KiCAD with Python

KiCAD already provides a tool to automatically annotate your schematics before moving on to PCB. But I prefer to to annotate my components according to their PCB position. That makes it easier to locate a certain component when you have PCB on your hand and schematic is open in the computer screen.

Some tools such as Altium, provides such function. It works like this;

First you annotate your components in schematic (presumably according to their position in schematic sheet)

You switch to PCB side, complete the placing of your components

Re-annotate the components on PCB

Back-annotate name changes to the schematic

KiCad doesn’t have this feature. So I decided to give it a try at implementing this feature with KiCad’s python scripting interface. And I’m successful (I think..).

I’ve created a script that when you run it; re-names all components on your PCB according to their PCB position. Then updates your schematic files with these changes. You don’t have to lift a finger.

First of all my script doesn’t use the KiCad’s python API directly. Instead it uses a wrapper written in python which provides a more pythonic API. It’s called kicad-python and it’s on github. Unfortunately original project had some problems, so I had to fork it and add some missing features. So you have to use my fork instead.

Note that you don’t have to install ‘kicad-python’ on your system. You can use it from the downloaded location. There is only 1 dependency at the moment (other than obvious kicad installation), which you can install via pip:

enum34

Update: now kicad-python comes with it’s copy of the ‘enum34’ module. I’ve added it because at the moment installing python modules on kicad’s windows version seems to be a pain.

To run the script, load your board with pcbnew. Start the python console from “Tools->Scripting Console”. Make sure ‘kicad-python’ is in your path (if you haven’t installed it):

import sys
sys.path.append("/home/user/path/to/kicad-python/")

Run the script with:

execfile("/home/user/path/to/kicad-python/examples/pcbannotate.py")

After you run a script, pcbnew doesn’t update the view immediately. You can force it to redraw by cycling canvases. For ex; switch to original canvas mode then switch to opengl canvas. Let me know if you know a better method for this.

Important notes:

Backup your files before running the script.

Make sure you have a recent version of KiCad (at least 4.0)

Script won’t rename components if their names doesn’t end with a number. For ex. something like ‘D12X’ won’t be renamed!

Schematics are updated with simple string replacement. So it may not work in some cases. Unfortunately at the moment KiCad has scripting only for PCB

After you run the script, update the netlist files. When you read netlist from pcbnew, it shouldn’t change any component footprints. Otherwise something went wrong! It may update some (automatically given) net names though. That’s expected.

You should really backup your files!

If you have any problems/questions with the script please let me know. I will try to help as much as I can.

Ok, i’ve looked into this a bit. Installing pip for kicad’s python seems to be harder then i thought. But good thing is you don’t have to install ‘enum34’ library. You can use it from the source similar to ‘kicad-python’.

Please note; there is a slight issue with the naming. Components are numbered from top-left to bottom-right. But when you switch to back side of the bottom layer (you flip the board), this ordering becomes top-left to bottom-right. Which is inconsistent with the top-layer. I will fix this in the coming days.

Hi, I haven’t tried your nice scripting yet. Does one be able to read out all x, y positions of all modules and edit them separately and then back annotate the new positions to KiCad? This would be a great chance to write one’s own placement algorithm or just some simple scripts to place the modules in a way that helps to reduce time consuming manual positioning …
Cheers
Aquila

I’m not familiar with python or the python shell of kicad and this is my first kicad project. I’m a newbie and would have found this example handy. It’s all explained in this excellent blog, but the blog assumes a level of familiarity which I didn’t have with any of the tools.

(1) Get to github using the ‘my fork’ link above.
(2) Using the green ‘Clone or Download’ button, I chose ‘Download Zip’ at the bottom
(3) I put the zip in ‘/home/yourUserID/Downloads’, unzipped to create the ‘/home/yourUserID/Downloads/kicad-python-master/’ directory. This is the home. Observe the ‘examples’ subdirectory and the ‘pcbannotate.py’ file within it.
(4) As directed, I selected ‘Tools | Scripting Console’ to open the KiCAD:PCBNEW – Python Shell.
(5) Within the shell enter lines:
(5a)import sys
(5b)sys.path.append(“/home/yourUserID/Downloads/kicad-python-master/”)
(5c)execfile(“/home/yourUserID/Downloads/kicad-python-master/examples/pcbannotate.py”)
(6)SAVE YOUR WORK!

I’m thinking that you are running the stable version (4.0) of kicad. Unfortunately this script requires a recent version (nightly build) of kicad. If you don’t want to bother installing it, maybe you can try this fork of my script: https://github.com/cculpepper/kicad-python/blob/master/examples/pcbannotate.py I haven’t tried it, but it should be easier to run since it doesn’t require any other external libraries.

The program works well, but I have found a bug. I am using Kicad Version: 4.0.6-e0-6349-53-ubuntu 16.04, release build. When I open Kicad and load the modified schematic file, Kicad crashes with a segmentation fault.
The problem is that with a hierarchical schematic, the format for fields in the schematic file is different for the top level. Most of the schematics have a space after the F, e.g. ‘F 0’. The top layer has no space, e.g. ‘F0’.
The schematic I have has a fuse with refDes of F1. The result is the program changes the field F1 to F101 because the replace function modifies the field line as if it was a refDes.

It shows how difficult it is to verify code. In this case, it is sensitive to the schematic and to the version of KiCad. Perhaps KiCad will be changed so all the field designaters will will have the same format, with the space.

I am new to Python and FreeCAD. “More pythonic” sounds cool, but how bad would this script be if it was written entirely in FreeCAD’s Python? Can it be done? If yes, would that not be a far better first step into this area?