#!/usr/bin/env python3 """Task 3 exploration: layer usage and long polylines / text clusters in a plan-profile DXF, to locate the tunnel centreline (본선 축선) candidates.""" import collections import json import sys import ezdxf path = sys.argv[1] doc = ezdxf.readfile(path) msp = doc.modelspace() by_layer = collections.Counter(e.dxf.layer for e in msp) print("layers used:", len(by_layer)) for name, n in by_layer.most_common(25): print(f" {n:6d} {name}") # long polylines = candidate alignment / outline strings polys = [] for e in msp: if e.dxftype() == "LWPOLYLINE": pts = list(e.get_points("xy")) if len(pts) >= 20: xs = [p[0] for p in pts]; ys = [p[1] for p in pts] polys.append({"layer": e.dxf.layer, "n": len(pts), "x": [round(min(xs), 1), round(max(xs), 1)], "y": [round(min(ys), 1), round(max(ys), 1)]}) polys.sort(key=lambda d: -d["n"]) print("long LWPOLYLINE (>=20 vertices):", len(polys)) for p in polys[:12]: print(" ", p) # text that looks like station (STA) or level (EL) labels import re pat = re.compile(r"(STA|No\.?|EL|E\.L|\d+\+\d+|\+\d{2,3}\.\d{3}|\d{2,3}\.\d{2,3})", re.I) hits = [] for e in msp: if e.dxftype() in ("TEXT", "MTEXT"): s = e.dxf.text if e.dxftype() == "TEXT" else e.plain_text() if s and pat.search(s): loc = e.dxf.insert if e.dxftype() == "TEXT" else e.dxf.insert hits.append((round(loc.x, 1), round(loc.y, 1), s.strip()[:40])) print("station/level-like texts:", len(hits)) for h in sorted(hits)[:25]: print(" ", h)