"""Plot router output over the board's pads, one colour per net, failed pins marked. tools/router/.venv/bin/python tools/router/plot_routes.py board.json routes.json out.png [x0 y0 x1 y1] Pads are grey (F.Cu darker, B.Cu-only lighter); F.Cu tracks solid, B.Cu tracks dashed; vias are circles; every net is labelled at its longest segment; pins listed in routes.json "failed" get a red cross. Use a window (mm) to zoom into a dense part. """ import json import sys import zlib import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt # noqa: E402 from matplotlib.patches import Circle, Polygon # noqa: E402 board, routes, out = sys.argv[1:4] win = [float(v) for v in sys.argv[4:8]] if len(sys.argv) >= 8 else None d = json.load(open(board)) r = json.load(open(routes)) x0, y0, x1, y1 = win or d["bounds"] scale = 12 if win else 4 fig = plt.figure(figsize=((x1 - x0) / 25.4 * scale, (y1 - y0) / 25.4 * scale), dpi=100) ax = fig.add_axes([0, 0, 1, 1]) ax.set_xlim(x0, x1) ax.set_ylim(y1, y0) ax.set_aspect("equal") ax.axis("off") cmap = plt.get_cmap("tab20") def col(net): return cmap(zlib.crc32(net.encode()) % 20) for p in d["pads"]: poly = p["F"] or p["B"] if poly: ax.add_patch(Polygon(poly, closed=True, fc="#9e9e9e" if p["F"] else "#d0d0d0", ec="none", zorder=1)) if win and x0 <= p["x"] <= x1 and y0 <= p["y"] <= y1: ax.text(p["x"], p["y"], "%s.%s" % (p["ref"], p["num"]), fontsize=4, ha="center", va="center", zorder=5) longest = {} for s in r["segs"]: lw = s["w"] * 72 / 25.4 * scale * 1.0 ax.plot([s["x1"], s["x2"]], [s["y1"], s["y2"]], color=col(s["net"]), lw=lw, solid_capstyle="round", ls="-" if s["layer"] == "F.Cu" else (0, (2, 1)), alpha=0.85 if s["layer"] == "F.Cu" else 0.6, zorder=3) L = abs(s["x2"] - s["x1"]) + abs(s["y2"] - s["y1"]) if L > longest.get(s["net"], (0,))[0]: longest[s["net"]] = (L, (s["x1"] + s["x2"]) / 2, (s["y1"] + s["y2"]) / 2) for v in r["vias"]: ax.add_patch(Circle((v["x"], v["y"]), v["d"] / 2, fc="white", ec=col(v["net"]), lw=1.5, zorder=4)) for net, (L, x, y) in longest.items(): if x0 <= x <= x1 and y0 <= y <= y1: ax.text(x, y, net.split("/")[-1], fontsize=6 if win else 4, color="black", zorder=6, bbox=dict(fc=col(net), ec="none", alpha=0.7, pad=0.5)) pads = {(p["ref"], p["num"]): p for p in d["pads"]} for f in r.get("failed", []): p = pads.get((f[1], f[2])) if len(f) >= 3 else None if p: ax.plot([p["x"]], [p["y"]], marker="x", color="red", ms=14, mew=3, zorder=7) fig.savefig(out) print("wrote", out)