Metadata-Version: 2.4
Name: kicad-cruncher
Version: 2026.11.30
Summary: Cross-platform KiCad CLI workflows built on public kicad-monkey
Project-URL: Homepage, https://github.com/wavenumber-eng/kicad_monkey/tree/main/packages/kicad_cruncher
Project-URL: Repository, https://github.com/wavenumber-eng/kicad_monkey/tree/main/packages/kicad_cruncher
Project-URL: Issues, https://github.com/wavenumber-eng/kicad_monkey/issues
Author: Wavenumber LLC
License: MIT
License-File: LICENSE
Requires-Python: <3.15,>=3.11
Requires-Dist: colorama>=0.4.6
Requires-Dist: fastapi>=0.115.0
Requires-Dist: kicad-monkey>=2026.11.30
Requires-Dist: openpyxl>=3.1.0
Requires-Dist: shapely<3,>=2.1.1
Requires-Dist: textual>=0.50.0
Requires-Dist: uvicorn>=0.30.0
Requires-Dist: wn-geometer==2026.11.22
Provides-Extra: test
Requires-Dist: build>=1.2.0; extra == 'test'
Requires-Dist: jsonschema>=4.22.0; extra == 'test'
Requires-Dist: pyright>=1.1.390; extra == 'test'
Requires-Dist: pytest-json-report>=1.5.0; extra == 'test'
Requires-Dist: pytest>=8.0.0; extra == 'test'
Requires-Dist: resvg-py<0.6,>=0.5.0; extra == 'test'
Requires-Dist: ruff>=0.8.0; extra == 'test'
Requires-Dist: twine>=5.0.0; extra == 'test'
Requires-Dist: wn-dev-std>=2026.10.1; (python_version >= '3.12') and extra == 'test'
Requires-Dist: wn-rack>=1.1.0; extra == 'test'
Description-Content-Type: text/markdown

# KiCad Cruncher (`kcr`)

`kicad-cruncher` (short alias **`kcr`**) is a cross-platform command-line
application for **KiCad >= 7** (KiCad 7–10) manufacturing, visualization,
library hygiene, and design-review workflows. It builds on the public
`kicad-monkey` package and keeps higher-level CLI behavior outside the core
library.

