kicad monkey v2026.11.30
On This Page (12)

kcr pcb

Board and footprint maintenance commands: clean documentation layers and embed external 3D STEP models.

CommandAliasesCategoryRuntimeConfigGuide
kcr pcb—Projects, Libraries & AssetsPython kcrExplicit --config <FILE> (pcb.clean.config, kicad_cruncher.pcb.clean.config.a0) for pcb clean; installed KiCad prefs for pcb embed-modelsKiCadPcb & Board Model
warning

Opposite Write Defaults: clean vs embed-models

  • kcr pcb clean is DRY-RUN / PLAN BY DEFAULT: it prints a JSON cleanup plan to stdout and touches nothing on disk until you pass --apply.
  • kcr pcb embed-models WRITES IN PLACE BY DEFAULT: it modifies the target .kicad_pcb or .kicad_mod immediately unless you pass --dry-run or redirect output with -o <path>.

Running kcr pcb without a subcommand prints help to stderr and exits with code 2.

Usage

sh
kcr pcb [-h] <pcb-action> ...

Subcommands

SubcommandDescription
kcr pcb cleanPlan (default) or apply (--apply) documentation-layer cleanup on a .kicad_pcb, .kicad_pro, .kicad_mod, or .pretty library.
kcr pcb embed-modelsResolve external STEP/STP models (and .wrl -> .step/.stp siblings) using KiCad preferences and environment variables, then embed them natively (KiCad 10.0.8 zstd level 15 + MMH3-128) at deduplicated board scope (.kicad_pcb) or footprint scope (.kicad_mod).

kcr pcb clean

Plan (default) or apply (--apply) documentation-layer cleanup on a .kicad_pcb, .kicad_pro, .kicad_mod, or .pretty library.

sh
kcr pcb clean [-h] [--config CONFIG]
    [--write-config WRITE_CONFIG] [--dry-run |
    --apply]
    [file]

Positional Arguments

ArgumentDescription
file.kicad_pcb, .kicad_pro, .kicad_mod, or .pretty library directory

Options

FlagDescription
--config CONFIGPCB clean JSONC config (pcb.clean.config)
--write-config WRITE_CONFIGWrite the default PCB clean config
--dry-runWrite a cleanup plan only. Mutually exclusive with --apply.
--applyApply cleanup mutations. Mutually exclusive with --dry-run.

kcr pcb embed-models

Resolve external STEP/STP models (and .wrl -> .step/.stp siblings) using KiCad preferences and environment variables, then embed them natively (KiCad 10.0.8 zstd level 15 + MMH3-128) at deduplicated board scope (.kicad_pcb) or footprint scope (.kicad_mod).

sh
kcr pcb embed-models [-h] [-o OUTPUT] [--doc PCBDOC]
    [--dry-run | --apply]
    [--refs REFS [REFS ...]]
    [--update-project-library] [--re-embed]
    [--prune-unused] [--backup]
    [--model-search-dir MODEL_SEARCH_DIR]
    [--no-installed-kicad] [--json]
    [--no-color]
    [file]

Positional Arguments

ArgumentDescription
file.kicad_pcb, .kicad_pro, or .kicad_mod input (auto-detected in CWD if omitted)

Options

FlagDescription
-o, --output OUTPUTWrite modified file to this path instead of editing in place
--doc, --pcbdoc PCBDOCWith .kicad_pro input, select a specific .kicad_pcb by filename or stem
--dry-runResolve models and report what would be embedded without writing any file. Mutually exclusive with --apply.
--applyExplicitly apply embedded model updates (default when --dry-run is not set). Mutually exclusive with --dry-run.
--refs REFSLimit embedding to specific footprint reference designators (e.g. --refs U4 U5)
--update-project-libraryAlso embed updated STEP payloads into matching project-local .kicad_mod footprints
--re-embedRe-encode existing embedded STEP models in KiCad 10.0.8 native format, consolidate footprint copies to board scope, and prune unused model entries
--prune-unusedRemove unreferenced type=model embedded_files entries while keeping fonts/other files
--backupSave a .bak backup alongside the input file before in-place replacement
--model-search-dir MODEL_SEARCH_DIRAdditional 3D model search directory (repeatable)
--no-installed-kicadDo not probe installed KiCad installations or kicad_common.json preferences
--jsonEmit machine-readable JSON summary to stdout
--no-colorDisable ANSI colors and animated spinners

Configuring pcb clean

kcr pcb clean runs the shared safe documentation-layer cleanup engine (the exact same engine used by kcr vdoc) against a .kicad_pcb, .kicad_pro, .kicad_mod, or .pretty footprint library, emitting a structured JSON plan (kicad_cruncher.pcb.clean.plan.a0) or mutation report to stdout.

Config Discovery & pcb.clean.config

Schema contract: kicad_cruncher.pcb.clean.config.a0.

