Filled zone (copper area) via Python script API

Hi,
I’m looking for documentation on how to create copper zones programmatically. I typically refer to the API docs, is that the latest and greatest?

Searching the forum I get results from back in 2015 pointing to (at the time) hacky solutions and I was hoping for something more actual.

I tried some variations of the following with no success, can you guys point me in the right direction?

fs = 10e6

sps = pcbnew.SHAPE_POLY_SET(self.board)
sps.SetVertex(fs,fs)
sps.SetVertex(-fs,fs)
sps.SetVertex(-fs,-fs)
sps.SetVertex(fs,-fs)
sps.SetVertex(fs,fs)

z = pcbnew.PCB_ZONE(self.board)
z.SetLayer(pcbnew.F_Cu)
z.SetFilledPolyList( pcbnew.F_Cu, sps)

self.board.Add(z)

cheers,
stef

I think you need to use AddPolygon() (or possibly SetOutline() ) instead of SetFilledPolyList() for defining the zone outline.

Hi @Jonathan_Haas ,
thanks for the quick reply. I’ve tried what you suggested, both methods alone and also together, passing the same SHAPE_POLY_SET as before.

Same result, nothing shows up in the layout unfortunately

I’m not sure you’re creating the SHAPE_POLY_SET correctly. I believe you have to call Append(…) to add points.

But at the moment I don’t have time to mess around with this. I can create an example this weekend maybe.

There are many different ways but I’m using AddPolygon(). To deal with differences of 5.1 and 6.0, I once create an unconnected kind of prototype zonedefaults object and use that with a copy constructor. It might have changed in 6.99 again, though…

2 Likes

Thank you guys,
with the input from @franzee (awesome code snippets man!), and fixing a bug of mine, I got it to work (see below). Other approaches might work as well but I didn’t test them.

fs = 10e6

points = (
    pcbnew.wxPoint(fs,fs),
    pcbnew.wxPoint(-fs,fs),
    pcbnew.wxPoint(-fs,-fs),
    pcbnew.wxPoint(fs,-fs)
)
z = pcbnew.ZONE(self.board)
z.SetLayer(pcbnew.F_Cu)
z.AddPolygon( pcbnew.wxPoint_Vector(points) )
z.SetIsFilled(True)
self.board.Add(z)

# fill board
filler = pcbnew.ZONE_FILLER(self.board)
filler.Fill(self.board.Zones())

pcbnew.Refresh()

cheers
stef

2 Likes