Phase 1 emit的 .kicad_sch 里组件位置 + 属性都对,但 lib_symbols 是空
stub —— KiCad 渲染时每个组件显示成红色 "?"。Phase 2 把 SYMBOL 文档
里的 PART + RECT/POLY/CIRCLE/TEXT/PIN primitives 翻成 KiCad lib symbol
块,填到 lib_symbols 里,让 KiCad 显示真正的原理图符号。
新增 tools/epro2/kicad/sym_writer.py:
write_lib_symbol(symbol_doc) → S-expr list 形如:
(symbol "facere:<partId>"
(pin_numbers (hide no))
(pin_names (offset 1.016))
(in_bom yes) (on_board yes)
(property "Reference" "U" ...)
(property "Value" "<title>" ...)
(property "Footprint" "" hide)
(property "Datasheet" "" hide)
(symbol "<partId>_1_1"
(rectangle ...) ← from RECT.dotX1/Y1/dotX2/Y2
(polyline (pts ...)) ← from POLY.points + closed → fill
(circle ...) ← from CIRCLE.center/radius
(text "..." ...) ← from TEXT.value/x/y/rotation
(pin <type> line (at ...) (length ...) (name ...) (number ...))
← from PIN + sibling ATTR ops
))
PIN 名字/编号/电气类型解析(这是关键数据探测点):
EPRO2 PIN 不直接带 number/name/type 字段;这些信息存为独立 ATTR 操作
(parentId=<pin_id>, key="Pin Name"/"Pin Number"/"Pin Type")
Pin Type 取值映射:IN→input, OUT→output, BIDIR→bidirectional,
POWER_IN→power_in, POWER_OUT→power_out, NC→no_connect, ...
默认 passive(保守)
sch_writer 集成(lib_symbols 自动填):
write_sch_page(doc, project_relations=pr) — 增 pr 可选参数
内部 _build_lib_symbols(): 收集本 sheet 用到的 partIds → 通过
ProjectRelations.parts_by_id 解析到 SYMBOL 文档 → write_lib_symbol →
组装 (lib_symbols ...) 块;同 partId 多 SYMBOL 候选取第一个,去重
WriteStats 增 lib_symbols_embedded / lib_symbols_missing
CLI 加 --no-lib-symbols 用于回到 Phase-1 行为(占位符调试用)。
ESP-VoCat 重导出验证:9/9 SCH_PAGE 全部 0 lib_miss
P1_45092758.kicad_sch wires=187 symbols=138 lib_emb=29
codec_0b0163fa.kicad_sch wires=190 symbols=112 lib_emb=20
Interface_b336a7c7.kicad_sch symbols=95 lib_emb=13
...
P1_408c9f4f.kicad_sch wires= 6 symbols= 10 lib_emb= 3
测试:6 个新单测覆盖 outer wrapper / pin ATTR pull / 多形状 primitives /
sch_writer 集成路径 / 缺失 lib 计数 / no-pr 回退到 Phase 1。
合计 **39/39 通过**(parser 6 + relations 9 + project_relations 6 +
sexpr 6 + sch_writer 6 + sym_writer 6)。
下一步 Phase 3:footprint library + .kicad_pcb 导出。
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
127 lines
5.3 KiB
Python
127 lines
5.3 KiB
Python
"""Symbol writer regression: synthetic SYMBOL doc → KiCad lib symbol entry."""
|
|
|
|
from tools.epro2.kicad._sexpr_reader import parse
|
|
from tools.epro2.kicad.sch_writer import write_sch_page
|
|
from tools.epro2.kicad.sexpr import to_sexpr
|
|
from tools.epro2.kicad.sym_writer import write_lib_symbol
|
|
from tools.epro2.project_relations import ProjectRelations
|
|
from tools.epro2.replay import Document, Project
|
|
|
|
|
|
def _sym_doc(uuid: str, part_id: str, primitives: list[tuple[str, dict]]) -> Document:
|
|
d = Document(doc_uuid=uuid, doc_type="SYMBOL")
|
|
d.objects[part_id] = {"_type": "PART", "BBOX": [-10, 10, 10, -10], "title": part_id}
|
|
for k, v in primitives:
|
|
d.objects[k] = v
|
|
return d
|
|
|
|
|
|
def _block(parsed, name):
|
|
return [c for c in parsed if isinstance(c, list) and c and c[0] == name]
|
|
|
|
|
|
def test_write_lib_symbol_emits_outer_wrapper_and_body():
|
|
d = _sym_doc("sym1", "MyPart.1", [])
|
|
entry = write_lib_symbol(d)
|
|
assert entry is not None
|
|
text = to_sexpr(entry)
|
|
p = parse(text)
|
|
assert p[0] == "symbol"
|
|
assert p[1] == "facere:MyPart.1"
|
|
# Properties: Reference / Value / Footprint / Datasheet
|
|
props = _block(p, "property")
|
|
by_name = {x[1]: x[2] for x in props}
|
|
assert by_name["Reference"] == "U"
|
|
assert by_name["Value"] == "MyPart.1"
|
|
assert by_name["Footprint"] == ""
|
|
# Inner body wrapper named <part>_1_1
|
|
inner = _block(p, "symbol")
|
|
assert len(inner) == 1
|
|
assert inner[0][1] == "MyPart.1_1_1"
|
|
|
|
|
|
def test_pin_renders_with_attr_pulled_metadata():
|
|
d = _sym_doc("sym1", "MyPart.1", [
|
|
("e5", {"_type": "PIN", "partId": "MyPart.1",
|
|
"x": -15, "y": 0, "length": 10, "rotation": 0}),
|
|
("e6", {"_type": "ATTR", "parentId": "e5", "key": "Pin Name", "value": "VCC"}),
|
|
("e7", {"_type": "ATTR", "parentId": "e5", "key": "Pin Number", "value": "1"}),
|
|
("e8", {"_type": "ATTR", "parentId": "e5", "key": "Pin Type", "value": "POWER_IN"}),
|
|
])
|
|
entry = write_lib_symbol(d)
|
|
text = to_sexpr(entry)
|
|
parsed = parse(text)
|
|
inner_body = next(c for c in parsed if isinstance(c, list) and c and c[0] == "symbol")
|
|
pins = [c for c in inner_body if isinstance(c, list) and c and c[0] == "pin"]
|
|
assert len(pins) == 1
|
|
pin = pins[0]
|
|
assert pin[1] == "power_in" # electrical type mapped
|
|
assert pin[2] == "line" # shape
|
|
name_block = next(c for c in pin if isinstance(c, list) and c and c[0] == "name")
|
|
num_block = next(c for c in pin if isinstance(c, list) and c and c[0] == "number")
|
|
assert name_block[1] == "VCC"
|
|
assert num_block[1] == "1"
|
|
|
|
|
|
def test_rect_poly_circle_text_emit_correct_shapes():
|
|
d = _sym_doc("sym1", "MyPart.1", [
|
|
("r1", {"_type": "RECT", "partId": "MyPart.1",
|
|
"dotX1": -10, "dotY1": -5, "dotX2": 10, "dotY2": 5}),
|
|
("p1", {"_type": "POLY", "partId": "MyPart.1",
|
|
"points": [{"x": 0, "y": 0}, {"x": 5, "y": 0}, {"x": 5, "y": 5}],
|
|
"closed": True}),
|
|
("c1", {"_type": "CIRCLE", "partId": "MyPart.1",
|
|
"centerX": 0, "centerY": 0, "radius": 3}),
|
|
("t1", {"_type": "TEXT", "partId": "MyPart.1",
|
|
"x": 0, "y": -5, "value": "label"}),
|
|
])
|
|
entry = write_lib_symbol(d)
|
|
parsed = parse(to_sexpr(entry))
|
|
inner = next(c for c in parsed if isinstance(c, list) and c[0] == "symbol")
|
|
types = [c[0] for c in inner if isinstance(c, list)]
|
|
assert "rectangle" in types
|
|
assert "polyline" in types
|
|
assert "circle" in types
|
|
assert "text" in types
|
|
|
|
|
|
def test_non_symbol_doc_returns_none():
|
|
d = Document(doc_uuid="x", doc_type="SCH_PAGE")
|
|
assert write_lib_symbol(d) is None
|
|
|
|
|
|
def test_sch_writer_embeds_lib_symbols_via_project_relations():
|
|
sym = _sym_doc("sym1", "MyPart.1", [
|
|
("e5", {"_type": "PIN", "partId": "MyPart.1", "x": 0, "y": 0, "length": 10}),
|
|
("e6", {"_type": "ATTR", "parentId": "e5", "key": "Pin Name", "value": "1"}),
|
|
("e7", {"_type": "ATTR", "parentId": "e5", "key": "Pin Number", "value": "1"}),
|
|
])
|
|
sch = Document(doc_uuid="sch1", doc_type="SCH_PAGE")
|
|
sch.objects["e1"] = {"_type": "COMPONENT", "partId": "MyPart.1", "x": 0, "y": 0, "rotation": 0}
|
|
sch.objects["e2"] = {"_type": "COMPONENT", "partId": "GhostPart.99", "x": 50, "y": 50}
|
|
|
|
p = Project(project_uuid="p")
|
|
p.documents["sym1"] = sym
|
|
p.documents["sch1"] = sch
|
|
pr = ProjectRelations.build(p)
|
|
|
|
text = write_sch_page(sch, project_relations=pr)
|
|
parsed = parse(text)
|
|
lib = next(c for c in parsed if isinstance(c, list) and c[0] == "lib_symbols")
|
|
embedded = [c for c in lib[1:] if isinstance(c, list) and c[0] == "symbol"]
|
|
assert len(embedded) == 1 # MyPart found, GhostPart missed
|
|
assert embedded[0][1] == "facere:MyPart.1"
|
|
stats = getattr(write_sch_page, "last_stats")
|
|
assert stats.lib_symbols_embedded == 1
|
|
assert stats.lib_symbols_missing == 1
|
|
|
|
|
|
def test_sch_writer_without_project_relations_emits_empty_lib_symbols():
|
|
sch = Document(doc_uuid="sch1", doc_type="SCH_PAGE")
|
|
sch.objects["e1"] = {"_type": "COMPONENT", "partId": "X.1", "x": 0, "y": 0}
|
|
text = write_sch_page(sch) # no pr passed
|
|
parsed = parse(text)
|
|
lib = next(c for c in parsed if isinstance(c, list) and c[0] == "lib_symbols")
|
|
# Phase-1 stub: just `(lib_symbols)` with no children
|
|
assert lib == ["lib_symbols"]
|