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.
You can find the script in examples/ folder of my kicad-python repository. Before running it make sure you BACKUP YOUR FILES.
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:
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:
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.
- 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.