kicad monkey v2026.11.30
On This Page (6)

Examples Showcase / pcb_barcodes_and_vias_matrix

Native KiCad 10 Barcodes, Multi-Span Via Drill Pairs & Tenting

Add native QR and DataMatrix barcodes to a 4-layer via-span board, inspect through/blind/buried drill pairs and tenting via KiCadFabView, emit split NC Drill files, and render Paper + Dark mask SVGs.

pcbfabsvgbarcodeqr-codeviatentingdrill-spanssvg

Output Previews

via_barcodes_top_dark.svg
Open SVG
Native KiCad 10 Barcodes, Multi-Span Via Drill Pairs & Tenting
via_mask_openings_dark.svg
Open SVG
Native KiCad 10 Barcodes, Multi-Span Via Drill Pairs & Tenting

Inspect a 4-layer board with through, blind, and buried via spans, add native KiCad 10 QR and DataMatrix barcodes, and emit split per-span Excellon NC Drill files.

The native KiCad 10 (barcode ...) primitives are authored directly into the saved .kicad_pcb, while the FabView SVG previews showcase copper routing alongside solder-mask tenting vs. untented via openings.

What It Shows

  1. Authoring native KiCad 10 QR and DataMatrix Barcode objects on F.SilkS
  2. Inspecting through/blind/buried drill spans via KiCadFabView.drill_pairs() and KiCadFabView.holes()
  3. Emitting split per-span Excellon drill files with Fab.to_nc_drill()
  4. Rendering Paper + Dark copper/barcode and solder-mask opening SVGs with Fab.to_svg()

Input

text
examples/assets/projects/via_spans/via_spans_and_tenting.kicad_pcb

Run

From the repository root:

sh
uv run python examples/pcb_barcodes_and_vias_matrix/pcb_barcodes_and_vias_matrix.py

Output

text
examples/pcb_barcodes_and_vias_matrix/output/via_spans_with_barcodes.kicad_pcb
examples/pcb_barcodes_and_vias_matrix/output/drill/
examples/pcb_barcodes_and_vias_matrix/output/via_barcodes_top.svg
examples/pcb_barcodes_and_vias_matrix/output/via_barcodes_top_dark.svg
examples/pcb_barcodes_and_vias_matrix/output/via_mask_openings.svg
examples/pcb_barcodes_and_vias_matrix/output/via_mask_openings_dark.svg
examples/pcb_barcodes_and_vias_matrix/output/vias_barcodes_report.json

Source Code

examples/pcb_barcodes_and_vias_matrix/pcb_barcodes_and_vias_matrix.py
"""KiCad 10 native Barcodes, multi-span via drill pairs (through/blind/buried), tenting & mask SVGs."""

from __future__ import annotations

import json
import re
from pathlib import Path

from kicad_monkey import Barcode, KiCadPcb

HERE = Path(__file__).resolve().parent
INPUT_PCB = HERE.parent / "assets" / "projects" / "via_spans" / "via_spans_and_tenting.kicad_pcb"
OUT_DIR = HERE / "output"
DRILL_DIR = OUT_DIR / "drill"