jsonc
{
  "schema": "kicad_cruncher.pcb.clean.config.a0",
  "targets": {
    "user_layers": true,             // Reset custom KiCad User.N display names on matched layers
    "generated_graphics": true,      // Remove generated cleanup metadata graphics
    "footprint_graphics": true,      // Remove matching footprint-local doc graphics
    "board_graphics": false,         // Disabled by default (enabled in kcr vdoc)
    "reference_fields": true,        // Hide visible Reference fields on matched layers
    "value_fields": true             // Hide visible Value fields on matched layers
  },
  "safety": {
    "protect_pads": true,            // Never remove pads
    "protect_models": true,          // Never remove 3D models
    "protect_mandatory_fields": true,// Never delete Reference/Value fields (hide only)
    "require_explicit_apply": true
  },
  "layers": {
    "include": ["*.User", "User.*", "*.Fab"],
    "exclude": ["F.Cu", "B.Cu", "In*.Cu", "Edge.Cuts"]
  },
  "metadata": {
    "field_name": "ALX_HLR_META",    // Legacy compatibility field name used to tag cleanup metadata
    "schema": "kicad_cruncher.pcb_clean.metadata.a0"
  }
}

pcb clean Conflict & Safety Rules

  1. layers.exclude ALWAYS beats layers.include: any layer matching an exclude glob is unconditionally protected even if it also matches an include glob.
  2. Mandatory fields are hidden, never deleted: Reference and Value properties on cleaned layers are marked (hide yes) rather than removed, preserving schematic/PCB identity.
  3. --dry-run vs --apply: mutually exclusive; omitting both behaves as plan/dry-run and never writes files.

How pcb embed-models Resolves & Locks Boards

Resolves external STEP/STP 3D models — including .wrl references that have a sibling .step or .stp file beside them — and embeds them directly into a .kicad_pcb (deduplicated by SHA-256 at board scope) or standalone .kicad_mod (at footprint scope) using KiCad 10.0.8 native encoding (one-shot zstd level 15 with frame content size + KiCad legacy-tail MMH3-128 checksum).

3D Model Discovery

By default, kcr pcb embed-models searches:

  1. ${KIPRJMOD} and paths relative to the board/project.
  2. Installed KiCad 3D libraries, kicad_common.json environment variables, and system.extra_3d_search_dirs (disable with --no-installed-kicad for hermetic CI runs).
  3. Process environment variables (KICAD10_3DMODEL_DIR, KICAD9_3DMODEL_DIR, etc.).
  4. Any explicit repeatable --model-search-dir <DIR> roots.

Inspect your workstation's resolved 3D paths with kcr kicad prefs.

pcb embed-models Safety Guards & Conflict Rules

  1. KiCad .lck lock-file guard:
    • Before any in-place write, kcr pcb embed-models checks for KiCad lock files (~<file>.lck, .<file>.lck, <file>.lck, ~<stem>.kicad_pro.lck, <stem>.kicad_pro.lck) beside the target board — and, when --update-project-library is set, pre-validates lock files on every touched local .kicad_mod before mutating the board on disk so partial writes never occur.
    • Refuses with exit code 1 if any lock file exists. Bypass safely by closing KiCad, using --dry-run, or writing to a separate file with -o <path>.
  2. --update-project-library + -o conflict:
    • Combining --update-project-library with -o <path> is rejected unless --dry-run is also passed, preventing a detached output board from mutating live project-local .pretty library files.
  3. --backup is in-place only:
    • --backup writes <input>.bak only when editing in place; when -o <path> targets a separate file, the original input is untouched so no .bak is created.
  4. Never converts VRML (.wrl) meshes:
    • If a .wrl reference has a sibling .step/.stp, it is upgraded and embedded cleanly. Truly WRL-only references are left untouched and reported as wrl_only warnings (never lossily mesh-converted).
  5. --re-embed, --prune-unused, and --refs semantics:
    • --re-embed re-encodes legacy embedded STEP payloads into KiCad 10.0.8 native zstd-15 + MMH3-128 format, consolidates footprint-level embedded STEP copies up to deduplicated board scope, and prunes orphaned type=model entries.
    • --prune-unused removes unreferenced type=model embedded_files entries while always preserving embedded fonts, worksheets, other non-model assets, WRL entries, and payloads referenced by footprints outside --refs.
    • --refs U4 U5 limits embedding to the listed designators (unselected footprints remain untouched).
    • --json emits the structured kicad_cruncher.pcb_embed_models.a0 summary to stdout.

Quick Examples

sh
# Preview PCB cleanup rules without modifying the board (dry-run is default for pcb clean)
kcr pcb clean --dry-run board.kicad_pcb
sh
# Write a starter pcb.clean.config JSONC template
kcr pcb clean --write-config pcb.clean.config
sh
# Preview which external STEP 3D models would be embedded natively (KiCad 10.0.8 format)
kcr pcb embed-models board.kicad_pcb --dry-run
sh
# Embed all resolved external STEP models at deduplicated board scope (and sync local .pretty footprints)
kcr pcb embed-models board.kicad_pcb --apply --update-project-library --backup
sh
# Re-encode legacy embedded STEP payloads in KiCad 10.0.8 native format and prune orphans
kcr pcb embed-models board.kicad_pcb --re-embed --prune-unused
kicad-monkey v2026.11.30·Docs updated 2026.12.01 15:43 UTC (fd9b75b)