On This Page (8)
Fabrication & Virtual Layers in Rust (kicad-monkey-fab)
kicad-monkey-fab is the pure-Rust manufacturing and virtual-layer engine for KiCad >= 7 PCBs (MIN_SUPPORTED_PCB_KICAD_MAJOR = 7, #![forbid(unsafe_code)], zero KiCad desktop runtime, zero OpenCASCADE).
Every manufacturing output lowers from a single normalized whole-nanometre board model — FabViewData — ensuring copper Gerbers, Excellon drills, IPC-2581B stackups, analytic SVGs, Micro-STEP solids, and all 17 virtual fab layers agree to the exact nanometre.
FabViewData & KiCadFab Architecture
FabViewData converts a parsed kicad_monkey_core::PcbView into mathematical +Y-up integer nanometres (CoordNm), normalizing board stackup rows (FabStackRow), outer profile + cutouts (FabProfile), per-layer items (FabLayerItems containing lands, strokes, areas), holes/slots (FabHole), drill spans (FabDrillPair), components (FabComponent), and auxiliary origin.
KiCadFab wraps FabViewData with ergonomic single-call and batch exporters:
| Entry Point | Options Struct | Output |
|---|---|---|
KiCadFab::from_pcb_file(path) / from_pcb_source(src, name) / from_pcb_view(&view, name) | — | Constructs one normalized FabViewData reused across all outputs |
fab.to_gerbers(&GerberOptions) | GerberOptions (stem, x2, use_aux_origin, include_profile, include_silk) | Vec<FabFile> (.GTL, .GBL, .G1…, .GTS, .GBS, .GTP, .GBP, .GTO, .GBO, .GM1) |
fab.to_nc_drill(&DrillOptions) | DrillOptions (stem, use_aux_origin) | Vec<FabFile> (multi-span PTH/NPTH + G85 routed oval slots) |
fab.to_ipc2581() | — | Schema-valid IPC-2581 Rev B .cvg XML String |
fab.to_micro_step() / write_layer_step(&view, &LayerStepOptions) | LayerStepOptions | Self-contained ISO 10303-21 Micro-STEP String / LayerStepResult |
fab.to_svg(&SvgOptions) / fab.to_layer_svgs(&SvgOptions) | SvgOptions (stem, bottom, padding_mm, include_silk) | Analytic composite board SVG String or per-layer Vec<FabFile> |
fab.to_pnp(use_aux_origin) / fab.to_bom() | bool | Pick-and-Place centroid CSV and grouped BOM CSV String |
fab.virtual_layer(name) / build_virtual_layer(&view, key) | Per-layer option structs (Pin1Options, DiodeOptions, CapacitorOptions, InductorOptions) | FabLayerItems for any of the 17 canonical virtual layers |
fab.write_all(out_dir, &FabAllOptions) | FabAllOptions | Writes any combination of Gerbers, drill, IPC-2581B, SVGs, PnP, BOM, Micro-STEP, and virtual-layer Gerbers into out_dir |
Calibration & Performance
kicad-monkey-fab is calibrated directly against kicad-cli 10.0.8 + gerbv across all 8 unified pcb_fab synthetic test boards (42/42 P1, 6/6 P2 drills/slots, 5/5 P3 silkscreen — 100.0%) and 22 KiCad >= 7 real-world boards up to 12 copper layers (100% copper parity, 39/39 P2 drills/slots, 189/192 P1 at mean 0.9961 / 0.9996), running 2.4x–7.5x faster than the Python KiCadFabView pipeline.
All 17 Canonical Virtual Fab Layers
ALL_VIRTUAL_LAYERS exposes all 17 canonical virtual layers, and resolve_virtual_layer_key normalizes common aliases (BOARD_CUTOUTS, PIN_1_TOP, PIN_1_BOTTOM, CAPS_TOP, CAPS_BOTTOM, ASSY_OUTLINES_*, ASSY_DESIGNATORS_*):
| Group | Canonical Keys | Rust Customization Helpers |
|---|---|---|
| Board Cutouts | CUTOUTS | build_virtual_layer(&view, "CUTOUTS") |
| Pin 1 / Polarity | PIN1_TOP, PIN1_BOTTOM | pin1::build_pin1_layer(&view, side, &Pin1Options) (Pin1Override, BGA/grid pad detection) |
| Diodes | DIODES_TOP, DIODES_BOTTOM | diodes::build_diodes_layer(&view, side, &DiodeOptions) (DiodeOverride, DiodeType, DiodeVisibilityMode) |
| Passives | CAPACITORS_TOP, CAPACITORS_BOTTOM, INDUCTORS_TOP, INDUCTORS_BOTTOM, RESISTORS_TOP, RESISTORS_BOTTOM | passives::build_capacitors_layer_with_options, build_inductors_layer_with_options, build_resistors_layer (CapacitorOptions, InductorOptions) |
| Assembly | ASSEMBLY_OUTLINES_TOP, ASSEMBLY_OUTLINES_BOTTOM, ASSEMBLY_DESIGNATORS_TOP, ASSEMBLY_DESIGNATORS_BOTTOM | designator_fit::DesignatorFitSession (DEFAULT_OBLIQUE_MIN_ASPECT_RATIO, subtract_cutouts, pad_envelope) |
| Pad Labels | PAD_LABELS_TOP, PAD_LABELS_BOTTOM | build_virtual_layer(&view, "PAD_LABELS_TOP") |
Any virtual layer's FabLayerItems can be rendered straight to Gerber with render_virtual_gerber(key, &items, shift, x2) or baked back into a KiCad source file via bake / bake_footprint.
Board & Footprint vdoc Baking (bake & bake_footprint)
kicad-monkey-fab implements the same non-destructive vdoc baking workflow exposed by k3 vdoc / kcr vdoc, using lossless byte-range edits so untouched board and footprint syntax is preserved byte-for-byte:
Board Baking (bake.rs)
bake_virtual_layers_source(source, name, &BakeVirtualLayersOptions)(orbake_virtual_layers_with_viewif you already constructed aFabViewData): cleans priorVirtual_<KEY>groups (VIRTUAL_GROUP_PREFIX) and optional legacy documentation layers (PcbCleanOptions), checks for user-layer collisions (detect_user_layer_collisions), and stamps deterministic KiCad primitives onto nativeF.Fab/B.Fab/User.*/Eco*.Userlayers.VdocLayerPreset:VdocLayerPreset::Fab(default):ASSEMBLY_OUTLINES_*+ASSEMBLY_DESIGNATORS_*shareF.Fab/B.Fab; other virtual layers go onUser.1–User.5/Eco1.User/Eco2.User.VdocLayerPreset::FabOutlines: outlines stay onF.Fab/B.Fab; designators move toUser.6/User.7.VdocLayerPreset::UserOnly: all virtual layers stay onUser.1–User.9/Eco1.User/Eco2.User.
DEFAULT_VDOC_PCB_LAYERS: defaults to 9 core layers (CUTOUTS,PIN1_TOP/BOTTOM,DIODES_TOP/BOTTOM,ASSEMBLY_OUTLINES_TOP/BOTTOM,ASSEMBLY_DESIGNATORS_TOP/BOTTOM).
Standalone Footprint Baking (bake_footprint.rs)
build_footprint_fab_view+bake_virtual_layers_to_footprint_source: wraps a standalone.kicad_modinside a transient 1-footprint(kicad_pcb ...)at(0, 0, 0)onF.Cuwith a fixed header byte length so all pad, polarity, diode, and passive heuristics run through the exact sameFabViewDataengine, then emits native footprint-localfp_line,fp_arc,fp_circle, andfp_polyforms right before the closing parenthesis of.kicad_mod.- Supports the 7 top-side footprint-applicable layers (
FOOTPRINT_SUPPORTED_VIRTUAL_LAYERS:ASSEMBLY_OUTLINES_TOP,PIN1_TOP,DIODES_TOP,CAPACITORS_TOP,INDUCTORS_TOP,RESISTORS_TOP,PAD_LABELS_TOP; defaults to 6 viaDEFAULT_FOOTPRINT_VIRTUAL_LAYERS).
use kicad_monkey_fab::{
BakeVirtualLayersOptions, DEFAULT_VDOC_PCB_LAYERS, VdocLayerPreset,
bake_virtual_layers_source, default_kicad_layer_for_virtual_preset,
};
pub fn bake_board_vdoc(pcb_source: &str) -> Result<String, kicad_monkey_fab::FabError> {
let mut opts = BakeVirtualLayersOptions::default();
opts.layer_map = DEFAULT_VDOC_PCB_LAYERS
.iter()
.filter_map(|&key| {
default_kicad_layer_for_virtual_preset(key, VdocLayerPreset::FabOutlines)
.map(|layer| (key.to_owned(), layer.to_owned()))
})
.collect();
let (updated_source, report) = bake_virtual_layers_source(pcb_source, "board", &opts)?;
println!("Baked {} virtual layer(s)", report.layers.len());
Ok(updated_source)
}Honest Parity Notes vs. Python KiCadFabView
kicad-monkey-fab is pure Rust with no Geometer / OpenCASCADE dependency and no Python runtime. Keep these deliberate differences in mind when choosing between the Rust library crate, the native k3 CLI, and Python kicad-monkey:
- 3D STEP HLR outlines: Standalone
kicad-monkey-fabderivesASSEMBLY_OUTLINES_*from nativeF.Fab/B.Fabartwork or pad bounding boxes (no STEP HLR ordnp_mode="crossout"). In thek3CLI binary,kicad-cruncher-clilayers Geometer STEP HLR on top viaoutline_override. CUTOUTSstyling: Rustbuild_virtual_layer(&view, "CUTOUTS")emits crisp0.15 mmcutout outline strokes (no 45° vector hatch or cleared"CUTOUT"text callout).PIN1_*&PAD_LABELS_*: RustPIN1_*uses thedot(+ full BGA pad fill) style withA/K,+/-, and inductor markers (nosquare/pad/knockoutstyles);PAD_LABELS_*emits horizontal (0°) stroke labels.- Aliases & defaults: Rust
resolve_virtual_layer_keyacceptsPIN_1_*,BOARD_CUTOUTS,CAPS_*, andASSY_*(notPOLARITY_*), andDEFAULT_VDOC_PCB_LAYERSdefaults to 9 layers (vs. Python's 15). - Wire model: Rust
FabViewDatauses handwrittenserdestructs rather than the Pythonkicad_monkey.fab_view.a0msgspecenvelope.
For the complete side-by-side table, see the Python vs. Native Rust (kicad-monkey-fab) Parity Matrix and the Native Rust CLI (k3) Parity Guide.
Related
- Practical Rust Cookbook — full Gerber/Drill/IPC-2581/Micro-STEP export recipe.
- Crate-by-Crate Reference —
kicad-monkey-fabdependencies and sibling crates. - Virtual Fab Layers (All 17 Layers) — full virtual layer reference and placement algorithms.
- Fabrication & Enriched SVG Wire Formats — Gerber X2, Excellon, IPC-2581B, and Micro-STEP conventions.