"""Convert one EPRO2 SCH_PAGE Document → an EasyEDA Std-shaped JSON file. Same Option-2 contract as ``pcb_writer.py``: hand the raw EPRO2 ``objects`` dict to a downstream adapter; don't pre-compute Std ``shape[]`` strings ourselves. docType=1, layers omitted (schematic has no copper stack-up), BBox in mil, ``head.units = "mil"``. """ from __future__ import annotations from dataclasses import dataclass from ..replay import Document @dataclass class WriteStats: objects: int = 0 bbox_x: float = 0.0 bbox_y: float = 0.0 bbox_w: float = 0.0 bbox_h: float = 0.0 _BBOX_POINT_FIELDS: list[tuple[str, str]] = [ ("x", "y"), ("startX", "startY"), ("endX", "endY"), ("centerX", "centerY"), ] def _gather_bbox_points(doc: Document) -> tuple[float, float, float, float]: xs: list[float] = [] ys: list[float] = [] for obj in doc.objects.values(): for fx, fy in _BBOX_POINT_FIELDS: x = obj.get(fx) y = obj.get(fy) if x is None or y is None: continue try: xs.append(float(x)) ys.append(float(y)) except (TypeError, ValueError): pass if not xs: return (0.0, 0.0, 0.0, 0.0) return (min(xs), min(ys), max(xs) - min(xs), max(ys) - min(ys)) def write_sch_std(doc: Document) -> dict: if doc.doc_type != "SCH_PAGE": raise ValueError(f"expected SCH_PAGE doc, got {doc.doc_type!r}") bbox_x, bbox_y, bbox_w, bbox_h = _gather_bbox_points(doc) epro2_editor = (doc.head or {}).get("editVersion", "") title = (doc.objects.get("META") or {}).get("title") or doc.doc_uuid[:12] result = { "uuid": doc.doc_uuid, "puuid": "", "title": title, "description": "", "docType": 1, "components": {}, "dataStr": { "head": { "docType": "1", "editorVersion": f"facere-epro2/0.1 (epro2 {epro2_editor})", "units": "mil", "epro2_doc_uuid": doc.doc_uuid, "epro2_editor_version": epro2_editor, }, "BBox": { "x": bbox_x, "y": bbox_y, "width": bbox_w, "height": bbox_h, }, "layers": [], # schematic has no copper stack-up "objects": dict(doc.objects), "preference": {}, "netColors": [], "DRCRULE": {}, }, } stats = WriteStats( objects=len(doc.objects), bbox_x=bbox_x, bbox_y=bbox_y, bbox_w=bbox_w, bbox_h=bbox_h, ) write_sch_std.last_stats = stats # type: ignore[attr-defined] return {"success": True, "code": 0, "result": result}