main
John Lauer
Preserve board29, component assets and scaffold-fit correction candidate with reviewed footage
c54169e
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
"""Isolated cooling-clearance placement candidate. Native pcbnew runs only on desktop."""
import pcbnew as k, json, shutil
from pathlib import Path
p=Path(__file__).parent
b=k.LoadBoard(str(p/'source.kicad_pcb'))
removed=[]
def mm(v): return k.ToMM(v)
def xy(v): return (mm(v.x),mm(v.y))
# Preserve all existing copper; candidate routes extend to relocated pads.
# Preserve source text except complete copper items selected by native UUID.
import re
raw=(p/'source.kicad_pcb').read_text(); ids={v['uuid'] for v in removed}; edits=[]
for match in re.finditer(r'\((?:segment|via)\s',raw):
start=match.start();depth=0;quoted=False;escaped=False
for i in range(start,len(raw)):
c=raw[i]
if quoted:
if escaped: escaped=False
elif c=='\\': escaped=True
elif c=='"': quoted=False
elif c=='"': quoted=True
elif c=='(': depth+=1
elif c==')':
depth-=1
if depth==0:
block=raw[start:i+1]
if any(uid in block for uid in ids):edits.append((start,i+1))
break
assert len(edits)==len(removed),(len(edits),len(removed))
for start,end in reversed(edits):raw=raw[:start]+raw[end:]
(p/'stripped.kicad_pcb').write_text(raw)
b=k.LoadBoard(str(p/'stripped.kicad_pcb'))
# Existing traces can terminate anywhere inside a pad, not only at its center.
# Preserve those junctions explicitly before moving the exposed probe pads.
added=[]
for ref in ['TP4','TP8','J9']:
f=b.FindFootprintByReference(ref)
for pad in f.Pads():
center=xy(pad.GetPosition());net=pad.GetNetname();radius=mm(pad.GetSize().x)/2
if ref=='J9':
v=k.PCB_VIA(b);v.SetPosition(pad.GetPosition());v.SetWidth(k.FromMM(.6));v.SetDrill(k.FromMM(.3));v.SetLayerPair(k.F_Cu,k.B_Cu);v.SetNetCode(pad.GetNetCode());b.Add(v);added.append({'net':net,'viaAt':center})
for t in list(b.GetTracks()):
if isinstance(t,k.PCB_VIA)or t.GetNetname()!=net:continue
if ref!='J9'and t.GetLayer()!=k.F_Cu:continue
for pos in [t.GetStart(),t.GetEnd()]:
a=xy(pos);dist=((a[0]-center[0])**2+(a[1]-center[1])**2)**.5
if 1e-6<dist<=radius+mm(t.GetWidth())/2:
q=k.PCB_TRACK(b);q.SetStart(pos);q.SetEnd(pad.GetPosition());q.SetWidth(min(t.GetWidth(),k.FromMM(.25)));q.SetLayer(t.GetLayer());q.SetNetCode(pad.GetNetCode());b.Add(q);added.append({'net':net,'start':a,'end':center,'layer':b.GetLayerName(t.GetLayer())})
moves=[('TP4',100.8,77.2,0),('TP8',100.8,80,0),('J9',154.6,98,-90)]
for ref,x,y,rot in moves:
f=b.FindFootprintByReference(ref);f.SetOrientationDegrees(rot);f.SetPosition(k.VECTOR2I(k.FromMM(x),k.FromMM(y)));f.Reference().SetPosition(k.VECTOR2I(k.FromMM(x),k.FromMM(y-2)))
for ref in ['TP4','TP8']:
f=b.FindFootprintByReference(ref);f.Reference().SetPosition(k.VECTOR2I(k.FromMM(99),f.GetPosition().y));f.Reference().SetTextAngle(k.EDA_ANGLE(90,k.DEGREES_T))
f=b.FindFootprintByReference('J9');f.Reference().SetPosition(k.VECTOR2I(k.FromMM(151.5),k.FromMM(100.5)));f.Reference().SetTextAngle(k.EDA_ANGLE(0,k.DEGREES_T))
for ref,x,y in [('D4',157,94),('TP6',103,81.3)]:
f=b.FindFootprintByReference(ref);f.Reference().SetPosition(k.VECTOR2I(k.FromMM(x),k.FromMM(y)));f.Reference().SetTextAngle(k.EDA_ANGLE(0,k.DEGREES_T))
k.ZONE_FILLER(b).Fill(b.Zones());k.SaveBoard(str(p/'cooling-clearance-filled.kicad_pcb'),b)
for z in b.Zones():z.UnFill()
k.SaveBoard(str(p/'cooling-clearance.kicad_pcb'),b)
for ext in ['kicad_pro','kicad_dru']:shutil.copy2(p/('source.'+ext),p/('cooling-clearance.'+ext))
(p/'cooling-clearance-changes.json').write_text(json.dumps({'moves':moves,'removed':removed,'junctions':added,'status':'isolated unaccepted candidate; rerouting and mechanical review required'},indent=2))