Auditing and counting a board's vias with pcbnew's Python API
List and count a board's existing vias by net, to spot an excess of vias or check a fabrication constraint — with pcbnew or KiCad's IPC API.
Before sending a board to fabrication, knowing how many vias it contains — and which nets they’re concentrated on — helps spot a missed de-duplication or check a limit imposed by the fab house (some charge extra past a certain via count, or require a minimum diameter depending on count).
Listing vias and counting them by net
GetTracks() returns a mix of tracks and vias; filter on the object’s class:
from collections import Counter
vias = [t for t in board.GetTracks() if t.GetClass() == "PCB_VIA"]
print(f"{len(vias)} vias found")
by_net = Counter(v.GetNetname() for v in vias)
for net, count in by_net.most_common():
print(f"{net}: {count}")Same idea, filtering the board’s items by type:
from collections import Counter
vias = [item for item in board.get_items() if item.type == "via"]
print(f"{len(vias)} vias found")
by_net = Counter(v.net.name for v in vias)
for net, count in by_net.most_common():
print(f"{net}: {count}")Going further
The same filter adapts easily to check a specific rule — for example raising an alert if a net exceeds a via count threshold, or if a via’s diameter is below the fab house’s minimum — directly in a validation script run before export.