On This Page (6)
Examples Showcase / fab_release_package
Variant-Aware Manufacturing Release (Gerber X2/X1, Drill, IPC-2581, PnP, BOM)
Produce a complete manufacturing release from impedance_4layer: Gerber X2 with virtual layers & cutouts, Gerber X1, Excellon NC Drill, IPC-2581B, per-variant PnP/BOM CSVs, and DNP crossout vs omit SVGs.
Output Previews
Build a complete, variant-aware manufacturing release from impedance_4layer.kicad_pro through a single KiCadFabView pipeline. Every fabrication deliverable—Gerber, NC Drill, IPC-2581B, variant BOM/PnP, and DNP assembly SVGs—lowers from the same whole-nanometre board model.
What It Shows
- Generating Gerber X2 with
Fab.to_gerbers(virtual_layers=..., profile_cutouts=True)and Gerber X1 withx2_attributes=False - Emitting Excellon NC Drill files via
Fab.to_nc_drill()and IPC-2581B XML viaFab.to_ipc2581() - Producing variant-filtered Pick-and-Place and BOM CSVs via
KiCadPcb.fab(variant=...),Fab.to_pnp(), andFab.to_bom() - Comparing
VirtualLayerOptions(dnp_mode="crossout")againstdnp_mode="omit"in Paper + Dark SVGs
Input
examples/assets/projects/impedance_4layer/impedance_4layer.kicad_pro
examples/assets/projects/impedance_4layer/impedance_4layer.kicad_pcbRun
From the repository root:
uv run python examples/fab_release_package/fab_release_package.pyOutput
examples/fab_release_package/output/gerber_x2/
examples/fab_release_package/output/gerber_x1/
examples/fab_release_package/output/drill/
examples/fab_release_package/output/ipc2581/
examples/fab_release_package/output/variants/
examples/fab_release_package/output/bare_board_fab.svg
examples/fab_release_package/output/bare_board_fab_dark.svg
examples/fab_release_package/output/variant_NoDebug_dnp_crossout.svg
examples/fab_release_package/output/variant_NoDebug_dnp_crossout_dark.svg
examples/fab_release_package/output/variant_NoDebug_dnp_omit.svg
examples/fab_release_package/output/variant_NoDebug_dnp_omit_dark.svg
examples/fab_release_package/output/release_manifest.jsonSource Code
"""Variant-aware manufacturing release: Gerber X2/X1, NC Drill, IPC-2581B, PnP, BOM & DNP SVGs."""
from __future__ import annotations
import json
import re
from pathlib import Path
from kicad_monkey import KiCadPcb, KiCadProject
from kicad_monkey.fab import (
AssemblyDesignatorLayerOptions,
AssemblyOutlineLayerOptions,
CutoutLayerOptions,
Pin1LayerOptions,
VirtualLayerOptions,
)
HERE = Path(__file__).resolve().parent
PROJECT_DIR = HERE.parent / "assets" / "projects" / "impedance_4layer"
PROJECT_FILE = PROJECT_DIR / "impedance_4layer.kicad_pro"
PCB_FILE = PROJECT_DIR / "impedance_4layer.kicad_pcb"
OUT_DIR = HERE / "output"
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 _render_pair(
fab,
layers: tuple[str, ...],
opts: VirtualLayerOptions,
stem: str,
*,
top_paper: str = "#CBD5E1",
top_dark: str = "#334155",
) -> None:
paper = next(
iter(
fab.to_svg(
layers=layers,
scale=8.0,
virtual_options=opts,
layer_colours={
"PROFILE": "#F1F5F9",
"TOP": top_paper,
"DRILLS": "#FFFFFF",
},
).values()
)
).text
(OUT_DIR / f"{stem}.svg").write_text(_add_canvas_bg(paper, "#FFFFFF"), encoding="utf-8")
dark = next(
iter(
fab.to_svg(
layers=layers,
scale=8.0,
virtual_options=opts,
layer_colours={
"PROFILE": "#1E293B",
"TOP": top_dark,
"DRILLS": "#11161D",
},
).values()
)
).text
(OUT_DIR / f"{stem}_dark.svg").write_text(_add_canvas_bg(dark, "#11161D"), encoding="utf-8")
def main() -> None:
OUT_DIR.mkdir(parents=True, exist_ok=True)
project = KiCadProject.from_file(PROJECT_FILE)
pcb = KiCadPcb.from_file(PCB_FILE)
base_opts = VirtualLayerOptions(
dnp_mode="crossout",
cutouts=CutoutLayerOptions(scope="interior", hatch=True, hatch_pitch_mm=1.0),
pin1=Pin1LayerOptions(style="dot"),
assembly_outlines=AssemblyOutlineLayerOptions(detail="simple"),
assembly_designators=AssemblyDesignatorLayerOptions(),
)
omit_opts = VirtualLayerOptions(
dnp_mode="omit",
cutouts=base_opts.cutouts,
pin1=base_opts.pin1,
assembly_outlines=base_opts.assembly_outlines,
assembly_designators=base_opts.assembly_designators,
)
# 1. Bare-board fabrication outputs (Gerber X2, Gerber X1, Excellon NC Drill, IPC-2581B)
fab = pcb.fab(stem="impedance_4layer")
gerber_x2 = fab.to_gerbers(
virtual_layers=("CUTOUTS", "PIN1_TOP", "ASSEMBLY_OUTLINES_TOP"),
profile_cutouts=True,
virtual_options=base_opts,
)
gerber_x2.write(OUT_DIR / "gerber_x2", overwrite=True)
gerber_x1 = fab.to_gerbers(x2_attributes=False, profile_cutouts=True)
gerber_x1.write(OUT_DIR / "gerber_x1", overwrite=True)
nc_drill = fab.to_nc_drill()
nc_drill.write(OUT_DIR / "drill", overwrite=True)
ipc2581 = fab.to_ipc2581(virtual_layers=("CUTOUTS", "PIN1_TOP"), virtual_options=base_opts)
ipc2581.write(OUT_DIR / "ipc2581", overwrite=True)
# 2. Bare-board preview SVG (Paper + Dark)
_render_pair(
fab,
("PROFILE", "TOP", "CUTOUTS", "DRILLS"),
base_opts,
"bare_board_fab",
top_paper="#B91C1C",
top_dark="#F87171",
)
# 3. Variant-aware assembly outputs (PnP CSV, BOM CSV, DNP crossout vs omit SVGs)
variant_summary: dict[str, dict[str, object]] = {}
for variant_name in ("Default", *(v.name for v in project.variants)):
var_arg = None if variant_name == "Default" else variant_name
vfab = pcb.fab(variant=var_arg, stem=f"impedance_4layer_{variant_name}")
vdir = OUT_DIR / "variants" / variant_name
pnp_out = vfab.to_pnp()
bom_out = vfab.to_bom()
pnp_out.write(vdir, overwrite=True)
bom_out.write(vdir, overwrite=True)
variant_summary[variant_name] = {
"bom_lines": len(vfab.view().bom()),
"pnp_files": sorted(pnp_out.keys()),
"bom_files": sorted(bom_out.keys()),
}
# Render NoDebug variant assembly comparison: DNP crossout vs DNP omit (Paper + Dark)
nodebug_fab = pcb.fab(variant="NoDebug", stem="impedance_4layer_NoDebug")
assy_layers = (
"PROFILE",
"CUTOUTS",
"TOP",
"DRILLS",
"ASSEMBLY_OUTLINES_TOP",
"ASSEMBLY_DESIGNATORS_TOP",
"PIN1_TOP",
)
_render_pair(nodebug_fab, assy_layers, base_opts, "variant_NoDebug_dnp_crossout")
_render_pair(nodebug_fab, assy_layers, omit_opts, "variant_NoDebug_dnp_omit")
manifest = {
"gerber_x2_files": sorted(gerber_x2.keys()),
"gerber_x1_files": sorted(gerber_x1.keys()),
"nc_drill_files": sorted(nc_drill.keys()),
"ipc2581_files": sorted(ipc2581.keys()),
"variants": variant_summary,
}
(OUT_DIR / "release_manifest.json").write_text(json.dumps(manifest, indent=2) + "\n", encoding="utf-8")
print(
f"Release package complete: {len(gerber_x2)} Gerber X2, {len(gerber_x1)} Gerber X1, "
f"{len(nc_drill)} NC Drill, {len(ipc2581)} IPC-2581B, "
f"{len(variant_summary)} variant PnP/BOM bundles + 6 Paper/Dark SVGs"
)
if __name__ == "__main__":
main()