altium monkey v2026.11.26 · gobblerito
On This Page (5)

Examples Showcase / fab_release_package

Generate a complete variant-aware PCB fab release package

Open Hydroscope.PrjPcb, build variant-scoped Fab sessions, emit Gerber X2/X1, NC Drill, IPC-2581, PnP, and BOM for Variant A (J6 DNP), and compare bare-board SVGs plus No Variant, Variant A (dnp_mode='omit' and 'crossout'), and Variant B virtual overlays.

fabpcbdocsvgfabgerbervirtual-layersdnpnc-drillipc2581

Output Previews

board_top.svg
Open SVG
board_top.svg
board_bottom.svg
Open SVG
board_bottom.svg
board_top_virtual_no_variant.svg
Open SVG
board_top_virtual_no_variant.svg
board_top_virtual_variant_a_omit.svg
Open SVG
board_top_virtual_variant_a_omit.svg
board_top_virtual_variant_a_crossout.svg
Open SVG
board_top_virtual_variant_a_crossout.svg
board_top_virtual_variant_b.svg
Open SVG
board_top_virtual_variant_b.svg

Generate a complete, variant-aware PCB manufacturing release package from Hydroscope.PrjPcb using pcbdoc.fab(variant=...), demonstrating how bare-board fabrication layers stay identical across assembly variants while assembly-scoped virtual layers automatically honor each variant's DNP list in both dnp_mode="omit" and dnp_mode="crossout" modes.

What It Shows

  1. Opening Hydroscope.PrjPcb with AltiumDesign.from_prjpcb(...) and building variant-scoped Fab sessions for Variant A (J6 Not Fitted / DNP), No Variant, and Variant B (both fully populated)
  2. Configuring shared VirtualLayerOptions (font_mode, stroke_font, dnp_mode, ComponentVisibilityPolicy, CutoutLayerOptions, Pin1LayerOptions, DiodeLayerOptions, AssemblyOutlineLayerOptions, AssemblyDesignatorLayerOptions) once across Gerber, IPC-2581B, and SVG
  3. Emitting Gerber X2 with explicit layer selection (keepout, profile, pad_masters=False, drill_guide=False, drill_drawing=True, mechanical_layers=("used",)), dual-key custom extensions ("PROFILE", "GTO", "CUTOUTS"), variant-aware virtual_layers, profile_cutouts=True, and virtual_options, alongside plain RS-274X Gerber X1 (x2_attributes=False)
  4. Emitting Excellon NC Drill (to_nc_drill()), IPC-2581B (to_ipc2581(...) with document_layers, virtual_layers, and virtual_options), Pick-and-Place (to_pnp()), and BOM (to_bom()) from the Variant A session
  5. Rendering outline-clipped, drill-clipped bare-board top and mirrored bottom inspection SVGs (black solder mask, ENIG gold pads, white silkscreen) plus four side-by-side virtual assembly overlay SVGs comparing:
    • No Variant (J6 populated)
    • Variant A — dnp_mode="omit" (J6 omitted from outlines, designators, and Pin 1 markers while bare pads remain intact)
    • Variant A — dnp_mode="crossout" (J6 rendered with a dimmed dashed outline, muted-red diagonal X, and dimmed designator, while still omitted from Pin 1 markers)
    • Variant B (J6 populated)
  6. Writing every output bundle with FabOutput.write(...) plus a structured release manifest (fab_release_manifest.json)

Run

From the repository root:

powershell
uv run python examples\fab_release_package\fab_release_package.py

Output

The script writes:

text
examples/fab_release_package/output/board_top.svg
examples/fab_release_package/output/board_bottom.svg
examples/fab_release_package/output/board_top_virtual_no_variant.svg
examples/fab_release_package/output/board_top_virtual_variant_a_omit.svg
examples/fab_release_package/output/board_top_virtual_variant_a_crossout.svg
examples/fab_release_package/output/board_top_virtual_variant_b.svg
examples/fab_release_package/output/gerbers_x2/
examples/fab_release_package/output/gerbers_x1/
examples/fab_release_package/output/nc_drill/
examples/fab_release_package/output/ipc2581/
examples/fab_release_package/output/assembly/
examples/fab_release_package/output/fab_release_manifest.json

