kicad monkey v2026.11.30
On This Page (4)

KiCadPcb & Board Model

KiCadPcb parses modern KiCad >= 7 (KiCad 7–10, with KiCad 11 tracked as features freeze) .kicad_pcb S-expression files into typed Python objects with lossless round-trip serialization. Legacy KiCad 5 and KiCad 6 board formats are outside the supported boundary.

python
from kicad_monkey import KiCadPcb

pcb = KiCadPcb.from_file("board.kicad_pcb")
print(f"{len(pcb.footprints)} footprints, {len(pcb.segments)} tracks, {len(pcb.vias)} vias")

for fp in pcb.footprints:
    ref = fp.get_property("Reference")
    print(ref, fp.library_link, fp.layer, (fp.at_x, fp.at_y, fp.at_angle))

# Lossless round-trip
pcb.save("board_out.kicad_pcb")

Key Capabilities

Embedding 3D STEP Models

All headless 3D-aware commands (kcr toon, kcr pcb-svg, kcr fab, kcr vdoc, and Python KiCadFabView) prefer embedded kicad-embed://... STEP/STP payloads first and automatically fall back to external STEP/STP files resolved from KiCad's kicad_common.json path variables (${KICAD*_3DMODEL_DIR}, user path variables), process environment variables, ${KIPRJMOD}, and .wrl → .step/.stp siblings. Pass --embedded-only (or set KCR_EMBEDDED_MODELS_ONLY=1) whenever you want strict machine-independent CI rendering that ignores external library paths.

Consumerkicad-embed:// STEP/STPExternal .step/.stp (via KiCad Prefs / Env / .wrl Sibling)Fallback When Missing
kcr toon (Python & Rust k3 shaded illustrations)PreferredResolved automatically (disable with --embedded-only)No 3D body illustration
Python KiCadFabView (ASSEMBLY_OUTLINES_*)Preferred (footprint then board embedded_files)Resolved automatically via KiCadModelResolverSingle-pad shape / pad bounds (pad_envelope for designators)
kcr pcb-svg wireframe HLR (ASSEMBLY_HLR_*, Python & Rust k3)PreferredResolved automatically (disable with --embedded-only)Pad bounds / pad_envelope
Rust kicad-monkey-fab (ASSEMBLY_OUTLINES_*)Not projectedNot projectedF.Fab/B.Fab or pad bounds

To make a board or footprint library 100% self-contained across machines, embed its resolved STEP models natively using kcr pcb embed-models (KiCad 10.0.8 zstd level 15 + MMH3-128 checksum format, deduplicated by content hash at board scope).

Auditing, Inspecting Preferences & Embedding With kcr

sh
# 1. Inspect resolved KiCad 3D model path variables from kicad_common.json & environment
kcr kicad prefs

# 2. Audit embedded vs external STEP/STP references and missing payloads
#    Use --require-embedded to fail CI if any model is not yet kicad-embed://
kcr health project.kicad_pro --require-embedded --fail-on-issues

# 3. Preview and embed external STEP models into a .kicad_pcb (or .kicad_mod)
kcr pcb embed-models board.kicad_pcb --dry-run
kcr pcb embed-models board.kicad_pcb --apply --backup

#    Optionally filter by designator or mirror into matching project-local .pretty footprints
kcr pcb embed-models board.kicad_pcb --apply --refs U1 U2 --update-project-library

#    Upgrade legacy SHA-256 / footprint-scope payloads to deduplicated board-scope MMH3-128
kcr pcb embed-models board.kicad_pcb --re-embed --prune-unused

# 4. Extract project-local .pretty/.kicad_sym libraries AND embed board STEP models in one step
kcr project-lib project.kicad_pro --relink-sources --embed-board-models

# 5. Non-destructive project dissection bundle (does not modify source .kicad_pcb)
kcr megamaid project.kicad_pro -o output/megamaid

Reading & Embedding Programmatically (Python API)

EmbeddedFile.from_kicad_bytes(...) packs raw STEP bytes into KiCad 10.0.8's native zstd (level 15) + base64 + 32-hex MMH3-128 checksum format (while EmbeddedFile.from_bytes(...) writes SHA-256 checksums), and decode_embedded_file(...) / embedded_file_decoder(pcb) verify and decode both formats transparently. Both footprint.embedded_files (checked first) and pcb.embedded_files (board-level fallback, ideal when sharing one STEP model across repeated footprints) are supported. For resolving ${KICAD*_3DMODEL_DIR} variables or finding .step/.stp siblings for .wrl paths programmatically, use KiCadModelResolver from kicad_monkey.

python
from pathlib import Path
from kicad_monkey import KiCadPcb, decode_embedded_file
from kicad_monkey.kicad_model import EmbeddedFile, Model

pcb = KiCadPcb.from_file("board.kicad_pcb")
step_path = Path("models/QFP64.step")
embedded = EmbeddedFile.from_kicad_bytes(step_path.name, "model", step_path.read_bytes())

# Attach once at board level (deduplicating by name) and point footprint(s) to it
if not any(ef.name == embedded.name for ef in pcb.embedded_files):
    pcb.embedded_files.append(embedded)
fp = pcb.footprints[0]
fp.models = [Model(path=f"kicad-embed://{embedded.name}")]
pcb.save("board_embedded.kicad_pcb")

# Later: verify checksum and decode any embedded payload
raw_step = decode_embedded_file(embedded)
assert raw_step == step_path.read_bytes()
kicad-monkey v2026.11.30·Docs updated 2026.12.01 15:43 UTC (fd9b75b)