文件

481 行
24 KiB
Python

此文件含有模棱两可的 Unicode 字符
此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。
# -*- coding: utf-8 -*-
u"""R2 刀片 PCB 的铺铜(M4c-3)。
/c/Python314/python .claude/r2/pcb_pour.py # 只算 + 自检
/c/Python314/python .claude/r2/pcb_pour.py --push # 清旧铜面 + 重建 + 重填
/c/Python314/python .claude/r2/pcb_pour.py --rebuild # 只重填(铜面没改,只是扇出/布线改了)
## 叠层(方案 §7.1)
| 设计 | EasyEDA | id | 内容 |
|---|---|---|---|
| L1 | Top Layer | 1 | 功率 + 信号;**D± 走这层、以 L2 为参考** |
| L2 | Inner1 | 15 | **完整地平面,不许割断** |
| L3 | Inner2 | 16 | `VO_P` 岛 + `+12V` 岛,其余 GND 底填 |
| L4 | Bottom | 2 | 控制信号 + GND 底填 |
## 三个只有这块板才有的约束
1. **金手指区不许铺外层铜**:`J1` 的 64 个指是 L1/L2 上的焊盘,间距只有 0.3 mm。
外层铜面一旦下探到 y < 指顶(5.6)+间距,就会把相邻的指连起来 —— 而且**是不是短路
要看它连到哪两根**,DRC 只报间距不报「这两根本来就该分开」。
⇒ 外层铜面一律 `y >= TOP_POUR_Y`(11.5,比缺口顶 7.45 还高一截)。
内层(L2/L3)没有指,可以一直铺到板边内缩,只要避开缺口。
2. **缺口要挖掉**:防呆缺口是真的铣穿的,四层都要让开(含 0.5 mm 内缩)。
3. **优先级是「数字小的先铺、先占地盘」**(R1 `pcb_pour.py` 头上那条已经查清的翻案)。
⇒ `VO_P` / `+12V` 两个岛给 1 / 2,GND 底填给 9。
## 三个 API 坑(全部照抄 R1 `pcb_pour.py` 的结论,别再踩一遍)
· 多边形**必须显式闭合**(末尾把首点再写一遍),否则 `create()` 一块都不建;
· `create()` **抛异常不等于失败** —— 判据只能是建完 `getAll()` 按名字(会被转成大写)核对;
· `rebuildCopperRegion()` 是坏的,**只能走 GUI**:工具 → 铺铜管理器... → 重建所有 → 确认。
判据是 `pcb_PrimitivePoured` 的数量,不是 `pcb_PrimitivePour`。
"""
import json
import math
import os
import sys
import time
sys.stdout.reconfigure(encoding='utf-8')
HERE = os.path.dirname(os.path.abspath(__file__))
# ⚠ 顺序不能反:`.claude` 下有同名的 R1 脚本(pcb_pour / pcb_rules / pcb_place),
# 先插 .claude 再插 HERE 的话,`import pcb_pour` 会拿到 R1 那一份,
# 而且**不报错**,只在调到 R2 专有的函数时才炸。
sys.path.insert(0, os.path.dirname(HERE))
sys.path.insert(0, HERE)
from eda import run # noqa: E402
import pcb_blade as B # noqa: E402
import fp_edge # noqa: E402
PCB = B.PCB
MIL = B.MIL
L_TOP, L_GND, L_PWR, L_BOT = 1, 15, 16, 2
INSET = 0.5 # 铜面离板边
TOP_POUR_Y = 11.5 # 外层铜面的下沿(金手指区一点外层铜都不许有)
# ⛔ 2026-09-12:板框开了台阶(`pcb_blade.STEP_H` / `TONGUE_X0/X1`)之后,**舌头两侧根本没有板材**
# ⇒ 那两段的铜下沿必须抬到 `STEP_H + INSET`,否则铜会悬在被切掉的地方。
# 舌头那一段仍按 TOP_POUR_Y / INSET 走(外层不进金手指区、内层绕缺口)。
STEP_POUR_Y = round(B.STEP_H + INSET, 3)
NOTCH_KEEP = 0.5 # 缺口周围再让 0.5
# L3 两个岛(x0, y0, x1, y1)
# ⛔⛔ 2026-09-11 晚(方案 §35):主板转 180°、功率链转 180° ⇒ 两个岛上下对调。
# · `VO_P` 岛在**上半**:功率链顶上的 Rs1 / U3 → 西去 J5 / 阻尼组 / DIS → 两槽之间的 C60。
# 电池槽是铣穿的,岛做成**绕开电池槽的 U 形单环**(三块矩形拼,`VO_RECTS`),不用带洞多边形。
# · `+12V` 岛在**下半**:金手指 12V 三指的 launch 孔(x 28.5~30.5, y 11.75)→ 东去 F1 的 +12V 脚(功率链最底)。
# 12V 从此**不在 L1 / L4 上走线**(原来那根 L4 x≈42.5 的竖线把整块板东西切成两半)。
# 下沿 9.0:金手指地母线的孔在 y 7.0 / 8.2,孔 + 净距到 8.8;上沿 20:信号 launch 孔在 y 11.75 一字排开
# (1 mm 一个,反焊盘把 11.25~12.25 那一条几乎切断),电流要从它上面 8 mm 宽的那条过。
# ⛔⛔ 2026-09-14 BLADE-PCB_1(71×62、金手指居中、用户新摆位):两个岛按新焊盘重画,R 版的定义见
# `archive_blade_R_0914/pcb_pour.py`。都用**点列直接给出的单环**,路由器用的 `*_RECTS` 是它们的矩形分解。
# · `VO_P`(4 A):主体 x 22.2~55.5 / y 34.8~电池槽底 −0.56,罩住 Rs1.1 (36.3,36.6)、R19.1、C60.1 (32.5,40.5)、
# J5.1 (47.9,43.0)、C61~C64.2 (51.6,46.7~53.4);再往南伸一条 x 22.2~27.5 的腿到 y 20.2 接 U3.8 / C65.2
# (INA226 的 VBUS 采样 + 去耦,电流 µA 级,腿被别的网的反焊盘啃几口也无妨)。
# · `+12V`(1.45 A):舌头段 y 9.0~19.0 横贯(接 J1 A2/A3/B1~B3 五指在 x 19~21 的 launch 孔),
# 东头拐上去一条 x 49.5~57.5 / y STEP_POUR_Y~30.0 的腿接 F1.1 (53.7,28.4)。
# 两岛最近处:12V 腿顶 30.0 vs VO_P 主体底 34.8,12V 横带顶 19.0 vs VO_P 腿底 20.2 ⇒ 净距 ≥ 1.2。
VO_TOP_Y = round(B.SLOT_Y[0] - 0.56, 3) # 主体上沿:离电池槽底 0.56(槽边到铜 ≥ 0.5)
VO_RECTS = [
(25.0, 32.3, 55.5, VO_TOP_Y), # 主体(2026-09-14 晚 主板南移 2.5:底 34.8 → 32.3)
]
# ⛔ 2026-09-14 晚:**西南腿拆掉、主体西沿 21.0 → 25.0**。
# ① 腿上压着 Kelvin 取样线 U3.9→Rs1.1 自己的两个换层孔 (22.25/21.5, 22.5),主体西沿压着第三个 (23.75, 34.75)
# ⇒ 取样线中途被岛短接,量到的是「分流电阻 + 13 mm 铜箔」。岛只该在 Rs1.1 焊盘处接取样线。
# ② 腿原本接的 U3.8 已短接到 U3.9、C65.2 由 hand_blade1.py 一段线接到这对脚上,腿已无用。
# ③ 让出 L3 西半给 +5V 岛(见 V5_PTS)。
VO_PTS = [(25.0, 32.3), (55.5, 32.3), (55.5, VO_TOP_Y), (25.0, VO_TOP_Y)]
V12_LOW_Y, V12_TOP_Y = 9.0, 15.5 # 顶 15.5:给南移后的 +5V 岛(底 16.1)让 0.6(原 18.0)
V12_LEG = (49.5, 57.5, 27.5) # 东腿 x0, x1, 顶(F1 随主板南移,30.0 → 27.5)
V12_RECTS = [
(B.TONGUE_X0, V12_LOW_Y, B.TONGUE_X1, V12_TOP_Y),
(V12_LEG[0], STEP_POUR_Y, V12_LEG[1], V12_LEG[2]),
]
V12_ISLAND = V12_RECTS[0] # 老名字留着
# ── `+5V` 岛(L3,2026-09-14 BLADE-PCB_1 新增)──
# 服务器 A* 在 0.6/0.4/0.3 三种线宽下都没法把金手指 A19/A32 接到最西边的 U5/C66(L1 横线、L4 竖线把全板织满)。
# L3 在 +12V 横带以北、VO_P 主体以南 / 以西本来空着 ⇒ 铺一块 +5V:横带 x 1.0~48.9 / y 18.6~33.9,
# 西边再立一段 x 1.0~20.4 到 y 38.8 罩住 U5.1 / C66.2 的两个 +5V 孔。
# 离 +12V 横带顶 18.0 / 东腿 49.5、离 VO_P 主体 (25.0, 34.8) 都 ≥ 0.6。载流 ≤ 0.5 A(方案 §5.2),1 oz 内层十几 mm 宽绰绰有余。
V5_PTS = [(1.0, 16.1), (48.9, 16.1), (48.9, 31.4), (20.4, 31.4), (20.4, 36.3), (1.0, 36.3)] # 南移 2.5
V5_RECTS = [(1.0, 16.1, 48.9, 31.4), (1.0, 31.4, 20.4, 36.3)]
def mil(v):
return round(v * MIL, 4)
def rrect(x0, y0, x1, y1, r=0.0, seg=8):
u"""圆角矩形点列(逆时针,闭合)。r=0 就是普通矩形。"""
if r <= 0:
pts = [(x0, y0), (x1, y0), (x1, y1), (x0, y1)]
else:
pts = []
for cx, cy, a0 in ((x1 - r, y0 + r, 270), (x1 - r, y1 - r, 0),
(x0 + r, y1 - r, 90), (x0 + r, y0 + r, 180)):
for k in range(seg + 1):
a = math.radians(a0 + 90.0 * k / seg)
pts.append((cx + r * math.cos(a), cy + r * math.sin(a)))
out = [pts[0]]
for p in pts[1:]:
if abs(p[0] - out[-1][0]) > 1e-9 or abs(p[1] - out[-1][1]) > 1e-9:
out.append(p)
out.append(out[0]) # ⚠ 必须显式闭合
return out
def notch_cut():
u"""缺口在铜面里要挖掉的矩形(含让位)。"""
ml, mr = B.notch_break()
return (ml - NOTCH_KEEP, -1.0, mr + NOTCH_KEEP, fp_edge.NOTCH_D + NOTCH_KEEP)
def inner_poly():
u"""内层铜面:板框内缩 INSET,底边在缺口处绕过去(一条闭合折线,不用挖孔)。"""
d = INSET
r = B.CORNER_R - d
x0, y0, x1, y1 = B.BX0 + d, d, B.BX1 - d, B.BH - d
nx0, _ny0, nx1, ny1 = notch_cut()
pts = [(nx1, y0)] # 缺口右侧起步,沿底边往右
pts.append((x1 - r, y0))
for cx, cy, a0 in ((x1 - r, y0 + r, 270), (x1 - r, y1 - r, 0),
(x0 + r, y1 - r, 90), (x0 + r, y0 + r, 180)):
for k in range(9):
a = math.radians(a0 + 90.0 * k / 8)
pts.append((cx + r * math.cos(a), cy + r * math.sin(a)))
pts.append((nx0, y0)) # 底边左段走到缺口左沿
pts.append((nx0, ny1)) # 绕缺口
pts.append((nx1, ny1))
out = [pts[0]]
for p in pts[1:]:
if abs(p[0] - out[-1][0]) > 1e-9 or abs(p[1] - out[-1][1]) > 1e-9:
out.append(p)
out.append(out[0])
return out
def outer_poly():
u"""外层铜面:只在 y >= TOP_POUR_Y 的部分,金手指区一点铜都没有。"""
d = INSET
r = B.CORNER_R - d
x0, x1, y1 = B.BX0 + d, B.BX1 - d, B.BH - d
pts = [(x0, TOP_POUR_Y), (x1, TOP_POUR_Y)]
for cx, cy, a0 in ((x1 - r, y1 - r, 0), (x0 + r, y1 - r, 90)):
for k in range(9):
a = math.radians(a0 + 90.0 * k / 8)
pts.append((cx + r * math.cos(a), cy + r * math.sin(a)))
out = [pts[0]]
for p in pts[1:]:
if abs(p[0] - out[-1][0]) > 1e-9 or abs(p[1] - out[-1][1]) > 1e-9:
out.append(p)
out.append(out[0])
return out
def src(pts):
u"""点列 -> TPCB_PolygonSourceArray(mil): x1 y1 'L' x2 y2 'L' ..."""
a = [mil(pts[0][0]), mil(pts[0][1])]
for p in pts[1:]:
a += ['L', mil(p[0]), mil(p[1])]
return a
def gnd_tiles(y_lo):
u"""地铜的**若干块单环矩形**,绕开铣穿的排线槽。
⛔ **不能用带洞的复杂多边形**:`pcb_MathPolygon.createComplexPolygon()` 传多个环时
会把客户端挂死 —— 请求 30 s 超时、铺铜边框停在半路、进度恒为 0/0,
和「GUI 按钮失灵」完全无法区分(2026-09-08 在这上面绕了很久)。
⇒ 一律单环。要挖洞就拆成几块矩形拼,铜面之间同网互相接壤会自然连成一片。
做法:按所有槽的 x 边把板切成竖条,每条里按 y 找槽之间的空隙。
(2026-09-09 改:原来的「上下横带」写法假设各窗 y 区间不重叠,两条并排的排线槽
会把槽本身当成一条带铺进去。)
`y_lo` = 这一层铜面的下沿(外层 11.5 / 内层 0.5)。
"""
d = INSET
x0, x1 = B.BX0 + d, B.BX1 - d # 2026-09-14 晚:板框左沿不再是 0(pcb_blade.BX0)
y1 = B.BH - d
cuts = [(a - INSET, b - INSET, c + INSET, e + INSET) for (a, b, c, e) in B.mill_cuts()]
# ⛔ 台阶两端也是切分点:舌头外的竖条下沿要抬到 STEP_POUR_Y(那边没有板材)
xs = sorted(set([x0, x1, B.TONGUE_X0, B.TONGUE_X1] + [v for c in cuts for v in (c[0], c[2])]))
tiles = []
for xa, xb in zip(xs, xs[1:]):
if xb - xa < 0.6:
continue
in_tongue = (xa >= B.TONGUE_X0 - 1e-9 and xb <= B.TONGUE_X1 + 1e-9)
ys = [y_lo if in_tongue else max(y_lo, STEP_POUR_Y)]
for c in sorted(cuts, key=lambda c: c[1]):
if c[0] < xb - 1e-9 and xa < c[2] - 1e-9:
ys += [c[1], c[3]]
ys.append(y1)
for k in range(0, len(ys) - 1, 2):
ya, yb = ys[k], ys[k + 1]
if yb - ya > 0.6:
tiles.append((xa, ya, xb, yb))
# ⛔ 内层的下沿会盖住金手指的**防呆缺口**(铣穿的)。把缺口那一段单独让开:
# y 抬到缺口顶之上,再补两块把缺口左右两侧的底部填回来。
nx0, _a, nx1, ny1 = notch_cut()
if y_lo < ny1:
cut = ny1
out = []
for (a, b, c, e) in tiles:
if e <= cut:
continue
if b < cut and (a < nx1 and c > nx0):
out.append((a, cut, c, e))
if a < nx0:
out.append((a, b, nx0, cut))
if c > nx1:
out.append((nx1, b, c, cut))
else:
out.append((a, b, c, e))
tiles = [t for t in out if t[2] - t[0] > 0.6 and t[3] - t[1] > 0.6]
return tiles
def _ring(pts):
out = [pts[0]]
for q in pts[1:]:
if abs(q[0] - out[-1][0]) > 1e-9 or abs(q[1] - out[-1][1]) > 1e-9:
out.append(q)
out.append(out[0]) # ⚠ 必须显式闭合
return out
def v12_poly():
u"""`+12V` 岛(L3):舌头段横带 + 东腿到 F1,**单环**(带洞的复杂多边形会把客户端挂死,见 `gnd_tiles`)。
舌头以东的部分下沿抬到 STEP_POUR_Y(台阶把那块板材铣掉了)。"""
x0, x1 = B.TONGUE_X0, B.TONGUE_X1
lx0, lx1, ly1 = V12_LEG
assert x0 < lx0 < x1 < lx1, u'+12V 东腿必须骑在舌头东端'
return _ring([(x0, V12_LOW_Y), (x1, V12_LOW_Y), (x1, STEP_POUR_Y), (lx1, STEP_POUR_Y),
(lx1, ly1), (lx0, ly1), (lx0, V12_TOP_Y), (x0, V12_TOP_Y)])
def vo_poly():
u"""VO_P 岛(L3):主体 + 西南腿,单环。"""
return _ring(VO_PTS)
def plan():
u"""[(name, net, layer, [环...], priority)],priority 小的先铺、先占地盘。"""
outs = []
outs.append(('VO-L3', 'VO_P', L_PWR, [src(vo_poly())], 1))
outs.append(('V12-L3', '+12V', L_PWR, [src(v12_poly())], 2))
outs.append(('V5-L3', '+5V', L_PWR, [src(_ring(V5_PTS))], 3))
for tag, layer, y_lo in (('L2', L_GND, INSET), ('TOP', L_TOP, TOP_POUR_Y),
('BOT', L_BOT, TOP_POUR_Y)):
for k, (a, b, c, d2) in enumerate(gnd_tiles(y_lo)):
outs.append(('GND-%s-%d' % (tag, k), 'GND', layer,
[src(rrect(a, b, c, d2))], 9))
return outs
def _ring_pts(ring):
u"""`src()` 生成的 [x, y, 'L', x, y, 'L', ...](mil)还原成 [(x, y)] 点列(mm)。"""
pts = [(float(ring[0]) / MIL, float(ring[1]) / MIL)]
for k in range(3, len(ring), 3):
pts.append((float(ring[k]) / MIL, float(ring[k + 1]) / MIL))
return pts
def selfcheck(ps):
bad = []
for name, _net, _lay, rings, _p in ps:
for k, s in enumerate(rings):
if s[0] != s[-2] or s[1] != s[-1]:
bad.append(u'%s 第 %d 个环没闭合' % (name, k))
# 外层铜面不许下探进金手指区
top = fp_edge.EDGE_CLR + fp_edge.LONG_L
if TOP_POUR_Y <= top:
bad.append(u'外层铜下沿 %.2f 压到指顶 %.2f' % (TOP_POUR_Y, top))
if TOP_POUR_Y <= fp_edge.NOTCH_D:
bad.append(u'外层铜下沿 %.2f 低于缺口顶 %.2f' % (TOP_POUR_Y, fp_edge.NOTCH_D))
# ⛔ 台阶:舌头两侧不许有任何铜低于 STEP_POUR_Y(那块板材已经铣掉了)。
# ⚠ 判据必须**逐边**走,不能按整个环的包围盒判:`+12V` 岛是阶梯形,
# 它的 y=9.0 那一段只在舌头内,按包围盒(min y 9.0 且 max x 84)会误报。
for name, _net, _lay, rings, _p in ps:
for ring in rings:
pts = _ring_pts(ring)
worst = None
for (ax, ay), (bx, by) in zip(pts, pts[1:]):
n = max(2, int(math.hypot(bx - ax, by - ay) / 0.25) + 1)
for i in range(n + 1):
t = i / float(n)
x, y = ax + (bx - ax) * t, ay + (by - ay) * t
if y < STEP_POUR_Y - 1e-3 and (x < B.TONGUE_X0 - 1e-3 or x > B.TONGUE_X1 + 1e-3): # 1e-3:mil 往返有 ~1e-5 的舍入(舌头 52.75 时实测误报)
if worst is None or y < worst[1]:
worst = (x, y)
if worst:
bad.append(u'%s 伸进台阶切掉的区域(最深处 x %.2f / y %.2f < %.2f)'
% (name, worst[0], worst[1], STEP_POUR_Y))
# 内层铜面必须绕开缺口
nx0, _a, nx1, ny1 = notch_cut()
if nx1 - nx0 < fp_edge.NOTCH_W + 2 * fp_edge.LEAD_IN:
bad.append(u'缺口挖得比口部还窄')
# 两个岛不许互相重叠(同层,优先级只有 1/2 之差,重叠就说不清谁裁谁)
b = V12_ISLAND
for a in VO_RECTS:
if a[0] < b[2] and b[0] < a[2] and a[1] < b[3] and b[1] < a[3]:
bad.append(u'VO 岛和 +12V 岛在 L3 上重叠')
# 岛不许压到铣穿的槽(含 0.5 净距)—— 客户端会自己让,但让出来的形状谁也没核过
for (x0, y0, x1, y1) in B.mill_cuts():
for a in VO_RECTS + [b]:
if a[0] < x1 + 0.5 and x0 - 0.5 < a[2] and a[1] < y1 + 0.5 and y0 - 0.5 < a[3]:
bad.append(u'L3 岛 %s 压到槽 (%.1f~%.1f, %.1f~%.1f)' % (a, x0, x1, y0, y1))
return bad
OPEN = ("await eda.dmt_EditorControl.openDocument('%s');"
"await new Promise(r=>setTimeout(r,2000));" % PCB)
JS_DEL = OPEN + u"""
let n = 0;
for (const old of await eda.pcb_PrimitivePour.getAll()) {
await eda.pcb_PrimitivePour.delete(old.primitiveId); n++; }
return {deleted: n};
"""
JS_MAKE = OPEN + u"""
const PLAN = %s;
for (const p of PLAN) {
const poly = await eda.pcb_MathPolygon.createComplexPolygon(p.src);
if (!poly) continue;
try { await eda.pcb_PrimitivePour.create(p.net, p.layer, poly, 'solid', false,
p.name, p.prio, 10); } catch (e) {}
}
return {made: (await eda.pcb_PrimitivePour.getAll())
.map(x => String(x.pourName || '').toUpperCase())};
"""
JS_PRIO = OPEN + u"""
const P = %s;
let k = 0;
for (const o of await eda.pcb_PrimitivePour.getAll()) {
const want = P[String(o.pourName || '').toUpperCase()];
if (want === undefined) continue;
try { await eda.pcb_PrimitivePour.modify(o.primitiveId, {pourPriority: want}); k++; }
catch (e) {}
}
return {set: k, now: (await eda.pcb_PrimitivePour.getAll())
.map(o => [String(o.pourName||''), o.pourPriority])};
"""
JS_REBUILD = OPEN + u"""
const sleep = ms => new Promise(r => setTimeout(r, ms));
const propsOf = el => { for (const k of Object.keys(el)) if (k.startsWith('__reactProps$')) return el[k]; return null; };
const fire = (el) => { let t = el; for (let i=0;i<8&&t;i++){const q=propsOf(t);
if(q&&typeof q.onClick==='function'){q.onClick({target:t,currentTarget:t,stopPropagation(){},preventDefault(){},nativeEvent:{},button:0,detail:1});return true;} t=t.parentElement;} return false; };
const leaf = (txt) => { for (const el of document.querySelectorAll('*'))
if (el.children.length===0 && (el.textContent||'').trim()===txt) return el; return null; };
const btn = (txt) => { for (const b of document.querySelectorAll('button'))
if ((b.innerText||'').trim()===txt && b.offsetParent!==null) return b; return null; };
if (!fire(leaf('工具'))) return {err: 'no 工具'};
await sleep(900);
if (!fire(leaf('铺铜管理器...'))) return {err: 'no 铺铜管理器'};
await sleep(2500);
const b = btn('重建所有');
if (!b) return {err: 'no 重建所有'};
return {fired: fire(b)};
"""
JS_POLL = u"return {poured: (await eda.pcb_PrimitivePoured.getAll()).length};"
JS_DONE = u"""
const propsOf = el => { for (const k of Object.keys(el)) if (k.startsWith('__reactProps$')) return el[k]; return null; };
const fire = (el) => { let t = el; for (let i=0;i<8&&t;i++){const q=propsOf(t);
if(q&&typeof q.onClick==='function'){q.onClick({target:t,currentTarget:t,stopPropagation(){},preventDefault(){},nativeEvent:{},button:0,detail:1});return true;} t=t.parentElement;} return false; };
let ok = null;
for (const b of document.querySelectorAll('button'))
if ((b.innerText||'').trim()==='确认' && b.offsetParent!==null) { ok = b; break; }
if (ok) fire(ok);
await new Promise(r => setTimeout(r, 1500));
await eda.pcb_Document.save();
return {poured: (await eda.pcb_PrimitivePoured.getAll()).length,
pour: (await eda.pcb_PrimitivePour.getAll()).length};
"""
def call(js, timeout=120):
r = run(js, timeout=timeout)
if not r.get('success'):
print(u'失败: ' + json.dumps(r, ensure_ascii=False)[:600])
sys.exit(1)
return r['result']
def main():
ps = plan()
print(u'计划建 %d 块铜面:' % len(ps))
for name, net, layer, rings, prio in ps:
npt = sum((len(r) + 1) // 3 for r in rings)
print(u' %-8s net=%-6s layer=%-3d %d 环 / %3d 点 prio=%d'
% (name, net, layer, len(rings), npt, prio))
bad = selfcheck(ps)
if bad:
print(u'[X] 自检不过:')
for b in bad:
print(u' ' + b)
sys.exit(1)
print(u'[OK] 自检通过:多边形全闭合 / 外层铜不进金手指区 / 内层绕开缺口 / 两岛不重叠')
if '--push' not in sys.argv and '--rebuild' not in sys.argv:
print(u'(未加 --push,没有下发)')
return
if '--rebuild' not in sys.argv:
print(u'清掉旧铜面: %d 块' % call(JS_DEL)['deleted'])
for k in range(0, len(ps), 3): # 3 块一批,稳在超时以内
batch = [{'name': n, 'net': net, 'layer': l, 'src': s, 'prio': pr}
for n, net, l, s, pr in ps[k:k + 3]]
made = call(JS_MAKE % json.dumps(batch), timeout=180)['made']
print(u' 已建 %d 块' % len(made))
want = {n.upper() for n, _, _, _, _ in ps}
got = set(call("return (await eda.pcb_PrimitivePour.getAll())"
".map(x=>String(x.pourName||'').toUpperCase());"))
if not want <= got:
print(u'[X] 没建上: %s' % (want - got))
sys.exit(1)
# ⚠ create() 的 pourPriority 参数不生效(R1 skill §9j),建完再逐块设
r = call(JS_PRIO % json.dumps({n.upper(): pr for n, _, _, _, pr in ps}), timeout=180)
print(u' 优先级已设 %d 块: %s' % (r['set'], r['now']))
before = len(call("return (await eda.pcb_PrimitivePour.getAll()).map(o=>o.pourName);"))
r = call(JS_REBUILD, timeout=120)
if r.get('err'):
print(u'[X] 打不开铺铜管理器: %s' % r['err'])
sys.exit(1)
# ⚠ 退出条件是「**数量稳定**」,不是「全部填上」。
# 有的铜面本来就填不出来(`V12-L3` 在 `+12V` 还没有过孔扎进 L3 之前是孤岛,
# 会被「移除孤岛」删掉),写成 `n >= len(ps)` 的话每次都要空转满 200 秒,
# 而且看上去像是卡死。
prev, same = -1, 0
for _i in range(40):
time.sleep(5)
n = call(JS_POLL, timeout=60)['poured']
print(u' 填充 %d / %d' % (n, len(ps)))
if n >= len(ps):
break
same = same + 1 if n == prev else 0
prev = n
if same >= 2 and n > 0:
print(u' (%d 块已稳定,另 %d 块没有可接的铜,属正常)' % (n, len(ps) - n))
break
fin = call(JS_DONE, timeout=120)
print(u' 收尾: %s' % fin)
# ⛔ 铺铜**边框**会莫名其妙变少(实测 6→3、5→2)。哪一步删的还没查清,
# 但「静默变少」是最坏的形态:铜面没了,DRC 只报连接性,看上去像布线没做完。
# 这里把它变成硬报错。
if fin['pour'] < before:
print(u' [X] 铺铜边框从 %d 变成 %d —— 有东西把它们删了,**不要在这个状态下导出**'
% (before, fin['pour']))
sys.exit(1)
if __name__ == '__main__':
main()