from pathlib import Path
import wx,json,math
_app=wx.App(False)
import pcbnew as k
R=Path(__file__).resolve().parent;b=k.LoadBoard(str(R/'board/led_driver.kicad_pcb'));f={x.GetReference():x for x in b.GetFootprints()};rows=[]
for i in range(1,4):
 u=f['U'+str(i)];vdd=next(p for p in u.Pads() if p.GetNumber()=='38');xy=(k.ToMM(vdd.GetPosition().x),k.ToMM(vdd.GetPosition().y))
 for ref in ['C'+str(i*2-1),'C'+str(i*2)]:
  p=next(p for p in f[ref].Pads() if p.GetNetname()=='/VDD_3V3');cx=(k.ToMM(p.GetPosition().x),k.ToMM(p.GetPosition().y));rows.append({'chip':u.GetReference(),'VDD_pad38_xy_mm':xy,'capacitor':ref,'value':f[ref].GetValue(),'supply_pad_xy_mm':cx,'straight_line_distance_mm':math.dist(xy,cx),'physical_loop_qualification':False})
out={'scope':'Native geometry straight-line distance only; not inductance, ESR, transient or EMC qualification','rows':rows,'next_action':'Move high-frequency decoupling close to chip VDD/GND pins and re-route/recheck without relaxing rules','manufacturing':'HOLD'};(R/'decoupling_geometry_audit.json').write_text(json.dumps(out,indent=2));print(json.dumps(rows))
