On This Page (6)
Examples Showcase / footprint_library_vdoc
Bake 3D HLR & Polarity vdoc Virtual Layers into .pretty Footprints
Extract IC, diode, capacitor, and resistor footprints into a .pretty library, check User.* collisions, bake 3D HLR outlines and polarity markers with bake_virtual_layers_to_footprint(), and render before/after SVGs.
Output Previews
Bake virtual documentation layers directly into standalone .kicad_mod library parts so assembly outlines, Pin 1 dots, and polarity glyphs travel with every footprint. This example extracts four representative parts from the showcase board, guards against User.* layer collisions, and renders before-and-after Paper and Dark SVG previews.
What It Shows
- Extracting standalone
.kicad_modfiles into a.prettylibrary withextract_footprints_from_pcb() - Checking target
User.*layers for pre-existing graphics withdetect_footprint_layer_collisions() - Baking 3D HLR assembly outlines, Pin-1 dots, diode/capacitor/resistor symbols directly into
.kicad_modfiles withbake_virtual_layers_to_footprint()andBakeFootprintVirtualLayersOptions - Rendering before/after Paper + Dark footprint SVGs via
KiCadFootprint.to_svg()
Input
examples/fab_virtual_layers/virtual_fab_layers.kicad_pcbRun
From the repository root:
uv run python examples/footprint_library_vdoc/footprint_library_vdoc.pyOutput
examples/footprint_library_vdoc/output/vdoc_parts.pretty/
examples/footprint_library_vdoc/output/*_before.svg
examples/footprint_library_vdoc/output/*_after.svg
examples/footprint_library_vdoc/output/footprint_vdoc_report.jsonSource Code
"""Extract diverse footprints into a .pretty library, bake vdoc virtual layers, and render before/after SVGs."""
from __future__ import annotations
import json
from pathlib import Path
from kicad_monkey import KiCadFootprint, KiCadPcb, KiCadSvgRenderOptions
from kicad_monkey.fab import (
AssemblyOutlineLayerOptions,
BakeFootprintVirtualLayersOptions,
CapacitorLayerOptions,
DiodeLayerOptions,
Pin1LayerOptions,
ResistorLayerOptions,
VirtualLayerOptions,
bake_virtual_layers_to_footprint,
detect_footprint_layer_collisions,
)
from kicad_monkey.kicad_footprint_extractor import extract_footprints_from_pcb
HERE = Path(__file__).resolve().parent
SOURCE_PCB = HERE.parent / "fab_virtual_layers" / "virtual_fab_layers.kicad_pcb"
OUT_DIR = HERE / "output"
PRETTY_DIR = OUT_DIR / "vdoc_parts.pretty"
# Representative parts spanning IC, diode/LED, polarized capacitor, and resistor
TARGET_REFS = ("U1", "D10", "C1", "R1")
VDOC_LAYERS = ("ASSEMBLY_OUTLINES_TOP", "PIN1_TOP", "DIODES_TOP", "CAPACITORS_TOP", "RESISTORS_TOP")
def _render_fp_pair(fp: KiCadFootprint, stem: str) -> None:
paper = fp.to_svg(
black_and_white=False,
options=KiCadSvgRenderOptions(background_color="#FFFFFF"),
)
(OUT_DIR / f"{stem}.svg").write_text(paper, encoding="utf-8")
dark = fp.to_svg(
fill="#E2E8F0",
stroke="#E2E8F0",
black_and_white=False,
options=KiCadSvgRenderOptions(
background_color="#11161D",
color_overrides={
"F.SilkS": "#F8FAFC",
"F.Fab": "#94A3B8",
"F.CrtYd": "#38BDF8",
"User.1": "#38BDF8",
"User.2": "#FB7185",
"User.3": "#FBBF24",
"User.4": "#34D399",
"User.5": "#A78BFA",
"User.6": "#F97316",
"User.7": "#F472B6",
},
),
)
(OUT_DIR / f"{stem}_dark.svg").write_text(dark, encoding="utf-8")
def main() -> None:
OUT_DIR.mkdir(parents=True, exist_ok=True)
PRETTY_DIR.mkdir(parents=True, exist_ok=True)
# 1. Extract all unique footprints from virtual_fab_layers.kicad_pcb into a .pretty folder
extract_footprints_from_pcb(SOURCE_PCB, PRETTY_DIR, overwrite=True)
# Map board references (U1, D10, C1, R1) to their extracted .kicad_mod files
pcb = KiCadPcb.from_file(SOURCE_PCB)
fp_by_ref = {fp.get_property("Reference") or "": fp for fp in pcb.footprints}
mod_by_stem = {p.stem: p for p in PRETTY_DIR.glob("*.kicad_mod")}
vopts = VirtualLayerOptions(
pin1=Pin1LayerOptions(style="dot"),
diodes=DiodeLayerOptions(color_mode="shaded"),
capacitors=CapacitorLayerOptions(visibility_mode="all", color_mode="shaded"),
resistors=ResistorLayerOptions(style="zigzag", color_mode="shaded"),
assembly_outlines=AssemblyOutlineLayerOptions(detail="simple"),
)
reports: list[dict[str, object]] = []
for ref in TARGET_REFS:
board_fp = fp_by_ref.get(ref)
if board_fp is None:
continue
fp_name = board_fp.library_link.split(":")[-1]
mod_path = mod_by_stem.get(fp_name)
if mod_path is None:
continue
fp = KiCadFootprint.from_file(mod_path)
_render_fp_pair(fp, f"{ref}_{fp_name}_before")
# 2. Check for User.* layer collisions before baking
collisions = detect_footprint_layer_collisions(fp, VDOC_LAYERS)
# 3. Bake virtual documentation layers directly into the standalone footprint
bake_report = bake_virtual_layers_to_footprint(
fp,
BakeFootprintVirtualLayersOptions(
layers=VDOC_LAYERS,
virtual_options=vopts,
synthetic_designator=ref,
force=bool(collisions),
),
)
fp.save(mod_path)
_render_fp_pair(fp, f"{ref}_{fp_name}_after")
total_written = sum(layer.items_written for layer in bake_report.layers)
reports.append(
{
"reference": ref,
"footprint": fp_name,
"collisions": len(collisions),
"primitives_baked": total_written,
"layers": [
{"key": l.virtual_key, "kicad_layer": l.kicad_layer, "items_written": l.items_written}
for l in bake_report.layers
if l.items_written > 0
],
}
)
(OUT_DIR / "footprint_vdoc_report.json").write_text(json.dumps(reports, indent=2) + "\n", encoding="utf-8")
for rep in reports:
print(
f"Baked {rep['reference']} ({rep['footprint']}): "
f"{rep['primitives_baked']} native User.* primitives ({rep['collisions']} collisions)"
)
if __name__ == "__main__":
main()