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

Examples Showcase / schdoc_add_power_port

Add power ports to an existing SchDoc

Load a blank schematic, create detached built-in power-port symbols with several styles, add them to the document, and save the modified SchDoc.

schdocmutationschdocmutationpower-portconnectivity

Open a blank schematic, add several built-in power-port symbols, and save the modified .SchDoc.

This example uses the detached-object pattern that the other public schematic mutation examples use:

  1. load an existing SchDoc
  2. describe each power port with public enums and SchPointMils
  3. call make_sch_power_port(...)
  4. insert each object with AltiumSchDoc.add_object(...)
  5. persist the change with AltiumSchDoc.save(...)

What It Shows

  1. creating detached power ports with make_sch_power_port(...)
  2. using PowerObjectStyle for built-in symbol selection
  3. using SchFontSpec instead of raw FontID values
  4. using ColorValue helpers instead of raw Win32 color integers
  5. controlling show_net_name explicitly for supply and ground symbols
  6. reopening the written file and inspecting the created objects

Scope

This public example is intentionally limited to the built-in Altium power-port styles. Advanced custom power-port graphics backed by object definitions are not part of this example surface.

Run

From the project root:

powershell
uv run python examples\schdoc_add_power_port\schdoc_add_power_port.py

Input

This sample uses:

text
examples/schdoc_add_power_port/input/blank.SchDoc

Output

The script writes:

text
examples/schdoc_add_power_port/output/blank_with_power_ports.SchDoc

Expected Result

The output file should contain:

  1. a row of supply-style ports including circle, arrow, bar, wave, GOST arrow, and GOST bar variants
  2. a row of ground-style ports including power ground, signal ground, earth, GOST power ground, and GOST earth variants
  3. explicit net-name visibility choices so both hidden-name and visible-name cases are easy to inspect in Altium

The script also prints the reopened style, orientation, font, color, and location for each power port so the example is easy to sanity-check without inspecting the binary file directly.

Source Code

examples/schdoc_add_power_port/schdoc_add_power_port.py
from __future__ import annotations

from pathlib import Path

from altium_monkey import (
    AltiumSchDoc,
    ColorValue,
    PowerObjectStyle,
    SchFontSpec,
    SchPointMils,
    TextOrientation,
    make_sch_power_port,
)


SAMPLE_DIR = Path(__file__).resolve().parent
INPUT_SCHDOC = SAMPLE_DIR / "input" / "blank.SchDoc"
OUTPUT_DIR = SAMPLE_DIR / "output"
OUTPUT_SCHDOC = OUTPUT_DIR / "blank_with_power_ports.SchDoc"


