On This Page (6)
Examples Showcase / schdoc_mutate_sheet_symbol
Mutate an existing sheet symbol group in a SchDoc
Open a schematic with one existing sheet symbol group, rename and reorder entries, replace the name and file labels, add and remove entries, and save the modified SchDoc.
Open a schematic with one existing sheet-symbol group, mutate it through the live record API, and save the modified .SchDoc.
This example demonstrates the attached-record mutation path after a document has already been parsed:
- load an existing
SchDoc - get the parsed
sheet_symbolfromschdoc.sheet_symbols - find existing entries with
symbol.get_entry(...) - rename, move, add, and remove entries through the live symbol API
- replace the sheet-name and file-name labels with new detached label records
- persist the result with
AltiumSchDoc.save(...)
What It Shows
- mutating entry text and font in place on already-attached records
- changing public entry offsets through
distance_from_top_mils - reordering entries with
move_entry(...) - removing an existing entry and adding a new one
- replacing the symbol-owned sheet-name and file-name labels
- reopening the written file and printing the resulting group summary
Run
From the project root:
uv run python examples\schdoc_mutate_sheet_symbol\schdoc_mutate_sheet_symbol.pyInput
This sample uses:
examples/schdoc_mutate_sheet_symbol/input/sheet_symbol_input.SchDocOutput
The script writes:
examples/schdoc_mutate_sheet_symbol/output/sheet_symbol_output.SchDocExpected Result
The output file should contain one updated sheet symbol with:
- renamed entries
FAULT_N,CLK_SYNC, andI2C_SDA - the removed
DBG_TXentry no longer present - a replacement sheet name of
CTRL_MUTATED - a replacement file name of
ctrl_mutated.SchDoc
This example intentionally uses the live parsed record objects. Once a sheet symbol is attached to a document, its entry list and owned labels stay grouped with the parent automatically when you save.
Source Code
from __future__ import annotations
from pathlib import Path
from altium_monkey import (
AltiumSchDoc,
ColorValue,
SchFontSpec,
SchPointMils,
SchSheetEntryIOType,
SheetEntrySide,
TextJustification,
TextOrientation,
make_sch_file_name,
make_sch_sheet_entry,
make_sch_sheet_name,
)
SAMPLE_DIR = Path(__file__).resolve().parent
INPUT_SCHDOC = SAMPLE_DIR / "input" / "sheet_symbol_input.SchDoc"
OUTPUT_DIR = SAMPLE_DIR / "output"
OUTPUT_SCHDOC = OUTPUT_DIR / "sheet_symbol_output.SchDoc"
def main() -> None:
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
schdoc = AltiumSchDoc(INPUT_SCHDOC)
sheet_symbol = schdoc.sheet_symbols[0]
clk_main = sheet_symbol.get_entry("CLK_MAIN")
rst_n = sheet_symbol.get_entry("RST_N")
dbg_tx = sheet_symbol.get_entry("DBG_TX")
if clk_main is None or rst_n is None or dbg_tx is None:
raise RuntimeError("Input sample is missing one or more expected sheet entries")
clk_main.name = "CLK_SYNC"
clk_main.font = SchFontSpec(name="Arial", size=12, bold=True)
rst_n.name = "FAULT_N"
rst_n.distance_from_top_mils = 500
rst_n.font = SchFontSpec(name="Arial", size=10, bold=True)
sheet_symbol.move_entry("FAULT_N", index=0)
if not sheet_symbol.remove_entry(dbg_tx):
raise RuntimeError("Expected to remove DBG_TX from the input symbol")
sheet_symbol.add_entry(
make_sch_sheet_entry(
name="I2C_SDA",
side=SheetEntrySide.RIGHT,
io_type=SchSheetEntryIOType.BIDIRECTIONAL,
distance_from_top_mils=900,
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"),
)
)
sheet_symbol.set_sheet_name(
make_sch_sheet_name(
text="CTRL_MUTATED",
location_mils=SchPointMils.from_mils(1700, 5100),
font=SchFontSpec(name="Arial", size=16, bold=True),
color=ColorValue.from_hex("#20374F"),
orientation=TextOrientation.DEGREES_0,
justification=TextJustification.BOTTOM_LEFT,
)
)
sheet_symbol.set_file_name(
make_sch_file_name(
text="ctrl_mutated.SchDoc",
location_mils=SchPointMils.from_mils(1700, 3300),
font=SchFontSpec(name="Arial", size=10, italic=True),
color=ColorValue.from_hex("#40576D"),
orientation=TextOrientation.DEGREES_0,
justification=TextJustification.TOP_LEFT,
)
)
schdoc.save(OUTPUT_SCHDOC)
reopened = AltiumSchDoc(OUTPUT_SCHDOC)
reopened_symbol = reopened.sheet_symbols[0]
print(f"Sheet symbols: {len(reopened.sheet_symbols)}")
print(
"Entries: "
f"{[(entry.name, entry.distance_from_top_mils) for entry in reopened_symbol.entries]}"
)
print(
"Sheet name: "
f"{reopened_symbol.sheet_name.text if reopened_symbol.sheet_name is not None else ''}"
)
print(
"File name: "
f"{reopened_symbol.file_name.text if reopened_symbol.file_name is not None else ''}"
)
print(f"Wrote: {OUTPUT_SCHDOC.relative_to(SAMPLE_DIR)}")
if __name__ == "__main__":
main()