- **Public documentation**: [wavenumber-eng.github.io/kicad_monkey](https://wavenumber-eng.github.io/kicad_monkey/)
- **CLI reference**: [wavenumber-eng.github.io/kicad_monkey/cli/commands.html](https://wavenumber-eng.github.io/kicad_monkey/cli/commands.html)

## Install

Install `uv` first if it is not already available:

```powershell
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
```

On macOS or Linux:

```bash
curl -LsSf https://astral.sh/uv/install.sh | sh
```

Install as a uv tool (Python 3.11–3.14):

```powershell
uv tool install kicad-cruncher
uv tool update-shell
kcr --version
```

During local development:

```powershell
git clone https://github.com/wavenumber-eng/kicad_monkey.git
cd kicad_monkey
uv sync --all-packages --group dev
uv run --package kicad-cruncher kcr --help
```

The Python `kcr` commands run pure Python on every platform (plus `wn-geometer`
for 3D STEP projection in `pcb-svg`, `toon`, and `vdoc`). A separate pure-Rust
native binary (`k3`, implementing the 10 performance-critical workflows `bom`,
`design`, `fab`, `jlc`, `pcb-layer-step`, `pcb-svg`, `pnp`, `toon`, `vdoc`,
and `version`) is built and packaged separately.

## Commands

Run `kcr <command> --help` for full options.

| Command | Purpose |
| --- | --- |
| `fab` | Generate analytic Gerber X2/X1, Excellon NC Drill (`G85` slots & multi-span), IPC-2581 Rev B, PCB SVG, Pick-and-Place, BOM, pure Micro-STEP, and all 17 virtual fab layers from `KiCadFabView` (`fab.jsonc`). Aliases: `gerbz`, `ipc2581`. |
| `vdoc` | Clean legacy documentation layers and bake virtual fab layers onto `F.Fab`/`B.Fab`/`User.*` across boards (`.kicad_pcb`), footprints (`.kicad_mod`), and `.pretty` libraries (`--preset`, `--fab-only`, `--clean-only`, `--clear-only`). Alias: `virtual-doc`. |
| `bom` | Generate KiCad BOM outputs with shared field alias coalescing, variant-aware DNP handling, grouped JSON/CSV/XLSX review tables, and JLC BOM rows. |
| `pnp` | Generate KiCad pick-and-place JSON, CSV, XLSX, and JLC CPL outputs using component-center coordinates relative to the aux axis / drill-place origin. |
| `jlc` | Generate paired JLCPCB BOM and CPL upload files (CSV/XLSX) and optional Gerbers + NC Drill. |
| `pcb-layer-step` | Generate self-contained ISO 10303-21 Micro-STEP alignment models directly from `KiCadFabView` (zero Geometer/OCCT dependency). |
| `pcb-svg` | Render configured physical KiCad layers, board domain (`BOARD_SUBSTRATE`, `SOLDERMASK_FILM_*`, `BOARD_OUTLINE`, `BOARD_CUTOUTS`, `DRILLS`, `SLOTS`), 3D wireframe HLR, and all 17 virtual fab layers. |
| `toon` | Render shaded top/bottom PCB illustrations with solder-mask themes, 3D models, physical clipping, assembly labels, and variant filtering. |
| `design` | Generate an agent-ready design review bundle with compiled schematic graph, schematic SVGs, PCB copper SVGs, netlists, manifest, and README. Aliases: `design-review`, `dr`. |
| `pcb` | PCB utilities: `kcr pcb clean` (safe documentation-layer cleanup) and `kcr pcb embed-models` (native KiCad 10.0.8 zstd + MMH3-128 STEP model embedding with `--refs`, `--update-project-library`, `--re-embed`, `--prune-unused`, and KiCad `.lck` lock protection). |
| `health` | Audit project assets, embedded vs external 3D model references (`--require-embedded`), and project-listed local libraries. |
| `kicad` | Inspect local KiCad installs, running processes, and resolved 3D model path variables (`kcr kicad prefs`). |
| `project` | Scaffold a new KiCad project (`.kicad_pro`, `.kicad_sch`, `.kicad_pcb`, library tables, title block, text variables). |
| `project-lib` | Extract project-local symbol and footprint libraries (`--relink-sources`, `--embed-board-models`). Aliases: `project-library`, `project-local-lib`, `local-library`. |
| `lib-extract` | Extract cleaned symbols, footprints, optional models, and separated metadata JSON. Alias: `library-extract`. |
| `megamaid` | Non-destructive project dissection bundle (libraries, embedded assets, models, design review artifacts, netlists, SVGs, manifest, README). |
| `daemon` | Experimental local KiCad IPC daemon workflow. |
| `plugin` | Experimental local KiCad IPC plugin installer. |
| `version` | Print `kicad-cruncher` and `kicad-monkey` versions. |

## Quick Examples

```powershell
# Manufacturing outputs (Gerber X2 + NC Drill + IPC-2581B + Micro-STEP + virtual layers)
kcr fab board.kicad_pcb --ipc2581 --micro-step -o output/fab

# Bake assembly outlines + designators onto F.Fab/B.Fab & symbols onto User.*
kcr vdoc board.kicad_pcb -o baked.kicad_pcb
kcr vdoc MyParts.pretty --fab-only -o MyParts_fab.pretty

# Inspect KiCad 3D model prefs and embed external STEP models natively
kcr kicad prefs
kcr pcb embed-models board.kicad_pcb --apply --update-project-library --backup

# Configured PCB SVG & shaded Toon renders (external STEP resolved automatically; add --embedded-only for CI)
kcr pcb-svg board.kicad_pcb -o output/pcb-svg
kcr toon board.kicad_pcb --theme green --assembly -o output/toon
```

The `design` command writes to `./output/design_review/` by default. Its aliases
`design-review` and `dr` produce the same output:

```powershell
kicad-cruncher design board.kicad_pro
kicad-cruncher design-review board.kicad_pro
kicad-cruncher dr board.kicad_sch --no-indexes
kicad-cruncher design -o output/design_review
```

The primary `<input-stem>_design.json` contains the compiled netlist and
schematic realization, with `<input-stem>_compiled_schematic_graph.json`,
`review_summary.json`, `design_review_report.json`, and
`design_review_manifest.json` (`kicad_cruncher.design_review_manifest.d0`)
indexing every generated schematic SVG and PCB copper review SVG.

PCB review SVGs include the copper layer, `Edge.Cuts`, and `kicad-monkey`
enriched drill/slot records.
Plated pads and vias, and KiCad `np_thru_hole` mechanical pads, are
distinguished with `data-hole-plating` and `data-hole-kind` attributes.
Design-review styling colors those existing records in place: plated drills are
blue, plated slots are cyan, non-plated drills are red, and non-plated slots are
orange. KiCad Cruncher does not add a second drill/slot overlay or duplicate
the `kicad-monkey` plating metadata.

For large boards, `design`/`design-review`/`dr` still produce full design JSON
and netlist artifacts, so they intentionally materialize broad project state.
The PCB review SVG pass reuses a command-scoped cached board render state while
writing one SVG per copper layer. On public corpus measurements with
`kicad-monkey 2026.7.17`, this mainly helps boards with several copper layers;
the full design JSON artifact can still dominate total runtime on smaller
layer counts.

The `pcb-svg` command writes to `./output/pcb-svg/` by default and uses
JSON/JSONC configs compatible with the A0 PCB SVG view contract: `--config`,
else `<stem>.pcb.svg.config` beside the input (the `.kicad_pro`'s stem, or a
lone board's own), else a folder `pcb.svg.config`. This remains a preview
feature: SVG structure,
virtual-layer metadata, default views, and config controls may change as more
real-world boards are tested.

Artifact publication is transactional: failed rendering leaves a new output
directory absent and preserves an existing output tree byte-for-byte. When no
config is found, the command intentionally authors `<stem>.pcb.svg.config`
before rendering; it is configuration input and is not
part of the transient artifact transaction. A written config's `components`
holds the assembly part rules' entries for the board: no entry for a part with
a visible 3D model (Geometer); `projection: "none"` with
`show_designator: false` for a model-less part that draws on `Edge.Cuts`, a
`board_only` part, or one with no pads; `pad_shape` for one distinct copper pad
number or holes only; `pad_bounds` for several pads. An existing config is
never regenerated. A `components` key that is a designator on the board
matches only that part; any other key is a case-insensitive selector (`TP`,
`TP*`, `U5-U15`, `U$1-U$9`). Selectors apply in file order, then the exact
designator, field by field. A selector that is neither a prefix nor a range
matches nothing; a reversed range (`U15-U5`) still matches. A config overlays
the one commented default,
`resources/pcb.svg.default.jsonc`, which both languages read, and
`global.canvas` gives each board one canvas for every view and layer output.
See the `pcb-svg` CLI design page for the part rules, the canvas and pad
geometry; a custom pad's anchor is a circle of diameter `size_x` unless
`(anchor rect)`.

```powershell
kicad-cruncher pcb-svg board.kicad_pcb
kicad-cruncher pcb-svg project.kicad_pro --views assembly-top
kicad-cruncher pcb-svg board.kicad_pcb --config board.pcb.svg.config -o output/pcb-svg
```

`pcb-svg` composes KiCad Monkey enriched physical layer SVG with explicit
virtual layers. `BOARD_OUTLINE` and `BOARD_CUTOUTS` are synthesized from closed
`Edge.Cuts` regions, with derived arc/curve/circle smoothness controlled by
`styles.board_outline.max_*_segment_mm`, `DRILLS` and `SLOTS` preserve KiCad
Monkey hole metadata, `PIN1_TOP`/`PIN1_BOTTOM` add configurable pad-linked
marker groups, `BOARD_SUBSTRATE`, `SOLDERMASK_FILM_TOP`/`SOLDERMASK_FILM_BOTTOM`
and `ILLUSTRATION_TOP`/`ILLUSTRATION_BOTTOM` draw the board, its mask films and
shaded models in any view, and
`ASSEMBLY_HLR_TOP`/`ASSEMBLY_HLR_BOTTOM` append geometer-backed STEP hidden-line
overlays or footprint-bound fallbacks. The assembly silhouette mode is named
`outline` and uses Geometer's mesh-shadow outline algorithm by default. Use
`outline`; legacy `simple` config values are no longer accepted. Default assembly views include
only top and bottom assembly outputs. They include board cutouts, drills, slots,
pin-1 markers, Geometer `outline` HLR with a pad-box fallback, and
`ASSEMBLY_DESIGNATORS_TOP`/`ASSEMBLY_DESIGNATORS_BOTTOM` labels drawn above the
75% opacity HLR/bounds overlay. Assembly labels are blue, bold monospace by
default. pcb-svg and Toon share one designator path: each label is fitted inside
the part on both of its axes, so a 45-degree part gets a 45-degree label. A
label takes the projection its view's HLR uses: the component's `projection`,
else the view's `assembly_hlr_mode` (`outline` in the default views). A
component projected `pad_bounds`, `pad_shape` or `none` is fitted to its pad shapes on its
side, wrapped in one outline; any other projection fits the area enclosed by
its model drawing's outline lines, which a view without an illustration layer
still asks Geometer for. There is no minimum label size. `box_fill_ratio` is
clamped to 0.05 to 1, with a warning for each value outside it.
Assembly designator style overrides can target exact refs, prefixes, wildcards,
or ranges, then each component; `min_font_size_mm`, `rotation_aspect_threshold`
and `rotation_direction` are no longer used and warn when set.

The Python and standalone native `toon` commands writes portable, digest-indexed SVGs and a
`manifest.json` to `./output/toon/` by default. It replaces that directory only
after every requested side has rendered successfully. Bottom views are mirrored
for presentation, and standard pad mask apertures honor resolved board,
footprint, and pad solder-mask margins, including negative pullback.
Its default presentation uses the saved
side-specific stackup mask color at 75% opacity (off-white fallback), contrasting
silkscreen, light-gray drill/slot virtual layers, and 0.025/0.01375 mm Geometer HLR outline/detail
widths. `BOARD_SUBSTRATE` is a distinct `#B6A26B` board-domain fill below copper
and solder mask. Physical drills, slots, and internal cutouts are subtracted from
that substrate; colored drill/slot virtual objects are then painted without a
stroke. Those objects are clipped only to the physical board domain so holes
crossing the outer profile or an interior cutout appear as castellated partial
holes.

```powershell
kcr toon board.kicad_pcb
kcr toon project.kicad_pro --side top -o output/toon
kcr toon board.kicad_pcb --side bottom
kcr toon board.kicad_pcb --theme black
kcr toon board.kicad_pcb --soldermask-color '#000000'
kcr toon --write-config toon.config
kcr toon board.kicad_pcb --config toon.config
kcr toon board.kicad_pcb --workers 4 --timings output/toon-profile.json
kcr toon board.kicad_pcb --cache-dir temp/toon-cache
kcr toon board.kicad_pcb --no-cache
kcr toon board.kicad_pcb --footprint U1
kcr toon project.kicad_pro --assembly --variant ADXL355
kcr toon project.kicad_pro --all-variants
```

Toon includes built-in `saved`, `white`, `black`, `blue`, `red`,
`purple`, `yellow`, and `green` mask/silk themes. Without an explicit theme it
loads `toon.config` beside the board, creating an editable JSONC preset there
when missing. `--config` selects another PCB-SVG config and `--write-config`
writes the resolved preset without loading a board or starting Geometer.
Explicit `--theme` or `--soldermask-color` is applied after the authored file.
Toon is the built-in pcb-svg config `resources/toon.config.json`: both
languages draw it through their pcb-svg pipeline, so its substrate, copper,
mask film, silk, cutout, drill/slot, outline and illustration settings mean
what they mean in any pcb-svg view.
Each enabled top/bottom view may override those styles and controls its painter
order and layer omissions through its `layers` list. Explicit CLI colors remain
the final override for both views.

`--assembly` adds an independent red assembly-designator layer after each
side's model illustration layer, drawn by pcb-svg's designator path. Its fitter
follows PCB Autodoc's polygon candidate search, binary maximum-size fit,
two-axis rotation, size cap, fill ratio, and horizontal tie preference. It fits
inside the area enclosed by each model's outline lines; a footprint without a
model, or whose model gives no drawing, is fitted to its pad shapes, the latter
with a warning. Component config entries with `show_designator: false`
suppress individual labels. Toon applies the assembly part rules' entries in
memory beneath its config's `components`.

Project inputs support `--variant NAME` and `--all-variants`. Variant DNP
components retain their physical board pads but omit model illustrations and
assembly labels. A named selection is written under its portable variant
directory; all-variants writes `base` plus every project variant in separate
directories. Geometry/model caches remain shared across those outputs, and the
manifest/timing contracts record the variant selection and exact per-variant
SVG digests. `--doc`/`--pcbdoc` selects an adjacent board by name when a project
contains a non-default board filename.

`--footprint REF` emits one transparent, tightly fitted Toon SVG for the exact
placed footprint and its embedded models. It preserves the footprint's board
rotation and side, includes only its copper, silkscreen, drills and slots, and
does not leak neighboring board geometry. The side is inferred; an explicit
opposite `--side` is rejected. The artifact name includes the reference, for
example `board__U1__toon_top.svg`.

Toon illustrates each unique request (model content, pose, footprint rotation
and side) once per job, draws it once and places every copy with SVG `<use>`,
and schedules cache misses over a bounded Geometer pool (`--workers`, default
4). Successful geometry is cached across invocations; failures are retried and
are never positive cache entries. The default cache is `.kcr/cache` in the
project folder (the `.kicad_pro`'s folder, else the board's), laid out as
`<product>/<product version>/<key>.json`; Toon's product is
`geometer-illustration`. Python and Rust compute the same keys and share these
entries. `--cache-dir` moves the cache and `--no-cache` disables only the
persistent layer. A fully warm render does not launch Geometer. `pcb-svg`
persists its HLR projections in the same cache (`geometer-hlr`, shared by
Python and Rust) and runs its misses on its own pool (`pcb-svg --workers N`,
1 to 64, default 4). See the `toon` and `pcb-svg`
CLI design pages for the key rule, bad entries and cleaning.

`--timings PATH` writes the portable `kicad_cruncher.toon_timings.a0` JSON
contract with phase durations, requested/started workers, cache mode and hit
counts, request counts, warnings, output sizes, SVG digests, and the portable
resolved-config digest also recorded in the Toon manifest.

The command discovers the qualified Geometer executable via `wn-geometer` or
`GEOMETER_EXECUTABLE`. Both `kcr toon` and `kcr pcb-svg` (as well as `kcr fab`
and `kcr vdoc`) prefer embedded `kicad-embed://` STEP/STP models and
automatically resolve external STEP/STP files (plus `.wrl` → `.step`/`.stp`
siblings) via installed KiCad `kicad_common.json` path variables and
environment variables. Pass `--embedded-only` (or set
`KCR_EMBEDDED_MODELS_ONLY=1`) to enforce strict embedded-only rendering, or run
`kcr pcb embed-models` to embed external STEP models natively in KiCad 10.0.8
format (zstd level 15 + MMH3-128).

The `bom`, `pnp`, and `jlc` commands provide initial KiCad manufacturing output
support. They share a documented `bom.config` JSONC file with a top block
summary, generated per-field comments, field aliases for
manufacturer/part/JLC/value/description/footprint parameters, variant
selection, DNP policy, grouping fields, PnP table fields, and output path
templates.

```powershell
kicad-cruncher bom project.kicad_pro
kicad-cruncher pnp project.kicad_pro --format xlsx
kicad-cruncher jlc project.kicad_pro --variant ADXL355
kicad-cruncher bom --write-config bom.config
```

PnP and JLC CPL output use the documented `component-center` mode, which maps to
KiCad's footprint placement point relative to the aux axis, also called the
drill/place file origin. Alternate geometric centroid modes are not exposed in
this release.

The `pcb-layer-step` command writes fixture-alignment STEP artifacts under
`./output/pcb-layer-step/` by default. The generated config is intentionally
comment-heavy and can enable tracks, arcs, poured copper, vias, component pads,
board outline/cutout bodies, drill overlays, and fused copper review bodies.

```powershell
kicad-cruncher pcb-layer-step board.kicad_pcb
kicad-cruncher pcb-layer-step project.kicad_pro --doc board.kicad_pcb --layer bottom
kicad-cruncher pcb-layer-step --init-config --config pcb-layer-step.jsonc
```

## Output Layout

Output-producing commands follow the same directory policy:

- when `-o/--output` is omitted, write artifacts under `./output/<command>/`;
- when `-o/--output` is supplied, use that directory directly;
- command modules own filenames inside their command output directory.

## Tests

Run the Rack suite from `packages/kicad_cruncher`:

```powershell
$env:RACK_TESTS_DIR = "tests"
uv run rack run --all
```

## Release Policy

Public PyPI releases for `kicad-monkey` and `kicad-cruncher` are published from
`wavenumber-eng/kicad_monkey` on combined `vYYYY.MM.DD` tags via GitHub Actions
Trusted Publishing, and the Blue Phosphor documentation site is published to
GitHub Pages.

Toon uses Geometer half-space clipping at the finished board surfaces and draws
each model through two board masks per view: the clipped model shows on board
material and the whole model through exterior space, routed cutouts, and
qualified through bores. Both ends of a bore must be open; a tent, fill, plug,
or cap at either end closes the opaque through view. Incomplete board topology
keeps authored-side artwork and reports a diagnostic. Slots use capsule
geometry, and visible overhangs expand the final SVG frame.

Python Toon performs KiCad reads, planning and SVG composition in Python on
every platform; Geometer supplies 3D geometry. Standalone Rust remains separate.
Both consume the same packaged Toon preset. Per-view `mirror`, `group_id`,
`output_svg` and `enabled` controls are honored; default filenames stay unchanged.
Output must be separate from board/project/config/cache inputs. Timing files
must be separate from these inputs and the published output directory. Python
and Rust share the project's `.kcr/cache` entries.

Python timing fields overlap: `geometer_start` accumulates worker client startup
durations, and `render_and_shutdown` measures the complete rendering interval
including those startups. Per-view timings measure preparation, physical
composition, model illustration and remaining SVG composition.
