altium monkey v2026.11.26 · gobblerito
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.

schdocmutationschdocmutationnotesdeletefont

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

  1. loading an existing SchDoc
  2. iterating existing note objects from schdoc.notes
  3. changing a note font with note.font = SchFontSpec(...)
  4. deleting an existing note with AltiumSchDoc.remove_object(...)
  5. persisting the result with AltiumSchDoc.save(...)
  6. reopening the written file and checking the surviving notes

Run

From the project root:

powershell
uv run python examples\schdoc_note_command\schdoc_note_command.py

Input

This sample uses:

text
examples/schdoc_note_command/input/note_commands_input.SchDoc

The input schematic contains two visible notes:

  1. change my font to courier 12
  2. delete me

Output

The script writes:

text
examples/schdoc_note_command/output/note_commands_output.SchDoc

Expected Result

The output file should contain exactly one note:

  1. text: change my font to courier 12
  2. 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

examples/schdoc_note_command/schdoc_note_command.py
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()
altium-monkey v2026.11.26 · gobblerito·Docs updated 2026.11.29 17:55 UTC (0665053)