Trying to write Python Script for creating Zone Cutout

In a python script for KiCad version 7, I’m trying to create a fill around my whole PCB and then make a zone cutout that uncovers the board. I can’t figure out how to make the cutout.

It’s a follow up related to the question I posted here:

that describes why I might want to do this.

I’m using a slightly modified version of the script from here
Filled zone (copper area) via Python script bindings - #5 by franzee
to create the fill, but using 0,0 and 298000000, 211000000 as my corners.

import pcbnew
board = pcbnew.GetBoard()
points =(pcbnew.VECTOR2I(0,0),pcbnew.VECTOR2I(0,211000000),pcbnew.VECTOR2I(298000000,211000000),pcbnew.VECTOR2I(298000000,0))
z = pcbnew.ZONE(board)
z.SetLayer(pcbnew.B_Cu)
z.AddPolygon( pcbnew.VECTOR_VECTOR2I(points) )
board.Add(z)
filler = pcbnew.ZONE_FILLER(board)
filler.Fill(board.Zones())
pcbnew.Refresh()

Before I make this fill I can use board.ComputeBoundingBox() to find the PCB outline, but I can’t figure out how to use this result to make the cutout.

Guidance from anyone would be appreciated.

Are you having trouble getting coordinates out of the bounding box or getting trouble creating cutout? If it’s the latter then look at Outline() of the zone that returns SHAPE_POLY_SET that has hole related methods.

Maybe Plugin howtos · Wiki · eelik-kicad / KiCad Documentation · GitLab helps. It’s been a while, I don’t remember the details, but at least you can create a polygon with a hole by drawing it manually and then inspect it in the python console. Then you can reconstruct it with new polygonal shapes.

Thanks for the reply.

I think I have a solution. It wasn’t the coordinates of the corners, but understanding how zones work. I didn’t understand that once you define a zone you can then create a hole with NewHole() that will have an index (probably 0 for the first hole). By setting corners using that index, you get a cutout.

Thanks again for taking the time to reply.

Thanks for the reference. It was helpful in understanding how zones work.

I found this solution to work. It is not exactly what I want to do but it is a prototype. Assuming that the index of the hole is 0 might be a problem for more general implementation

import pcbnew
board = pcbnew.GetBoard()
zone = pcbnew.ZONE(board)
zone.SetLayer(pcbnew.F_Cu)
#outline of zone
corner1 = pcbnew.VECTOR2I(pcbnew.FromMM(120), pcbnew.FromMM(120))
corner2 = pcbnew.VECTOR2I(pcbnew.FromMM(130), pcbnew.FromMM(120))
corner3 = pcbnew.VECTOR2I(pcbnew.FromMM(130), pcbnew.FromMM(130))
corner4 = pcbnew.VECTOR2I(pcbnew.FromMM(120), pcbnew.FromMM(130))
for c in [corner1, corner2, corner3, corner4]: zone.AppendCorner(c, -1)

#make a hole and define the corners
zone.NewHole()
corner1 = pcbnew.VECTOR2I(pcbnew.FromMM(122), pcbnew.FromMM(122))
corner2 = pcbnew.VECTOR2I(pcbnew.FromMM(128), pcbnew.FromMM(122))
corner3 = pcbnew.VECTOR2I(pcbnew.FromMM(128), pcbnew.FromMM(128))
corner4 = pcbnew.VECTOR2I(pcbnew.FromMM(122), pcbnew.FromMM(128))
for c in [corner1, corner2, corner3, corner4]: zone.AppendCorner(c, 0)

#add to the board
board.Add(zone)
pcbnew.Refresh()

filler = pcbnew.ZONE_FILLER(board)
filler.Fill(board.Zones())
pcbnew.Refresh()

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.