Extracting footprint information from KiCAD
/Sometimes being lazy is really hard work. I’ve got the PCB design for the Bluetooth Chord keyboard and I want to extract the key positions for the case. I’ve discovered the really useful KiCadSetupUp plugin for FreeCad CAD that lets me import my PCB as an object in drawing, but what I really want to do is get the positions of keys on the PCB so that I can cut holes in the case in exactly the right positions.
I could spend five minutes copying the values from the KiCad PCB design. But I’m lazy. I’d much rather spend most of a day discovering how I can run Python code inside KiCad and write a program that does it for me. There is a bit of method in my madness, in that if the design of the PCB changes (I’m going to have to move the keys a little bit to stop the keycaps from clashing) I just have to run my program again. There’s probably some kind of integrated manufacturing trick that lets you automagically import this kind of information into FreeCAD but I’m really too lazy to learn how that works. Instead I wrote this little program:
file = "C:\\Users\\rsmil\\pcbs\\keyboard\\flippedkeyboard.kicad_pcb"
board = pcbnew.LoadBoard(file)
o = board.GetOrigin()
x = o.x / 1000000.0
y = o.y / 1000000.0
l = []
for f in board.GetFootprints():
b = f.GetBoundingBox()
ref = f.GetReference()
p = f.GetPosition()
px = (p[0] / 1000000.0)-x
py = (p[1] / 1000000.0)-y
height = b.GetHeight() / 1000000.0
width = b.GetWidth() / 1000000.0
print(ref)
t = ' makeKey("{0}",{1:.2f},{2:.2f},{3:.2f},{3:.2f})'.format(ref,px,py,height,width)
l.append(t)
l.sort()
for t in l:
print(t)
It prints out a function call for each footprint. This is what I get from my design:
makeKey("Control_1",100.07,21.29,17.38,17.38)
makeKey("Control_2",100.07,38.43,17.38,17.38)
makeKey("Control_3",100.07,55.58,17.38,17.38)
makeKey("Control_4",100.07,72.72,17.38,17.38)
makeKey("Control_5",100.07,89.87,17.38,17.38)
makeKey("Index_1",64.51,17.16,17.38,17.38)
makeKey("Index_2",64.51,33.99,17.38,17.38)
makeKey("Little_1",11.17,21.29,17.38,17.38)
makeKey("Little_2",11.17,38.43,17.38,17.38)
makeKey("Little_3",11.17,55.58,17.38,17.38)
makeKey("Little_4",11.17,72.72,17.38,17.38)
makeKey("Little_5",11.17,89.87,17.38,17.38)
makeKey("Middle_1",46.73,17.16,17.38,17.38)
makeKey("Middle_2",46.73,33.99,17.38,17.38)
makeKey("Ring_1",28.95,21.29,17.38,17.38)
makeKey("Ring_2",28.95,38.43,17.38,17.38)
makeKey("Ring_3",28.95,55.58,17.38,17.38)
makeKey("Ring_4",28.95,72.72,17.38,17.38)
makeKey("Ring_5",28.95,89.87,17.38,17.38)
makeKey("Thumb_1",82.29,21.29,17.38,17.38)
makeKey("Thumb_2",82.29,38.43,17.38,17.38)
makeKey("Thumb_3",82.29,55.58,17.38,17.38)
makeKey("Thumb_4",82.29,72.72,17.38,17.38)
makeKey("Thumb_5",82.29,89.87,17.38,17.38)
This runs inside my little Python layout program that builds the box (which I really need to write sometime). It turns out that it is very easy to run code inside KiCad, although some of the help online seems to be a bit out of date. You also need to be very careful with some parts of your code. If you get the path to the file wrong this won’t just crash your program, it will take out the KiCad program completely. I tried a number of different ways to catch the exceptions and errors that might be thrown by the LoadBoard function but I was never able to stop the crashing. Just make sure that your path is correct and that any backslash path separators are properly escaped as you can see above.