1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# KiCad notes
Setup the tools expect, and pcbnew Python quirks met while writing them.
## Setup
- **KiCad 10.** The tools find `kicad-cli` and KiCad's Python through [tools/kicad_env.sh](../../tools/kicad_env.sh) (shell) and the stock libraries through [tools/kicad_paths.py](../../tools/kicad_paths.py) (Python):
- macOS: `/Applications/KiCad/KiCad.app` is found automatically. `kicad-cli` is not on PATH there.
- Linux: the system Python has `pcbnew` when KiCad is installed from packages.
- Overrides: `KICAD_PYTHON`, `KICAD_CLI`, `KICAD_SHARE`, and KiCad's own `KICAD10_SYMBOL_DIR` / `KICAD10_FOOTPRINT_DIR` / `KICAD10_3DMODEL_DIR`.
- **The repo root** (`ROOT` in `kicad_paths.py`) is the directory with `libraries/`, found upward from the current directory. Set `HW_ROOT` to override.
- **Shared libraries:** set a KiCad path variable `HW_LIB` to `<repo>/libraries` (Preferences → Configure Paths). Add `HW` entries that use `${HW_LIB}` to the global `sym-lib-table`, `fp-lib-table` and `design-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, plus `meta/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()` and `Flip()` segfault on a footprint that is not on a board yet. Call `board.Add(fp)` first, then flip.
- **`pcbnew.NewBoard(path)` + `SaveBoard()`** also writes a default `.kicad_pro`, a `.kicad_prl` and a `~<project>.kicad_pro.lck` next to the board. Overwrite the `.kicad_pro` with 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_pro` against git and read the net classes back (`net_settings.classes`); DRC alone will not catch it.
- **`kicad-cli pcb render`** renders 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 with `GetField(name).SetVisible(False)`. For schematic linkage, set:
- `SetPath(KIID_PATH("/<sheet uuid>/<symbol uuid>"))`;
- `SetSheetname` and `SetSheetfile`;
- the lib id via `SetFPID(LIB_ID(nick, name))`.
DRC schematic parity then reports 0 issues without opening the GUI.
- **Use `BOARD.Delete(item)`, not `BOARD.Remove(footprint)`.** `Remove` breaks the board's lists: afterwards `GetTracks()`, `Zones()` and `GetDrawings()` raise `'SwigPyObject' object is not iterable` or segfault, and `FindFootprintByReference()` and `fp.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-routing` drops all tracks and vias, for a board that `tools/pnr.sh` will route again.
- **Dangling test:** `CONNECTIVITY_DATA.TestTrackEndpointDangling(track, False)` after `board.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`; their `priority` keeps the matching order. Compare classes by name, not by position.
## Writing schematics as files
- **`lib_symbols` must 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 (or `tools/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/plugins` with KiCad's Python:
``` sh
$KPY -m com_github_bennymeg_JLC-Plugin-for-KiCad.cli -p <abs board path> -t -f -nI -nB
```
It writes `production/` next to the board: the gerber zip, `bom.csv`, `positions.csv`, `designators.csv` and `netlist.ipc`. It ends with a harmless wx assert, and still makes `production/backups/` despite `-nB`; delete that.
- **CPL rotations:** the plugin's `transformations.csv` defaults are per package, but JLC's library entries differ per part.
1. Check every polarised part in JLC's placement preview (pink dot = JLC's pin 1).
2. Set the symbol field `FT Rotation Offset` (degrees CCW, added after the default).
3. `tools/sync_board.py` copies it to the footprint.
|