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

Examples Showcase / pcbdoc_embedded_board_panel

Author a 1x4 HydroScope tab-rout panel with NPTH mouse bites

Build a 1x4 HydroScope Mainboard panel using add_embedded_board_array with distinct board outlines and rout channels, copper-free NPTH tooling holes and 4-side mouse bites, IPC-7351 circular fiducials, panel PnP, and top/bottom SVGs.

pcbdocfabsvgpcbdocpanelembedded-board-arraymouse-bitesfiducialsfab

Output Previews

panel_1x4_top.svg
Open SVG
panel_1x4_top.svg
panel_1x4_bottom.svg
Open SVG
panel_1x4_bottom.svg

Build a 1x4 tab-routed manufacturing panel of the rectangular HydroScope Mainboard (TZ-SB-0001-PCB-[A] (HydroScope Mainboard).PcbDoc) using AltiumPcbDoc.add_embedded_board_array(...), with breakaway rails, copper-free NPTH tooling holes, IPC-7351 circular global fiducials, distinct board outlines and rout paths, copper-free NPTH mouse bites on all 4 sides of every board, and panel-level Pick-and-Place / SVG outputs through Fab.

What It Shows

  1. Placing a rectangular board (4500 x 970 mil) in a 1x4 array via add_embedded_board_array(...) with a portable relative document_path
  2. Separating panel fabrication layers:
    • MECHANICAL1 (gold in SVG): outer panel border and each child board's outline
    • MECHANICAL2 (cyan in SVG): 100 mil router channels interrupted at every breakaway tab
  3. Adding copper-free NPTH features (width_mils=0, height_mils=0, plated=False, shape=PadShape.CIRCLE): 125 mil corner tooling holes and 5-hole 20 mil mouse-bite clusters on all 4 sides of every board
  4. Adding 3 asymmetric IPC-7351 / SMEMA global fiducials on both top and bottom rails (PadShape.CIRCLE, 1.0 mm copper disk, 2.0 mm circular solder mask opening, solder paste suppressed)
  5. Inspecting typed PcbEmbeddedBoardSource records via interpret_embedded_boards(...) and FabView step-repeat vs. flattened views
  6. Emitting panel Pick-and-Place CSV (panel={"copy_designators": "$Component_$CopyIndex"}) and top/bottom inspection SVGs

See also pcbdoc_vgroove_panel for a 0-gap V-score (V-groove) panel of the same board.

Run

From the repository root:

powershell
uv run python examples\pcbdoc_embedded_board_panel\pcbdoc_embedded_board_panel.py

Output

The script writes:

text
examples/pcbdoc_embedded_board_panel/output/hydroscope_tab_rout_panel_1x4.PcbDoc
examples/pcbdoc_embedded_board_panel/output/panel_1x4_top.svg
examples/pcbdoc_embedded_board_panel/output/panel_1x4_bottom.svg
examples/pcbdoc_embedded_board_panel/output/panel_pnp.csv
examples/pcbdoc_embedded_board_panel/output/panel_manifest.json

Source Code

examples/pcbdoc_embedded_board_panel/pcbdoc_embedded_board_panel.py
from __future__ import annotations

import json
import shutil
from pathlib import Path

from altium_monkey import (
    AltiumBoardOutline,
    AltiumPcbDoc,
    BoardOutlineVertex,
    PadShape,
    PcbLayer,
)
from altium_monkey.altium_pcb_embedded_board import (
    PcbEmbeddedBoardOriginMode,
    interpret_embedded_boards,
)
from altium_monkey.altium_pcb_mask_expansion import PcbMaskExpansionMode

SAMPLE_DIR = Path(__file__).resolve().parent
EXAMPLES_DIR = SAMPLE_DIR.parent
CHILD_PCBDOC = (
    EXAMPLES_DIR
    / "assets"
    / "projects"
    / "hydroscope"
    / "TZ-SB-0001-PCB-[A] (HydroScope Mainboard).PcbDoc"
)
CHILD_REL_FROM_OUTPUT = (
    "../../assets/projects/hydroscope/TZ-SB-0001-PCB-[A] (HydroScope Mainboard).PcbDoc"
)
OUTPUT_DIR = SAMPLE_DIR / "output"
PANEL_PATH = OUTPUT_DIR / "hydroscope_tab_rout_panel_1x4.PcbDoc"
TOP_SVG_PATH = OUTPUT_DIR / "panel_1x4_top.svg"
BOTTOM_SVG_PATH = OUTPUT_DIR / "panel_1x4_bottom.svg"
PNP_CSV_PATH = OUTPUT_DIR / "panel_pnp.csv"
MANIFEST_PATH = OUTPUT_DIR / "panel_manifest.json"

