from pathlib import Path
import pcbnew,wx,json
app=wx.App(False);quiet=wx.LogNull();root=Path(__file__).resolve().parent;d=root/'cad';stem='vacuum_control_evt_a5_logic_fet';info=json.loads((root/'changes.json').read_text());b=pcbnew.LoadBoard(str(root/'baseline'/(stem+'.kicad_pcb')));nets={n.GetNetname():n for n in b.GetNetsByNetcode().values()}
for ref,pos,net in [('R18',(122,85),'/PANEL_SCL'),('R19',(126,85),'/PANEL_SDA')]:
 f=pcbnew.FootprintLoad('/Applications/KiCad/KiCad.app/Contents/SharedSupport/footprints/Resistor_SMD.pretty','R_0603_1608Metric');f.SetReference(ref);f.SetValue('4.7k');f.SetFPID(pcbnew.LIB_ID('Resistor_SMD','R_0603_1608Metric'));f.SetPosition(pcbnew.VECTOR2I(*(pcbnew.FromMM(x) for x in pos)))
 path=pcbnew.KIID_PATH();path.push_back(pcbnew.KIID(info['sheet_uuid']));path.push_back(pcbnew.KIID(info['instance'][ref]));f.SetPath(path);b.Add(f)
 for pad in f.Pads():pad.SetNet(nets['/CTRL_3V3' if pad.GetNumber()=='1' else net])
 b.BuildConnectivity()
pcbnew.SaveBoard(str(d/(stem+'.kicad_pcb')),b)
