main
John Lauer Preserve board29, component assets and scaffold-fit correction candidate with reviewed footage c54169e
"""Isolated USB ESD 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))
for t in list(b.GetTracks()):
 n=t.GetNetname();a=xy(t.GetStart());z=xy(t.GetEnd())
 via=isinstance(t,k.PCB_VIA)
 box=lambda x0,y0,x1,y1: max(a[0],z[0])>=x0 and min(a[0],z[0])<=x1 and max(a[1],z[1])>=y0 and min(a[1],z[1])<=y1
 drop=n in ['USB_DP_CONN','USB_DM_CONN','CC1']
 drop |= n=='FAN_TACH' and box(149,40.5,155,49)
 drop |= n=='SWCLK' and box(149.5,43.5,155,48.8)
 drop |= n=='USB_VBUS' and box(151.6,49.8,152.9,50.4)
 drop |= n=='GND' and not via and t.GetLayer()==k.F_Cu and box(149,49.9,149.5,50.1)
 if drop:
  removed.append({'uuid':t.m_Uuid.AsString(),'net':n,'via':via,'start':a,'end':z})
# 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'))
u=b.FindFootprintByReference('U10');u.SetPosition(k.VECTOR2I(k.FromMM(152.4),k.FromMM(45.6)))
added=[]
def track(net,points,width=.25,layer=k.F_Cu):
 for a,z in zip(points,points[1:]):
  t=k.PCB_TRACK(b);t.SetStart(k.VECTOR2I(k.FromMM(a[0]),k.FromMM(a[1])));t.SetEnd(k.VECTOR2I(k.FromMM(z[0]),k.FromMM(z[1])));t.SetWidth(k.FromMM(width));t.SetLayer(layer);t.SetNetCode(b.FindNet(net).GetNetCode());b.Add(t);added.append({'net':net,'start':a,'end':z,'layer':b.GetLayerName(layer),'width':width})
track('USB_DP_CONN',[(150.21,44.8),(150.975,44.8),(151.125,44.65),(153.5375,44.65)])
track('USB_DM_CONN',[(150.21,46.5),(151.2125,46.5),(151.2625,46.55),(153.5375,46.55)])
track('GND',[(151.2625,45.6),(150.4,45.6)])
v=k.PCB_VIA(b);v.SetPosition(k.VECTOR2I(k.FromMM(150.4),k.FromMM(45.6)));v.SetWidth(k.FromMM(.6));v.SetDrill(k.FromMM(.3));v.SetLayerPair(k.F_Cu,k.B_Cu);v.SetNetCode(b.FindNet('GND').GetNetCode());b.Add(v)
k.ZONE_FILLER(b).Fill(b.Zones());k.SaveBoard(str(p/'usb-inline.kicad_pcb'),b)
for ext in ['kicad_pro','kicad_dru']:shutil.copy2(p/('source.'+ext),p/('usb-inline.'+ext))
(p/'usb-inline-changes.json').write_text(json.dumps({'moved':{'U10':[152.4,45.6]},'removed':removed,'fixedTracks':added,'addedGroundVia':[150.4,45.6,.6,.3],'status':'candidate only; disconnected routes require replanning and native DRC'},indent=2))
print('Saved isolated USB candidate with',len(removed),'removed copper items')