"""Okseong P1 G4 helper: match the shape sheet (구조도(4) 철근 상세) to the material table by (L, N) -> 판독/g4_shapes.json.""" import json import re from pathlib import Path HERE = Path(__file__).resolve().parent T = json.loads((HERE / "dxf" / "texts.json").read_text(encoding="utf-8")) tab = {} for x in T: if 714800 <= x["x"] <= 725000 and 324500 <= x["y"] <= 339900 and x["layer"].startswith("CS-TABL"): tab.setdefault(round(x["y"]), []).append((x["x"], x["text"].strip())) table = {} for y, cells in tab.items(): cells = [s for _, s in sorted(cells)] if re.fullmatch(r"[CSP]\d+|UM|B\d+", cells[0]): table[cells[0]] = cells # the table repeats the diameter as '"': resolve it dia = None for m in sorted(table, key=lambda m: -[y for y, c in tab.items() if c and sorted(c)[0][1].strip() == m][0]): c = table[m] if c[1].startswith("H"): dia = c[1] else: c[1] = dia sh = [x for x in T if 689900 <= x["x"] <= 712000 and 324000 <= x["y"] <= 343000 and x["layer"] == "CS-TEXT"] blocks = [(689900, 694500), (695000, 700000), (700500, 706500), (707000, 712000)] rows = [] for x in sh: m = re.fullmatch(r"L=([\d.]+)", x["text"].strip()) if m: L = float(m.group(1)) b = [i for i, (a, c) in enumerate(blocks) if a <= x["x"] < c][0] n = [y for y in sh if re.fullmatch(r"N=\d+", y["text"].strip()) and abs(y["y"] - x["y"]) < 5 and blocks[b][0] <= y["x"] < blocks[b][1]] N = int(n[0]["text"].strip()[2:]) if n else None params = sorted(((y["x"], y["y"], y["text"].strip()) for y in sh if blocks[b][0] <= y["x"] < blocks[b][1] and x["y"] - 500 <= y["y"] < x["y"] - 100 and not re.match(r"[LN]=", y["text"].strip())), key=lambda p: (-round(p[1] / 50), p[0])) rows.append({"L": L, "N": N, "block": b, "y": x["y"], "params": [p[2] for p in params]}) out = {} for r in rows: cand = [m for m, c in table.items() if abs(float(c[2]) - r["L"]) < 1e-6 and int(c[3]) == r["N"]] out.setdefault("/".join(cand) or f"?{r['L']}/{r['N']}", []).append(r) (HERE / "판독" / "g4_shapes.json").write_text(json.dumps({"table": table, "shapes": out}, ensure_ascii=False, indent=1), encoding="utf-8") for k, v in out.items(): print(k, table.get(k.split("/")[0], ["", ""])[1], v[0]["L"], v[0]["N"], v[0]["params"])