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

Examples Showcase / schdoc_add_text_frame

Add text frames to an existing SchDoc

Load a blank schematic, create several detached text frames with different alignments, border widths, and fonts, add them to the document, and save the modified SchDoc.

schdocmutationschdocmutationtext-framefontgeometry

Open a blank schematic, add several text frames, and save the modified .SchDoc.

This example is the text-frame companion to schdoc_add_note. It shows the canonical public create-then-insert path for the standalone text-frame object:

  1. load an existing SchDoc
  2. describe absolute frame rectangles in mils
  3. call make_sch_text_frame(...)
  4. insert each object with AltiumSchDoc.add_object(...)
  5. persist the change with AltiumSchDoc.save(...)

What It Shows

  1. creating detached text frames with make_sch_text_frame(...)
  2. using SchRectMils for absolute placement
  3. using SchFontSpec instead of raw font_id
  4. using SchHorizontalAlign instead of raw alignment integers
  5. using LineWidth for border thickness
  6. using ColorValue helpers instead of raw Win32 color integers
  7. reopening the written file and inspecting the created text frames

Run

From the project root:

powershell
uv run python examples\schdoc_add_text_frame\schdoc_add_text_frame.py

Input

This sample uses:

text
examples/schdoc_add_text_frame/input/blank.SchDoc

Output

The script writes:

text
examples/schdoc_add_text_frame/output/blank_with_text_frames.SchDoc

Expected Result

The output file should contain three visible text frames:

  1. left aligned, LineWidth.SMALL, Arial 10
  2. center aligned, LineWidth.MEDIUM, Courier New 12
  3. right aligned, LineWidth.LARGE, Times New Roman 14

Each frame uses a different absolute rectangle so it is easy to verify the placement visually in Altium. The script also prints the reopened frame count, alignment, line width, font, and bounds for a quick terminal sanity check.

Source Code

examples/schdoc_add_text_frame/schdoc_add_text_frame.py
from __future__ import annotations

from pathlib import Path

from altium_monkey import (
    AltiumSchDoc,
    ColorValue,
    LineWidth,
    SchFontSpec,
    SchHorizontalAlign,
    SchRectMils,
    make_sch_text_frame,
)


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


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

    schdoc = AltiumSchDoc(INPUT_SCHDOC)
    frame_specs = [
        {
            "bounds_mils": SchRectMils.from_corners_mils(800, 5800, 3200, 7000),
            "text": "Left aligned\nSmall border\nArial 10",
            "font": SchFontSpec(name="Arial", size=10),
            "border_color": ColorValue.from_hex("#000000"),
            "fill_color": ColorValue.from_hex("#FFF4CC"),
            "text_color": ColorValue.from_hex("#000000"),
            "alignment": SchHorizontalAlign.LEFT,
            "line_width": LineWidth.SMALL,
        },
        {
            "bounds_mils": SchRectMils.from_corners_mils(3400, 5800, 5800, 7000),
            "text": "Center aligned\nMedium border\nCourier New 12",
            "font": SchFontSpec(name="Courier New", size=12),
            "border_color": ColorValue.from_hex("#000000"),
            "fill_color": ColorValue.from_hex("#E8F7EC"),
            "text_color": ColorValue.from_hex("#102040"),
            "alignment": SchHorizontalAlign.CENTER,
            "line_width": LineWidth.MEDIUM,
        },
        {
            "bounds_mils": SchRectMils.from_corners_mils(6000, 5800, 8400, 7000),
            "text": "Right aligned\nLarge border\nTimes New Roman 14",
            "font": SchFontSpec(name="Times New Roman", size=14),
            "border_color": ColorValue.from_hex("#000000"),
            "fill_color": ColorValue.from_hex("#E8F0FF"),
            "text_color": ColorValue.from_hex("#330000"),
            "alignment": SchHorizontalAlign.RIGHT,
            "line_width": LineWidth.LARGE,
        },
    ]

    for spec in frame_specs:
        frame = make_sch_text_frame(
            bounds_mils=spec["bounds_mils"],
            text=spec["text"],
            font=spec["font"],
            border_color=spec["border_color"],
            fill_color=spec["fill_color"],
            text_color=spec["text_color"],
            alignment=spec["alignment"],
            line_width=spec["line_width"],
            word_wrap=True,
            clip_to_rect=True,
            text_margin_mils=5,
            show_border=True,
            fill_background=True,
        )
        schdoc.add_object(frame)

    schdoc.save(OUTPUT_SCHDOC)

    reopened = AltiumSchDoc(OUTPUT_SCHDOC)
    print(f"Loaded: {INPUT_SCHDOC.name}")
    print(f"Text frames written: {len(reopened.text_frames)}")
    for index, frame in enumerate(reopened.text_frames, start=1):
        font_info = reopened.font_manager.get_font_info(frame.font_id)
        alignment_name = SchHorizontalAlign(int(frame.alignment)).name
        print(
            f"Frame {index}: {alignment_name}, {frame.line_width.name}, "
            f"{font_info['name']} {font_info['size']}, "
            f"bounds=({frame.bounds_mils.x1_mils:.0f}, {frame.bounds_mils.y1_mils:.0f})"
            f" -> ({frame.bounds_mils.x2_mils:.0f}, {frame.bounds_mils.y2_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)