Bisect found two semantics mismatches between EPRO2 and KiCad that cause the 850 real-connectivity ERC violations on the ESP-VoCat ref project: 1. sym_writer was emitting lib coords without negating Y, but KiCad lib uses Y-up and re-flips Y on placement (Y-down schematic). So vertically arranged pins ended up at Y-mirrored absolute positions and wires that reach the geometric pin tip in EPRO2 missed the rendered pin tip in KiCad. Fix: lib_y = -epro2_y, lib_rot = (360 - rot) % 360 for pin/text. 2. sch_writer was treating each LINE as an isolated wire — but EPRO2 binds segments into nets by NAME (WIRE.NET attr), not just geometry. Multi-segment nets like GND/VBUS show up as N disconnected stubs to KiCad. Fix: per-LINE, look up lineGroup → WIRE → NET attr and emit a `(label "<NET>")` at the LINE's start. Same-named labels on distinct physical wires is how KiCad's ERC recognizes a multi-segment net. ESP-VoCat 9 sheets: wire_dangling 444 → 52 (-88%) pin_not_connected 406 → 196 (-52%) real connectivity total 850 → 248 (-71%) Why we did NOT round to grid (the obvious-looking fix): EPRO2 places some pins on a 10-mil pitch (e.g. magnetic socket); rounding to KiCad's default 50-mil ERC grid would collapse those pins. The 248 residual is fundamentally cross-sheet — single-sheet ERC can't see a net's other endpoints on sibling sheets — and is a Phase-3 (hierarchical sheet) problem, not a per-sheet one. 41 → 46 unit tests pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
97 lines
3.3 KiB
Python
97 lines
3.3 KiB
Python
"""CLI: convert EPRO2 SCH_PAGE docs to KiCad ``.kicad_sch`` files.
|
|
|
|
Usage:
|
|
uv run python -m tools.epro2.kicad <project_dir> --doc <sch_uuid> --out <dir>
|
|
uv run python -m tools.epro2.kicad <project_dir> --all-sch --out <dir>
|
|
|
|
The ``--all-sch`` form converts every SCH_PAGE in the project, naming each
|
|
output by its document title (or doc_uuid prefix as fallback).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import re
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from ..project_relations import ProjectRelations
|
|
from ..replay import Project, replay_project
|
|
from .sch_writer import write_sch_page
|
|
|
|
|
|
_SAFE_CHARS = re.compile(r"[^A-Za-z0-9._\-一-鿿]+")
|
|
|
|
|
|
def _safe_filename(s: str) -> str:
|
|
s = _SAFE_CHARS.sub("_", s).strip("_")
|
|
return s or "untitled"
|
|
|
|
|
|
def _convert_one(
|
|
proj: Project,
|
|
doc_uuid: str,
|
|
out_dir: Path,
|
|
pr: ProjectRelations | None = None,
|
|
) -> Path:
|
|
if doc_uuid not in proj.documents:
|
|
candidates = [u for u in proj.documents if u.startswith(doc_uuid)]
|
|
if len(candidates) != 1:
|
|
raise SystemExit(f"no unique match for {doc_uuid!r} (candidates: {candidates[:5]})")
|
|
doc_uuid = candidates[0]
|
|
doc = proj.documents[doc_uuid]
|
|
if doc.doc_type != "SCH_PAGE":
|
|
raise SystemExit(f"doc {doc_uuid} is {doc.doc_type!r}, not SCH_PAGE")
|
|
|
|
text = write_sch_page(doc, project_relations=pr)
|
|
title = (doc.objects.get("META") or {}).get("title") or doc_uuid[:12]
|
|
out_path = out_dir / f"{_safe_filename(title)}_{doc_uuid[:8]}.kicad_sch"
|
|
out_path.write_text(text, encoding="utf-8")
|
|
stats = getattr(write_sch_page, "last_stats", None)
|
|
if stats:
|
|
print(
|
|
f" {out_path.name}: wires={stats.wires} symbols={stats.symbol_placements} "
|
|
f"text={stats.text} labels={stats.labels} skipped={stats.skipped} "
|
|
f"lib_emb={stats.lib_symbols_embedded} lib_miss={stats.lib_symbols_missing}"
|
|
)
|
|
return out_path
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
ap = argparse.ArgumentParser(description="EPRO2 → KiCad schematic exporter (Phase 1)")
|
|
ap.add_argument("project_dir", type=Path)
|
|
g = ap.add_mutually_exclusive_group(required=True)
|
|
g.add_argument("--doc", help="SCH_PAGE doc uuid (or unique prefix) to convert")
|
|
g.add_argument("--all-sch", action="store_true", help="convert every SCH_PAGE")
|
|
ap.add_argument("--out", type=Path, default=Path("data/processed/kicad_sch"))
|
|
ap.add_argument(
|
|
"--no-lib-symbols",
|
|
action="store_true",
|
|
help="skip lib_symbols generation (Phase 1 mode — placements only, red ?)",
|
|
)
|
|
args = ap.parse_args(argv)
|
|
|
|
proj = replay_project(args.project_dir)
|
|
args.out.mkdir(parents=True, exist_ok=True)
|
|
pr = None if args.no_lib_symbols else ProjectRelations.build(proj)
|
|
|
|
if args.doc:
|
|
_convert_one(proj, args.doc, args.out, pr=pr)
|
|
return 0
|
|
|
|
targets = [u for u, d in proj.documents.items() if d.doc_type == "SCH_PAGE"]
|
|
if not targets:
|
|
print("no SCH_PAGE docs in this project", file=sys.stderr)
|
|
return 1
|
|
print(f"Converting {len(targets)} SCH_PAGE docs → {args.out}")
|
|
for u in targets:
|
|
try:
|
|
_convert_one(proj, u, args.out, pr=pr)
|
|
except Exception as e: # noqa: BLE001
|
|
print(f" FAIL {u[:12]}: {e}", file=sys.stderr)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|