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.
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:
- load an existing
SchDoc - create several detached line objects with
make_sch_line(...) - insert each object with
AltiumSchDoc.add_object(...) - persist the change with
AltiumSchDoc.save(...)
What It Shows
- using the public mil-facing API for absolute line start/end coordinates
- creating detached lines with different
LineWidthvalues - creating detached lines with different
LineStylevalues - creating detached lines with different lengths, slopes, and colors
- 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:
uv run python examples\schdoc_add_line\schdoc_add_line.pyInput
This sample uses:
examples/schdoc_add_line/input/blank.SchDocOutput
The script writes:
examples/schdoc_add_line/output/blank_with_lines.SchDocExpected Result
The output file should contain:
- a top row of black solid lines showing
SMALLEST,SMALL,MEDIUM, andLARGEline widths - a middle row of black
SOLID,DASHED,DOTTED, andDASH_DOTlines - 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
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()