Creating a via with pcbnew's Python API

Published on September 8, 2026 by leZ

Add, position, and size a through-hole via on a PCB by scripting it, with pcbnew or KiCad's IPC API.

A stitching-via grid around a ground plane, or a row of thermal vias under a footprint: as soon as you need more than a handful of identical vias, placing them by hand gets slow and imprecise. Both pcbnew and the IPC API let you create these vias by script, with exact control over position, pad diameter, and drill size.

Creating the via

Instantiate a PCB_VIA object attached to the board, then add it:

from pcbnew import PCB_VIA

via = PCB_VIA(board)
board.Add(via)

With the IPC API, the board exposes a creation method directly:

from kicad import KiCad

kicad = KiCad()
board = kicad.get_board()
via = board.create_via()

Positioning and sizing the via

Position, pad diameter, and drill size are set separately, in internal units (nanometers), converted from millimeters with FromMM:

from pcbnew import VECTOR2I, FromMM

via.SetPosition(VECTOR2I(FromMM(40), FromMM(25)))
via.SetWidth(FromMM(0.6))
via.SetDrill(FromMM(0.3))

The IPC API works directly in millimeters, with no conversion:

from kicad.geometry import Vector2

via.position = Vector2.from_mm(40, 25)
via.pad_diameter = 0.6
via.drill_diameter = 0.3
board.update_items(via)

Try it below: drag the sliders to see how position, pad diameter, and drill size affect the via on the board.

via.SetPosition(VECTOR2I(FromMM(40), FromMM(25)))
via.SetWidth(FromMM(0.6))
via.SetDrill(FromMM(0.3))
40
25
0.6
0.3

Connecting the via to a net

The via needs to be attached to an existing net (typically GND for a stitching via):

via.SetNet(board.FindNet("GND"))

Same idea on the IPC API side, with an explicit sync back to KiCad:

via.net = board.get_net("GND")
board.update_items(via)

Going further

Placing a single via is rarely the actual goal. The real value of scripting is generating an entire stitching-via grid around a ground plane, or a row of thermal vias under a power footprint, in a few lines of loop rather than by hand.