KiCad notes
Setup the tools expect, and pcbnew Python quirks met while writing them.
Setup
-
KiCad 10. The tools find
kicad-cliand KiCad's Python through tools/kicad_env.sh (shell) and the stock libraries through tools/kicad_paths.py (Python):- macOS:
/Applications/KiCad/KiCad.appis found automatically.kicad-cliis not on PATH there. - Linux: the system Python has
pcbnewwhen KiCad is installed from packages. - Overrides:
KICAD_PYTHON,KICAD_CLI,KICAD_SHARE, and KiCad's ownKICAD10_SYMBOL_DIR/KICAD10_FOOTPRINT_DIR/KICAD10_3DMODEL_DIR.
- macOS:
-
The repo root (
ROOTinkicad_paths.py) is the directory withlibraries/, found upward from the current directory. SetHW_ROOTto override. -
Shared libraries: set a KiCad path variable
HW_LIBto<repo>/libraries(Preferences → Configure Paths). AddHWentries that use${HW_LIB}to the globalsym-lib-table,fp-lib-tableanddesign-block-lib-table:${HW_LIB}/symbols/HW.kicad_sym${HW_LIB}/footprints/HW.pretty${HW_LIB}/blocks/HW.kicad_blocks
Footprints reference models as
${HW_LIB}/3dmodels/HW.3dshapes/<name>.step. Never use absolute paths. -
Project templates:
libraries/templates/<name>/(project files named after the folder, plusmeta/info.html) appear under User Templates once symlinked into~/Documents/KiCad/10.0/template/.
Modules as design blocks
Reusable circuits live as design blocks in HW.kicad_blocks. Each block has a test project in modules/<block>/, so that it can pass ERC and DRC on its own. A module is never built alone, so its test project has no breakout header:
- the root sheet holds the block sheet, no-connect flags on its pins and PWR_FLAGs;
- the test board is the block's tile.
pcbnew Python quirks
-
The wxApp assert (
assert "traits" failed … create wxApp) that KiCad's bundled Python prints is harmless. -
Flipping:
FOOTPRINT.SetLayerAndFlip()andFlip()segfault on a footprint that is not on a board yet. Callboard.Add(fp)first, then flip. -
pcbnew.NewBoard(path)+SaveBoard()also writes a default.kicad_pro, a.kicad_prland a~<project>.kicad_pro.lcknext to the board. Overwrite the.kicad_prowith the settings you want, and delete the other two. -
pcbnew.LoadBoard()+SaveBoard()on an existing project board resets the.kicad_pro. It comes back with only the Default net class, no patterns, and no track-width or via palette, and DRC still passes if nothing was routed against a class rule. Copy the project file before the save and write it back afterwards; every tool here does this. Before committing, diff the.kicad_proagainst git and read the net classes back (net_settings.classes); DRC alone will not catch it. -
kicad-cli pcb renderrenders the 3D view to PNG (--side,--rotate "-60,0,30",--perspective). It is handy for checking a 3D model's placement without the GUI. -
Fields and schematic linkage:
FOOTPRINT.SetField(name, value)adds a user field (LCSC, MPN); hide it withGetField(name).SetVisible(False). For schematic linkage, set:SetPath(KIID_PATH("/<sheet uuid>/<symbol uuid>"));SetSheetnameandSetSheetfile;- the lib id via
SetFPID(LIB_ID(nick, name)).
DRC schematic parity then reports 0 issues without opening the GUI.
-
Use
BOARD.Delete(item), notBOARD.Remove(footprint).Removebreaks the board's lists: afterwardsGetTracks(),Zones()andGetDrawings()raise'SwigPyObject' object is not iterableor segfault, andFindFootprintByReference()andfp.Pads()return untyped objects. -
Footprint order: KiCad orders footprints by UUID. A board regenerated from a script gets new UUIDs, so anything that iterates footprints or pads (a router dump, label placement) must sort them to give the same result twice.
-
Update PCB from Schematic without the GUI:
tools/sync_board.py NETLIST BOARD [--at X,Y] [--strip-routing](KiCad's Python, board closed). It:- deletes footprints no longer in the netlist;
- adds new ones from the library at
--at; - writes value, LCSC/MPN/Datasheet/Description, schematic path and pad nets on every footprint;
- only reports changed footprint ids;
- leaves board-only footprints (housing posts) alone.
--strip-routingdrops all tracks and vias, for a board thattools/pnr.shwill route again. -
Dangling test:
CONNECTIVITY_DATA.TestTrackEndpointDangling(track, False)afterboard.BuildConnectivity()is DRC's own dangling test for tracks and vias. Zones count once they are filled. -
Net class order: KiCad sorts the net classes by name when it saves the
.kicad_pro; theirprioritykeeps the matching order. Compare classes by name, not by position.
Writing schematics as files
lib_symbolsmust hold flattened copies. A derived symbol (extends) gets the parent's units and its own properties.- Field angles are relative to the symbol. On a symbol at 90°, a field with angle 90 reads horizontally.
- Stacked pins share one connection point; one label or power symbol connects all of them.
- A label placed exactly on a pin end connects. Check with the netlist, not only ERC.
- PWR_FLAGs: a PWR_FLAG wired only to a power symbol (nothing else on that sheet) gives
pin_not_connected. Put the PWR_FLAG on a global label of the same name instead (+5V,GND). - Sheet pins: no-connect flags on a sheet's pins are accepted; the nets inside keep the sheet's names (
/Sheet/SCLK). Removing root-sheet labels renames the nets, so the board then needs Update from Schematic (ortools/prune_copper.py --netlist).
JLC production files without the GUI
-
The JLC plugin (bennymeg,
com_github_bennymeg_JLC-Plugin-for-KiCad) has a CLI. Run it from~/Documents/KiCad/10.0/3rdparty/pluginswith KiCad's Python:$KPY -m com_github_bennymeg_JLC-Plugin-for-KiCad.cli -p <abs board path> -t -f -nI -nBIt writes
production/next to the board: the gerber zip,bom.csv,positions.csv,designators.csvandnetlist.ipc. It ends with a harmless wx assert, and still makesproduction/backups/despite-nB; delete that. -
CPL rotations: the plugin's
transformations.csvdefaults are per package, but JLC's library entries differ per part.- Check every polarised part in JLC's placement preview (pink dot = JLC's pin 1).
- Set the symbol field
FT Rotation Offset(degrees CCW, added after the default). tools/sync_board.pycopies it to the footprint.
