On This Page (5)
Examples Showcase / pcb_board_stats_and_stackup
Inspect PCB Board Stats, Stackup & Multi-Span Drills
Inspect 4-layer KiCad 10 boards with KiCadPcb and KiCadFabView to report outline size, interior cutouts, copper stackups, drill pairs, via tenting, and counts in JSON.
Inspect two 4-layer KiCad 10 boards (impedance_4layer.kicad_pcb and via_spans_and_tenting.kicad_pcb) using KiCadPcb and KiCadFabView, extracting board outline dimensions, copper stackup layers, pad/track/via/net counts, through/blind/buried drill spans, and via tenting metadata into a clean JSON report.
What It Shows
- Loading boards with
KiCadPcb.from_file()and computingEdge.Cutsbounds viaKiCadPcb.get_bounds() - Opening the analytic fabrication model with
KiCadPcb.fab()andFab.view() - Inspecting stackup rows via
KiCadFabView.stackup()and copper layers viaKiCadFabView.layers() - Reporting plated/non-plated holes, drill-span pairs (
KiCadFabView.drill_pairs()), and via tenting viaKiCadFabView.holes()
Input
examples/assets/projects/impedance_4layer/impedance_4layer.kicad_pcb
examples/assets/projects/via_spans/via_spans_and_tenting.kicad_pcbRun
From the repository root:
uv run python examples/pcb_board_stats_and_stackup/pcb_board_stats_and_stackup.pyOutput
examples/pcb_board_stats_and_stackup/output/board_stats.jsonSource Code
"""Inspect KiCad 10 PCB board stats, stackups, drill spans, and via tenting."""
from __future__ import annotations
import json
from collections import Counter
from pathlib import Path
from kicad_monkey import KiCadPcb
HERE = Path(__file__).resolve().parent
ASSETS = HERE.parent / "assets" / "projects"
BOARDS = [
ASSETS / "impedance_4layer" / "impedance_4layer.kicad_pcb",
ASSETS / "via_spans" / "via_spans_and_tenting.kicad_pcb",
]
OUT_DIR = HERE / "output"
def inspect_board(pcb_path: Path) -> dict[str, object]:
pcb = KiCadPcb.from_file(pcb_path)
view = pcb.fab().view()
profile = view.profile()
if profile is not None:
xs = [pt.x / 1_000_000.0 for pt in profile.outer.points]
ys = [pt.y / 1_000_000.0 for pt in profile.outer.points]
outline_w = round(max(xs) - min(xs), 3)
outline_h = round(max(ys) - min(ys), 3)
cutout_count = len(profile.cutouts)
else:
bounds = pcb.get_bounds(layers=["Edge.Cuts"])
outline_w = round(bounds.width, 3)
outline_h = round(bounds.height, 3)
cutout_count = 0
stackup = view.stackup()
copper_layers = [layer.key for layer in view.layers() if layer.role == "conductor"]
drill_pairs = [
{
"id": dp.id,
"kind": dp.kind,
"span": list(dp.span),
"direction": dp.direction,
}
for dp in view.drill_pairs()
]
holes = view.holes()
hole_roles = Counter(h.role for h in holes)
tented_top = sum(
1
for h in holes
if getattr(h, "via_protection", None) not in (None, "")
and getattr(h.via_protection, "tented_top", False)
)
tented_bottom = sum(
1
for h in holes
if getattr(h, "via_protection", None) not in (None, "")
and getattr(h.via_protection, "tented_bottom", False)
)
stackup_rows = [
{
"name": row.name,
"kind": row.kind,
"layer_key": str(row.layer_key) if str(row.layer_key) != "UNSET" else None,
"thickness_mm": round(row.thickness / 1_000_000.0, 4) if isinstance(row.thickness, (int, float)) else None,
}
for row in stackup.rows
]
return {
"file": pcb_path.name,
"outline_mm": {
"width": outline_w,
"height": outline_h,
"interior_cutouts": cutout_count,
},
"board_thickness_mm": round(stackup.thickness / 1_000_000.0, 3),
"copper_layers": copper_layers,
"stackup_rows": stackup_rows,
"counts": {
"footprints": len(pcb.footprints),
"components_fabview": len(view.components()),
"pads": sum(len(fp.pads) for fp in pcb.footprints),
"segments": len(pcb.segments),
"arcs": len(pcb.arcs),
"vias": len(pcb.vias),
"zones": len(pcb.zones),
"nets": len([n for n in pcb.nets if n.name]),
"holes_total": len(holes),
"holes_by_role": dict(sorted(hole_roles.items())),
"vias_tented_top": tented_top,
"vias_tented_bottom": tented_bottom,
},
"drill_pairs": drill_pairs,
}
def main() -> None:
OUT_DIR.mkdir(parents=True, exist_ok=True)
reports = [inspect_board(path) for path in BOARDS]
out_path = OUT_DIR / "board_stats.json"
out_path.write_text(json.dumps({"boards": reports}, indent=2) + "\n", encoding="utf-8")
for rep in reports:
counts = rep["counts"]
outline = rep["outline_mm"]
print(
f"{rep['file']}: {outline['width']}x{outline['height']} mm, "
f"{len(rep['copper_layers'])} Cu layers, {counts['footprints']} footprints, "
f"{counts['holes_total']} holes across {len(rep['drill_pairs'])} drill pair(s)"
)
print(f"Wrote {out_path.name}")
if __name__ == "__main__":
main()