文件

41 行
1.4 KiB
Python

#!/usr/bin/env python3
"""导出 EasyEDA 编译后的整工程网表(三种格式)作为转 KiCad 的对账基准。
注意:getNetlistFile() 是**整工程**的,不是当前页 —— 但它仍要求有一个原理图文档
处于打开状态,所以先 openDocument。API 标着 BETA。"""
import json, os, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import eda
ROOT = os.path.dirname(os.path.abspath(__file__))
OUT = os.path.join(ROOT, "eda_src")
PAGE = json.load(open(os.path.join(ROOT, "eda-parts.json"), encoding="utf-8"))["pages"]["P1-INPUT"]
JS = '''
await eda.dmt_EditorControl.openDocument("%s");
await new Promise(r=>setTimeout(r,1200));
const out = {};
for (const t of ["EasyEDA","Protel2","PADS"]) {
const f = await eda.sch_ManufactureData.getNetlistFile("nl", t);
out[t] = f ? await f.text() : null;
}
return out;
''' % PAGE
EXT = {"EasyEDA": "json", "Protel2": "net", "PADS": "asc"}
def main():
os.makedirs(OUT, exist_ok=True)
r = eda.run(JS, timeout=300)
if not r.get("success"):
sys.exit("网表导出失败: " + json.dumps(r, ensure_ascii=False)[:400])
for t, txt in r["result"].items():
if txt is None:
print(f" !! {t} 返回 null"); continue
p = os.path.join(OUT, f"netlist.{EXT[t]}")
open(p, "w", encoding="utf-8", newline="").write(txt)
print(f" {t:9s} -> {os.path.basename(p):15s} {len(txt):>9,d} B")
if __name__ == "__main__":
main()