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

Examples Showcase / schdoc_add_sheet_symbol

Add sheet symbols to an existing SchDoc

Load a blank schematic, create detached sheet symbols with detached entries and labels, add the top-level groups to the document, and save the modified SchDoc.

schdocmutationhierarchyschdocmutationsheet-symbolsheet-entryhierarchy

Open a blank schematic, append detached sheet-symbol groups, and save the modified .SchDoc.

This example shows the canonical public path for hierarchy records:

  1. load an existing SchDoc
  2. build one detached make_sch_sheet_symbol(...)
  3. attach detached make_sch_sheet_entry(...) records through symbol.add_entry(...)
  4. attach detached make_sch_sheet_name(...) and make_sch_file_name(...)
  5. add only the top-level sheet symbol with AltiumSchDoc.add_object(...)
  6. persist the result with AltiumSchDoc.save(...)

What It Shows

  1. using SchRectMils for the symbol body bounds
  2. using SchSheetSymbolType, SheetEntrySide, SchSheetEntryIOType, and SchSheetEntryArrowKind instead of raw integers
  3. resolving entry and label fonts from SchFontSpec
  4. using ColorValue helpers instead of raw Win32 color integers
  5. keeping entries, sheet name, and file name symbol-owned rather than top-level document objects
  6. reopening the written file and printing a concise summary of the created hierarchy groups

Run

From the project root:

powershell
uv run python examples\schdoc_add_sheet_symbol\schdoc_add_sheet_symbol.py

Input

This sample uses:

text
examples/schdoc_add_sheet_symbol/input/blank.SchDoc

Output

The script writes:

text
examples/schdoc_add_sheet_symbol/output/blank_with_sheet_symbols.SchDoc

Expected Result

The output file should contain two hierarchical sheet symbols:

  1. a normal CTRL_CORE symbol with left-side clock and reset entries plus a right-side ready entry
  2. a device-sheet POWER_IO symbol with a rounded body and mixed input, output, and bidirectional entries

Only the parent sheet symbols are added to the document. Their child entries and labels are symbol-owned and follow the parent automatically when the file is saved.

Source Code

examples/schdoc_add_sheet_symbol/schdoc_add_sheet_symbol.py
from __future__ import annotations

from pathlib import Path

from altium_monkey import (
    AltiumSchDoc,
    AltiumSchSheetSymbol,
    ColorValue,
    LineWidth,
    SchFontSpec,
    SchPointMils,
    SchRectMils,
    SchSheetEntryArrowKind,
    SchSheetEntryIOType,
    SchSheetSymbolType,
    SheetEntrySide,
    TextJustification,
    TextOrientation,
    make_sch_file_name,
    make_sch_sheet_entry,
    make_sch_sheet_name,
    make_sch_sheet_symbol,
)


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


def _build_ctrl_symbol() -> AltiumSchSheetSymbol:
    symbol = make_sch_sheet_symbol(
        bounds_mils=SchRectMils.from_corners_mils(1000, 5200, 3000, 3600),
        border_width=LineWidth.LARGE,
        border_color=ColorValue.from_hex("#202020"),
        fill_color=ColorValue.from_hex("#F0E6C8"),
        fill_background=True,
        symbol_type=SchSheetSymbolType.NORMAL,
        show_hidden_fields=False,
        design_item_id="CTRL_CORE",
        source_library_name="generated",
        revision_name="A",
    )
    symbol.add_entry(
        make_sch_sheet_entry(
            name="CLK_MAIN",
            side=SheetEntrySide.LEFT,
            io_type=SchSheetEntryIOType.INPUT,
            distance_from_top_mils=100,
            font=SchFontSpec(name="Arial", size=10, bold=True),
            border_color=ColorValue.from_hex("#101010"),
            fill_color=ColorValue.from_hex("#FFFFFF"),
            text_color=ColorValue.from_hex("#101010"),
            arrow_kind=SchSheetEntryArrowKind.TRIANGLE,
        )
    )
    symbol.add_entry(
        make_sch_sheet_entry(
            name="RST_N",
            side=SheetEntrySide.LEFT,
            io_type=SchSheetEntryIOType.INPUT,
            distance_from_top_mils=300,
            font=SchFontSpec(name="Arial", size=10, bold=True),
            border_color=ColorValue.from_hex("#101010"),
            fill_color=ColorValue.from_hex("#FFFFFF"),
            text_color=ColorValue.from_hex("#101010"),
        )
    )
    symbol.add_entry(
        make_sch_sheet_entry(
            name="READY_N",
            side=SheetEntrySide.RIGHT,
            io_type=SchSheetEntryIOType.OUTPUT,
            distance_from_top_mils=500,
            font=SchFontSpec(name="Arial", size=10),
            border_color=ColorValue.from_hex("#101010"),
            fill_color=ColorValue.from_hex("#FFFFFF"),
            text_color=ColorValue.from_hex("#101010"),
        )
    )
    symbol.set_sheet_name(
        make_sch_sheet_name(
            text="CTRL_CORE",
            location_mils=SchPointMils.from_mils(1100, 5300),
            font=SchFontSpec(name="Arial", size=14, bold=True),
            color=ColorValue.from_hex("#303030"),
            orientation=TextOrientation.DEGREES_0,
            justification=TextJustification.BOTTOM_LEFT,
        )
    )
    symbol.set_file_name(
        make_sch_file_name(
            text="ctrl_core.SchDoc",
            location_mils=SchPointMils.from_mils(1100, 3500),
            font=SchFontSpec(name="Arial", size=10, italic=True),
            color=ColorValue.from_hex("#505050"),
            orientation=TextOrientation.DEGREES_0,
            justification=TextJustification.TOP_LEFT,
        )
    )
    return symbol


