main
John Lauer
Preserve board29, component assets and scaffold-fit correction candidate with reviewed footage
c54169e
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
"""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','USB_DP','USB_DM','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.SetOrientationDegrees(90);u.SetPosition(k.VECTOR2I(k.FromMM(151.9),k.FromMM(44.4)))
u.Reference().SetPosition(k.VECTOR2I(k.FromMM(151.9),k.FromMM(41.0)))
for ref,x,y in [('R31',149.4,45.0),('R32',149.4,46.7)]:
f=b.FindFootprintByReference(ref);f.SetPosition(k.VECTOR2I(k.FromMM(x),k.FromMM(y)));f.Reference().SetPosition(k.VECTOR2I(k.FromMM(x),k.FromMM(y+1.4 if ref=='R32' else y-1.4)))
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',[(149.91,45.0),(150.95,45.0)])
track('USB_DP_CONN',[(150.95,43.2625),(150.95,45.5375)])
track('USB_DM_CONN',[(149.91,46.7),(152.85,46.7),(152.85,43.2625)])
track('USB_DP',[(147.175,45.0),(148.89,45.0)])
track('USB_DM',[(147.175,45.8),(148.25,45.8),(148.5,46.05),(148.5,46.31),(148.89,46.7)])
track('USB_DP_CONN',[(155.225,42.25),(154.2,42.25),(153.15,41.2),(151.6,41.2),(150.95,41.85),(150.95,43.2625)])
track('USB_DP_CONN',[(155.225,43.25),(154.2,43.25),(153.7,42.75),(153.7,41.75)])
track('USB_DM_CONN',[(155.225,42.75),(156.3,42.75),(156.3,43.8),(155.225,43.8),(155.225,43.75)])
track('USB_DM_CONN',[(156.3,43.8),(153.9,43.8),(152.85,42.75),(152.85,42.0)],layer=k.B_Cu)
track('USB_DM_CONN',[(152.85,42.0),(152.85,43.2625)])
track('GND',[(151.9,45.5375),(151.9,44.55)])
for net,x,y in [('USB_DM_CONN',156.3,43.8),('USB_DM_CONN',152.85,42.0),('GND',151.9,44.55)]:
v=k.PCB_VIA(b);v.SetPosition(k.VECTOR2I(k.FromMM(x),k.FromMM(y)));v.SetWidth(k.FromMM(.6));v.SetDrill(k.FromMM(.3));v.SetLayerPair(k.F_Cu,k.B_Cu);v.SetNetCode(b.FindNet(net).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':[151.9,44.4,90],'R31':[149.4,45.0,180],'R32':[149.4,46.7,180]},'removed':removed,'fixedTracks':added,'addedGroundVia':[151.9,44.55,.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')