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

Examples Showcase / hello_schlib

Create a SchLib hello example

Create a simple 4-pin IC symbol in a SchLib to demonstrate the basic mechanics of programmatic symbol generation.

schlibhello-worldgenerationlibrarysymbol

Create a schematic library from scratch and author a tiny fictitious 6-pin IC symbol.

This example is intentionally small. It is meant to show the public AltiumSchLib and make_sch_pin(...) APIs, not internal library-stream details.

What It Shows

  1. AltiumSchLib.add_symbol(...)
  2. AltiumSymbol.set_description(...)
  3. make_sch_pin(...)
  4. SchPointMils.from_mils(...)
  5. AltiumSymbol.add_pin(...)
  6. AltiumSymbol.add_rectangle(...)
  7. centering a 1000 x 600 mil symbol body around (0, 0)
  8. setting pin-name and pin-designator text to Arial 10 with SchFontSpec
  9. rotating custom VCC/GND pin-name and pin-designator text with PinTextRotation
  10. keeping pin lengths and pin hotspots on the 100 mil schematic grid
  11. AltiumSymbol.add_designator(...)
  12. AltiumSymbol.add_parameter(...)
  13. AltiumSchLib(show_comments_designators=True) so Altium's SchLib editor shows the symbol comment and designator by default
  14. AltiumSchLib.save(...)

Run

From the repo root:

powershell
uv run python examples\hello_schlib\hello_schlib.py

Output

The script writes:

text
examples/hello_schlib/output/hello_schlib.SchLib

Source Code

examples/hello_schlib/hello_schlib.py
from pathlib import Path

from altium_monkey import (
    AltiumSchLib,
    PinElectrical,
    PinTextRotation,
    Rotation90,
    SchFontSpec,
    SchPointMils,
    make_sch_pin,
)

SAMPLE_DIR = Path(__file__).resolve().parent
OUTPUT_PATH = SAMPLE_DIR / "output" / "hello_schlib.SchLib"
PIN_TEXT_FONT = SchFontSpec(name="Arial", size=10)
BODY_LEFT_MILS = -500
BODY_RIGHT_MILS = 500
BODY_TOP_MILS = 300
BODY_BOTTOM_MILS = -300
PIN_LENGTH_MILS = 200


def _is_100_mil_grid(value_mils: float) -> bool:
    return abs((value_mils / 100.0) - round(value_mils / 100.0)) < 1e-9


def _assert_pin_grid(symbol: object) -> None:
    for pin in getattr(symbol, "pins", []):
        hot_spot = pin.get_hot_spot()
        for field_name, value_mils in (
            ("x", pin.x_mils),
            ("y", pin.y_mils),
            ("length", pin.length_mils),
            ("hot spot x", hot_spot.x_mils),
            ("hot spot y", hot_spot.y_mils),
        ):
            if not _is_100_mil_grid(float(value_mils)):
                raise RuntimeError(
                    f"Pin {pin.designator} {field_name} is off-grid: {value_mils} mil"
                )


def build_schlib(output_path: Path = OUTPUT_PATH) -> Path:
    output_path.parent.mkdir(parents=True, exist_ok=True)

    schlib = AltiumSchLib(show_comments_designators=True)
    symbol = schlib.add_symbol("HELLO_IC_6PIN")
    symbol.set_description("Hello world 6-pin example")
    symbol.add_rectangle(
        BODY_LEFT_MILS, BODY_BOTTOM_MILS, BODY_RIGHT_MILS, BODY_TOP_MILS
    )
    symbol.add_pin(
        make_sch_pin(
            designator="1",
            name="IN_A",
            location_mils=SchPointMils.from_mils(BODY_LEFT_MILS, 100),
            orientation=Rotation90.DEG_180,
            length_mils=PIN_LENGTH_MILS,
            electrical_type=PinElectrical.PASSIVE,
            name_font=PIN_TEXT_FONT,
            designator_font=PIN_TEXT_FONT,
        )
    )
    symbol.add_pin(
        make_sch_pin(
            designator="2",
            name="IN_B",
            location_mils=SchPointMils.from_mils(BODY_LEFT_MILS, -100),
            orientation=Rotation90.DEG_180,
            length_mils=PIN_LENGTH_MILS,
            electrical_type=PinElectrical.PASSIVE,
            name_font=PIN_TEXT_FONT,
            designator_font=PIN_TEXT_FONT,
        )
    )
    symbol.add_pin(
        make_sch_pin(
            designator="3",
            name="OUT_A",
            location_mils=SchPointMils.from_mils(BODY_RIGHT_MILS, 100),
            orientation=Rotation90.DEG_0,
            length_mils=PIN_LENGTH_MILS,
            electrical_type=PinElectrical.PASSIVE,
            name_font=PIN_TEXT_FONT,
            designator_font=PIN_TEXT_FONT,
        )
    )
    symbol.add_pin(
        make_sch_pin(
            designator="4",
            name="OUT_B",
            location_mils=SchPointMils.from_mils(BODY_RIGHT_MILS, -100),
            orientation=Rotation90.DEG_0,
            length_mils=PIN_LENGTH_MILS,
            electrical_type=PinElectrical.PASSIVE,
            name_font=PIN_TEXT_FONT,
            designator_font=PIN_TEXT_FONT,
        )
    )
    symbol.add_pin(
        make_sch_pin(
            designator="5",
            name="VCC",
            location_mils=SchPointMils.from_mils(0, BODY_TOP_MILS),
            orientation=Rotation90.DEG_90,
            length_mils=PIN_LENGTH_MILS,
            electrical_type=PinElectrical.POWER,
            name_font=PIN_TEXT_FONT,
            name_rotation=PinTextRotation.VERTICAL,
            designator_font=PIN_TEXT_FONT,
            designator_rotation=PinTextRotation.VERTICAL,
        )
    )
    symbol.add_pin(
        make_sch_pin(
            designator="6",
            name="GND",
            location_mils=SchPointMils.from_mils(0, BODY_BOTTOM_MILS),
            orientation=Rotation90.DEG_270,
            length_mils=PIN_LENGTH_MILS,
            electrical_type=PinElectrical.POWER,
            name_font=PIN_TEXT_FONT,
            name_rotation=PinTextRotation.VERTICAL,
            designator_font=PIN_TEXT_FONT,
            designator_rotation=PinTextRotation.VERTICAL,
        )
    )
    symbol.add_designator("U?", 0, 520)
    symbol.add_parameter("Comment", "Hello 6-pin IC", x=0, y=-520)
    _assert_pin_grid(symbol)
    schlib.save(output_path)
    return output_path


def main() -> None:
    output_path = build_schlib()
    print(f"Wrote {output_path}")


if __name__ == "__main__":
    main()
altium-monkey v2026.11.26 · gobblerito·Docs updated 2026.11.29 17:55 UTC (0665053)