# HydroScope Mainboard is a clean 4500 x 970 mil (114.30 x 24.64 mm) rectangle.
BOARD_WIDTH_MILS = 4500.0
BOARD_HEIGHT_MILS = 970.0
ROWS = 4
COLUMNS = 1
ROUT_CHANNEL_MILS = 100.0  # ~2.54 mm router bit channel between boards and rails
RAIL_X_MILS = 350.0  # ~8.89 mm left/right breakaway tooling rails
RAIL_Y_MILS = 350.0  # ~8.89 mm top/bottom breakaway tooling rails
TAB_WIDTH_MILS = 200.0  # ~5.08 mm breakaway tab width (5 mouse-bite holes)
BOARD_ORIGIN_X = RAIL_X_MILS + ROUT_CHANNEL_MILS
BOARD_ORIGIN_Y = RAIL_Y_MILS + ROUT_CHANNEL_MILS
ROW_PITCH_MILS = BOARD_HEIGHT_MILS + ROUT_CHANNEL_MILS
PANEL_WIDTH_MILS = 2.0 * RAIL_X_MILS + 2.0 * ROUT_CHANNEL_MILS + BOARD_WIDTH_MILS
PANEL_HEIGHT_MILS = (
    2.0 * RAIL_Y_MILS + (ROWS + 1) * ROUT_CHANNEL_MILS + ROWS * BOARD_HEIGHT_MILS
)

# IPC-7351 / SMEMA global panel fiducial dimensions:
# 1.0 mm (39.37 mil) solid circular copper disk, 2.0 mm circular solder mask
# opening (+19.685 mil radial expansion), solder paste suppressed.
FIDUCIAL_CU_DIA_MILS = 39.37
FIDUCIAL_MASK_EXP_MILS = 19.685


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


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


def _rect_outline(width_mils: float, height_mils: float) -> AltiumBoardOutline:
    return AltiumBoardOutline(
        vertices=[
            BoardOutlineVertex.line(0.0, 0.0),
            BoardOutlineVertex.line(width_mils, 0.0),
            BoardOutlineVertex.line(width_mils, height_mils),
            BoardOutlineVertex.line(0.0, height_mils),
        ]
    )


def _draw_rect_tracks(
    panel: AltiumPcbDoc,
    x0: float,
    y0: float,
    x1: float,
    y1: float,
    *,
    width_mils: float,
    layer: PcbLayer,
) -> None:
    corners = [(x0, y0), (x1, y0), (x1, y1), (x0, y1)]
    for idx in range(4):
        panel.add_track(
            corners[idx],
            corners[(idx + 1) % 4],
            width_mils=width_mils,
            layer=layer,
        )


def _add_segmented_horizontal_rout(
    panel: AltiumPcbDoc,
    x0: float,
    x1: float,
    y: float,
    tab_centers_x: tuple[float, ...],
    *,
    tab_width_mils: float = TAB_WIDTH_MILS,
    layer: PcbLayer = PcbLayer.MECHANICAL_2,
) -> None:
    """Draw a horizontal rout path interrupted at each breakaway tab."""
    cursor = x0
    half_tab = tab_width_mils / 2.0
    for cx in sorted(tab_centers_x):
        seg_end = cx - half_tab
        if seg_end > cursor:
            panel.add_track((cursor, y), (seg_end, y), width_mils=14.0, layer=layer)
        cursor = max(cursor, cx + half_tab)
    if x1 > cursor:
        panel.add_track((cursor, y), (x1, y), width_mils=14.0, layer=layer)


def _add_segmented_vertical_rout(
    panel: AltiumPcbDoc,
    x: float,
    y0: float,
    y1: float,
    tab_centers_y: tuple[float, ...],
    *,
    tab_width_mils: float = TAB_WIDTH_MILS,
    layer: PcbLayer = PcbLayer.MECHANICAL_2,
) -> None:
    """Draw a vertical rout path interrupted at each breakaway tab."""
    cursor = y0
    half_tab = tab_width_mils / 2.0
    for cy in sorted(tab_centers_y):
        seg_end = cy - half_tab
        if seg_end > cursor:
            panel.add_track((x, cursor), (x, seg_end), width_mils=14.0, layer=layer)
        cursor = max(cursor, cy + half_tab)
    if y1 > cursor:
        panel.add_track((x, cursor), (x, y1), width_mils=14.0, layer=layer)


