import fitz, math from collections import defaultdict from trace import nets doc=fitz.open('Xiaomi_Mi_8_Schematic.pdf') NETCOL={0xFF0000, 0x400040} def labels(page): out=[] for b in page.get_text('dict')['blocks']: for l in b.get('lines',[]): for s in l['spans']: t=s['text'].strip() if s['color'] in NETCOL and t and t not in ('o','<','>'): out.append((t, fitz.Rect(s['bbox']))) return out def gnd_comps(segs,dsu,key): by=defaultdict(list) for a,b in segs: by[dsu.find(key(a))].append((a,b)) g=set() for c,ss in by.items(): sh=[round(math.dist(*s),1) for s in ss if 1.0=3 and len(set(sh))>=3: g.add(c) return g def dump(pgno, pinxy, maxd=14): page=doc[pgno-1] segs,dsu,key,comp=nets(page) pts=[] for a,b in segs: pts.append(a); pts.append(b) lb=labels(page); gc=gnd_comps(segs,dsu,key) res={} for pin,q in sorted(pinxy.items()): d,p=min((math.dist(p,q),p) for p in pts) if d>maxd: res[pin]=('NO-WIRE',d,0,''); continue c=dsu.find(key(p)); cp=comp[c] best=None for t,r in lb: dm=min(math.hypot(max(r.x0-x,0,x-r.x1), max(r.y0-y,0,y-r.y1)) for x,y in cp) if best is None or dm