Source Code

examples/fab_release_package/fab_release_package.py
from __future__ import annotations

import json
import shutil
from pathlib import Path

from altium_monkey import AltiumDesign
from altium_monkey.fab import (
    AssemblyDesignatorLayerOptions,
    AssemblyOutlineLayerOptions,
    ComponentVisibilityPolicy,
    CutoutLayerOptions,
    DiodeLayerOptions,
    Pin1LayerOptions,
    VirtualDnpMode,
    VirtualLayerOptions,
)

SAMPLE_DIR = Path(__file__).resolve().parent
EXAMPLES_DIR = SAMPLE_DIR.parent
PROJECT_FILE = (
    EXAMPLES_DIR / "assets" / "projects" / "hydroscope" / "Hydroscope.PrjPcb"
)
OUTPUT_DIR = SAMPLE_DIR / "output"
GERBERS_X2_DIR = OUTPUT_DIR / "gerbers_x2"
GERBERS_X1_DIR = OUTPUT_DIR / "gerbers_x1"
DRILL_DIR = OUTPUT_DIR / "nc_drill"
IPC_DIR = OUTPUT_DIR / "ipc2581"
ASSEMBLY_DIR = OUTPUT_DIR / "assembly"
TOP_SVG_PATH = OUTPUT_DIR / "board_top.svg"
BOTTOM_SVG_PATH = OUTPUT_DIR / "board_bottom.svg"
VIRTUAL_NO_VARIANT_SVG_PATH = OUTPUT_DIR / "board_top_virtual_no_variant.svg"
VIRTUAL_VARIANT_A_OMIT_SVG_PATH = OUTPUT_DIR / "board_top_virtual_variant_a_omit.svg"
VIRTUAL_VARIANT_A_CROSSOUT_SVG_PATH = (
    OUTPUT_DIR / "board_top_virtual_variant_a_crossout.svg"
)
VIRTUAL_VARIANT_B_SVG_PATH = OUTPUT_DIR / "board_top_virtual_variant_b.svg"
MANIFEST_PATH = OUTPUT_DIR / "fab_release_manifest.json"
SELECTED_VARIANT = "A"

VIRTUAL_LAYERS: tuple[str, ...] = (
    "CUTOUTS",
    "PIN1_TOP",
    "DIODES_TOP",
    "ASSEMBLY_OUTLINES_TOP",
    "ASSEMBLY_DESIGNATORS_TOP",
)

VIRTUAL_SVG_LAYERS: tuple[str, ...] = (
    "PROFILE",
    "TOP",
    "TOPSOLDER",
    "DRILLS",
    "CUTOUTS",
    "ASSEMBLY_OUTLINES_TOP",
    "ASSEMBLY_DESIGNATORS_TOP",
    "PIN1_TOP",
    "DIODES_TOP",
)

VIRTUAL_SVG_COLOURS: dict[str, str] = {
    "PROFILE": "#ECEEE9",
    "TOP": "#D4D8DE",
    "TOPSOLDER": "rgba(235, 193, 74, 0.65)",
    "DRILLS": "#2B3038",
    "ASSEMBLY_OUTLINES_TOP": "#374151",
    "ASSEMBLY_DESIGNATORS_TOP": "#111827",
}


def _examples_relative(path: Path) -> str:
    return path.relative_to(EXAMPLES_DIR).as_posix()


def _sample_relative(path: Path) -> str:
    return path.relative_to(SAMPLE_DIR).as_posix()