def _add_npth_hole(
    panel: AltiumPcbDoc,
    *,
    designator: str,
    position_mils: tuple[float, float],
    hole_size_mils: float,
) -> None:
    """Add a copper-free non-plated through hole (zero copper pad diameter)."""
    panel.add_pad(
        designator=designator,
        position_mils=position_mils,
        width_mils=0.0,
        height_mils=0.0,
        hole_size_mils=hole_size_mils,
        plated=False,
        shape=PadShape.CIRCLE,
        layer=PcbLayer.MULTI_LAYER,
        solder_mask_expansion_mode=PcbMaskExpansionMode.MANUAL,
        solder_mask_expansion_mils=0.0,
    )


def _add_ipc_fiducial(
    panel: AltiumPcbDoc,
    *,
    designator: str,
    position_mils: tuple[float, float],
) -> None:
    """Add an IPC-7351 circular global fiducial on both TOP and BOTTOM rails."""
    for layer in (PcbLayer.TOP, PcbLayer.BOTTOM):
        panel.add_pad(
            designator=designator,
            position_mils=position_mils,
            width_mils=FIDUCIAL_CU_DIA_MILS,
            height_mils=FIDUCIAL_CU_DIA_MILS,
            hole_size_mils=0.0,
            shape=PadShape.CIRCLE,
            layer=layer,
            solder_mask_expansion_mode=PcbMaskExpansionMode.MANUAL,
            solder_mask_expansion_mils=FIDUCIAL_MASK_EXP_MILS,
            paste_mask_expansion_mode=PcbMaskExpansionMode.MANUAL,
            paste_mask_expansion_mils=-FIDUCIAL_MASK_EXP_MILS,
        )


