From d296acd34eedf7a32eb28192ccb942daddc2c30e Mon Sep 17 00:00:00 2001 From: lhj <403133128@qq.com> Date: Fri, 4 Dec 2026 00:48:34 +0800 Subject: [PATCH] =?UTF-8?q?test(spatial):=20=E5=86=B2=E7=AA=81=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E5=8A=A0=E9=98=B3=E6=80=A7=E5=AF=B9=E7=85=A7=E2=80=94?= =?UTF-8?q?=E2=80=94=E5=88=A0=E6=8E=89=E4=B8=80=E6=9D=A1=E7=BD=91=E7=9A=84?= =?UTF-8?q?=E9=93=9C=EF=BC=8C=E7=9C=8B=E5=AE=83=E8=AE=A4=E4=B8=8D=E8=AE=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spatial/providers/kicad_board.py | 65 +++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/spatial/providers/kicad_board.py b/spatial/providers/kicad_board.py index 9dff44f..a99453c 100644 --- a/spatial/providers/kicad_board.py +++ b/spatial/providers/kicad_board.py @@ -46,6 +46,18 @@ def _ratsnest(board, code: int) -> int: return -1 +def _unrouted(board) -> tuple: + """(nets with a break, total missing copper connections) over the whole board.""" + codes = sorted({p.GetNetCode() for p in _pads(board) if p.GetNetCode() != 0}) + bad = pairs = 0 + for code in codes: + n = _ratsnest(board, code) + if n > 0: + bad += 1 + pairs += n + return bad, pairs + + def report(paths) -> None: try: import pcbnew @@ -115,14 +127,63 @@ def coverage(board_path: str, netlist_path: str) -> None: print("它正好是 scope 契约里'清单比交付多'的实物版本:网表交出的东西比板子少。") +def control(board_path: str) -> None: + """Positive control: cut one net's copper and check the detector notices. + + Every demo board is fully routed, so the disagreement count is zero -- and a + metric that always returns zero has not been tested. This removes the tracks + of one small net and shows the count go up. If it does not, the detector is + reading the wrong thing and every "0" above is meaningless. + """ + try: + import pcbnew + except Exception: + _die("pcbnew 不可用") + return + board = pcbnew.LoadBoard(board_path) + try: + board.BuildConnectivity() + except Exception: + pass + before = _unrouted(board) + + per_net = {} + for t in board.GetTracks(): + code = t.GetNetCode() + if code: + per_net.setdefault(code, []).append(t) + if not per_net: + print("no tracks on this board") + return + code = min(per_net, key=lambda c: (len(per_net[c]), c)) + net = board.FindNet(code) + name = net.GetNetname() if net else str(code) + cut = len(per_net[code]) + for t in per_net[code]: + board.Remove(t) + board.BuildConnectivity() + after = _unrouted(board) + + print(f"control on {board_path.rsplit('/', 1)[-1]}") + print(f" cut net {name!r}: removed {cut} track(s)") + print(f" before: {before[0]} nets broken, {before[1]} missing connections") + print(f" after : {after[0]} nets broken, {after[1]} missing connections") + ok = after[1] > before[1] + print(f" {'PASS' if ok else 'FAIL'} the detector " + f"{'notices' if ok else 'did NOT notice'} the cut") + + def main(argv=None) -> int: argv = list(sys.argv[1:] if argv is None else argv) - if len(argv) == 2: + if argv and argv[0] == "control": + control(argv[1] if len(argv) > 1 else "") + elif len(argv) == 2: coverage(argv[0], argv[1]) elif argv: report(argv) else: - print("usage: kicad_board ... | kicad_board ") + print("usage: kicad_board ... | kicad_board | " + "kicad_board control ") return 2 return 0