def _build_power_symbol() -> AltiumSchSheetSymbol:
    symbol = make_sch_sheet_symbol(
        bounds_mils=SchRectMils.from_corners_mils(3800, 5200, 5900, 3600),
        border_width=LineWidth.MEDIUM,
        border_color=ColorValue.from_hex("#2D2D2D"),
        fill_color=ColorValue.from_hex("#DCEAFF"),
        fill_background=True,
        symbol_type=SchSheetSymbolType.DEVICE_SHEET,
        show_hidden_fields=True,
        design_item_id="POWER_IO",
        source_library_name="generated",
        revision_name="B",
    )
    symbol.add_entry(
        make_sch_sheet_entry(
            name="VIN_12V",
            side=SheetEntrySide.LEFT,
            io_type=SchSheetEntryIOType.INPUT,
            distance_from_top_mils=200,
            font=SchFontSpec(name="Arial", size=10, bold=True),
            border_color=ColorValue.from_hex("#101010"),
            fill_color=ColorValue.from_hex("#FFFFFF"),
            text_color=ColorValue.from_hex("#101010"),
        )
    )
    symbol.add_entry(
        make_sch_sheet_entry(
            name="PWR_GOOD",
            side=SheetEntrySide.RIGHT,
            io_type=SchSheetEntryIOType.OUTPUT,
            distance_from_top_mils=400,
            font=SchFontSpec(name="Arial", size=10),
            border_color=ColorValue.from_hex("#101010"),
            fill_color=ColorValue.from_hex("#FFFFFF"),
            text_color=ColorValue.from_hex("#101010"),
            arrow_kind=SchSheetEntryArrowKind.ARROW,
        )
    )
    symbol.add_entry(
        make_sch_sheet_entry(
            name="I2C_SDA",
            side=SheetEntrySide.RIGHT,
            io_type=SchSheetEntryIOType.BIDIRECTIONAL,
            distance_from_top_mils=700,
            font=SchFontSpec(name="Arial", size=10),
            border_color=ColorValue.from_hex("#101010"),
            fill_color=ColorValue.from_hex("#FFFFFF"),
            text_color=ColorValue.from_hex("#101010"),
        )
    )
    symbol.set_sheet_name(
        make_sch_sheet_name(
            text="POWER_IO",
            location_mils=SchPointMils.from_mils(3900, 5300),
            font=SchFontSpec(name="Arial", size=14, bold=True),
            color=ColorValue.from_hex("#2C4058"),
            orientation=TextOrientation.DEGREES_0,
            justification=TextJustification.BOTTOM_LEFT,
        )
    )
    symbol.set_file_name(
        make_sch_file_name(
            text="power_io.SchDoc",
            location_mils=SchPointMils.from_mils(3900, 3500),
            font=SchFontSpec(name="Arial", size=10, italic=True),
            color=ColorValue.from_hex("#4F647C"),
            orientation=TextOrientation.DEGREES_0,
            justification=TextJustification.TOP_LEFT,
        )
    )
    return symbol


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

    schdoc = AltiumSchDoc(INPUT_SCHDOC)
    schdoc.add_object(_build_ctrl_symbol())
    schdoc.add_object(_build_power_symbol())
    schdoc.save(OUTPUT_SCHDOC)

    reopened = AltiumSchDoc(OUTPUT_SCHDOC)
    print(f"Sheet symbols: {len(reopened.sheet_symbols)}")
    for index, symbol in enumerate(reopened.sheet_symbols, start=1):
        entry_names = [entry.name for entry in symbol.entries]
        symbol_kind = "device-sheet" if symbol.is_device_sheet else "normal"
        sheet_name = symbol.sheet_name.text if symbol.sheet_name is not None else ""
        file_name = symbol.file_name.text if symbol.file_name is not None else ""
        print(
            f"Symbol {index}: {symbol_kind} {sheet_name} -> {file_name} entries={entry_names}"
        )
    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)