How to extract courtyard polygon using python

Using python, I am trying to extract all the points in the courtyard, for a component.

board = pcbnew.LoadBoard(filename)
for fp in board.Footprints():
    crtyd = fp.GetCourtyard(F_Courtyard) # returns pcbnew.SHAPE_POLY_SET

How can I get all the points from the “crtyd” variable ? I wasn’t able to find a solution, so I dug into the kicad source code. I found one method that gives me access to the points in a round-about way

shapeText = crtyd.Format(True)
print(shapeText)

This gives me:

SHAPE_LINE_CHAIN poly;
{ auto tmp = SHAPE_LINE_CHAIN( { VECTOR2I( 61799999, 57160001), VECTOR2I( 61799999, 63309999), VECTOR2I( 58200001, 63309999), VECTOR2I( 58200001, 57160001)}, true );;
poly.AddOutline(tmp); }

I can extract individual VECTOR2I values from this string, but I think I am missing something in the abstractions. Is there a more “pthonic” way to do this ? Any hints would be most appreciated.

Right now, I am using this hack to get the points. I am worried it might not work for all courtyard shapes:

import re
verts = []
for s_pt in re.findall('VECTOR2I\\( [0-9]+, [0-9]+\\)',shapeText):
    coord_parts = s_pt.split(' ')[1:]
    x = int(coord_parts[0][:-1])
    y = int(coord_parts[1][:-1])
    verts.append([x,y]) # YES, and yikes!
pprint(verts)

The same POLY_SHAPE_SET shows up while parsing edges of a board, in the Edge.Cuts layer. So I am hopeful of finding a clean method. Ideally, I don’t want to parse the s-expressions in the actual board file.

See if Plugin howtos · Wiki · eelik-kicad / KiCad Documentation · GitLab helps.

1 Like

You can also check out real example here InteractiveHtmlBom/InteractiveHtmlBom/ecad/kicad.py at 5915a249b14bd91986c6062b551b18dc9de394e0 · openscopeproject/InteractiveHtmlBom · GitHub

1 Like

Yes, that was exactly what I needed. Works for me. Thank you very much!