Files
FacereDataset/tools/epro2/tests/test_sch_writer.py
Knowit 9213429a57 tools/epro2/kicad: Phase-1 EPRO2 → KiCad schematic exporter
写第一版 EPRO2 → .kicad_sch 转换:把 SCH_PAGE Document 的 wires +
COMPONENT placements + TEXT 输出到一个可被 KiCad 7+ 打开的 sch 文件。
不含 symbol 主体(lib_symbols 留空 stub),所以 KiCad 里组件会渲染
成红色 "?" 占位,但布线 + 位置 + Designator/Value 属性都正确。完整
symbol 库导出留 Phase 2。

模块结构:
  tools/epro2/kicad/sexpr.py        手写 S-expr emitter,Sym 标记裸符号,
                                    str 自动加引号 + 转义;float 去尾零;
                                    bool→yes/no;NaN/Inf 主动报错
  tools/epro2/kicad/_sexpr_reader.py  极简 S-expr parser,仅给 round-trip
                                    测试用(非完整 KiCad reader)
  tools/epro2/kicad/sch_writer.py   write_sch_page(doc) → str;处理:
                                      LINE  → (wire (pts ...) ...)
                                      COMPONENT → (symbol (lib_id facere:<partId>)
                                                  (at x y rot) (property Reference ...) ...)
                                      TEXT  → (text "..." (at ...))
                                    单位 mil → mm × 0.0254;零长 wire 跳过
  tools/epro2/kicad/__main__.py     CLI: --doc <uuid> | --all-sch

ESP-VoCat 验证(python -m tools.epro2.kicad <project> --all-sch):
  9 SCH_PAGE 全部转换成功
  P1_408c9f4f.kicad_sch    wires=  6  symbols= 10  text=  0  skipped= 2  (370 lines)
  P1_ee409917.kicad_sch    wires= 20  symbols= 14  text=  0  skipped= 3
  P1_54743d77.kicad_sch    wires= 42  symbols= 30  text=  3
  Overview_dc13d6d2.kicad_sch wires=  0  symbols=  1  text= 34   (说明页)
  MCU_510cff33.kicad_sch   wires= 91  symbols= 86  text=  9
  Interface_b336a7c7.kicad_sch wires= 99  symbols= 95  text=  6
  P1_5c38f45b.kicad_sch    wires=179  symbols= 86  text=  9
  P1_45092758.kicad_sch    wires=187  symbols=138  text= 10  (主图)
  codec_0b0163fa.kicad_sch wires=190  symbols=112  text= 10

输出落在 data/processed/kicad_sch/<filename>.kicad_sch(gitignore 内,
可重新生成;不入库)。

测试:6 个 sexpr 测 + 6 个 sch_writer 测,含 round-trip parse 验证。
parser/relations/project_relations 的旧 21 个不动,合计 **33/33 通过**。

下一步:
1. Phase 2 — symbol library 导出 (.kicad_sym),把 SYMBOL doc 的 PIN/RECT/
   TEXT primitives 转 KiCad symbol 主体;填 lib_symbols 块让组件渲染
   出真正的 schematic 符号
2. footprint library + .kicad_pcb 导出
3. 用 KiCad CLI (kicad-cli sch erc) 跑 ERC 校验

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-28 22:29:15 +08:00

110 lines
3.7 KiB
Python

"""Schematic writer regression: synthetic SCH_PAGE → kicad_sch → re-parse."""
from tools.epro2.kicad._sexpr_reader import parse
from tools.epro2.kicad.sch_writer import MIL_TO_MM, write_sch_page
from tools.epro2.replay import Document
def _doc(objs, doc_uuid="d_test"):
d = Document(doc_uuid=doc_uuid, doc_type="SCH_PAGE")
d.head = {"docType": "SCH_PAGE", "editVersion": "3.2.91"}
for k, v in objs:
d.objects[k] = v
return d
def _block(parsed, name):
"""Return all top-level child blocks of a kicad_sch named ``name``."""
return [c for c in parsed if isinstance(c, list) and c and c[0] == name]
def test_writer_emits_paper_title_and_lib_symbols():
d = _doc([("META", {"_type": "META", "title": "Test"})])
text = write_sch_page(d)
p = parse(text)
assert p[0] == "kicad_sch"
assert _block(p, "paper")[0][1] == "A4"
title = _block(p, "title_block")[0]
assert title[1] == ["title", "Test"]
# lib_symbols stub present (Phase 1)
assert _block(p, "lib_symbols") == [["lib_symbols"]]
def test_wires_emit_one_block_per_line_with_mil_to_mm_conversion():
d = _doc([
("ln1", {"_type": "LINE", "lineGroup": "w1",
"startX": 0, "startY": 0, "endX": 1000, "endY": 0}),
])
text = write_sch_page(d, sheet_origin_mm=(0.0, 0.0))
p = parse(text)
wires = _block(p, "wire")
assert len(wires) == 1
pts = wires[0][1] # ['pts', ['xy', x1, y1], ['xy', x2, y2]]
assert pts[0] == "pts"
assert pts[1][1:] == [0.0, 0.0]
assert pts[2][1] == 1000 * MIL_TO_MM # 25.4 mm
assert pts[2][2] == 0.0
assert getattr(write_sch_page, "last_stats").wires == 1
def test_zero_length_wire_skipped():
d = _doc([
("ln1", {"_type": "LINE", "startX": 5, "startY": 5, "endX": 5, "endY": 5}),
])
text = write_sch_page(d)
p = parse(text)
assert _block(p, "wire") == []
stats = getattr(write_sch_page, "last_stats")
assert stats.wires == 0
assert stats.skipped == 1
def test_component_emits_symbol_placement_with_designator():
d = _doc([
("e1", {"_type": "COMPONENT", "partId": "MyPart.1",
"x": 100, "y": 200, "rotation": 90}),
("a1", {"_type": "ATTR", "parentId": "e1",
"key": "Designator", "value": "R1"}),
("a2", {"_type": "ATTR", "parentId": "e1",
"key": "Value", "value": "10kΩ"}),
])
text = write_sch_page(d, sheet_origin_mm=(0.0, 0.0))
p = parse(text)
syms = _block(p, "symbol")
assert len(syms) == 1
sym = syms[0]
# lib_id contains our partId
lib_id = next(c for c in sym if isinstance(c, list) and c[0] == "lib_id")
assert "MyPart.1" in lib_id[1]
# at xy applied
at = next(c for c in sym if isinstance(c, list) and c[0] == "at")
assert at[1] == 100 * MIL_TO_MM
assert at[2] == 200 * MIL_TO_MM
assert at[3] == 90.0
# properties carry ATTR values
props = [c for c in sym if isinstance(c, list) and c[0] == "property"]
by_name = {p[1]: p[2] for p in props}
assert by_name["Reference"] == "R1"
assert by_name["Value"] == "10kΩ"
def test_text_object_emits_text_block_when_non_empty():
d = _doc([
("t1", {"_type": "TEXT", "value": "Hello", "x": 50, "y": 50}),
("t2", {"_type": "TEXT", "value": "", "x": 60, "y": 60}), # skipped
])
text = write_sch_page(d, sheet_origin_mm=(0.0, 0.0))
p = parse(text)
texts = _block(p, "text")
assert len(texts) == 1
assert texts[0][1] == "Hello"
def test_non_sch_page_doc_rejected():
d = Document(doc_uuid="x", doc_type="PCB")
try:
write_sch_page(d)
except ValueError:
return
raise AssertionError("expected ValueError for non-SCH_PAGE doc")