On This Page (5)
Examples Showcase / sch_paint_ir_views_and_png
Render Schematic Paint IR, editor views, outlined text, Skia PNG, and PrjPcb variants
Extract sch-paint-ir:a0 commands from Hydroscope schematics, render static/editor/outlined-text SVGs and 150 DPI Skia PNGs, and compare Variant A (J6 Not Fitted) vs. Variant B through AltiumDesign.
Output Previews
Inspect Schematic Paint IR (sch-paint-ir:a0) and render schematics as static SVG, editor-view SVG, outlined-text SVG (text_mode="paths"), Skia PNG, and variant-aware physical pages through AltiumDesign.
What It Shows
- Loading project parameters from
Hydroscope.PrjPcbviaAltiumPrjPcb(...)so title-block and schematic special strings resolve on standalone sheets - Extracting the Paint IR payload with
schdoc.to_paint_ir(...) - Rendering a static print SVG with
schdoc.to_svg(...) - Configuring an editor view with
SchPaintView.editor(...)andSchJunctionColors(...)(grid=True,hotspots=True,auto_junctions_on_top=True) - Outlining all schematic text to vector
<path>glyphs withSchPaintSvgOptions(text_mode="paths", text_path_backend="harfbuzz") - Rasterizing to PNG with
schdoc.to_paint_png(..., png_options=SchPaintPngOptions(dpi=150.0, backend="skia")) - Compiling
Hydroscope.PrjPcbviaAltiumDesign.from_prjpcb(...), selecting project assembly variants ("A"whereJ6is marked Not Fitted / DNP onTOP_LEVEL.SchDoc, vs."B"where all parts are fitted), and rendering physical pages viadesign.to_physical_paint_ir(...),design.to_physical_svg(...), andSchPaintPngRenderer
Run
From the repository root:
uv run python examples\sch_paint_ir_views_and_png\sch_paint_ir_views_and_png.pyOutput
The script writes:
examples/sch_paint_ir_views_and_png/output/power_supply_static.svg
examples/sch_paint_ir_views_and_png/output/power_supply_editor.svg
examples/sch_paint_ir_views_and_png/output/power_supply_text_paths.svg
examples/sch_paint_ir_views_and_png/output/power_supply_150dpi.png
examples/sch_paint_ir_views_and_png/output/power_supply_paint_ir.json
examples/sch_paint_ir_views_and_png/output/top_level_variant_a.svg
examples/sch_paint_ir_views_and_png/output/top_level_variant_a_150dpi.png
examples/sch_paint_ir_views_and_png/output/top_level_variant_b.svg
examples/sch_paint_ir_views_and_png/output/sch_paint_ir_manifest.jsonSource Code
from __future__ import annotations
import json
import shutil
from pathlib import Path
from altium_monkey import (
AltiumDesign,
AltiumSchDoc,
SchJunctionColors,
SchPaintPngOptions,
SchPaintPngRenderer,
SchPaintSvgOptions,
SchPaintView,
)
from altium_monkey.altium_prjpcb import AltiumPrjPcb
SAMPLE_DIR = Path(__file__).resolve().parent
EXAMPLES_DIR = SAMPLE_DIR.parent
PROJECT_FILE = EXAMPLES_DIR / "assets" / "projects" / "hydroscope" / "Hydroscope.PrjPcb"
INPUT_SCHDOC = (
EXAMPLES_DIR / "assets" / "projects" / "hydroscope" / "POWER_SUPPLY.SchDoc"
)
OUTPUT_DIR = SAMPLE_DIR / "output"
STATIC_SVG_PATH = OUTPUT_DIR / "power_supply_static.svg"
EDITOR_SVG_PATH = OUTPUT_DIR / "power_supply_editor.svg"
PATHS_SVG_PATH = OUTPUT_DIR / "power_supply_text_paths.svg"
PNG_PATH = OUTPUT_DIR / "power_supply_150dpi.png"
VARIANT_A_SVG_PATH = OUTPUT_DIR / "top_level_variant_a.svg"
VARIANT_A_PNG_PATH = OUTPUT_DIR / "top_level_variant_a_150dpi.png"
VARIANT_B_SVG_PATH = OUTPUT_DIR / "top_level_variant_b.svg"
PAINT_IR_PATH = OUTPUT_DIR / "power_supply_paint_ir.json"
MANIFEST_PATH = OUTPUT_DIR / "sch_paint_ir_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 _render_project_variant_page(
project_file: Path,
*,
variant_name: str,
instance_path: str = "TOP_LEVEL",
) -> tuple[str, object, list[str]]:
"""Compile a PrjPcb with one selected variant and build Paint IR + SVG."""
design = AltiumDesign.from_prjpcb(project_file)
assert design.project is not None
design.project.set_current_variant(variant_name)
design_json = design.to_json()
page_ref = next(
page["page_occurrence_ref"]
for page in design_json["physical_page_metadata"]
if page["physical_instance_path"] == instance_path
)
dnp_designators = sorted(
comp["designator"]
for comp in design_json["components"]
if comp.get("dnp")
)
paint_ir = design.to_physical_paint_ir(page_ref)
svg_text = design.to_physical_svg(
page_ref,
options=SchPaintSvgOptions(include_group_metadata=True),
)
return svg_text, paint_ir, dnp_designators
def build_sch_paint_ir_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)
# Load project parameters from the owning .PrjPcb so title-block and
# schematic special strings (=PROJECT_TITLE_LINE_1, =PROJECT_STATUS, etc.)
# resolve across standalone Paint IR, SVG, and PNG renders.
prj = AltiumPrjPcb(PROJECT_FILE)
project_parameters = dict(prj.parameters)
schdoc = AltiumSchDoc(INPUT_SCHDOC)
# 1. Inspect the versioned Paint IR payload directly.
paint_ir = schdoc.to_paint_ir(project_parameters=project_parameters)
paint_ir_payload = (
paint_ir.to_dict()
if hasattr(paint_ir, "to_dict")
else json.loads(paint_ir.to_json())
)
paint_ir_path = output_dir / "power_supply_paint_ir.json"
paint_ir_path.write_text(
json.dumps(paint_ir_payload, indent=2) + "\n",
encoding="utf-8",
newline="\n",
)
# 2. Render a static SVG (no editor grid or pin hotspots).
static_svg = schdoc.to_svg(project_parameters=project_parameters)
static_svg_path = output_dir / "power_supply_static.svg"
static_svg_path.write_text(static_svg, encoding="utf-8", newline="\n")
# 3. Render an editor-style SVG with grid, hotspots, and custom junction colors.
editor_view = SchPaintView.editor(
grid=True,
hotspots=True,
junction_colors=SchJunctionColors(wire_bgr=0x0000A0, bus_bgr=0xA00000),
auto_junctions_on_top=True,
)
editor_svg = schdoc.to_svg(
options=SchPaintSvgOptions(include_group_metadata=True),
project_parameters=project_parameters,
view=editor_view,
)
editor_svg_path = output_dir / "power_supply_editor.svg"
editor_svg_path.write_text(editor_svg, encoding="utf-8", newline="\n")
# 4. Render a portable SVG with all text outlined as vector paths via HarfBuzz.
paths_svg = schdoc.to_svg(
options=SchPaintSvgOptions(
text_mode="paths",
text_path_backend="harfbuzz",
),
project_parameters=project_parameters,
)
paths_svg_path = output_dir / "power_supply_text_paths.svg"
paths_svg_path.write_text(paths_svg, encoding="utf-8", newline="\n")
# 5. Rasterize directly to PNG via Skia at 150 DPI.
png_options = SchPaintPngOptions(dpi=150.0, backend="skia")
png_bytes = schdoc.to_paint_png(
project_parameters=project_parameters,
png_options=png_options,
)
png_path = output_dir / "power_supply_150dpi.png"
png_path.write_bytes(png_bytes)
# 6. Render a compiled physical page through AltiumDesign with PrjPcb
# assembly variants: Variant "A" marks J6 Not Fitted (DNP) on TOP_LEVEL,
# whereas Variant "B" fits all components.
variant_a_svg, variant_a_ir, variant_a_dnp = _render_project_variant_page(
PROJECT_FILE,
variant_name="A",
)
variant_a_svg_path = output_dir / "top_level_variant_a.svg"
variant_a_svg_path.write_text(variant_a_svg, encoding="utf-8", newline="\n")
variant_a_png_bytes = SchPaintPngRenderer(png_options).render(variant_a_ir)
variant_a_png_path = output_dir / "top_level_variant_a_150dpi.png"
variant_a_png_path.write_bytes(variant_a_png_bytes)
variant_b_svg, _variant_b_ir, variant_b_dnp = _render_project_variant_page(
PROJECT_FILE,
variant_name="B",
)
variant_b_svg_path = output_dir / "top_level_variant_b.svg"
variant_b_svg_path.write_text(variant_b_svg, encoding="utf-8", newline="\n")
manifest: dict[str, object] = {
"project": _examples_relative(PROJECT_FILE),
"input_schdoc": _examples_relative(INPUT_SCHDOC),
"project_parameter_count": len(project_parameters),
"paint_ir_schema": paint_ir_payload.get("schema"),
"paint_command_count": len(paint_ir_payload.get("commands", [])),
"project_variants": {
"A": {"dnp": variant_a_dnp},
"B": {"dnp": variant_b_dnp},
},
"outputs": {
"paint_ir_json": {
"path": _sample_relative(paint_ir_path),
"byte_count": paint_ir_path.stat().st_size,
},
"static_svg": {
"path": _sample_relative(static_svg_path),
"byte_count": static_svg_path.stat().st_size,
},
"editor_svg": {
"path": _sample_relative(editor_svg_path),
"byte_count": editor_svg_path.stat().st_size,
"has_editor_grid": "editor-grid" in editor_svg
or "grid" in editor_svg.lower(),
},
"paths_svg": {
"path": _sample_relative(paths_svg_path),
"byte_count": paths_svg_path.stat().st_size,
"text_element_count": paths_svg.count("<text"),
},
"png_150dpi": {
"path": _sample_relative(png_path),
"byte_count": len(png_bytes),
"png_signature_ok": png_bytes.startswith(b"\x89PNG\r\n\x1a\n"),
},
"top_level_variant_a_svg": {
"path": _sample_relative(variant_a_svg_path),
"byte_count": variant_a_svg_path.stat().st_size,
"variant_title_rendered": ">A</text>" in variant_a_svg,
"j6_dimmed_not_fitted": "#bfbebc" in variant_a_svg,
},
"top_level_variant_a_png": {
"path": _sample_relative(variant_a_png_path),
"byte_count": len(variant_a_png_bytes),
"png_signature_ok": variant_a_png_bytes.startswith(b"\x89PNG\r\n\x1a\n"),
},
"top_level_variant_b_svg": {
"path": _sample_relative(variant_b_svg_path),
"byte_count": variant_b_svg_path.stat().st_size,
"variant_title_rendered": ">B</text>" in variant_b_svg,
"j6_dimmed_not_fitted": "#bfbebc" in variant_b_svg,
},
},
}
manifest_path = output_dir / "sch_paint_ir_manifest.json"
manifest_path.write_text(
json.dumps(manifest, indent=2) + "\n",
encoding="utf-8",
newline="\n",
)
return manifest
def main() -> None:
manifest = build_sch_paint_ir_example()
outputs = manifest["outputs"]
assert isinstance(outputs, dict)
print(f"Input SchDoc: {manifest['input_schdoc']}")
print(f"Paint IR schema: {manifest['paint_ir_schema']}")
print(f"Paint IR commands: {manifest['paint_command_count']}")
print(f"Static SVG bytes: {outputs['static_svg']['byte_count']}")
print(f"Editor SVG bytes: {outputs['editor_svg']['byte_count']}")
print(
f"Outlined-text SVG bytes: {outputs['paths_svg']['byte_count']} "
f"(<text> elements: {outputs['paths_svg']['text_element_count']})"
)
print(f"150 DPI PNG bytes: {outputs['png_150dpi']['byte_count']}")
print(
f"Variant A TOP_LEVEL SVG bytes: {outputs['top_level_variant_a_svg']['byte_count']} "
f"(J6 dimmed Not Fitted: {outputs['top_level_variant_a_svg']['j6_dimmed_not_fitted']})"
)
print(
f"Variant B TOP_LEVEL SVG bytes: {outputs['top_level_variant_b_svg']['byte_count']} "
f"(J6 dimmed Not Fitted: {outputs['top_level_variant_b_svg']['j6_dimmed_not_fitted']})"
)
if __name__ == "__main__":
main()