kicad monkey v2026.11.30
On This Page (6)

Examples Showcase / hello_symbol_lib

Hello World: Author a Custom Symbol Library & Render SVGs

Author a custom 3-pin LDO LibSymbol inside KiCadSymbolLib, merge symbols from a staged .kicad_sym library, save the combined library, and render symbol SVG previews.

symbolsvghello-worldlibrarysvg

Output Previews

symbol_DUAL_GATE_unit1.svg
Open SVG
Hello World: Author a Custom Symbol Library & Render SVGs
symbol_DUAL_GATE_unit2.svg
Open SVG
Hello World: Author a Custom Symbol Library & Render SVGs
symbol_GND_Local.svg
Open SVG
Hello World: Author a Custom Symbol Library & Render SVGs
symbol_LDO_3V3.svg
Open SVG
Hello World: Author a Custom Symbol Library & Render SVGs

Author a custom 3-pin LDO regulator LibSymbol programmatically inside a KiCadSymbolLib, merge existing symbols from a staged .kicad_sym library, save the combined library, and render SVG previews for each symbol.

What It Shows

  1. Constructing a LibSymbol with LibSubSymbol, SymRectangle, SymPin, and SymProperty
  2. Loading an existing .kicad_sym file with KiCadSymbolLib.from_file()
  3. Merging symbols via KiCadSymbolLib.add_symbol() and saving with KiCadSymbolLib.save()
  4. Rendering per-symbol SVG previews via KiCadSymbolLib.to_svg()

Input

text
examples/assets/symbols/sample_parts.kicad_sym

Run

From the repository root:

sh
uv run python examples/hello_symbol_lib/hello_symbol_lib.py

Output

text
examples/hello_symbol_lib/output/hello_symbols.kicad_sym
examples/hello_symbol_lib/output/symbol_LDO_3V3.svg

Source Code

examples/hello_symbol_lib/hello_symbol_lib.py
"""Hello World: author a custom KiCad 10 symbol library and render symbol SVGs."""

from __future__ import annotations

from pathlib import Path

from kicad_monkey import (
    Effects,
    Font,
    KiCadSymbolLib,
    LibSubSymbol,
    LibSymbol,
    PinElectricalType,
    PinGraphicStyle,
    Stroke,
    SymPin,
    SymProperty,
    SymRectangle,
)

HERE = Path(__file__).resolve().parent
SAMPLE_LIB = HERE.parent / "assets" / "symbols" / "sample_parts.kicad_sym"
OUT_DIR = HERE / "output"


def _make_ldo_symbol() -> LibSymbol:
    """Create a 3-pin LDO voltage regulator symbol from scratch."""
    body = LibSubSymbol(
        name="LDO_3V3_0_1",
        unit=0,
        style=1,
        rectangles=[
            SymRectangle(-5.08, 3.81, 5.08, -3.81, stroke=Stroke(width=0.254)),
        ],
    )
    pins = LibSubSymbol(
        name="LDO_3V3_1_1",
        unit=1,
        style=1,
        pins=[
            SymPin(
                PinElectricalType.POWER_IN,
                PinGraphicStyle.LINE,
                at_x=-7.62,
                at_y=1.27,
                at_angle=0.0,
                length=2.54,
                name="VIN",
                number="1",
            ),
            SymPin(
                PinElectricalType.POWER_IN,
                PinGraphicStyle.LINE,
                at_x=0.0,
                at_y=-6.35,
                at_angle=90.0,
                length=2.54,
                name="GND",
                number="2",
            ),
            SymPin(
                PinElectricalType.POWER_OUT,
                PinGraphicStyle.LINE,
                at_x=7.62,
                at_y=1.27,
                at_angle=180.0,
                length=2.54,
                name="VOUT",
                number="3",
            ),
        ],
    )
    return LibSymbol(
        name="LDO_3V3",
        properties=[
            SymProperty("Reference", "U", id=0, at_x=0.0, at_y=5.5, effects=Effects(font=Font(size_x=1.27, size_y=1.27))),
            SymProperty("Value", "LDO_3V3", id=1, at_x=0.0, at_y=-8.2, effects=Effects(font=Font(size_x=1.27, size_y=1.27))),
            SymProperty("Footprint", "Package_TO_SOT_SMD:SOT-23", id=2, hide=True),
            SymProperty("Description", "3.3V Low-Dropout Linear Regulator", id=3, hide=True),
        ],
        subsymbols=[body, pins],
    )


def main() -> None:
    OUT_DIR.mkdir(parents=True, exist_ok=True)

    # 1. Start a fresh KiCadSymbolLib and add our custom LDO symbol
    lib = KiCadSymbolLib()
    lib.add_symbol(_make_ldo_symbol())

    # 2. Load existing symbols from the staged sample library and merge them in
    sample = KiCadSymbolLib.from_file(SAMPLE_LIB)
    for sym in sample.symbols:
        lib.add_symbol(sym)

    # 3. Save the combined .kicad_sym library
    lib_path = OUT_DIR / "hello_symbols.kicad_sym"
    lib.save(lib_path)

    # 4. Render SVG previews for every symbol in the library
    rendered = lib.to_svg()
    svg_count = 0
    for sym_name, unit_map in rendered.items():
        safe = sym_name.replace("/", "_").replace(":", "_")
        for unit_idx, svg_text in unit_map.items():
            suffix = "" if len(unit_map) == 1 else f"_unit{unit_idx}"
            (OUT_DIR / f"symbol_{safe}{suffix}.svg").write_text(svg_text, encoding="utf-8")
            svg_count += 1

    print(
        f"Saved {lib_path.name} with {len(lib.symbols)} symbols "
        f"({', '.join(s.name for s in lib.symbols)}) and rendered {svg_count} SVGs"
    )


if __name__ == "__main__":
    main()
kicad-monkey v2026.11.30·Docs updated 2026.12.01 15:43 UTC (fd9b75b)