On This Page (6)
Examples Showcase / schdoc_note_command
Mutate and delete existing notes in a SchDoc
Open a schematic with existing notes, change one note font in place, delete another note, and save the modified SchDoc.
Open an existing schematic that already contains two notes, mutate one note in place, delete the other, and save the modified .SchDoc.
This example is the first explicit query-then-mutate workflow for schematic objects. It shows that records returned from the document can be edited through their public properties and removed through the document container API.
What It Shows
- loading an existing
SchDoc - iterating existing note objects from
schdoc.notes - changing a note font with
note.font = SchFontSpec(...) - deleting an existing note with
AltiumSchDoc.remove_object(...) - persisting the result with
AltiumSchDoc.save(...) - reopening the written file and checking the surviving notes
Run
From the project root:
uv run python examples\schdoc_note_command\schdoc_note_command.pyInput
This sample uses:
examples/schdoc_note_command/input/note_commands_input.SchDocThe input schematic contains two visible notes:
change my font to courier 12delete me
Output
The script writes:
examples/schdoc_note_command/output/note_commands_output.SchDocExpected Result
The output file should contain exactly one note:
- text:
change my font to courier 12 - font:
Courier New 12
The note with text delete me should be removed from the document.
The script prints the note counts before and after the mutation, the number of updated and removed notes, and the surviving note's resolved font.
Source Code
from __future__ import annotations
from pathlib import Path
from altium_monkey import AltiumSchDoc, SchFontSpec
SAMPLE_DIR = Path(__file__).resolve().parent
INPUT_SCHDOC = SAMPLE_DIR / "input" / "note_commands_input.SchDoc"
OUTPUT_DIR = SAMPLE_DIR / "output"
OUTPUT_SCHDOC = OUTPUT_DIR / "note_commands_output.SchDoc"
FONT_COMMAND_TEXT = "change my font to courier 12"
DELETE_COMMAND_TEXT = "delete me"
def main() -> None:
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
schdoc = AltiumSchDoc(INPUT_SCHDOC)
notes_before = len(schdoc.notes)
updated_count = 0
removed_count = 0
for note in list(schdoc.notes):
normalized_text = note.text.strip().lower()
if normalized_text == FONT_COMMAND_TEXT:
note.font = SchFontSpec(name="Courier New", size=12)
updated_count += 1
elif normalized_text == DELETE_COMMAND_TEXT:
if schdoc.remove_object(note):
removed_count += 1
if updated_count != 1:
raise RuntimeError(
f"Expected to update exactly one note, but updated {updated_count}"
)
if removed_count != 1:
raise RuntimeError(
f"Expected to remove exactly one note, but removed {removed_count}"
)
schdoc.save(OUTPUT_SCHDOC)
reopened = AltiumSchDoc(OUTPUT_SCHDOC)
notes_after = len(reopened.notes)
if notes_after != 1:
raise RuntimeError(f"Expected exactly one note after save, found {notes_after}")
surviving_note = reopened.notes[0]
surviving_font = reopened.font_manager.get_font_info(surviving_note.font_id) or {}
print(f"Loaded: {INPUT_SCHDOC.name}")
print(f"Notes before: {notes_before}")
print(f"Updated notes: {updated_count}")
print(f"Removed notes: {removed_count}")
print(f"Notes after: {notes_after}")
print(f"Surviving note text: {surviving_note.text}")
print(
"Surviving note font: "
f"{surviving_font.get('name', 'Unknown')} {surviving_font.get('size', '?')}"
)
print(f"Wrote: {OUTPUT_SCHDOC.relative_to(SAMPLE_DIR)}")
if __name__ == "__main__":
main()