KiCad 6 Python Scripting: Place Footprints, Create Tracks, Curved Tracks, Vias, and Edge Cut Lines
If you find yourself in a situation where you are placing component footprints
at multiple locations on PCB using KiCad, or routing a pattern of
tracks repeatedly (like in a keyboard) you’ll save time by automating through a
Python script.
KiCad 6 has decent support for scripting but
documentation can be hard to grok. Reading their code is often
the only recourse. I’ll cover the basics of placing footprints and routing
tracks with code examples.
How to Run Python Script
Copy (or symlink) your python script to KiCad plugins directory, which (on a
Mac) is located in ~/Documents/KiCad/6.0/scripting/plugins. You can find out
where KiCad looks for plugins and scripts by running import pcbnew; print(pcbnew.PLUGIN_DIRECTORIES_SEARCH)
from Python Console in PCB Editor window (icon is at the right hand top corner).
In the console window simply import the python module using import filename
(if your python script is named filename.py). This will execute the script. It
works the first time, but Python interpreter will not import the same module
twice. There is a solution. You reload the module again using import
importlib followed by importlib.reload(filename).
There is an alternate approach of using plugins, but above method is simpler;
your print output appears in the console window instead of having it
redirected to a file.
KiCad Coordinate System
The Cartesian plane KiCad uses has Y-axis pointing down and X-axis pointing to
the right. Moreover, distance is specified in millionth of millimeter, and
angles are in tenths of a degree. These aspects will become clear later. Points
in space are represented by wxPoint. There is also a millimeter
variant called wxPointMM, and a conversion routine fromMM(). Using mils
instead of millimeter is also possible but not covered here.
Footprints
Footprints are needed to position components on pcb.
Place Footprint
Get a reference to the footprint object from the
Board object. You can then place the footprint and set the orientation by calling the
footprint object itself.
In the following example we have three (SMD) footprints, for 2 resistors and a diode.
Tracks
KiCad 6 supports straight line as well as curved tracks.
Straight Track
To route a track, you need a start and end point. You need to also locate the
center of the pads that terminate the track.
Curved Track
KiCad 6 has support for drawing curved tracks, be it circular arcs or Bezier curves.
Only circular arcs are covered here. Use PCB_ARC object and specify start, mid and end points of arc.
For low frequency applications, curved tracks are mostly for aesthetic reasons. Moreover, during manual routing if
you use the “shove” option KiCad may decide to convert rounded edges
to sharp corners. To manually route a rounded track use Ctrl-/ (or Cmd-/ on
Mac) shortcut to switch among following options: corners at 45 deg -> rounded corners at 45 deg
-> corners at 90 deg -> rounded corners at 90 deg, after you click on
the starting point of track.
The following example adds a rounded corner to two straight line tracks.
Create Via
Create a via at 1mm offset from pad #2 of footprint R2 and connect a track to it.
Remove All Tracks and Vias
You may need to remove stale tracks before adding new ones.
Edge Cuts
Edge Cut lines define the boundary of the pcb. KiCad 6 has support for drawing
straight lines and arcs on any layer, not just on Edge Cuts. To draw a line you
specify the end points. To draw an arc you specify starting point, center of
the arc, and the angle. This API is slightly different from drawing curved
tracks where you specify mid-point of the curve. There is also API to draw
Bezier curves.
Draw Line
Use PCB_SHAPE object and set the shape to SHAPE_T_SEGMENT.
You can specify the layer and line width. Use the search box in the
documentation to search for symbols.
Add an edge cuts border around the components. Draw lines on all four
sides and connect them by rounded corners.
Draw Arc
Use PCB_SHAPE object and set the shape to SHAPE_T_ARC.
Remove All Lines
You may want start with a fresh slate.
Conclusion
KiCad 6 provides adequate scripting capability for designing pcb’s of moderate complexity.