# -*- coding: utf-8 -*- u"""全工程扫一遍「旗/端口的引脚有没有真的接在导线端点上」。 /c/Python314/python .claude/port_audit.py ## 为什么需要它(2026-09-03 踩到的第三种断线) `verify_sch.py` 判端口连接的条件是「引脚**落在导线上**」。这个条件太松: EasyEDA Pro 只在**导线端点**处认连接,引脚压在一条导线的**中段**是不连的 —— 渲染出来看着完全正常,DRC 不报,`verify_sch --nets` 也算它连上。 实测证据(P1-INPUT,U13 INA226): | 端口 | 引脚坐标 | 落在 | PCB 里的网络 | |---|---|---|---| | SDA | (910, 380) | 导线 **端点** `[910,380 → 910,570]` | `SDA` ✅ | | SCL | (920, 340) | 导线 **中段** `[920,320 → 920,570]` | `$1N257` ❌ | 后果:U13 的 SCL 没接上 I²C 总线,**输入侧检流整片芯片是死的**, 而且因为 A0 就搭在这个节点上,地址 0x43 也无从谈起。 ## 三种「几何过、电气不过」的断法(本工程都真踩过) 1. **零长度导线**:端口引脚与元件引脚同坐标,中间那段导线长度为 0(skill §8a)。 2. **中段搭接**:端口引脚压在导线中段而非端点(本文件抓的就是这个)。 3. **只有名字没有线**:旗/端口悬空,`verify_sch` 的「无网络名」那一条能抓。 ## 判定口径 - `端点` —— 引脚与某条导线的首/末点重合 → 正常。 - `顶点` —— 引脚与折线的中间顶点重合 → **可疑**(同样不是端点)。 - `中段` —— 引脚落在某段的内部 → **可疑**。 - `悬空` —— 压根不在任何导线上 → 可疑(除非它和元件引脚同坐标,那是第 1 类)。 坐标单位是原理图的 0.01 inch,全工程都吸附在 5 的倍数上,所以容差取 0.6 足够。 """ import io, json, os, sys HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) from eda import run TOL = 0.6 _CACHE = json.load(io.open(os.path.join(HERE, 'eda-parts.json'), encoding='utf-8')) # 默认扫下板;`--board Board2` 扫载板(两块板的页表都在 eda-parts.json 里) if '--board' in sys.argv: _b = sys.argv[sys.argv.index('--board') + 1] PAGES = _CACHE['schematic_boards'][_b]['pages'] else: PAGES = _CACHE['pages'] JS = u""" await eda.dmt_EditorControl.openDocument('%s'); await new Promise(r => setTimeout(r, 1200)); const polys = []; for (const w of await eda.sch_PrimitiveWire.getAll()) { const l = w.getState_Line(); if (!l || l.length < 4) continue; // ⚠ getState_Line() 返回的是**端点对**(每 4 个数一段),不是折线路径。 // 当折线解析会凭空造出斜线段,进而把 197 个好的 GND 旗误报成可疑。 for (let i = 0; i + 3 < l.length; i += 4) polys.push([[Math.round(l[i]), Math.round(l[i+1])], [Math.round(l[i+2]), Math.round(l[i+3])]]); } const ports = [], pins = []; for (const c of await eda.sch_PrimitiveComponent.getAll()) { const t = c.getState_ComponentType(); let nn = null; try { nn = c.getState_Net ? c.getState_Net() : null; } catch (e) {} const ps = await c.getAllPins(); for (const q of (ps || [])) { const rec = [Math.round(q.getState_X()), Math.round(q.getState_Y())]; if (t === 'netport' || t === 'netflag') ports.push({t, net: nn, d: c.getState_Designator() || '', x: rec[0], y: rec[1]}); else pins.push({d: c.getState_Designator() || '', n: q.getState_PinNumber(), x: rec[0], y: rec[1]}); } } return {polys, ports, pins}; """ def near(ax, ay, bx, by): return abs(ax - bx) <= TOL and abs(ay - by) <= TOL def on_seg(px, py, ax, ay, bx, by): u"""点在线段内部(不含两端)。只处理水平/垂直/斜的一般情况。""" dx, dy = bx - ax, by - ay if abs(dx) <= TOL and abs(dy) <= TOL: return False cross = (px - ax) * dy - (py - ay) * dx if abs(cross) > TOL * max(1.0, (dx * dx + dy * dy) ** 0.5): return False dot = (px - ax) * dx + (py - ay) * dy L2 = dx * dx + dy * dy return TOL < dot < L2 - TOL def classify(p, segs, pins): u"""端点 = 连;其余三种都是真断线。""" for a, b in segs: if near(p['x'], p['y'], *a) or near(p['x'], p['y'], *b): return u'端点' for a, b in segs: if on_seg(p['x'], p['y'], a[0], a[1], b[0], b[1]): return u'中段' for q in pins: if near(p['x'], p['y'], q['x'], q['y']): return u'压引脚' return u'悬空' def main(): bad = [] total = 0 for name, uuid in PAGES.items(): r = run(JS % uuid, timeout=120) if not r.get('success'): print(u'%-12s 读失败: %s' % (name, json.dumps(r, ensure_ascii=False)[:200])) continue d = r['result'] rows = [] for p in d['ports']: total += 1 k = classify(p, [(s[0], s[1]) for s in d['polys']], d['pins']) if k != u'端点': rows.append((k, p)) print(u'%-12s 旗/端口 %3d 个,可疑 %d' % (name, len(d['ports']), len(rows))) for k, p in sorted(rows, key=lambda kp: (kp[0], kp[1]["net"] or "", kp[1]["x"], kp[1]["y"])): print(u' %-4s %-12s (%s) @ (%d, %d)' % (k, p['net'] or u'(无名)', p['t'], p['x'], p['y'])) bad.append((name, k, p)) print(u'') print(u'合计 %d 个旗/端口,可疑 %d 个。' % (total, len(bad))) if bad: print(u'「中段」「顶点」「悬空」都是真断线;「压引脚」是零长度导线(skill §8a)。') sys.exit(1 if bad else 0) if __name__ == '__main__': main()