def _add_mouse_bite_cluster(
    panel: AltiumPcbDoc,
    center_x: float,
    center_y: float,
    *,
    horizontal: bool,
    hole_size_mils: float = 20.0,
    pitch_mils: float = 32.0,
) -> int:
    """Place a 5-hole copper-free NPTH breakaway mouse-bite cluster."""
    offsets = (-2.0 * pitch_mils, -pitch_mils, 0.0, pitch_mils, 2.0 * pitch_mils)
    for idx, delta in enumerate(offsets, start=1):
        px = center_x + (delta if horizontal else 0.0)
        py = center_y + (0.0 if horizontal else delta)
        _add_npth_hole(
            panel,
            designator=f"MB{idx}",
            position_mils=(px, py),
            hole_size_mils=hole_size_mils,
        )
    return len(offsets)


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

    panel_path = output_dir / "hydroscope_tab_rout_panel_1x4.PcbDoc"
    top_svg_path = output_dir / "panel_1x4_top.svg"
    bottom_svg_path = output_dir / "panel_1x4_bottom.svg"
    pnp_csv_path = output_dir / "panel_pnp.csv"
    manifest_path = output_dir / "panel_manifest.json"

    loaded_child = AltiumPcbDoc.from_file(CHILD_PCBDOC)

    panel = AltiumPcbDoc()
    panel.set_board_outline(_rect_outline(PANEL_WIDTH_MILS, PANEL_HEIGHT_MILS))
    panel.set_origin_to_outline_lower_left()

    # 1. Place a 1x4 embedded board array of the HydroScope Mainboard.
    panel.add_embedded_board_array(
        loaded_child,
        document_path=CHILD_REL_FROM_OUTPUT,
        position_mils=(BOARD_ORIGIN_X, BOARD_ORIGIN_Y),
        rows=ROWS,
        columns=COLUMNS,
        row_pitch_mils=ROW_PITCH_MILS,
        column_pitch_mils=BOARD_WIDTH_MILS + ROUT_CHANNEL_MILS,
        origin_mode=PcbEmbeddedBoardOriginMode.BOTTOM_LEFT,
        layer=PcbLayer.TOP,
    )

    # 2. Add 4 corner tooling holes (3.175 mm / 125 mil copper-free NPTH).
    tooling_coords = (
        (175.0, 175.0),
        (PANEL_WIDTH_MILS - 175.0, 175.0),
        (175.0, PANEL_HEIGHT_MILS - 175.0),
        (PANEL_WIDTH_MILS - 175.0, PANEL_HEIGHT_MILS - 175.0),
    )
    for idx, pos in enumerate(tooling_coords, start=1):
        _add_npth_hole(
            panel,
            designator=f"TH{idx}",
            position_mils=pos,
            hole_size_mils=125.0,
        )

    # 3. Add 3 asymmetric IPC-7351 circular global fiducials (1 mm Cu / 2 mm mask).
    fiducial_coords = (
        (175.0, 480.0),
        (PANEL_WIDTH_MILS - 175.0, 480.0),
        (175.0, PANEL_HEIGHT_MILS - 480.0),
    )
    for idx, (fx, fy) in enumerate(fiducial_coords, start=1):
        _add_ipc_fiducial(panel, designator=f"FID{idx}", position_mils=(fx, fy))
        panel.add_text(
            text=f"FID{idx}",
            position_mils=(fx - 70.0, fy + 80.0),
            height_mils=45.0,
            stroke_width_mils=8.0,
            layer=PcbLayer.TOP_OVERLAY,
        )

    # 4. Draw outer panel outline + individual child board outlines on
    # MECHANICAL1, interrupted rout channels on MECHANICAL2, and NPTH
    # mouse-bite perforations on all 4 sides of every board.
    _draw_rect_tracks(
        panel,
        0.0,
        0.0,
        PANEL_WIDTH_MILS,
        PANEL_HEIGHT_MILS,
        width_mils=12.0,
        layer=PcbLayer.MECHANICAL_1,
    )

    h_tab_xs = (
        BOARD_ORIGIN_X + BOARD_WIDTH_MILS * 0.25,
        BOARD_ORIGIN_X + BOARD_WIDTH_MILS * 0.50,
        BOARD_ORIGIN_X + BOARD_WIDTH_MILS * 0.75,
    )
    half_rout = ROUT_CHANNEL_MILS / 2.0
    mouse_bite_clusters = 0
    mouse_bite_holes = 0

    # Vertical rout channels on left and right of the 4-board stack
    v_tab_ys = tuple(
        BOARD_ORIGIN_Y + row * ROW_PITCH_MILS + BOARD_HEIGHT_MILS / 2.0
        for row in range(ROWS)
    )
    _add_segmented_vertical_rout(
        panel,
        BOARD_ORIGIN_X - half_rout,
        RAIL_Y_MILS,
        PANEL_HEIGHT_MILS - RAIL_Y_MILS,
        v_tab_ys,
    )
    _add_segmented_vertical_rout(
        panel,
        BOARD_ORIGIN_X + BOARD_WIDTH_MILS + half_rout,
        RAIL_Y_MILS,
        PANEL_HEIGHT_MILS - RAIL_Y_MILS,
        v_tab_ys,
    )

    # Horizontal rout channels above/below every board
    for slot_idx in range(ROWS + 1):
        slot_y = RAIL_Y_MILS + half_rout + slot_idx * ROW_PITCH_MILS
        _add_segmented_horizontal_rout(
            panel,
            RAIL_X_MILS,
            PANEL_WIDTH_MILS - RAIL_X_MILS,
            slot_y,
            h_tab_xs,
        )

    # Child board outline lines + mouse bites on all 4 sides of every board
    for row in range(ROWS):
        by0 = BOARD_ORIGIN_Y + row * ROW_PITCH_MILS
        by1 = by0 + BOARD_HEIGHT_MILS
        bx0 = BOARD_ORIGIN_X
        bx1 = bx0 + BOARD_WIDTH_MILS

        _draw_rect_tracks(
            panel,
            bx0,
            by0,
            bx1,
            by1,
            width_mils=10.0,
            layer=PcbLayer.MECHANICAL_1,
        )

        # Bottom and top edges of each board (3 tabs each)
        for tx in h_tab_xs:
            mouse_bite_holes += _add_mouse_bite_cluster(
                panel, tx, by0, horizontal=True
            )
            mouse_bite_holes += _add_mouse_bite_cluster(
                panel, tx, by1, horizontal=True
            )
            mouse_bite_clusters += 2

        # Left and right edges of each board (1 tab each)
        by_mid = (by0 + by1) / 2.0
        mouse_bite_holes += _add_mouse_bite_cluster(
            panel, bx0, by_mid, horizontal=False
        )
        mouse_bite_holes += _add_mouse_bite_cluster(
            panel, bx1, by_mid, horizontal=False
        )
        mouse_bite_clusters += 2

    # 5. Add rail silkscreen labels on top overlay.
    panel.add_text(
        text=(
            f"HYDROSCOPE 1x4 TAB-ROUT PANEL "
            f"({PANEL_WIDTH_MILS / 1000.0:.2f} x {PANEL_HEIGHT_MILS / 1000.0:.2f} IN)"
        ),
        position_mils=(BOARD_ORIGIN_X + 20.0, PANEL_HEIGHT_MILS - 210.0),
        height_mils=65.0,
        stroke_width_mils=10.0,
        layer=PcbLayer.TOP_OVERLAY,
    )
    panel.add_text(
        text="M1: BOARD OUTLINE | M2: ROUT PATH | NPTH: 125MIL TOOLING & 20MIL MOUSE BITES",
        position_mils=(BOARD_ORIGIN_X + 20.0, 125.0),
        height_mils=44.0,
        stroke_width_mils=7.0,
        layer=PcbLayer.TOP_OVERLAY,
    )

    panel.save(panel_path)

    # 6. Reload, inspect EmbeddedBoards6 records, and emit Fab outputs.
    reloaded = AltiumPcbDoc.from_file(panel_path)
    arrays = interpret_embedded_boards(reloaded.embedded_boards)
    fab = reloaded.fab()

    top_svg = fab.to_svg(
        scale=3.0,
        layers=(
            "PROFILE",
            "TOP",
            "TOPSOLDER",
            "TOPOVERLAY",
            "MECHANICAL1",
            "MECHANICAL2",
            "DRILLS",
        ),
        layer_colours={
            "PROFILE": "#14171C",
            "TOP": "#2C333D",
            "TOPSOLDER": "rgba(235, 193, 74, 0.90)",
            "TOPOVERLAY": "#F5F7FA",
            "MECHANICAL1": "#F5C542",
            "MECHANICAL2": "#38BDF8",
            "DRILLS": "#FFFFFF",
        },
        clip_to_outline=True,
        metadata=True,
    )
    top_svg_path.write_bytes(top_svg["board.svg"].data)

    bottom_svg = fab.to_svg(
        scale=3.0,
        layers=(
            "PROFILE",
            "BOTTOM",
            "BOTTOMSOLDER",
            "BOTTOMOVERLAY",
            "MECHANICAL1",
            "MECHANICAL2",
            "DRILLS",
        ),
        layer_colours={
            "PROFILE": "#14171C",
            "BOTTOM": "#2C333D",
            "BOTTOMSOLDER": "rgba(235, 193, 74, 0.90)",
            "BOTTOMOVERLAY": "#F5F7FA",
            "MECHANICAL1": "#F5C542",
            "MECHANICAL2": "#38BDF8",
            "DRILLS": "#FFFFFF",
        },
        mirror=True,
        clip_to_outline=True,
        metadata=True,
    )
    bottom_svg_path.write_bytes(bottom_svg["board.svg"].data)

    pnp_out = fab.to_pnp(panel={"copy_designators": "$Component_$CopyIndex"})
    pnp_file = next(iter(pnp_out.values()))
    pnp_csv_path.write_bytes(pnp_file.data)
    pnp_rows = [line for line in pnp_file.text.splitlines() if line.strip()]

    step_view = fab.view(copies="steps")
    repeats = step_view.repeats()
    flat_view = fab.view(copies="flatten")

    manifest: dict[str, object] = {
        "child_board": _examples_relative(CHILD_PCBDOC),
        "panel_file": _sample_relative(panel_path),
        "panel_size_mils": [PANEL_WIDTH_MILS, PANEL_HEIGHT_MILS],
        "embedded_board_count": len(arrays),
        "arrays": [
            {
                "document_path": src.document_path,
                "kind": src.kind.value,
                "rows": src.rows,
                "columns": src.columns,
                "row_pitch_mils": ROW_PITCH_MILS,
                "column_pitch_mils": BOARD_WIDTH_MILS + ROUT_CHANNEL_MILS,
                "rotation_degrees": src.rotation_degrees,
                "mirror": src.mirror,
                "origin_anchor": (
                    src.origin_anchor.name if src.origin_anchor is not None else None
                ),
            }
            for src in arrays
        ],
        "panel_features": {
            "tooling_holes_npth": len(tooling_coords),
            "fiducials_ipc7351_circular": len(fiducial_coords),
            "mouse_bite_clusters_npth": mouse_bite_clusters,
            "mouse_bite_holes_npth": mouse_bite_holes,
            "rout_channel_mils": ROUT_CHANNEL_MILS,
        },
        "fab_step_repeats": len(repeats),
        "flattened_hole_count": len(flat_view.holes()),
        "panel_pnp_rows": max(0, len(pnp_rows) - 1),
        "outputs": {
            "top_svg": _sample_relative(top_svg_path),
            "bottom_svg": _sample_relative(bottom_svg_path),
            "pnp_csv": _sample_relative(pnp_csv_path),
        },
    }
    manifest_path.write_text(
        json.dumps(manifest, indent=2) + "\n", encoding="utf-8", newline="\n"
    )
    return manifest


def main() -> None:
    manifest = build_panel_example()
    print(json.dumps(manifest, indent=2))


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