kicad monkey v2026.11.30
On This Page (5)

Examples Showcase / hello_pcb

Hello World: Create a KiCad 10 PCB & Render SVGs

Create a KiCad 10 board from scratch with an Edge.Cuts outline, silkscreen text, copper tracks, vias, and a native QR barcode, then render Paper and Dark SVG previews.

pcbsvghello-worldauthoringsvgbarcode

Output Previews

hello_pcb_dark.svg
Open SVG
Hello World: Create a KiCad 10 PCB & Render SVGs

Author a minimal KiCad 10 PCB from scratch with no external project files, then render Paper and Dark SVG previews through KiCadPcb.fab().

The script writes a native KiCad 10 (barcode ...) primitive directly into hello_pcb.kicad_pcb for KiCad 10, while FabView's analytic SVG preview renders the board outline, copper tracks and vias, silkscreen text, and drills.

What It Shows

  1. Creating a fresh board with KiCadPcb.new() and declaring a Net / NetRef
  2. Drawing a rectangular Edge.Cuts board outline using GrLine and Stroke
  3. Placing top silkscreen GrText and a native KiCad 10 QR Barcode
  4. Routing copper Segment tracks and through-hole Via primitives
  5. Saving with KiCadPcb.save() and rendering Paper + Dark SVGs via Fab.to_svg()

Run

From the repository root:

sh
uv run python examples/hello_pcb/hello_pcb.py

Output

text
examples/hello_pcb/output/hello_pcb.kicad_pcb
examples/hello_pcb/output/hello_pcb.svg
examples/hello_pcb/output/hello_pcb_dark.svg

Source Code

examples/hello_pcb/hello_pcb.py
"""Hello World: create a KiCad 10 PCB from scratch and render Paper + Dark SVGs."""

from __future__ import annotations

import re
from pathlib import Path

from kicad_monkey import (
    Barcode,
    Effects,
    Font,
    GrLine,
    GrText,
    KiCadPcb,
    Net,
    NetRef,
    Segment,
    Stroke,
    Via,
)

HERE = Path(__file__).resolve().parent
OUT_DIR = HERE / "output"


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)

    # 1. Start a fresh KiCad 10 board and declare a signal net
    pcb = KiCadPcb.new(paper="A4")
    pcb.nets = [Net(0, ""), Net(1, "HELLO_SIG")]
    sig = NetRef(1, "HELLO_SIG")

    # 2. Add a 50 x 30 mm rectangular board outline on Edge.Cuts
    corners = [(0.0, 0.0), (50.0, 0.0), (50.0, 30.0), (0.0, 30.0)]
    for i in range(4):
        ax, ay = corners[i]
        bx, by = corners[(i + 1) % 4]
        pcb.gr_lines.append(GrLine(ax, ay, bx, by, layer="Edge.Cuts", stroke=Stroke(width=0.15)))

    # 3. Add top silkscreen text and a native KiCad 10 QR barcode
    pcb.gr_texts.append(
        GrText(
            "kicad-monkey hello",
            at_x=25.0,
            at_y=6.5,
            layer="F.SilkS",
            effects=Effects(font=Font(size_x=1.8, size_y=1.8, thickness=0.25)),
        )
    )
    pcb.barcodes.append(
        Barcode(
            at_x=38.0,
            at_y=19.0,
            layer="F.SilkS",
            width=12.0,
            height=12.0,
            text="KM-HELLO-001",
            barcode_type="qr",
            ecc_level="M",
            show_text=False,
        )
    )

    # 4. Route a copper track with two vias on F.Cu / B.Cu
    pcb.segments.append(Segment(8.0, 16.0, 22.0, 16.0, width=0.5, layer="F.Cu", net=sig))
    pcb.segments.append(Segment(22.0, 16.0, 26.0, 22.0, width=0.5, layer="F.Cu", net=sig))
    pcb.vias.append(Via(8.0, 16.0, size=1.4, drill=0.7, layers=["F.Cu", "B.Cu"], net=sig))
    pcb.vias.append(Via(26.0, 22.0, size=1.4, drill=0.7, layers=["F.Cu", "B.Cu"], net=sig))

    # 5. Save native .kicad_pcb and render Paper + Dark previews via KiCadFabView
    pcb_path = OUT_DIR / "hello_pcb.kicad_pcb"
    pcb.save(pcb_path)

    fab = pcb.fab(stem="hello_pcb")
    layers = ("PROFILE", "TOP", "TOPOVERLAY", "DRILLS")

    paper_svg = next(
        iter(
            fab.to_svg(
                layers=layers,
                scale=8.0,
                layer_colours={
                    "PROFILE": "#F1F5F9",
                    "TOP": "#B91C1C",
                    "TOPOVERLAY": "#0F172A",
                    "DRILLS": "#FFFFFF",
                },
            ).values()
        )
    ).text
    (OUT_DIR / "hello_pcb.svg").write_text(_add_canvas_bg(paper_svg, "#FFFFFF"), encoding="utf-8")

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

    print(
        f"Created {pcb_path.name}: {len(pcb.gr_lines)} outline edges, "
        f"{len(pcb.segments)} tracks, {len(pcb.vias)} vias, {len(pcb.barcodes)} QR barcode"
    )


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