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

Examples Showcase / schdoc_add_port

Add ports to an existing SchDoc

Load a blank schematic, create detached hierarchical ports with several styles and I/O types, add them to the document, and save the modified SchDoc.

schdocmutationschdocmutationportconnectivityhierarchy

Open a blank schematic, add several hierarchical ports, and save the modified .SchDoc.

This example follows the detached-object mutation pattern used by the other public schematic examples:

  1. load an existing SchDoc
  2. describe each port with public enums and mil-based dimensions
  3. call make_sch_port(...)
  4. insert each object with AltiumSchDoc.add_object(...)
  5. persist the change with AltiumSchDoc.save(...)

What It Shows

  1. creating detached ports with make_sch_port(...)
  2. using PortStyle and PortIOType instead of raw integers
  3. using SchFontSpec instead of raw FontID values
  4. using mil-based width_mils and height_mils inputs
  5. using SchHorizontalAlign and LineWidth for text alignment and border thickness
  6. reopening the written file and inspecting the created objects

Scope

This public example is intentionally limited to ordinary named ports. Advanced cross-reference display mode, harness-specific port behavior, and custom object-definition-backed port graphics are not part of this example surface.

Run

From the project root:

powershell
uv run python examples\schdoc_add_port\schdoc_add_port.py

Input

This sample uses:

text
examples/schdoc_add_port/input/blank.SchDoc

Output

The script writes:

text
examples/schdoc_add_port/output/blank_with_ports.SchDoc

Expected Result

The output file should contain:

  1. a horizontal row with left, right, left-right, and flat horizontal port styles
  2. a vertical row with top, bottom, top-bottom, and flat vertical port styles
  3. a mix of input, output, bidirectional, and unspecified I/O types
  4. explicit examples of alignment, border thickness, font, and hidden-name behavior

The script also prints the reopened style, I/O type, alignment, size, font, and location for each port so the example is easy to sanity-check without inspecting the binary file directly.

Source Code

examples/schdoc_add_port/schdoc_add_port.py
from __future__ import annotations

from pathlib import Path

