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

Examples Showcase / compiled_design_c0_and_paint_bindings

Query C0 compiled design, net explanations, and paint bindings

Compile Hydroscope.PrjPcb with AltiumCompilation, inspect AltiumCompiledView net explanations, source tracing, and field impacts, and render a schematic page paired with paint_bindings.c0.

designschdocsvgdesigncompilation-c0compiled-viewpaint-bindingssvg

Output Previews

page_preview.svg
Open SVG
page_preview.svg

Compile the multi-sheet Hydroscope.PrjPcb project with AltiumCompilation, inspect the C0 compiled view (explain_net, sources_for_net, impacts_for_field, realized_sources_for_net), and generate a schematic page SVG paired with its paint_bindings.c0 hit-test map.

What It Shows

  1. Compiling a multi-sheet project into AltiumCompilation (compilation.c0)
  2. Opening compilation.compiled_view() and view.structural_graph()
  3. Explaining a compiled net (view.explain_net(...)), tracing its logical and realized schematic sources (sources_for_net, realized_sources_for_net), and querying downstream field-edit impacts (impacts_for_field)
  4. Rendering a physical schematic page with compilation.schematic_graphics(...) and querying graphics.occurrences_for_net(...) against paint_bindings.c0

Run

From the repository root:

powershell
uv run python examples\compiled_design_c0_and_paint_bindings\compiled_design_c0_and_paint_bindings.py

Output

The script writes:

text
examples/compiled_design_c0_and_paint_bindings/output/page_preview.svg
examples/compiled_design_c0_and_paint_bindings/output/paint_bindings_c0.json
examples/compiled_design_c0_and_paint_bindings/output/compiled_design_c0_manifest.json

Source Code

examples/compiled_design_c0_and_paint_bindings/compiled_design_c0_and_paint_bindings.py
from __future__ import annotations

import json
import shutil
from pathlib import Path

from altium_monkey import AltiumCompilation
from altium_monkey.sch_compiled_design.generated import NetOccurrence

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"
PAGE_SVG_PATH = OUTPUT_DIR / "page_preview.svg"
BINDINGS_PATH = OUTPUT_DIR / "paint_bindings_c0.json"
MANIFEST_PATH = OUTPUT_DIR / "compiled_design_c0_manifest.json"


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 build_compiled_design_c0_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)

    compilation = AltiumCompilation.from_file(PROJECT_FILE)
    dto = compilation.to_dto()
    view = compilation.compiled_view
    graph = view.structural_graph()
    graph_dto = graph.to_dto()

    root_def = next(
        d for d in graph_dto.definitions if d.id == graph_dto.root_definition_ref
    )
    target_net = next((net for net in root_def.nets if net.name), root_def.nets[0])
    target_occ = NetOccurrence(instance_path=[], net_ref=target_net.id)
    explanation = view.explain_net(target_occ)
    net_sources = view.sources_for_net(target_occ)
    realized_sources = compilation.realized_sources_for_net(target_occ)

    # Trace field-edit impacts for the first authored source field contributing
    # to the selected net, when one is available.
    impact_summary: dict[str, object] | None = None
    for src in net_sources.sources:
        field_key = getattr(src, "field_key", None) or getattr(
            src, "source_field", None
        )
        if field_key is not None:
            impacts = view.impacts_for_field(field_key)
            impact_summary = {
                "field_kind": type(field_key).__name__,
                "impact_count": len(impacts),
                "impacted_kinds": sorted({type(item).__name__ for item in impacts}),
            }
            break

    # Pick the first realized schematic page and emit C0 paint bindings + SVG.
    comp_root = next(d for d in dto.definitions if d.id == dto.root_definition_ref)
    first_page = comp_root.schematic.pages[0]
    graphics = compilation.schematic_graphics(first_page.id)

    page_svg_path = output_dir / "page_preview.svg"
    page_svg_path.write_bytes(graphics.svg)

    bindings_path = output_dir / "paint_bindings_c0.json"
    bindings_payload = json.loads(graphics.bindings_json)
    bindings_path.write_text(
        json.dumps(bindings_payload, indent=2) + "\n",
        encoding="utf-8",
        newline="\n",
    )

    occurrences_for_net = graphics.occurrences_for_net(target_occ)

    manifest: dict[str, object] = {
        "project": _examples_relative(PROJECT_FILE),
        "compilation_schema": dto.schema,
        "structural_graph_schema": graph_dto.schema,
        "definition_count": len(dto.definitions),
        "root_net_count": len(root_def.nets),
        "root_instance_count": len(root_def.instances),
        "realized_page_count": len(comp_root.schematic.pages),
        "selected_net": {
            "id": target_net.id,
            "name": target_net.name,
            "explanation_schema": explanation.schema,
            "net_source_count": len(net_sources.sources),
            "realized_source_count": len(realized_sources.sources),
            "paint_occurrences_on_first_page": len(occurrences_for_net.occurrence_ids),
        },
        "field_impact_sample": impact_summary,
        "rendered_page": {
            "page_id": first_page.id,
            "svg": _sample_relative(page_svg_path),
            "bindings_json": _sample_relative(bindings_path),
            "bindings_schema": bindings_payload.get("schema"),
            "bound_occurrence_count": len(bindings_payload.get("bindings", [])),
        },
    }

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


def main() -> None:
    manifest = build_compiled_design_c0_example()
    selected = manifest["selected_net"]
    rendered = manifest["rendered_page"]
    assert isinstance(selected, dict)
    assert isinstance(rendered, dict)
    print(f"Project: {manifest['project']}")
    print(f"Compilation schema: {manifest['compilation_schema']}")
    print(
        f"Root nets: {manifest['root_net_count']}, "
        f"instances: {manifest['root_instance_count']}, "
        f"pages: {manifest['realized_page_count']}"
    )
    print(
        f"Selected net: {selected['name']} ({selected['id']}) -> "
        f"{selected['net_source_count']} source entries"
    )
    print(
        f"Rendered page {rendered['page_id']}: "
        f"{rendered['bound_occurrence_count']} paint bindings"
    )


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