def main() -> None:
    OUTPUT_DIR.mkdir(parents=True, exist_ok=True)

    schdoc = AltiumSchDoc(INPUT_SCHDOC)
    power_port_specs = [
        {
            "location_mils": SchPointMils.from_mils(1400, 6900),
            "text": "VCC_CIRCLE",
            "style": PowerObjectStyle.CIRCLE,
            "font": SchFontSpec(name="Arial", size=10),
            "color": ColorValue.from_hex("#800000"),
            "orientation": TextOrientation.DEGREES_0,
            "show_net_name": True,
        },
        {
            "location_mils": SchPointMils.from_mils(2900, 6900),
            "text": "+3V3",
            "style": PowerObjectStyle.ARROW,
            "font": SchFontSpec(name="Arial", size=10),
            "color": ColorValue.from_hex("#800000"),
            "orientation": TextOrientation.DEGREES_0,
            "show_net_name": True,
        },
        {
            "location_mils": SchPointMils.from_mils(4400, 6900),
            "text": "VBAT",
            "style": PowerObjectStyle.BAR,
            "font": SchFontSpec(name="Arial", size=10),
            "color": ColorValue.from_hex("#800000"),
            "orientation": TextOrientation.DEGREES_90,
            "show_net_name": True,
        },
        {
            "location_mils": SchPointMils.from_mils(5900, 6900),
            "text": "AC_REF",
            "style": PowerObjectStyle.WAVE,
            "font": SchFontSpec(name="Arial", size=10),
            "color": ColorValue.from_hex("#800000"),
            "orientation": TextOrientation.DEGREES_0,
            "show_net_name": True,
        },
        {
            "location_mils": SchPointMils.from_mils(7400, 6900),
            "text": "VCC_GOST",
            "style": PowerObjectStyle.GOST_ARROW,
            "font": SchFontSpec(name="Arial", size=10),
            "color": ColorValue.from_hex("#800000"),
            "orientation": TextOrientation.DEGREES_180,
            "show_net_name": True,
        },
        {
            "location_mils": SchPointMils.from_mils(8900, 6900),
            "text": "BAR_GOST",
            "style": PowerObjectStyle.GOST_BAR,
            "font": SchFontSpec(name="Arial", size=10),
            "color": ColorValue.from_hex("#800000"),
            "orientation": TextOrientation.DEGREES_270,
            "show_net_name": True,
        },
        {
            "location_mils": SchPointMils.from_mils(1400, 4700),
            "text": "PGND",
            "style": PowerObjectStyle.GND_POWER,
            "font": SchFontSpec(name="Arial", size=10),
            "color": ColorValue.from_hex("#800000"),
            "orientation": TextOrientation.DEGREES_0,
            "show_net_name": False,
        },
        {
            "location_mils": SchPointMils.from_mils(2900, 4700),
            "text": "AGND",
            "style": PowerObjectStyle.GND_SIGNAL,
            "font": SchFontSpec(name="Arial", size=10),
            "color": ColorValue.from_hex("#800000"),
            "orientation": TextOrientation.DEGREES_0,
            "show_net_name": False,
        },
        {
            "location_mils": SchPointMils.from_mils(4400, 4700),
            "text": "EARTH_REF",
            "style": PowerObjectStyle.GND_EARTH,
            "font": SchFontSpec(name="Arial", size=10),
            "color": ColorValue.from_hex("#800000"),
            "orientation": TextOrientation.DEGREES_0,
            "show_net_name": True,
        },
        {
            "location_mils": SchPointMils.from_mils(5900, 4700),
            "text": "PGND_GOST",
            "style": PowerObjectStyle.GOST_GND_POWER,
            "font": SchFontSpec(name="Arial", size=10),
            "color": ColorValue.from_hex("#800000"),
            "orientation": TextOrientation.DEGREES_0,
            "show_net_name": False,
        },
        {
            "location_mils": SchPointMils.from_mils(7400, 4700),
            "text": "CHASSIS",
            "style": PowerObjectStyle.GOST_GND_EARTH,
            "font": SchFontSpec(name="Arial", size=10),
            "color": ColorValue.from_hex("#800000"),
            "orientation": TextOrientation.DEGREES_0,
            "show_net_name": True,
        },
    ]

    for spec in power_port_specs:
        schdoc.add_object(
            make_sch_power_port(
                location_mils=spec["location_mils"],
                text=spec["text"],
                style=spec["style"],
                font=spec["font"],
                color=spec["color"],
                orientation=spec["orientation"],
                show_net_name=spec["show_net_name"],
            )
        )

    schdoc.save(OUTPUT_SCHDOC)

    reopened = AltiumSchDoc(OUTPUT_SCHDOC)
    print(f"Loaded: {INPUT_SCHDOC.name}")
    print(f"Power ports written: {len(reopened.power_ports)}")
    for index, power_port in enumerate(reopened.power_ports, start=1):
        font = power_port.font
        color_text = ColorValue.from_win32(power_port.color or 0).hex
        font_text = "unresolved"
        if font is not None:
            font_text = f"{font.name} {font.size}"
        print(
            f"Power port {index}: {power_port.text!r}, "
            f"{power_port.style.name}, "
            f"{power_port.orientation.name}, "
            f"show_net_name={power_port.show_net_name}, "
            f"font={font_text}, "
            f"location=({power_port.location_mils.x_mils:.0f}, "
            f"{power_port.location_mils.y_mils:.0f}), "
            f"color={color_text}"
        )
    print(f"Wrote: {OUTPUT_SCHDOC.relative_to(SAMPLE_DIR)}")


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