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.
Output Previews
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
AltiumPcbLib.from_file(...)- iterating
pcblib.footprints footprint.to_svg(...), aFabOutputholdingboard.svgfootprint.to_layer_svgs(...), one<key>.svgper layer key andDRILLS.svg- the SVG options
scale=10.0,layer_colours={"DRILLS": "#00A651"}andannotations=[{"kind": "pad_labels"}, {"kind": "origin_marker"}] - writing a JSON manifest that records generated SVG paths
Run
From the package root:
uv run python examples\pcblib_footprint_svg\pcblib_footprint_svg.pyInput
examples/assets/pcblib/RT_SUPER_C1.PcbLibOutput
examples/pcblib_footprint_svg/output/footprints/
examples/pcblib_footprint_svg/output/layers/
examples/pcblib_footprint_svg/output/pcblib_footprint_svg_manifest.jsonSource Code
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()