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

Examples Showcase / pcbdoc_pick_n_place

Export pick-and-place data from an Altium project

Load an existing PrjPcb with AltiumDesign, read the design JSON PNP block, and write all-placement plus no-BOM-filtered JSON files.

designpnppcbdocprojectdesignpnppick-and-placeno-bomjson

Load an existing Altium project with AltiumDesign, generate PCB-backed pick-and-place data, and write JSON outputs.

This example shows two related paths:

  1. AltiumDesign.to_json(...) includes a root pnp block when the project has a referenced PcbDoc.
  2. AltiumDesign.to_pnp(...) is the direct API for pick-and-place placements.

What It Shows

  1. Loading a project with AltiumDesign.from_prjpcb(...)
  2. Reading the design JSON pnp contract
  3. Calling AltiumDesign.to_pnp(exclude_no_bom=True) to skip components marked as no-BOM, including standard no-BOM components
  4. Reading the position_mode metadata that documents how center_x and center_y were computed
  5. Preserving JSON placement data for downstream automation

The default position_mode is altium-pick-place: center of the bounding box of component-owned pad anchor points, with component-origin fallback when a component has no owned pads. Direct callers can use position_mode="component-origin" when they want the footprint placement origin instead.

Run

From the repository root:

powershell
uv run python examples\pcbdoc_pick_n_place\pcbdoc_pick_n_place.py

Input Project

This sample uses:

text
examples/assets/projects/rt_super_c1/RT_SUPER_C1.PrjPcb

Output

The script writes:

text
examples/pcbdoc_pick_n_place/output/altium_design.json
examples/pcbdoc_pick_n_place/output/pick_and_place.json
examples/pcbdoc_pick_n_place/output/pick_and_place_exclude_no_bom.json

The standalone pick_and_place.json file is the same pnp object embedded in altium_design.json. The pick_and_place_exclude_no_bom.json file shows the direct to_pnp(..., exclude_no_bom=True, position_mode="altium-pick-place") path.

Source Code

examples/pcbdoc_pick_n_place/pcbdoc_pick_n_place.py
from __future__ import annotations

import json
from pathlib import Path

from altium_monkey import AltiumDesign


SAMPLE_DIR = Path(__file__).resolve().parent
PROJECT_DIR = SAMPLE_DIR.parent / "assets" / "projects" / "rt_super_c1"
PROJECT_FILE = PROJECT_DIR / "RT_SUPER_C1.PrjPcb"
OUTPUT_DIR = SAMPLE_DIR / "output"


def _write_json(path: Path, payload: object) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(json.dumps(payload, indent=2) + "\n", encoding="utf-8")


def _pnp_payload(
    *,
    source_pcbdoc: object,
    position_mode: object,
    placements: list[object],
    units: str = "mm",
) -> dict[str, object]:
    return {
        "units": units,
        "position_mode": position_mode,
        "source_pcbdoc": source_pcbdoc,
        "placements": [
            placement.to_json() if hasattr(placement, "to_json") else placement
            for placement in placements
        ],
    }


def main() -> None:
    OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
    stale_csv = OUTPUT_DIR / "pick_and_place.csv"
    if stale_csv.exists():
        stale_csv.unlink()

    design = AltiumDesign.from_prjpcb(PROJECT_FILE)
    design_json = design.to_json()
    pnp_data = design_json.get("pnp")
    if not isinstance(pnp_data, dict):
        raise RuntimeError(f"Project has no PCB-backed PNP data: {PROJECT_FILE}")

    placements = pnp_data["placements"]
    if not isinstance(placements, list):
        raise RuntimeError("Design JSON PNP block does not contain placements")

    bom_only_pnp = _pnp_payload(
        source_pcbdoc=pnp_data["source_pcbdoc"],
        position_mode=pnp_data["position_mode"],
        placements=design.to_pnp(
            units="mm",
            exclude_no_bom=True,
            position_mode=str(pnp_data["position_mode"]),
        ),
    )
    bom_only_placements = bom_only_pnp["placements"]
    if not isinstance(bom_only_placements, list):
        raise RuntimeError("Filtered PNP payload does not contain placements")

    _write_json(OUTPUT_DIR / "altium_design.json", design_json)
    _write_json(OUTPUT_DIR / "pick_and_place.json", pnp_data)
    _write_json(OUTPUT_DIR / "pick_and_place_exclude_no_bom.json", bom_only_pnp)

    print(f"Loaded project: {PROJECT_FILE.name}")
    print(f"Source PcbDoc: {pnp_data['source_pcbdoc']}")
    print(f"PNP position mode: {pnp_data['position_mode']}")
    print(f"PNP placements: {len(placements)}")
    print(f"PNP placements excluding no-BOM components: {len(bom_only_placements)}")
    print("Wrote:")
    print("  output/altium_design.json")
    print("  output/pick_and_place.json")
    print("  output/pick_and_place_exclude_no_bom.json")


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