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.
Load an existing Altium project with AltiumDesign, generate PCB-backed pick-and-place data, and write JSON outputs.
This example shows two related paths:
AltiumDesign.to_json(...)includes a rootpnpblock when the project has a referenced PcbDoc.AltiumDesign.to_pnp(...)is the direct API for pick-and-place placements.
What It Shows
- Loading a project with
AltiumDesign.from_prjpcb(...) - Reading the design JSON
pnpcontract - Calling
AltiumDesign.to_pnp(exclude_no_bom=True)to skip components marked as no-BOM, including standard no-BOM components - Reading the
position_modemetadata that documents howcenter_xandcenter_ywere computed - 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:
uv run python examples\pcbdoc_pick_n_place\pcbdoc_pick_n_place.pyInput Project
This sample uses:
examples/assets/projects/rt_super_c1/RT_SUPER_C1.PrjPcbOutput
The script writes:
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.jsonThe 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
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()