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),('GND',136.5,93.0),('GND',139.4,89.2)]:
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 list(board.GetTracks()):
uid=str(item.m_Uuid.AsString())
if uid in ['91eed079-74e4-4863-8dc1-f211a19b6ee0','ad353074-a567-44a9-a20a-744b8057c5a9']:
board.Remove(item);changes.append({'kind':'remove-track','uuid':uid})
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})
def track(net,layer,a,b,width):
t=pcbnew.PCB_TRACK(board);t.SetStart(pcbnew.VECTOR2I(pcbnew.FromMM(a[0]),pcbnew.FromMM(a[1])));t.SetEnd(pcbnew.VECTOR2I(pcbnew.FromMM(b[0]),pcbnew.FromMM(b[1])));t.SetWidth(pcbnew.FromMM(width));t.SetLayer(layer);t.SetNet(nets[net]);board.Add(t);changes.append({'kind':'track','net':net,'start':a,'end':b,'widthMm':width,'layer':board.GetLayerName(layer)})
for a,b in [((127.46,82),(127.46,83.3)),((127.46,83.3),(126.4,84.36)),((126.4,84.36),(126.4,86.5))]:track('LOAD_POS',pcbnew.F_Cu,a,b,1.5)
for a,b in [((139.4,90.4),(139.4,92)),((139.4,92),(138.4,93)),((138.4,93),(136.5,93))]:track('GND',pcbnew.B_Cu,a,b,1.5)
for item in board.GetTracks():
if isinstance(item,pcbnew.PCB_VIA) and item.GetNetname()=='GND':
p=item.GetPosition();xy=(round(pcbnew.ToMM(p.x),3),round(pcbnew.ToMM(p.y),3))
if xy in [(139.4,86.8),(136.5,93.0),(139.4,90.4)]:item.SetWidth(pcbnew.FromMM(1));item.SetDrill(pcbnew.FromMM(.6));changes.append({'kind':'resize-via','at':xy,'sizeMm':1,'drillMm':.6})
for fp in board.GetFootprints():
delta={'Q2':-.3,'U3':.3,'R7':.1}.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)
expected_nets={str(item.m_Uuid.AsString()):item.GetNetname() for item in board.GetTracks()}
board.BuildConnectivity()
actual_nets={str(item.m_Uuid.AsString()):item.GetNetname() for item in board.GetTracks()}
assert actual_nets==expected_nets, {'unexpectedNetChanges':[(k,v,actual_nets.get(k)) for k,v in expected_nets.items() if actual_nets.get(k)!=v]}
for zone in board.Zones():zone.SetNeedRefill(True)
pcbnew.ZONE_FILLER(board).Fill(board.Zones());assert {str(item.m_Uuid.AsString()):item.GetNetname() for item in board.GetTracks()}==expected_nets, 'Zone fill changed copper nets'
pcbnew.SaveBoard(str(out),board)
readback=pcbnew.LoadBoard(str(out))
assert {str(item.m_Uuid.AsString()):item.GetNetname() for item in readback.GetTracks()}==expected_nets, 'Save/readback changed copper nets'
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}))