I’ve written a program which takes a footprint (.kicad_mod
file) and converts it into a Python script. The generated Python script uses KicadModTree and outputs the original footprint.
The idea is to make it easy to parameterize an existing footprint, and generate a family of similar footprints. (Terminal blocks, for example.) Also, certain types of edits are easier in script format, because the script contains a variable for each unique x or y coordinate, so this makes it easier to move things around, without having to change the coordinate each time it appears.
There are also command-line options for translating and rotating the coordinates before the script is generated, to make it easier to get pin 1 into the upper-left position to comply with the KLC.
My program is written in Haskell, using the kicad-data package, and can be easily built from source using the stack tool. However, I also have precompiled binaries for Linux, Mac, and Windows.
As an example, here is the script my program produces when run on TerminalBlock_Pheonix_PT-3.5mm_2pol.kicad_mod:
#!/usr/bin/env python3
from KicadModTree import *
footprint_name = "TerminalBlock_Pheonix_PT-3.5mm_2pol"
kicad_mod = Footprint(footprint_name)
kicad_mod.setDescription("2-way 3.5mm pitch terminal block, Phoenix PT series")
d1 = [1.2, 1.2]
p1 = [2.4, 2.4]
t1 = [1.0, 1.0]
w1 = 0.15
w2 = 0.05
x1 = 1.75
x2 = -1.9
x3 = 5.4
x4 = -1.75
x5 = 5.25
x6 = 3.5
x7 = 0.0
y1 = -4.3
y2 = 6.0
y3 = -3.3
y4 = 4.7
y5 = 4.1
y6 = 4.5
y7 = 3.0
y8 = -3.1
y9 = 0.0
kicad_mod.append(Text(type="reference", text="REF**", at=[x1, y1], layer="F.SilkS", size=t1, thickness=w1))
kicad_mod.append(Text(type="value", text=footprint_name, at=[x1, y2], layer="F.Fab", size=t1, thickness=w1))
kicad_mod.append(Line(start=[x2, y3], end=[x3, y3], layer="F.CrtYd", width=w2))
kicad_mod.append(Line(start=[x2, y4], end=[x2, y3], layer="F.CrtYd", width=w2))
kicad_mod.append(Line(start=[x3, y4], end=[x2, y4], layer="F.CrtYd", width=w2))
kicad_mod.append(Line(start=[x3, y3], end=[x3, y4], layer="F.CrtYd", width=w2))
kicad_mod.append(Line(start=[x1, y5], end=[x1, y6], layer="F.SilkS", width=w1))
kicad_mod.append(Line(start=[x4, y7], end=[x5, y7], layer="F.SilkS", width=w1))
kicad_mod.append(Line(start=[x4, y5], end=[x5, y5], layer="F.SilkS", width=w1))
kicad_mod.append(Line(start=[x4, y8], end=[x4, y6], layer="F.SilkS", width=w1))
kicad_mod.append(Line(start=[x5, y6], end=[x5, y8], layer="F.SilkS", width=w1))
kicad_mod.append(Line(start=[x5, y8], end=[x4, y8], layer="F.SilkS", width=w1))
kicad_mod.append(Pad(number="2", type=Pad.TYPE_THT, shape=Pad.SHAPE_CIRCLE, at=[x6, y9], size=p1, layers=Pad.LAYERS_THT, drill=d1))
kicad_mod.append(Pad(number="1", type=Pad.TYPE_THT, shape=Pad.SHAPE_RECT, at=[x7, y9], size=p1, layers=Pad.LAYERS_THT, drill=d1))
file_handler = KicadFileHandler(kicad_mod)
file_handler.writeFile(footprint_name + ".kicad_mod")