def _add_canvas_bg(svg_text: str, color: str) -> str:
    match = re.search(r'viewBox="([^"]+)"', svg_text)
    if not match:
        return svg_text
    x, y, w, h = match.group(1).split()
    bg = f'<rect x="{x}" y="{y}" width="{w}" height="{h}" fill="{color}"/>'
    return svg_text.replace(">\n", f">\n{bg}\n", 1)


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

    # 1. Load the 4-layer via-spans & tenting board
    pcb = KiCadPcb.from_file(INPUT_PCB)
    bounds = pcb.get_bounds(layers=["Edge.Cuts"])

    # 2. Add native KiCad 10 QR and DataMatrix Barcode primitives on top silkscreen
    pcb.barcodes.extend([
        Barcode(
            at_x=bounds.min_x + 24.0,
            at_y=bounds.min_y + 6.0,
            layer="F.SilkS",
            width=7.5,
            height=7.5,
            text="KM-QR-VIA-4L",
            barcode_type="qr",
            ecc_level="M",
            show_text=False,
        ),
        Barcode(
            at_x=bounds.min_x + 24.0,
            at_y=bounds.min_y + 14.5,
            layer="F.SilkS",
            width=6.5,
            height=6.5,
            text="DM-2026-01",
            barcode_type="datamatrix",
            show_text=False,
        ),
    ])

    annotated_pcb_path = OUT_DIR / "via_spans_with_barcodes.kicad_pcb"
    pcb.save(annotated_pcb_path)

    # 3. Inspect drill spans (through, blind, buried) & via tenting via KiCadFabView
    fab = pcb.fab(stem="via_spans_matrix")
    view = fab.view()

    nc_drills = fab.to_nc_drill()
    nc_drills.write(DRILL_DIR, overwrite=True)

    # 4. Render Top Copper + Silkscreen + Drills (Paper + Dark)
    copper_layers = ("PROFILE", "TOP", "TOPOVERLAY", "DRILLS")
    copper_paper = next(
        iter(
            fab.to_svg(
                layers=copper_layers,
                scale=14.0,
                layer_colours={
                    "PROFILE": "#F1F5F9",
                    "TOP": "#C2410C",
                    "TOPOVERLAY": "#0F172A",
                    "DRILLS": "#FFFFFF",
                },
            ).values()
        )
    ).text
    (OUT_DIR / "via_barcodes_top.svg").write_text(_add_canvas_bg(copper_paper, "#FFFFFF"), encoding="utf-8")

    copper_dark = next(
        iter(
            fab.to_svg(
                layers=copper_layers,
                scale=14.0,
                layer_colours={
                    "PROFILE": "#1E293B",
                    "TOP": "#F97316",
                    "TOPOVERLAY": "#F8FAFC",
                    "DRILLS": "#11161D",
                },
            ).values()
        )
    ).text
    (OUT_DIR / "via_barcodes_top_dark.svg").write_text(_add_canvas_bg(copper_dark, "#11161D"), encoding="utf-8")

    # 5. Render Solder Mask Openings comparison (revealing tented vs untented vias) in Paper + Dark
    mask_layers = ("PROFILE", "TOP", "TOPSOLDER", "DRILLS")
    mask_paper = next(
        iter(
            fab.to_svg(
                layers=mask_layers,
                scale=14.0,
                layer_colours={
                    "PROFILE": "#F8FAFC",
                    "TOP": "#CBD5E1",
                    "TOPSOLDER": "#059669",
                    "DRILLS": "#FFFFFF",
                },
            ).values()
        )
    ).text
    (OUT_DIR / "via_mask_openings.svg").write_text(_add_canvas_bg(mask_paper, "#FFFFFF"), encoding="utf-8")

    mask_dark = next(
        iter(
            fab.to_svg(
                layers=mask_layers,
                scale=14.0,
                layer_colours={
                    "PROFILE": "#1E293B",
                    "TOP": "#334155",
                    "TOPSOLDER": "#34D399",
                    "DRILLS": "#11161D",
                },
            ).values()
        )
    ).text
    (OUT_DIR / "via_mask_openings_dark.svg").write_text(_add_canvas_bg(mask_dark, "#11161D"), encoding="utf-8")

    matrix_report = {
        "board": annotated_pcb_path.name,
        "barcodes": [{"type": b.barcode_type, "text": b.text, "layer": b.layer} for b in pcb.barcodes],
        "drill_pairs": [
            {"id": dp.id, "kind": dp.kind, "span": list(dp.span)} for dp in view.drill_pairs()
        ],
        "holes": [
            {
                "id": h.id,
                "drill_pair": h.drill_pair,
                "diameter_mm": round(h.drill.tool / 1_000_000.0, 3) if getattr(h, "drill", None) else None,
                "net": h.net,
            }
            for h in view.holes()
        ],
        "nc_drill_files": sorted(nc_drills.keys()),
    }
    (OUT_DIR / "vias_barcodes_report.json").write_text(json.dumps(matrix_report, indent=2) + "\n", encoding="utf-8")

    print(
        f"Saved {annotated_pcb_path.name}: {len(pcb.barcodes)} barcodes, "
        f"{len(view.drill_pairs())} drill span pairs -> {len(nc_drills)} NC Drill files + 4 Paper/Dark SVGs"
    )


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