main
John Lauer Preserve board29, component assets and scaffold-fit correction candidate with reviewed footage c54169e
"""Run using native desktop KiCad Python; modifies only an isolated board copy."""
import pcbnew,sys,json
from pathlib import Path
source=Path(sys.argv[1]);out=Path(sys.argv[2]);board=pcbnew.LoadBoard(str(source));nets=board.GetNetsByName()
changes=[]
for net,x,y in [('GND',133.0,100.8),('GND',138.0,100.8),('GND',139.0,93.0)]:
 via=pcbnew.PCB_VIA(board);via.SetPosition(pcbnew.VECTOR2I(pcbnew.FromMM(x),pcbnew.FromMM(y)));via.SetWidth(pcbnew.FromMM(.8));via.SetDrill(pcbnew.FromMM(.4));via.SetLayerPair(pcbnew.F_Cu,pcbnew.B_Cu);via.SetNet(nets[net]);board.Add(via);changes.append({'kind':'via','net':net,'at':[x,y],'sizeMm':.8,'drillMm':.4,'uuid':str(via.m_Uuid.AsString())})
for item in board.GetTracks():
 uid=str(item.m_Uuid.AsString())
 if uid=='32514162-b870-4c7a-95a2-b85b8db79257':
  item.SetWidth(pcbnew.FromMM(1.0));item.SetDrill(pcbnew.FromMM(.6));changes.append({'kind':'resize-via','uuid':uid,'net':'LOAD_POS','sizeMm':1.0,'drillMm':.6})
for item in board.GetTracks():
 uid=str(item.m_Uuid.AsString())
 if uid=='91eed079-74e4-4863-8dc1-f211a19b6ee0':
  item.SetEnd(pcbnew.VECTOR2I(pcbnew.FromMM(126.4),pcbnew.FromMM(84.3)));changes.append({'kind':'shorten-track','uuid':uid,'endMm':[126.4,84.3]})
  t=pcbnew.PCB_TRACK(board);t.SetStart(pcbnew.VECTOR2I(pcbnew.FromMM(126.4),pcbnew.FromMM(84.3)));t.SetEnd(pcbnew.VECTOR2I(pcbnew.FromMM(126.4),pcbnew.FromMM(86.5)));t.SetWidth(pcbnew.FromMM(1.5));t.SetLayer(pcbnew.F_Cu);t.SetNet(nets['LOAD_POS']);board.Add(t);changes.append({'kind':'wide-approach','start':[126.4,84.3],'end':[126.4,86.5],'widthMm':1.5})
 if uid=='aa6c5964-74d4-410e-a69e-ffb2cd9c9999':
  item.SetWidth(pcbnew.FromMM(1.0));item.SetDrill(pcbnew.FromMM(.6));changes.append({'kind':'resize-via','uuid':uid,'sizeMm':1.0,'drillMm':.6})
for fp in board.GetFootprints():
 delta={'Q2':-.3,'U3':.3}.get(fp.GetReference())
 if delta:
  pos=fp.GetPosition();pos.x+=pcbnew.FromMM(delta);fp.SetPosition(pos);changes.append({'kind':'move-footprint','ref':fp.GetReference(),'dxMm':delta})
for item in list(board.GetTracks()):
 if item.GetNetname()=='SHUNT_P_SENSE' and isinstance(item,pcbnew.PCB_VIA):
  pos=item.GetPosition()
  if abs(pcbnew.ToMM(pos.x)-123.6)<.001 and abs(pcbnew.ToMM(pos.y)-85.4)<.001:
   pos.x=pcbnew.FromMM(123.3);item.SetPosition(pos);changes.append({'kind':'move-sense-via','from':[123.6,85.4],'to':[123.3,85.4]})
 if isinstance(item,pcbnew.PCB_VIA):continue
 if item.GetNetname()=='SHUNT_P_SENSE':
  for getter,setter in [(item.GetStart,item.SetStart),(item.GetEnd,item.SetEnd)]:
   pos=getter()
   if abs(pcbnew.ToMM(pos.x)-123.6)<.001 and abs(pcbnew.ToMM(pos.y)-85.4)<.001:pos.x=pcbnew.FromMM(123.3);setter(pos)
 if item.GetNetname()=='SHUNT_N_SENSE' and item.GetLayer()==pcbnew.F_Cu:
  for getter,setter in [(item.GetStart,item.SetStart),(item.GetEnd,item.SetEnd)]:
   pos=getter();x,y=pcbnew.ToMM(pos.x),pcbnew.ToMM(pos.y)
   if 128.79<x<129.31 and 87.29<y<88.81:pos.x+=pcbnew.FromMM(.2);setter(pos)
pcbnew.ZONE_FILLER(board).Fill(board.Zones());pcbnew.SaveBoard(str(out),board)
out.with_suffix('.changes.json').write_text(json.dumps({'source':str(source),'candidate':str(out),'changes':changes},indent=2));print(json.dumps({'saved':str(out),'changes':changes}))