Creating a routing keepout zone with pcbnew's Python API
Reserve a region where no track, via, or copper fill can be placed, by script — useful around a mechanical connector or an antenna — with pcbnew or KiCad's IPC API.
A keepout zone stops tracks, vias, and copper fill from encroaching on a specific region — typically around a mechanical connector, an antenna, or a board cutout. It’s built just like a regular copper zone, with a few extra prohibition flags.
Creating a rule-area zone
SetIsRuleArea(True) turns the zone into a rule area instead of a filled copper zone, then each flag enables a specific restriction:
from pcbnew import ZONE, F_Cu
zone = ZONE(board)
zone.SetIsRuleArea(True)
zone.SetLayer(F_Cu)
zone.SetDoNotAllowTracks(True)
zone.SetDoNotAllowVias(True)
zone.SetDoNotAllowCopperPour(True)
board.Add(zone)With the IPC API, the same restrictions are declared at creation time:
from kicad import KiCad
kicad = KiCad()
board = kicad.get_board()
zone = board.create_zone(layer="F.Cu", rule_area=True)
zone.keepout_tracks = True
zone.keepout_vias = True
zone.keepout_copper_pour = True
board.update_items(zone)Defining the outline
The outline is built exactly like a regular copper zone:
from pcbnew import SHAPE_POLY_SET, FromMM
outline = SHAPE_POLY_SET()
outline.NewOutline()
for x, y in [(0, 0), (50, 0), (50, 30), (0, 30)]:
outline.Append(FromMM(x), FromMM(y))
zone.SetOutline(outline)Same logic here as for the standard copper zone:
from kicad.geometry import Vector2
points = [(0, 0), (50, 0), (50, 30), (0, 30)]
zone.outline = [Vector2.from_mm(x, y) for x, y in points]
board.update_items(zone)Try it below: adjust width and height to see the keepout zone change on the board.
Going further
The same mechanism protects a mechanical mounting area, keeps an antenna clear of any ground plane, or blocks routing under a connector through several rule areas stacked across the relevant layers.