def _virtual_options(dnp_mode: VirtualDnpMode = "omit") -> VirtualLayerOptions:
    return VirtualLayerOptions(
        font_mode="stroke",
        stroke_font="sans_serif",
        dnp_mode=dnp_mode,
        visibility=ComponentVisibilityPolicy(exclude_kinds=("graphical", "net_tie")),
        cutouts=CutoutLayerOptions(scope="interior", hatch=True, hatch_pitch_mm=1.0),
        pin1=Pin1LayerOptions(
            exclude_designator_prefixes=("R", "C", "L"),
            diode_ak_labels=True,
        ),
        diodes=DiodeLayerOptions(color_mode="shaded", default_cathode_pad="2"),
        assembly_outlines=AssemblyOutlineLayerOptions(detail="simple"),
        assembly_designators=AssemblyDesignatorLayerOptions(
            min_font_size_mm=0.08,
            max_font_size_mm=2.40,
            fill_ratio=0.78,
        ),
    )


def build_fab_release_package(output_dir: Path = OUTPUT_DIR) -> dict[str, object]:
    if output_dir.exists():
        shutil.rmtree(output_dir)
    output_dir.mkdir(parents=True, exist_ok=True)

    design = AltiumDesign.from_prjpcb(PROJECT_FILE)
    pcbdoc = design.load_pcbdoc()
    project_parameters = design.get_pcb_project_parameters()

    # Build one variant-aware Fab session for Variant A (where J6 is Not Fitted / DNP)
    # plus sibling sessions for No Variant and Variant B (both fully populated) so we
    # can compare assembly-scoped virtual overlays side by side.
    fab_variant_a = pcbdoc.fab(variant=SELECTED_VARIANT, parameters=project_parameters)
    fab_no_variant = pcbdoc.fab(parameters=project_parameters)
    fab_variant_b = pcbdoc.fab(variant="B", parameters=project_parameters)

    opts_omit = _virtual_options(dnp_mode="omit")
    opts_crossout = _virtual_options(dnp_mode="crossout")

    # Gerber X2 with explicit layer selection, dual-key custom extensions (matching
    # either canonical FabView keys like "PROFILE"/"CUTOUTS" or Altium default
    # extensions like "GTO"/"GKO"), variant-aware virtual layers, and .GM cutouts.
    # Bare-board copper/mask/silkscreen stay strictly invariant across variants.
    gerbers_x2 = fab_variant_a.to_gerbers(
        x2_attributes=True,
        keepout=True,
        profile=True,
        pad_masters=False,
        drill_guide=False,
        drill_drawing=True,
        mechanical_layers=("used",),
        extensions={
            "PROFILE": "GKO_OUTLINE",  # canonical FabLayerKey
            "GTO": "GTO_SILK",  # Altium default extension key
            "CUTOUTS": "GCU",  # virtual layer key
        },
        virtual_layers=VIRTUAL_LAYERS,
        profile_cutouts=True,
        virtual_options=opts_omit,
    )
    gerbers_x1 = fab_variant_a.to_gerbers(x2_attributes=False)
    nc_drill = fab_variant_a.to_nc_drill()

    # IPC-2581B with curated document layers plus virtual DOCUMENT/ASSEMBLY layers
    # (written as <Layer> + <LayerFeature> outside <StackupGroup>).
    ipc2581 = fab_variant_a.to_ipc2581(
        merge_net_ties=True,
        document_layers=("KEEPOUT", "MECHANICAL13"),
        virtual_layers=VIRTUAL_LAYERS,
        virtual_options=opts_omit,
    )
    pnp = fab_variant_a.to_pnp()
    bom = fab_variant_a.to_bom()

    # Render bare-board top and mirrored bottom inspection SVGs matching HydroScope's
    # physical black solder mask, ENIG gold pads, and white silkscreen legend.
    top_svg = fab_variant_a.to_svg(
        scale=8.0,
        layers=("PROFILE", "TOP", "TOPSOLDER", "TOPOVERLAY", "DRILLS"),
        layer_colours={
            "PROFILE": "#1E242C",
            "TOP": "#3A4656",
            "TOPSOLDER": "rgba(235, 193, 74, 0.90)",
            "TOPOVERLAY": "#F4F5F2",
            "DRILLS": "#0E1116",
        },
        clip_to_outline=True,
        clip_holes=True,
        metadata=True,
    )
    bottom_svg = fab_variant_a.to_svg(
        scale=8.0,
        layers=("PROFILE", "BOTTOM", "BOTTOMSOLDER", "BOTTOMOVERLAY", "DRILLS"),
        layer_colours={
            "PROFILE": "#1E242C",
            "BOTTOM": "#3A4656",
            "BOTTOMSOLDER": "rgba(235, 193, 74, 0.90)",
            "BOTTOMOVERLAY": "#F4F5F2",
            "DRILLS": "#0E1116",
        },
        mirror=True,
        clip_to_outline=True,
        clip_holes=True,
        metadata=True,
    )

    # Composite assembly review SVGs comparing No Variant, Variant A (dnp_mode="omit"),
    # Variant A (dnp_mode="crossout"), and Variant B. Note how bare copper/pads stay
    # identical across all four views while J6 is fitted (No Variant & Variant B),
    # omitted (Variant A omit), or dimmed dashed with a red X (Variant A crossout).
    virtual_no_variant_svg = fab_no_variant.to_svg(
        scale=8.0,
        layers=VIRTUAL_SVG_LAYERS,
        layer_colours=VIRTUAL_SVG_COLOURS,
        clip_to_outline=True,
        clip_holes=True,
        metadata=True,
        virtual_options=opts_omit,
    )
    virtual_variant_a_omit_svg = fab_variant_a.to_svg(
        scale=8.0,
        layers=VIRTUAL_SVG_LAYERS,
        layer_colours=VIRTUAL_SVG_COLOURS,
        clip_to_outline=True,
        clip_holes=True,
        metadata=True,
        virtual_options=opts_omit,
    )
    virtual_variant_a_crossout_svg = fab_variant_a.to_svg(
        scale=8.0,
        layers=VIRTUAL_SVG_LAYERS,
        layer_colours=VIRTUAL_SVG_COLOURS,
        clip_to_outline=True,
        clip_holes=True,
        metadata=True,
        virtual_options=opts_crossout,
    )
    virtual_variant_b_svg = fab_variant_b.to_svg(
        scale=8.0,
        layers=VIRTUAL_SVG_LAYERS,
        layer_colours=VIRTUAL_SVG_COLOURS,
        clip_to_outline=True,
        clip_holes=True,
        metadata=True,
        virtual_options=opts_omit,
    )

    gerbers_x2.write(output_dir / "gerbers_x2")
    gerbers_x1.write(output_dir / "gerbers_x1")
    nc_drill.write(output_dir / "nc_drill")
    ipc2581.write(output_dir / "ipc2581")
    pnp.write(output_dir / "assembly")
    bom.write(output_dir / "assembly")

    top_svg_path = output_dir / "board_top.svg"
    bottom_svg_path = output_dir / "board_bottom.svg"
    virtual_no_variant_path = output_dir / "board_top_virtual_no_variant.svg"
    virtual_var_a_omit_path = output_dir / "board_top_virtual_variant_a_omit.svg"
    virtual_var_a_crossout_path = output_dir / "board_top_virtual_variant_a_crossout.svg"
    virtual_var_b_path = output_dir / "board_top_virtual_variant_b.svg"

    top_svg_path.write_bytes(top_svg["board.svg"].data)
    bottom_svg_path.write_bytes(bottom_svg["board.svg"].data)
    virtual_no_variant_path.write_bytes(virtual_no_variant_svg["board.svg"].data)
    virtual_var_a_omit_path.write_bytes(virtual_variant_a_omit_svg["board.svg"].data)
    virtual_var_a_crossout_path.write_bytes(
        virtual_variant_a_crossout_svg["board.svg"].data
    )
    virtual_var_b_path.write_bytes(virtual_variant_b_svg["board.svg"].data)

    # Summarize fitted vs DNP component counts for each assembly variant.
    dnp_a = sorted(
        c.designator
        for c in fab_variant_a.view(geometry="plotted").placements()
        if not c.fitted
    )
    dnp_b = sorted(
        c.designator
        for c in fab_variant_b.view(geometry="plotted").placements()
        if not c.fitted
    )

    manifest: dict[str, object] = {
        "project": _examples_relative(PROJECT_FILE),
        "pcbdoc": _examples_relative(Path(pcbdoc.filepath or "")),
        "variant": SELECTED_VARIANT,
        "variants_compared": {
            "no_variant": {"dnp": []},
            "A": {"dnp": dnp_a, "dnp_modes": ["omit", "crossout"]},
            "B": {"dnp": dnp_b},
        },
        "virtual_layers": list(VIRTUAL_LAYERS),
        "outputs": {
            "gerbers_x2": {
                "file_count": len(gerbers_x2),
                "files": sorted(gerbers_x2.keys()),
            },
            "gerbers_x1": {
                "file_count": len(gerbers_x1),
                "files": sorted(gerbers_x1.keys()),
            },
            "nc_drill": {
                "file_count": len(nc_drill),
                "files": sorted(nc_drill.keys()),
            },
            "ipc2581": {
                "file_count": len(ipc2581),
                "files": sorted(ipc2581.keys()),
            },
            "pnp": {
                "file_count": len(pnp),
                "files": sorted(pnp.keys()),
            },
            "bom": {
                "file_count": len(bom),
                "files": sorted(bom.keys()),
            },
            "previews": {
                "top_svg": _sample_relative(top_svg_path),
                "bottom_svg": _sample_relative(bottom_svg_path),
                "virtual_no_variant_svg": _sample_relative(virtual_no_variant_path),
                "virtual_variant_a_omit_svg": _sample_relative(virtual_var_a_omit_path),
                "virtual_variant_a_crossout_svg": _sample_relative(
                    virtual_var_a_crossout_path
                ),
                "virtual_variant_b_svg": _sample_relative(virtual_var_b_path),
            },
        },
        "diagnostics": {
            "gerbers_x2": len(gerbers_x2.diagnostics),
            "nc_drill": len(nc_drill.diagnostics),
            "ipc2581": len(ipc2581.diagnostics),
            "pnp": len(pnp.diagnostics),
            "bom": len(bom.diagnostics),
        },
    }

    manifest_path = output_dir / "fab_release_manifest.json"
    manifest_path.write_text(
        json.dumps(manifest, indent=2) + "\n",
        encoding="utf-8",
        newline="\n",
    )
    return manifest


def main() -> None:
    manifest = build_fab_release_package()
    outputs = manifest["outputs"]
    assert isinstance(outputs, dict)
    print(f"Loaded project: {manifest['project']}")
    print(f"Selected variant: {manifest['variant']}")
    print(f"Gerber X2 files: {outputs['gerbers_x2']['file_count']}")
    print(f"Gerber X1 files: {outputs['gerbers_x1']['file_count']}")
    print(f"NC Drill files: {outputs['nc_drill']['file_count']}")
    print(f"IPC-2581 files: {outputs['ipc2581']['file_count']}")
    print(f"PnP files: {outputs['pnp']['file_count']}")
    print(f"BOM files: {outputs['bom']['file_count']}")
    print(f"Wrote manifest: {_sample_relative(MANIFEST_PATH)}")


if __name__ == "__main__":
    main()
altium-monkey v2026.11.26 · gobblerito·Docs updated 2026.11.29 17:55 UTC (0665053)