On This Page (6)
Examples Showcase / schdoc_add_wire_and_net_label
Add wires and net labels to an existing SchDoc
Load a blank schematic, create one detached wire path plus one matching detached net label, add them to the document, and save the modified SchDoc.
Open a blank schematic, add one wire plus one matching net label, and save the modified .SchDoc.
This example keeps the related connectivity objects together because net labels are normally attached to wires:
- load an existing
SchDoc - describe the wire path with
SchPointMils - call
make_sch_wire(...) - call
make_sch_net_label(...) - insert every object with
AltiumSchDoc.add_object(...) - persist the change with
AltiumSchDoc.save(...)
What It Shows
- creating a detached wire path with
make_sch_wire(...) - creating a detached net label with
make_sch_net_label(...) - using
SchPointMilsfor all public connectivity coordinates - using
LineWidthfor wire thickness - using
SchFontSpec,TextOrientation, andTextJustificationfor label formatting - using
ColorValuehelpers instead of raw Win32 color integers - reopening the written file and inspecting the created objects
Run
From the project root:
uv run python examples\schdoc_add_wire_and_net_label\schdoc_add_wire_and_net_label.pyInput
This sample uses:
examples/schdoc_add_wire_and_net_label/input/blank.SchDocOutput
The script writes:
examples/schdoc_add_wire_and_net_label/output/blank_with_wires_and_net_labels.SchDocExpected Result
The output file should contain:
- one visible horizontal wire named
CLK_MAIN - one visible net label using
TextJustification.BOTTOM_LEFT - the common horizontal, non-mirrored net-label orientation pattern
The script also prints the reopened wire point list plus the net label's text, orientation, justification, and location 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,
LineWidth,
SchFontSpec,
SchPointMils,
TextJustification,
TextOrientation,
make_sch_net_label,
make_sch_wire,
)
SAMPLE_DIR = Path(__file__).resolve().parent
INPUT_SCHDOC = SAMPLE_DIR / "input" / "blank.SchDoc"
OUTPUT_DIR = SAMPLE_DIR / "output"
OUTPUT_SCHDOC = OUTPUT_DIR / "blank_with_wires_and_net_labels.SchDoc"
def main() -> None:
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
schdoc = AltiumSchDoc(INPUT_SCHDOC)
schdoc.add_object(
make_sch_wire(
points_mils=[
SchPointMils.from_mils(1500, 6500),
SchPointMils.from_mils(3600, 6500),
],
color=ColorValue.from_hex("#000080"),
line_width=LineWidth.SMALL,
)
)
schdoc.add_object(
make_sch_net_label(
location_mils=SchPointMils.from_mils(2550, 6500),
text="CLK_MAIN",
font=SchFontSpec(name="Arial", size=10),
color=ColorValue.from_hex("#000080"),
orientation=TextOrientation.DEGREES_0,
justification=TextJustification.BOTTOM_LEFT,
mirrored=False,
)
)
schdoc.save(OUTPUT_SCHDOC)
reopened = AltiumSchDoc(OUTPUT_SCHDOC)
print(f"Loaded: {INPUT_SCHDOC.name}")
print(f"Wires written: {len(reopened.wires)}")
print(f"Net labels written: {len(reopened.net_labels)}")
for index, wire in enumerate(reopened.wires, start=1):
points_text = ", ".join(
f"({point.x_mils:.0f}, {point.y_mils:.0f})" for point in wire.points_mils
)
print(f"Wire {index}: width={wire.line_width.name}, points={points_text}")
for index, label in enumerate(reopened.net_labels, start=1):
font = label.font
font_text = "unresolved"
if font is not None:
font_text = f"{font.name} {font.size}"
print(
f"Net label {index}: {label.text!r}, "
f"{label.orientation.name}, "
f"{label.justification.name}, "
f"mirrored={label.is_mirrored}, "
f"font={font_text}, "
f"location=({label.location_mils.x_mils:.0f}, {label.location_mils.y_mils:.0f})"
)
print(f"Wrote: {OUTPUT_SCHDOC.relative_to(SAMPLE_DIR)}")
if __name__ == "__main__":
main()