mohan-bee/stm32-midi-keys
Clips rectangular ground-region geometry to fit within the board’s scaled rounded-edge outline using polygon intersection.
- Version
- 1.0.2
- License
- unset
- Stars
- 0
scripts/kicad_pad_shapes.py
"""Run with KiCad's bundled Python; preserves custom pad unions and copper offsets."""
import pcbnew, json
from pathlib import Path
board=pcbnew.LoadBoard('source/stm32_midi_keys.kicad_pcb')
records=[]
for f in board.GetFootprints():
for p in f.Pads():
offset=p.GetOffset()
if p.GetShape()!=pcbnew.PAD_SHAPE_CUSTOM and not (offset.x or offset.y): continue
pos=p.ShapePos(pcbnew.F_Cu)
r={'component':f.GetReference(),'pin':p.GetNumber(),'center':{'x':pcbnew.ToMM(pos.x)-125,'y':70-pcbnew.ToMM(pos.y)}}
if p.GetShape()==pcbnew.PAD_SHAPE_CUSTOM:
poly=p.GetEffectivePolygon(pcbnew.F_Cu)
assert poly.OutlineCount()==1
outline=poly.COutline(0)
r['points']=[{'x':pcbnew.ToMM(outline.CPoint(i).x)-125,'y':70-pcbnew.ToMM(outline.CPoint(i).y)} for i in range(outline.PointCount())]
records.append(r)
Path('source/pad-shapes.json').write_text(json.dumps(records,indent=2))
print(f'Extracted {len(records)} offset/custom pad geometries')