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

Examples Showcase / pcblib_footprint_svg

Render PcbLib footprints to SVG

Load an existing PcbLib, iterate its footprints, and write composed plus per-layer SVG files for each footprint.

pcblibsvglibraryfootprintsvgrenderinglayers

Output Previews

001_SOIC127P830X200-8L.svg
Open SVG
001_SOIC127P830X200-8L.svg
002_R0402_0.40MM_HD.svg
Open SVG
002_R0402_0.40MM_HD.svg
003_W25Q_XSON_8.svg
Open SVG
003_W25Q_XSON_8.svg
004_MACHINE_PIN_SOLDERHOLE.svg
Open SVG
004_MACHINE_PIN_SOLDERHOLE.svg
005_TC2030_NO_LATCH.svg
Open SVG
005_TC2030_NO_LATCH.svg
006_SOT23-5.svg
Open SVG
006_SOT23-5.svg
007_XTAL_3215.svg
Open SVG
007_XTAL_3215.svg
008_C0402_0.55MM_HD.svg
Open SVG
008_C0402_0.55MM_HD.svg
009_LED_0603_-_RED.svg
Open SVG
009_LED_0603_-_RED.svg
010_LED_0603_-_GREEN.svg
Open SVG
010_LED_0603_-_GREEN.svg

Load an existing PcbLib, iterate its footprints, and render footprint-local SVG files.

This sample uses the RT Super C1 PcbLib asset because it contains many footprint shapes. It writes one composed SVG for each footprint and one SVG per layer key of the footprint's Fab view that draws anything. The output is useful for quick visual review of pad, overlay, solder-mask, paste-mask, and drill geometry without placing the footprint on a board first. Each image labels the pads with their designators and marks the footprint origin.

What It Shows

  1. AltiumPcbLib.from_file(...)
  2. iterating pcblib.footprints
  3. footprint.to_svg(...), a FabOutput holding board.svg
  4. footprint.to_layer_svgs(...), one <key>.svg per layer key and DRILLS.svg
  5. the SVG options scale=10.0, layer_colours={"DRILLS": "#00A651"} and annotations=[{"kind": "pad_labels"}, {"kind": "origin_marker"}]
  6. writing a JSON manifest that records generated SVG paths

Run

From the package root:

powershell
uv run python examples\pcblib_footprint_svg\pcblib_footprint_svg.py

Input

text
examples/assets/pcblib/RT_SUPER_C1.PcbLib

Output

text
examples/pcblib_footprint_svg/output/footprints/
examples/pcblib_footprint_svg/output/layers/
examples/pcblib_footprint_svg/output/pcblib_footprint_svg_manifest.json

Source Code

examples/pcblib_footprint_svg/pcblib_footprint_svg.py
from __future__ import annotations

import json
import re
import shutil
from pathlib import Path

from altium_monkey import AltiumPcbLib


SAMPLE_DIR = Path(__file__).resolve().parent
EXAMPLES_DIR = SAMPLE_DIR.parent
ASSETS_DIR = EXAMPLES_DIR / "assets"
INPUT_PCBLIB = ASSETS_DIR / "pcblib" / "RT_SUPER_C1.PcbLib"
OUTPUT_DIR = SAMPLE_DIR / "output"
FOOTPRINT_SVG_DIR = OUTPUT_DIR / "footprints"
LAYER_SVG_DIR = OUTPUT_DIR / "layers"
OUTPUT_MANIFEST = OUTPUT_DIR / "pcblib_footprint_svg_manifest.json"
SCALE_FACTOR = 10.0


def _example_relative(path: Path) -> str:
    return str(path.relative_to(EXAMPLES_DIR)).replace("\\", "/")


def _sample_relative(path: Path) -> str:
    return str(path.relative_to(SAMPLE_DIR)).replace("\\", "/")


def _safe_filename_part(value: str) -> str:
    safe = re.sub(r"[^A-Za-z0-9_.-]+", "_", value.strip())
    return safe.strip("._") or "footprint"


def _footprint_svg_stem(index: int, footprint_name: str) -> str:
    return f"{index:03d}_{_safe_filename_part(footprint_name)}"


def _reset_output_dirs() -> None:
    if OUTPUT_DIR.exists():
        shutil.rmtree(OUTPUT_DIR)
    FOOTPRINT_SVG_DIR.mkdir(parents=True, exist_ok=True)
    LAYER_SVG_DIR.mkdir(parents=True, exist_ok=True)


# The SVG options every image takes: ten times life size, the holes green,
# each pad labelled with its designator and the footprint origin marked.
SVG_OPTIONS = {
    "scale": SCALE_FACTOR,
    "layer_colours": {"DRILLS": "#00A651"},
    "annotations": [{"kind": "pad_labels"}, {"kind": "origin_marker"}],
}


def _render_footprint_svgs(pcblib: AltiumPcbLib) -> list[dict[str, object]]:
    footprint_entries: list[dict[str, object]] = []

    for footprint_index, footprint in enumerate(pcblib.footprints, start=1):
        safe_name = _footprint_svg_stem(footprint_index, footprint.name)
        composed_svg_path = FOOTPRINT_SVG_DIR / f"{safe_name}.svg"
        composed_svg_path.write_bytes(footprint.to_svg(**SVG_OPTIONS)["board.svg"].data)

        layer_entries: list[dict[str, object]] = []
        for image in footprint.to_layer_svgs(**SVG_OPTIONS).values():
            if not image.count:
                continue
            layer_svg_path = LAYER_SVG_DIR / f"{safe_name}_{image.name}"
            layer_svg_path.write_bytes(image.data)
            layer_entries.append(
                {
                    "layer": image.key or "DRILLS",
                    "svg": _sample_relative(layer_svg_path),
                    "byte_count": layer_svg_path.stat().st_size,
                }
            )

        footprint_entries.append(
            {
                "index": footprint_index,
                "name": footprint.name,
                "pads": len(footprint.pads),
                "tracks": len(footprint.tracks),
                "arcs": len(footprint.arcs),
                "texts": len(footprint.texts),
                "composed_svg": _sample_relative(composed_svg_path),
                "composed_byte_count": composed_svg_path.stat().st_size,
                "layers": layer_entries,
            }
        )

    return footprint_entries


def main() -> None:
    _reset_output_dirs()

    pcblib = AltiumPcbLib.from_file(INPUT_PCBLIB)
    if not pcblib.footprints:
        raise RuntimeError(f"No footprints found in {INPUT_PCBLIB.name}")

    footprint_entries = _render_footprint_svgs(pcblib)
    svg_count = len(footprint_entries) + sum(
        len(entry["layers"]) for entry in footprint_entries
    )

    manifest = {
        "input_pcblib": _example_relative(INPUT_PCBLIB),
        "scale_factor": SCALE_FACTOR,
        "footprint_count": len(footprint_entries),
        "svg_count": svg_count,
        "footprints": footprint_entries,
    }
    OUTPUT_MANIFEST.write_text(
        json.dumps(manifest, indent=2) + "\n",
        encoding="utf-8",
    )

    print(f"Input PcbLib: {_example_relative(INPUT_PCBLIB)}")
    print(f"Footprints rendered: {manifest['footprint_count']}")
    print(f"SVG files written: {manifest['svg_count']}")
    print(f"Wrote manifest: {_sample_relative(OUTPUT_MANIFEST)}")


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