tools/epro2/kicad: Phase-2 lib_symbols — render symbol bodies from SYMBOL docs
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>
This commit is contained in:
@@ -19,6 +19,7 @@ import math
|
||||
import uuid as _uuid
|
||||
from dataclasses import dataclass
|
||||
|
||||
from ..project_relations import ProjectRelations
|
||||
from ..relations import Relations
|
||||
from ..replay import Document
|
||||
from .sexpr import Sym, to_sexpr
|
||||
@@ -41,6 +42,8 @@ class WriteStats:
|
||||
symbol_placements: int = 0
|
||||
text: int = 0
|
||||
skipped: int = 0
|
||||
lib_symbols_embedded: int = 0
|
||||
lib_symbols_missing: int = 0
|
||||
|
||||
|
||||
def _new_uuid() -> str:
|
||||
@@ -66,11 +69,18 @@ def write_sch_page(
|
||||
*,
|
||||
title: str | None = None,
|
||||
sheet_origin_mm: tuple[float, float] = (25.4, 25.4),
|
||||
project_relations: ProjectRelations | None = None,
|
||||
) -> str:
|
||||
"""Render a single SCH_PAGE Document as kicad_sch text.
|
||||
|
||||
``sheet_origin_mm`` is added to every coordinate so the schematic doesn't
|
||||
sit at (0,0) (KiCad's title block lives there). Default 1 inch margin.
|
||||
|
||||
When ``project_relations`` is given, the ``lib_symbols`` block is
|
||||
populated by resolving each placement's ``partId`` to the SYMBOL doc(s)
|
||||
hosting that PART; the body of the first matching SYMBOL is rendered via
|
||||
:func:`sym_writer.write_lib_symbol`. If a partId can't be resolved, we
|
||||
still emit the placement (KiCad shows a red ``?``).
|
||||
"""
|
||||
if doc.doc_type != "SCH_PAGE":
|
||||
raise ValueError(f"expected SCH_PAGE doc, got {doc.doc_type!r}")
|
||||
@@ -165,6 +175,10 @@ def write_sch_page(
|
||||
or doc.doc_uuid[:12]
|
||||
)
|
||||
|
||||
# Populate lib_symbols block from used partIds (Phase 2).
|
||||
lib_symbols = _build_lib_symbols(rel, project_relations, stats) \
|
||||
if project_relations is not None else [Sym("lib_symbols")]
|
||||
|
||||
sch: list = [
|
||||
Sym("kicad_sch"),
|
||||
[Sym("version"), KICAD_SCH_VERSION],
|
||||
@@ -175,7 +189,7 @@ def write_sch_page(
|
||||
[Sym("title"), sch_title],
|
||||
[Sym("comment"), 1, f"epro2 doc_uuid: {doc.doc_uuid}"],
|
||||
[Sym("comment"), 2, f"editor: {doc.head.get('editVersion','')}"]],
|
||||
[Sym("lib_symbols")], # empty for Phase 1; Phase 2 will populate
|
||||
lib_symbols,
|
||||
*elements,
|
||||
[Sym("sheet_instances"),
|
||||
[Sym("path"), "/", [Sym("page"), "1"]]],
|
||||
@@ -184,3 +198,46 @@ def write_sch_page(
|
||||
# Stash stats on the function for tests / CLI to inspect.
|
||||
write_sch_page.last_stats = stats # type: ignore[attr-defined]
|
||||
return to_sexpr(sch, pretty=True)
|
||||
|
||||
|
||||
def _build_lib_symbols(
|
||||
sch_rel: Relations,
|
||||
pr: ProjectRelations,
|
||||
stats: WriteStats,
|
||||
) -> list:
|
||||
"""Resolve every COMPONENT.partId on this sheet to a SYMBOL doc and
|
||||
embed its body in the ``(lib_symbols ...)`` block.
|
||||
|
||||
Returns the full ``[Sym("lib_symbols"), <symbol entry>, ...]`` list.
|
||||
"""
|
||||
from .sym_writer import write_lib_symbol # local to avoid cycle
|
||||
|
||||
used_part_ids: set[str] = set()
|
||||
for cid, comp in sch_rel.components.items():
|
||||
pid = comp.get("partId")
|
||||
if pid:
|
||||
used_part_ids.add(str(pid))
|
||||
|
||||
block: list = [Sym("lib_symbols")]
|
||||
seen_emitted: set[str] = set()
|
||||
for pid in sorted(used_part_ids):
|
||||
sym_docs = pr.parts_by_id.get(pid, [])
|
||||
if not sym_docs:
|
||||
stats.lib_symbols_missing += 1
|
||||
continue
|
||||
# Use first SYMBOL doc. Skip if same partId already emitted (dedupe).
|
||||
if pid in seen_emitted:
|
||||
continue
|
||||
sym_doc = pr.project.documents.get(sym_docs[0])
|
||||
if not sym_doc:
|
||||
stats.lib_symbols_missing += 1
|
||||
continue
|
||||
entry = write_lib_symbol(sym_doc)
|
||||
if entry is None:
|
||||
stats.lib_symbols_missing += 1
|
||||
continue
|
||||
block.append(entry)
|
||||
seen_emitted.add(pid)
|
||||
stats.lib_symbols_embedded += 1
|
||||
|
||||
return block
|
||||
|
||||
Reference in New Issue
Block a user