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

Examples Showcase / schdoc_add_line

Add line segments to an existing SchDoc

Load a blank schematic, create detached plain line segments, add them to the document, and save the modified SchDoc.

schdocmutationschdocmutationlinedrawing-tools

Open a blank schematic, add a small gallery of plain line segments, and save the modified .SchDoc.

This example focuses on the native schematic Line object:

  1. load an existing SchDoc
  2. create several detached line objects with make_sch_line(...)
  3. insert each object with AltiumSchDoc.add_object(...)
  4. persist the change with AltiumSchDoc.save(...)

What It Shows

  1. using the public mil-facing API for absolute line start/end coordinates
  2. creating detached lines with different LineWidth values
  3. creating detached lines with different LineStyle values
  4. creating detached lines with different lengths, slopes, and colors
  5. reopening the written file and inspecting the created line objects

Important Note

Altium schematic endpoint decorations such as arrows, tails, circles, and squares belong to the separate Polyline object, not the plain Line object. See schdoc_add_polyline.

Run

From the project root:

powershell
uv run python examples\schdoc_add_line\schdoc_add_line.py

Input

This sample uses:

text
examples/schdoc_add_line/input/blank.SchDoc

Output

The script writes:

text
examples/schdoc_add_line/output/blank_with_lines.SchDoc

Expected Result

The output file should contain:

  1. a top row of black solid lines showing SMALLEST, SMALL, MEDIUM, and LARGE line widths
  2. a middle row of black SOLID, DASHED, DOTTED, and DASH_DOT lines
  3. a bottom row of lines with different lengths, slopes, colors, widths, and styles

The script also prints the reopened start/end points, widths, styles, and colors so the example is easy to sanity-check without inspecting the binary file directly.

Source Code

examples/schdoc_add_line/schdoc_add_line.py
from __future__ import annotations

from pathlib import Path

from altium_monkey import (
    AltiumSchDoc,
    ColorValue,
    LineStyle,
    LineWidth,
    SchPointMils,
    make_sch_line,
)


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


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

    schdoc = AltiumSchDoc(INPUT_SCHDOC)

    # Row 1: line-width variations with the same solid style.
    row1_y = 5900
    for x1, x2, line_width in (
        (1100, 2500, LineWidth.SMALLEST),
        (3000, 4400, LineWidth.SMALL),
        (4900, 6300, LineWidth.MEDIUM),
        (6800, 8200, LineWidth.LARGE),
    ):
        schdoc.add_object(
            make_sch_line(
                start_mils=SchPointMils.from_mils(x1, row1_y),
                end_mils=SchPointMils.from_mils(x2, row1_y),
                color=ColorValue.from_hex("#000000"),
                line_width=line_width,
                line_style=LineStyle.SOLID,
            )
        )

    # Row 2: line-style variations at a consistent width.
    row2_y = 3900
    for x1, x2, line_style in (
        (1100, 2500, LineStyle.SOLID),
        (3000, 4400, LineStyle.DASHED),
        (4900, 6300, LineStyle.DOTTED),
        (6800, 8200, LineStyle.DASH_DOT),
    ):
        schdoc.add_object(
            make_sch_line(
                start_mils=SchPointMils.from_mils(x1, row2_y),
                end_mils=SchPointMils.from_mils(x2, row2_y),
                color=ColorValue.from_hex("#000000"),
                line_width=LineWidth.MEDIUM,
                line_style=line_style,
            )
        )

    # Row 3: different lengths, slopes, and colors.
    for start, end, color_hex, line_width, line_style in (
        (
            SchPointMils.from_mils(1200, 1800),
            SchPointMils.from_mils(2800, 2400),
            "#000000",
            LineWidth.SMALL,
            LineStyle.SOLID,
        ),
        (
            SchPointMils.from_mils(3200, 1700),
            SchPointMils.from_mils(5200, 1100),
            "#1F77B4",
            LineWidth.MEDIUM,
            LineStyle.DASHED,
        ),
        (
            SchPointMils.from_mils(5600, 1200),
            SchPointMils.from_mils(7000, 2200),
            "#D62728",
            LineWidth.LARGE,
            LineStyle.DOTTED,
        ),
        (
            SchPointMils.from_mils(7400, 1700),
            SchPointMils.from_mils(8400, 1700),
            "#2CA02C",
            LineWidth.SMALL,
            LineStyle.DASH_DOT,
        ),
    ):
        schdoc.add_object(
            make_sch_line(
                start_mils=start,
                end_mils=end,
                color=ColorValue.from_hex(color_hex),
                line_width=line_width,
                line_style=line_style,
            )
        )

    schdoc.save(OUTPUT_SCHDOC)

    reopened = AltiumSchDoc(OUTPUT_SCHDOC)
    print(f"Loaded: {INPUT_SCHDOC.name}")
    print(f"Lines written: {len(reopened.lines)}")
    for index, line in enumerate(reopened.lines, start=1):
        print(
            f"Line {index}: "
            f"start=({line.location_mils.x_mils:.0f}, {line.location_mils.y_mils:.0f}), "
            f"end=({line.corner_mils.x_mils:.0f}, {line.corner_mils.y_mils:.0f}), "
            f"width={line.line_width.name}, "
            f"style={line.line_style.name}, "
            f"color={ColorValue.from_win32(int(line.color or 0)).hex}"
        )
    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)