from altium_monkey import (
    AltiumSchDoc,
    ColorValue,
    LineWidth,
    PortIOType,
    PortStyle,
    SchFontSpec,
    SchHorizontalAlign,
    SchPointMils,
    make_sch_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_ports.SchDoc"


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

    schdoc = AltiumSchDoc(INPUT_SCHDOC)
    port_specs = [
        {
            "location_mils": SchPointMils.from_mils(1200, 7100),
            "name": "CLK_IN",
            "width_mils": 700,
            "height_mils": 100,
            "io_type": PortIOType.INPUT,
            "style": PortStyle.LEFT,
            "font": SchFontSpec(name="Arial", size=10),
            "border_color": ColorValue.from_hex("#000000"),
            "fill_color": ColorValue.from_hex("#D9EAF7"),
            "text_color": ColorValue.from_hex("#073763"),
            "alignment": SchHorizontalAlign.LEFT,
            "border_width": LineWidth.SMALL,
            "auto_size": False,
            "show_net_name": True,
        },
        {
            "location_mils": SchPointMils.from_mils(2600, 7100),
            "name": "DATA_OUT",
            "width_mils": 700,
            "height_mils": 130,
            "io_type": PortIOType.OUTPUT,
            "style": PortStyle.RIGHT,
            "font": SchFontSpec(name="Courier New", size=12),
            "border_color": ColorValue.from_hex("#000000"),
            "fill_color": ColorValue.from_hex("#FCE5CD"),
            "text_color": ColorValue.from_hex("#7F6000"),
            "alignment": SchHorizontalAlign.CENTER,
            "border_width": LineWidth.MEDIUM,
            "auto_size": False,
            "show_net_name": True,
        },
        {
            "location_mils": SchPointMils.from_mils(4100, 7100),
            "name": "DATA_IO",
            "width_mils": 800,
            "height_mils": 130,
            "io_type": PortIOType.BIDIRECTIONAL,
            "style": PortStyle.LEFT_RIGHT,
            "font": SchFontSpec(name="Times New Roman", size=12),
            "border_color": ColorValue.from_hex("#000000"),
            "fill_color": ColorValue.from_hex("#D9EAD3"),
            "text_color": ColorValue.from_hex("#274E13"),
            "alignment": SchHorizontalAlign.CENTER,
            "border_width": LineWidth.LARGE,
            "auto_size": False,
            "show_net_name": True,
        },
        {
            "location_mils": SchPointMils.from_mils(5800, 7100),
            "name": "PLAIN_BUS",
            "width_mils": 700,
            "height_mils": 100,
            "io_type": PortIOType.UNSPECIFIED,
            "style": PortStyle.NONE_HORIZONTAL,
            "font": SchFontSpec(name="Arial", size=10),
            "border_color": ColorValue.from_hex("#000000"),
            "fill_color": ColorValue.from_hex("#EFEFEF"),
            "text_color": ColorValue.from_hex("#222222"),
            "alignment": SchHorizontalAlign.RIGHT,
            "border_width": LineWidth.SMALLEST,
            "auto_size": False,
            "show_net_name": True,
        },
        {
            "location_mils": SchPointMils.from_mils(1500, 4700),
            "name": "UPLINK",
            "width_mils": 600,
            "height_mils": 100,
            "io_type": PortIOType.OUTPUT,
            "style": PortStyle.TOP,
            "font": SchFontSpec(name="Arial", size=10),
            "border_color": ColorValue.from_hex("#000000"),
            "fill_color": ColorValue.from_hex("#D9EAF7"),
            "text_color": ColorValue.from_hex("#073763"),
            "alignment": SchHorizontalAlign.CENTER,
            "border_width": LineWidth.SMALL,
            "auto_size": False,
            "show_net_name": True,
        },
        {
            "location_mils": SchPointMils.from_mils(3000, 4700),
            "name": "DOWNLINK",
            "width_mils": 600,
            "height_mils": 130,
            "io_type": PortIOType.INPUT,
            "style": PortStyle.BOTTOM,
            "font": SchFontSpec(name="Courier New", size=12),
            "border_color": ColorValue.from_hex("#000000"),
            "fill_color": ColorValue.from_hex("#FCE5CD"),
            "text_color": ColorValue.from_hex("#7F6000"),
            "alignment": SchHorizontalAlign.CENTER,
            "border_width": LineWidth.MEDIUM,
            "auto_size": False,
            "show_net_name": True,
        },
        {
            "location_mils": SchPointMils.from_mils(4500, 4700),
            "name": "VERT_IO",
            "width_mils": 700,
            "height_mils": 130,
            "io_type": PortIOType.BIDIRECTIONAL,
            "style": PortStyle.TOP_BOTTOM,
            "font": SchFontSpec(name="Times New Roman", size=12),
            "border_color": ColorValue.from_hex("#000000"),
            "fill_color": ColorValue.from_hex("#D9EAD3"),
            "text_color": ColorValue.from_hex("#274E13"),
            "alignment": SchHorizontalAlign.CENTER,
            "border_width": LineWidth.LARGE,
            "auto_size": False,
            "show_net_name": True,
        },
        {
            "location_mils": SchPointMils.from_mils(6200, 4700),
            "name": "VERT_PLAIN",
            "width_mils": 600,
            "height_mils": 100,
            "io_type": PortIOType.UNSPECIFIED,
            "style": PortStyle.NONE_VERTICAL,
            "font": SchFontSpec(name="Arial", size=10),
            "border_color": ColorValue.from_hex("#000000"),
            "fill_color": ColorValue.from_hex("#EFEFEF"),
            "text_color": ColorValue.from_hex("#222222"),
            "alignment": SchHorizontalAlign.CENTER,
            "border_width": LineWidth.SMALLEST,
            "auto_size": False,
            "show_net_name": False,
        },
    ]

    for spec in port_specs:
        schdoc.add_object(
            make_sch_port(
                location_mils=spec["location_mils"],
                name=spec["name"],
                width_mils=spec["width_mils"],
                height_mils=spec["height_mils"],
                io_type=spec["io_type"],
                style=spec["style"],
                font=spec["font"],
                border_color=spec["border_color"],
                fill_color=spec["fill_color"],
                text_color=spec["text_color"],
                alignment=spec["alignment"],
                border_width=spec["border_width"],
                auto_size=spec["auto_size"],
                show_net_name=spec["show_net_name"],
            )
        )

    schdoc.save(OUTPUT_SCHDOC)

    reopened = AltiumSchDoc(OUTPUT_SCHDOC)
    print(f"Loaded: {INPUT_SCHDOC.name}")
    print(f"Ports written: {len(reopened.ports)}")
    for index, port in enumerate(reopened.ports, start=1):
        font = port.font
        font_text = "unresolved"
        if font is not None:
            font_text = f"{font.name} {font.size}"
        print(
            f"Port {index}: {port.name!r}, "
            f"{port.style.name}, "
            f"{port.io_type.name}, "
            f"{port.alignment.name}, "
            f"size=({port.width_mils}mil x {port.height_mils}mil), "
            f"auto_size={port.auto_size}, "
            f"show_net_name={port.show_net_name}, "
            f"font={font_text}, "
            f"location=({port.location_mils.x_mils:.0f}, {port.location_mils.y_mils:.0f})"
        )
    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)