On This Page (6)
Examples Showcase / hello_altium_design
Load an existing project with AltiumDesign
Load an existing PrjPcb, print variants and document paths, and write summary, BOM, design JSON, physical-page SVGs, compiled net-name examples, and netlist artifacts.
Output Previews
Load an existing Altium project with AltiumDesign, print the available variants and document paths, and write the full design JSON plus BOM/netlist artifacts to disk.
This example is intentionally small. It is meant to show the public project-loading and design-analysis API, not internal netlist plumbing.
What It Shows
AltiumDesign.from_prjpcb(...)AltiumDesign.to_json(...)AltiumDesign.to_netlist(...)AltiumDesign.to_bom_payload()for versioned schematic BOM JSONAltiumDesign.to_bom(...)for the established list-returning APIAltiumDesign.get_variants(...)- Reading compiled net
winning_name,alternate_names, andname_sources - Reading the Design b0 compiled schematic graph to find realized pages, components, local nets, terminals, and repeated/channel-safe drawing links
- Rendering project-aware physical schematic SVGs with
AltiumDesign.to_physical_svg(page_occurrence_ref)
Run
From the repository root:
uv run python examples\hello_altium_design\hello_altium_design.pyInput Project
This sample uses the redistributable project staged at:
examples/assets/projects/rt_super_c1/RT_SUPER_C1.PrjPcbOutput
The script writes:
examples/hello_altium_design/output/project_summary.json
examples/hello_altium_design/output/altium_design.json
examples/hello_altium_design/output/compiled_schematic_graph_summary.json
examples/hello_altium_design/output/physical_svg_manifest.json
examples/hello_altium_design/output/physical_svgs/<physical-page>.svg
examples/hello_altium_design/output/netlist.json
examples/hello_altium_design/output/schematic_bom.json
examples/hello_altium_design/output/compiled_net_name_examples.json
examples/hello_altium_design/output/bom_all.json
examples/hello_altium_design/output/variant_boms/<variant>.jsonaltium_design.json uses the altium_monkey.design.b0 schema. netlist.json uses the current altium_monkey.netlist.b0 schema, including structured source_pages and stable component identities. schematic_bom.json uses altium_monkey.schematic_bom.a0. The Design b0 root nets retain their own established source_sheets representation and are not standalone Netlist b0 rows.
compiled_schematic_graph_summary.json is a compact consumer-oriented view of the required altium_monkey.compiled_schematic_graph.a0 transport. It shows the scoped drawing identity rule:
page_occurrence_ref + artifact_key + element_idDefinitions describe reusable source material; occurrences describe the realized compiled design. physical_page_metadata adds channel, room, and document presentation facts without repeating components or nets. For repeated sheets or channels, the same source drawing ID can therefore appear on several page occurrences without becoming an ambiguous semantic identity.
physical_svg_manifest.json lists the SVG files written through AltiumDesign.to_physical_svg(page_occurrence_ref). These SVGs render source schematic geometry in project compile context, so resolved physical designators are used before text measurement and SVG emission.
compiled_net_name_examples.json demonstrates the canonical pattern for compiled net names: winning_name is the selected compiled net name, while alternate_names and name_sources expose other labels or source objects that contributed to the same compiled net. It can be empty for projects without alternate names.
When the project references a PcbDoc, altium_design.json also includes the optional root pnp block with pick-and-place placements in millimeters. See pcbdoc_pick_n_place for a focused CSV/JSON pick-and-place example.
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"
VARIANT_BOM_DIR = OUTPUT_DIR / "variant_boms"
PHYSICAL_SVG_DIR = OUTPUT_DIR / "physical_svgs"
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 _safe_variant_filename(variant: str) -> str:
safe = [
char if char.isalnum() or char in {"-", "_"} else "_"
for char in variant.strip()
]
return "".join(safe) or "default"
def _safe_output_stem(value: str) -> str:
safe = [
char if char.isalnum() or char in {"-", "_"} else "_" for char in value.strip()
]
return "".join(safe) or "page"
def _compiled_net_name_examples(design_json: dict) -> list[dict]:
examples: list[dict] = []
for net in design_json.get("nets", []):
aliases = list(net.get("aliases") or [])
name_sources = list(net.get("name_sources") or [])
if not aliases and len(name_sources) <= 1:
continue
examples.append(
{
"uid": net.get("uid", ""),
"winning_name": net.get("name", ""),
"alternate_names": aliases,
"name_sources": name_sources,
}
)
return examples
def _compiled_schematic_graph_summary(design_json: dict) -> dict:
graph = design_json["compiled_schematic_graph"]
metadata_by_page = {
row["page_occurrence_ref"]: row for row in design_json["physical_page_metadata"]
}
component_counts: dict[str, int] = {}
net_counts: dict[str, int] = {}
terminal_counts: dict[str, int] = {}
drawing_link_counts: dict[str, int] = {}
for collection, counts in (
(graph["component_occurrences"], component_counts),
(graph["local_net_occurrences"], net_counts),
(graph["terminal_occurrences"], terminal_counts),
(graph["graphical_artifact_links"], drawing_link_counts),
):
for row in collection:
page_ref = row["page_occurrence_ref"]
counts[page_ref] = counts.get(page_ref, 0) + 1
pages: list[dict] = []
for page in graph["page_occurrences"]:
page_ref = page["id"]
metadata = metadata_by_page.get(page_ref, {})
pages.append(
{
"page_occurrence_ref": page_ref,
"display_name": page.get("display_name", ""),
"sheet_number": page.get("sheet_number", ""),
"instance_order": page.get("instance_order", 0),
"unit_occurrence_ref": page.get("unit_occurrence_ref", ""),
"page_definition_ref": page.get("page_definition_ref", ""),
"physical_instance_path": metadata.get("physical_instance_path", ""),
"channel_index": metadata.get("channel_index", 0),
"room_name": metadata.get("physical_room_name", ""),
"component_count": component_counts.get(page_ref, 0),
"local_net_count": net_counts.get(page_ref, 0),
"terminal_count": terminal_counts.get(page_ref, 0),
"drawing_link_count": drawing_link_counts.get(page_ref, 0),
}
)
return {
"schema": design_json.get("schema", ""),
"graph_schema": graph["schema"],
"identity_namespace": graph["identity_namespace"],
"drawing_identity_rule": ("page_occurrence_ref + artifact_key + element_id"),
"page_occurrence_count": len(pages),
"pages": pages,
}
def _write_physical_svgs(design: AltiumDesign, design_json: dict) -> dict:
PHYSICAL_SVG_DIR.mkdir(parents=True, exist_ok=True)
for stale_svg in PHYSICAL_SVG_DIR.glob("*.svg"):
stale_svg.unlink()
pages: list[dict] = []
graph = design_json["compiled_schematic_graph"]
metadata_by_page = {
row["page_occurrence_ref"]: row for row in design_json["physical_page_metadata"]
}
for ordinal, page in enumerate(graph["page_occurrences"]):
page_id = str(page["id"])
if not page_id:
continue
metadata = metadata_by_page.get(page_id, {})
page_name = str(
metadata.get("physical_instance_path")
or page.get("display_name")
or f"page_{ordinal}"
)
output_name = f"{ordinal:02d}_{_safe_output_stem(page_name)}.svg"
output_path = PHYSICAL_SVG_DIR / output_name
output_path.write_text(design.to_physical_svg(page_id), encoding="utf-8")
pages.append(
{
"page_occurrence_ref": page_id,
"physical_instance_path": metadata.get("physical_instance_path", ""),
"display_name": page.get("display_name", ""),
"svg_path": f"physical_svgs/{output_name}",
"identity_rule": ("page_occurrence_ref + artifact_key + element_id"),
}
)
return {
"schema": design_json.get("schema", ""),
"physical_svg_count": len(pages),
"pages": pages,
}
def main() -> None:
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
VARIANT_BOM_DIR.mkdir(parents=True, exist_ok=True)
for stale_variant_bom in VARIANT_BOM_DIR.glob("*.json"):
stale_variant_bom.unlink()
stale_variant_bundle = OUTPUT_DIR / "variant_boms.json"
if stale_variant_bundle.exists():
stale_variant_bundle.unlink()
design = AltiumDesign.from_prjpcb(PROJECT_FILE)
design_json = design.to_json(include_indexes=True)
graph_summary = _compiled_schematic_graph_summary(design_json)
physical_svg_manifest = _write_physical_svgs(design, design_json)
project = design.project
if project is None:
raise RuntimeError(
"AltiumDesign.from_prjpcb(...) did not retain project context"
)
netlist = design.to_netlist()
variants = design.get_variants()
current_variant = project.get_current_variant()
schdoc_paths = project.get_schdoc_paths()
pcbdoc_paths = design.get_pcbdoc_paths()
print(f"Loaded project: {PROJECT_FILE.name}")
print(f"Current variant: {current_variant or '(none)'}")
print(
f"Variants ({len(variants)}): {', '.join(variants) if variants else '(none)'}"
)
print(f"Schematic documents ({len(schdoc_paths)}):")
for path in schdoc_paths:
print(f" {path.name}")
print(f"PCB documents ({len(pcbdoc_paths)}):")
for path in pcbdoc_paths:
print(f" {path.name}")
print(f"Components: {len(netlist.components)}")
print(f"Nets: {len(netlist.nets)}")
summary = {
"project_file": PROJECT_FILE.name,
"current_variant": current_variant,
"variants": variants,
"schematic_documents": [path.name for path in schdoc_paths],
"pcb_documents": [path.name for path in pcbdoc_paths],
"pcb_project_parameters": design.get_pcb_project_parameters(),
"component_count": len(netlist.components),
"net_count": len(netlist.nets),
}
_write_json(OUTPUT_DIR / "project_summary.json", summary)
_write_json(OUTPUT_DIR / "altium_design.json", design_json)
_write_json(OUTPUT_DIR / "compiled_schematic_graph_summary.json", graph_summary)
_write_json(OUTPUT_DIR / "physical_svg_manifest.json", physical_svg_manifest)
_write_json(OUTPUT_DIR / "netlist.json", netlist.to_json())
_write_json(
OUTPUT_DIR / "schematic_bom.json",
design.to_bom_payload().to_json(),
)
_write_json(
OUTPUT_DIR / "compiled_net_name_examples.json",
_compiled_net_name_examples(design_json),
)
_write_json(OUTPUT_DIR / "bom_all.json", design.to_bom())
for variant in variants:
variant_filename = _safe_variant_filename(variant) + ".json"
_write_json(VARIANT_BOM_DIR / variant_filename, design.to_bom(variant=variant))
print("Wrote:")
print(" output/project_summary.json")
print(" output/altium_design.json")
print(" output/compiled_schematic_graph_summary.json")
print(" output/physical_svg_manifest.json")
print(" output/netlist.json")
print(" output/schematic_bom.json")
print(" output/compiled_net_name_examples.json")
print(" output/bom_all.json")
for page in physical_svg_manifest["pages"]:
print(f" output/{page['svg_path']}")
for variant in variants:
variant_filename = _safe_variant_filename(variant) + ".json"
print(f" output/variant_boms/{variant_filename}")
if __name__ == "__main__":
main()