On This Page (6)
Examples Showcase / pcb_layer_svgs_and_annotations
Query KiCadFabView, Per-Layer SVGs & Annotated Inspection Views
Query KiCadFabView layers, holes, components, and JSON round-trip, emit per-layer SVGs via to_layer_svgs(), and render annotated top (pad labels + origin) and mirrored bottom views in Paper + Dark themes.
Output Previews
Query KiCadFabView directly (layers(), holes(), components(), nets()), round-trip its portable JSON snapshot (to_json() / from_json()), emit one SVG per board layer via Fab.to_layer_svgs(), and render annotated top inspection (pad_labels + origin_marker) and mirrored bottom views in both Paper and Dark themes.
What It Shows
- Querying
KiCadFabView.layers(),KiCadFabView.holes(),KiCadFabView.components(), andKiCadFabView.nets() - Serializing and reloading portable FabView JSON via
KiCadFabView.to_json()andKiCadFabView.from_json() - Emitting individual per-layer SVGs with
Fab.to_layer_svgs() - Rendering annotated top SVGs (
annotations=[{"kind": "pad_labels"}, {"kind": "origin_marker"}]) and mirrored bottom SVGs (mirror=True) in Paper + Dark themes viaFab.to_svg()
Input
examples/assets/projects/impedance_4layer/impedance_4layer.kicad_pcbRun
From the repository root:
uv run python examples/pcb_layer_svgs_and_annotations/pcb_layer_svgs_and_annotations.pyOutput
examples/pcb_layer_svgs_and_annotations/output/board_top_annotated.svg
examples/pcb_layer_svgs_and_annotations/output/board_top_annotated_dark.svg
examples/pcb_layer_svgs_and_annotations/output/board_bottom_mirrored.svg
examples/pcb_layer_svgs_and_annotations/output/board_bottom_mirrored_dark.svg
examples/pcb_layer_svgs_and_annotations/output/layers/
examples/pcb_layer_svgs_and_annotations/output/fabview_summary.jsonSource Code
"""Query KiCadFabView, emit per-layer SVGs, and render annotated top & mirrored bottom views."""
from __future__ import annotations
import json
import re
from pathlib import Path
from kicad_monkey import KiCadPcb
from kicad_monkey.fab import KiCadFabView
HERE = Path(__file__).resolve().parent
INPUT_PCB = HERE.parent / "assets" / "projects" / "impedance_4layer" / "impedance_4layer.kicad_pcb"
OUT_DIR = HERE / "output"
LAYERS_DIR = OUT_DIR / "layers"
def _add_canvas_bg(svg_text: str, color: str) -> str:
match = re.search(r'viewBox="([^"]+)"', svg_text)
if not match:
return svg_text
x, y, w, h = match.group(1).split()
bg = f'<rect x="{x}" y="{y}" width="{w}" height="{h}" fill="{color}"/>'
return svg_text.replace(">\n", f">\n{bg}\n", 1)
def main() -> None:
OUT_DIR.mkdir(parents=True, exist_ok=True)
LAYERS_DIR.mkdir(parents=True, exist_ok=True)
pcb = KiCadPcb.from_file(INPUT_PCB)
fab = pcb.fab(stem="impedance_4layer")
view = fab.view()
# 1. Serialize & reload KiCadFabView JSON round-trip (portable fabrication IR)
fabview_bytes = view.to_json()
reloaded_view = KiCadFabView.from_json(fabview_bytes)
assert len(reloaded_view.layers()) == len(view.layers())
assert len(reloaded_view.holes()) == len(view.holes())
summary = {
"layers": [{"key": l.key, "role": l.role} for l in view.layers()],
"component_count": len(view.components()),
"net_count": len(view.nets()),
"hole_count": len(view.holes()),
"fabview_json_bytes": len(fabview_bytes),
}
(OUT_DIR / "fabview_summary.json").write_text(json.dumps(summary, indent=2) + "\n", encoding="utf-8")
# 2. Emit one SVG per fabrication layer
layer_svgs = fab.to_layer_svgs(scale=8.0)
layer_svgs.write(LAYERS_DIR, overwrite=True)
# 3. Render annotated TOP view (pad labels + origin marker) in Paper + Dark themes
top_layers = ("PROFILE", "TOP", "TOPOVERLAY", "DRILLS")
annotations = ({"kind": "pad_labels"}, {"kind": "origin_marker"})
top_paper = next(
iter(
fab.to_svg(
layers=top_layers,
scale=8.0,
annotations=annotations,
layer_colours={
"PROFILE": "#F1F5F9",
"TOP": "#C2410C",
"TOPOVERLAY": "#0F172A",
"DRILLS": "#FFFFFF",
},
).values()
)
).text
(OUT_DIR / "board_top_annotated.svg").write_text(_add_canvas_bg(top_paper, "#FFFFFF"), encoding="utf-8")
top_dark = next(
iter(
fab.to_svg(
layers=top_layers,
scale=8.0,
annotations=annotations,
layer_colours={
"PROFILE": "#1E293B",
"TOP": "#F97316",
"TOPOVERLAY": "#E2E8F0",
"DRILLS": "#11161D",
},
).values()
)
).text
(OUT_DIR / "board_top_annotated_dark.svg").write_text(_add_canvas_bg(top_dark, "#11161D"), encoding="utf-8")
# 4. Render mirrored BOTTOM inspection view in Paper + Dark themes
bot_layers = ("PROFILE", "BOTTOM", "BOTTOMOVERLAY", "DRILLS")
bot_paper = next(
iter(
fab.to_svg(
layers=bot_layers,
scale=8.0,
mirror=True,
annotations=({"kind": "origin_marker"},),
layer_colours={
"PROFILE": "#F1F5F9",
"BOTTOM": "#1D4ED8",
"BOTTOMOVERLAY": "#0F172A",
"DRILLS": "#FFFFFF",
},
).values()
)
).text
(OUT_DIR / "board_bottom_mirrored.svg").write_text(_add_canvas_bg(bot_paper, "#FFFFFF"), encoding="utf-8")
bot_dark = next(
iter(
fab.to_svg(
layers=bot_layers,
scale=8.0,
mirror=True,
annotations=({"kind": "origin_marker"},),
layer_colours={
"PROFILE": "#1E293B",
"BOTTOM": "#38BDF8",
"BOTTOMOVERLAY": "#E2E8F0",
"DRILLS": "#11161D",
},
).values()
)
).text
(OUT_DIR / "board_bottom_mirrored_dark.svg").write_text(_add_canvas_bg(bot_dark, "#11161D"), encoding="utf-8")
print(
f"Queried KiCadFabView: {len(view.layers())} layers, {len(view.components())} components, "
f"{len(view.holes())} holes, {len(layer_svgs)} per-layer SVGs + annotated top/bottom views"
)
if __name__ == "__main__":
main()