muse/book-reading-clip-lamp
A tscircuit package created by muse.
- Version
- 0.4.6
- License
- unset
- Stars
- 0
find-via-repair.py
import json,math
c=json.load(open('dist/index/circuit.json'));target='source_net_43_0';tr=next(e for e in c if e.get('pcb_trace_id')==target and e['type']=='pcb_trace');old=(9.263362836235084,5.061586779006138)
def ds(p,a,b):
dx=b[0]-a[0];dy=b[1]-a[1]; d=dx*dx+dy*dy
t=max(0,min(1,((p[0]-a[0])*dx+(p[1]-a[1])*dy)/d)) if d else 0
return math.hypot(p[0]-a[0]-t*dx,p[1]-a[1]-t*dy)
def orient(a,b,c):return (b[0]-a[0])*(c[1]-a[1])-(b[1]-a[1])*(c[0]-a[0])
def seg(a,b,d,e):
if orient(a,b,d)*orient(a,b,e)<0 and orient(d,e,a)*orient(d,e,b)<0:return 0
return min(ds(a,d,e),ds(b,d,e),ds(d,a,b),ds(e,a,b))
obs=[]
for e in c:
typ=e['type']
if typ=='pcb_trace' and e['pcb_trace_id']!=target:
for a,b in zip(e['route'],e['route'][1:]):
if a['route_type']==b['route_type']=='wire' and a['layer']==b['layer']:
obs.append(('seg',a['layer'],(a['x'],a['y']),(b['x'],b['y']),max(a['width'],b['width'])/2))
if typ=='pcb_via' and e.get('pcb_trace_id')!=target:
obs.append(('point','all',(e['x'],e['y']),None,e['outer_diameter']/2))
if typ=='pcb_smtpad':
if 'x' in e:
if e.get('shape')=='circle':obs.append(('point',e['layer'],(e['x'],e['y']),None,e.get('radius',e.get('diameter',0)/2)))
else:
x,y=e['x'],e['y'];w=e.get('width',0)/2;h=e.get('height',0)/2
obs.append(('rect',e['layer'],(x-w,y-h),(x+w,y+h),0))
def pointclear(p,o):
typ,ly,a,b,r=o
if typ=='point':return math.dist(p,a)-r
if typ=='seg':return ds(p,a,b)-r
if typ=='rect':return math.hypot(max(a[0]-p[0],0,p[0]-b[0]),max(a[1]-p[1],0,p[1]-b[1]))
def lineclear(a,b,o):
typ,ly,d,e,r=o
if typ=='point':return ds(d,a,b)-r
if typ=='seg':return seg(a,b,d,e)-r
if typ=='rect':
vs=[d,(d[0],e[1]),e,(e[0],d[1])]
return min(seg(a,b,x,y) for x,y in zip(vs,vs[1:]+vs[:1]))
# Existing adjacent vertices before/after the moved via, with their actual layers.
a=(9.43978232278141,5.623791175852792);b=(9.331708594936522,4.792231020261142)
choices=[]
for xi in range(620,1171,5):
for yi in range(300,750,5):
p=xi/100,yi/100
vc=min(pointclear(p,o)-.3 for o in obs)
if vc<.2:continue
lc=min([lineclear(a,p,o)-.075 for o in obs if o[1] in ['all','inner1']]+[lineclear(p,b,o)-.075 for o in obs if o[1] in ['all','bottom']])
if lc>=.2:choices.append((math.dist(p,old),p,round(vc,3),round(lc,3)))
print(sorted(choices)[:12])