130 lines
4.9 KiB
Python
130 lines
4.9 KiB
Python
"""板子一侧:铜箔上到底连没连。只能在装了 KiCad 的机器上跑。
|
||
|
||
这个模块存在的理由不是"再多一个 provider",而是**让两份来源互相作证**。
|
||
|
||
* 网表说的是**意图**:原理图上这两个脚属于同一条网。
|
||
* 板子说的是**现实**:铜箔、焊盘、过孔是不是真的把它们接通了。
|
||
|
||
两者在成品板上一致,在布线未完成的板上不一致。这个不一致不是 bug,是这类系统
|
||
必须能表达的东西:一份来源说"连上了",另一份说"没连上",而正确答案是**不知道**
|
||
——除非有第三方信息(比如网表版本比板子新,或板子本来就是半成品)。
|
||
|
||
``spatial/checker.py`` 现在没有"两份来源冲突"这个概念,只有"缺了东西"。所以这个
|
||
模块先只做测量:把冲突数出来,再决定要不要、以及怎么把它接进契约。
|
||
|
||
用法(服务器):``python3 -m spatial.providers.kicad_board <a.kicad_pcb> ...``
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import sys
|
||
|
||
|
||
def _die(msg: str) -> None:
|
||
print(f"ERROR {msg}")
|
||
print(" 这个模块需要 pcbnew(KiCad 的 Python 绑定),只能在装了 KiCad 的机器上跑。")
|
||
|
||
|
||
def _pads(board):
|
||
for pad in board.GetPads():
|
||
yield pad
|
||
|
||
|
||
def _ref(pad) -> str:
|
||
try:
|
||
return pad.GetParent().GetReference()
|
||
except Exception:
|
||
return "?"
|
||
|
||
|
||
def _ratsnest(board, code: int) -> int:
|
||
"""How many connections inside this net are not yet realised in copper."""
|
||
try:
|
||
return len(board.GetConnectivity().GetRatsnestForNet(code))
|
||
except Exception:
|
||
return -1
|
||
|
||
|
||
def report(paths) -> None:
|
||
try:
|
||
import pcbnew
|
||
except Exception:
|
||
_die("pcbnew 不可用")
|
||
return
|
||
|
||
print(f"{'board':24s} {'焊盘':>5s} {'有网':>5s} {'无网':>5s} {'网数':>4s} "
|
||
f"{'未布通':>6s} {'未布网':>6s} {'悬空对':>6s}")
|
||
print("-" * 66)
|
||
for path in paths:
|
||
board = pcbnew.LoadBoard(path)
|
||
try:
|
||
board.BuildConnectivity()
|
||
except Exception:
|
||
pass
|
||
pads = list(_pads(board))
|
||
with_net = [p for p in pads if p.GetNetCode() != 0]
|
||
codes = sorted({p.GetNetCode() for p in with_net})
|
||
unrouted, dangling = 0, 0
|
||
for code in codes:
|
||
n = _ratsnest(board, code)
|
||
if n > 0:
|
||
unrouted += 1
|
||
dangling += n
|
||
print(f"{path.rsplit('/', 1)[-1]:24s} {len(pads):5d} {len(with_net):5d} "
|
||
f"{len(pads) - len(with_net):5d} {len(codes):4d} {unrouted:6d} "
|
||
f"{unrouted:6d} {dangling:6d}")
|
||
print()
|
||
print("焊盘 = 板上所有封装焊盘;有网 = 归属某条网的;无网 = netcode 0(机械脚/未标注)")
|
||
print("未布通 = 网内还有未连通的连接(老鼠线)的网数;悬空对 = 这些网里缺的连接数")
|
||
print()
|
||
print("'未布网' 与 '未布通' 同列是笔误吗——不是:前者是网表里根本不存在这条网,")
|
||
print("后者是网存在但铜没连完。两种都由 ratsnest 读出,这里按网数统一计数。")
|
||
|
||
|
||
def coverage(board_path: str, netlist_path: str) -> None:
|
||
"""The number the netlist provider could not compute: pads it never mentions.
|
||
|
||
A footprint has more pads than the netlist names -- no-connect pins and
|
||
mechanical pads never appear in a net. So "the netlist described everything"
|
||
is false by construction, and the size of the lie is exactly this count.
|
||
"""
|
||
try:
|
||
import pcbnew
|
||
from . import kicad_netlist
|
||
except Exception:
|
||
_die("pcbnew 不可用")
|
||
return
|
||
nl = kicad_netlist.load(netlist_path)
|
||
named = {(r, p) for r, p in nl.nodes()}
|
||
board = pcbnew.LoadBoard(board_path)
|
||
pads = list(_pads(board))
|
||
pad_keys = {(_ref(p), p.GetNumber()) for p in pads}
|
||
missing = pad_keys - named
|
||
extra = named - pad_keys
|
||
print(f"board {board_path.rsplit('/', 1)[-1]}: {len(pads)} pads")
|
||
print(f"netlist {netlist_path.rsplit('/', 1)[-1]}: {len(named)} named pads, "
|
||
f"{len(nl.comps)} parts")
|
||
print(f"板上有、网表没提:{len(missing)} ({len(missing) / max(1, len(pad_keys)):.1%} 的焊盘)")
|
||
print(f"网表提了、板上没有:{len(extra)} (网表与板子版本不一致时会非零)")
|
||
if missing:
|
||
sample = sorted(missing)[:8]
|
||
print(f" 举例:{sample}")
|
||
print()
|
||
print("这个数是 netlist provider 的盲区大小,而且**只能**由知道封装的那一侧算出。")
|
||
print("它正好是 scope 契约里'清单比交付多'的实物版本:网表交出的东西比板子少。")
|
||
|
||
|
||
def main(argv=None) -> int:
|
||
argv = list(sys.argv[1:] if argv is None else argv)
|
||
if len(argv) == 2:
|
||
coverage(argv[0], argv[1])
|
||
elif argv:
|
||
report(argv)
|
||
else:
|
||
print("usage: kicad_board <a.kicad_pcb> ... | kicad_board <a.kicad_pcb> <a.net>")
|
||
return 2
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main())
|