import ezdxf, sys, math from collections import defaultdict sys.stdout.reconfigure(encoding='utf-8') SRC = r'D:\★Pier 자료\심수석님 검토 파일\철근 모델랑 운곡교P1_바이브코딩\1.dxf' d = ezdxf.readfile(SRC) ms = d.modelspace() REG = tuple(float(v) for v in sys.argv[1:5]) # x0 x1 y0 y1 : region for dots + bubbles CX = float(sys.argv[5]) # section centre x (to convert x to Y) x0, x1, y0, y1 = REG dots = [] bub = [] lines = [] txt = [] for e in ms: t = e.dxftype() if t == 'CIRCLE': c = e.dxf.center if e.dxf.radius == 27 and x0 < c.x < x1 and y0 < c.y < y1: dots.append((c.x, c.y)) if e.dxf.radius == 240 and x0 - 4000 < c.x < x1 + 4000 and y0 - 1500 < c.y < y1 + 6000: bub.append((c.x, c.y)) elif t == 'LINE' and e.dxf.layer in ('CS-DIML', 'CS-SYMB-LIN3', 'CS-SYMB'): a, b = e.dxf.start, e.dxf.end if x0 - 5000 < min(a.x, b.x) and max(a.x, b.x) < x1 + 5000 and y0 - 1500 < min(a.y, b.y) and max(a.y, b.y) < y1 + 6500: lines.append(((a.x, a.y), (b.x, b.y))) elif t == 'TEXT': p = e.dxf.insert if x0 - 5000 < p.x < x1 + 5000 and y0 - 1500 < p.y < y1 + 6500: txt.append((p.x, p.y, e.dxf.text.strip())) def near(p, q, tol=2.0): return abs(p[0] - q[0]) < tol and abs(p[1] - q[1]) < tol # union-find lines by shared endpoints n = len(lines) par = list(range(n)) def find(i): while par[i] != i: par[i] = par[par[i]] i = par[i] return i pts = defaultdict(list) for i, (a, b) in enumerate(lines): pts[(round(a[0] / 3), round(a[1] / 3))].append(i) pts[(round(b[0] / 3), round(b[1] / 3))].append(i) for k, v in pts.items(): for j in v[1:]: par[find(v[0])] = find(j) comp = defaultdict(list) for i in range(n): comp[find(i)].append(i) res = [] for (bx, by) in bub: # label text inside bubble lab = [t for t in txt if abs(t[0] - bx) < 260 and abs(t[1] - by) < 260 and t[2].startswith('C') and t[2][1:].isdigit()] if not lab: continue name = lab[0][2] ul = by - 252 cand = [i for i, (a, b) in enumerate(lines) if abs(a[1] - ul) < 4 and abs(b[1] - ul) < 4 and min(a[0], b[0]) < bx + 300 and max(a[0], b[0]) > bx - 300] if not cand: res.append((name, 'no underline')) continue got = set() for i in cand: for j in comp[find(i)]: a, b = lines[j] for p in (a, b): for dd in dots: if abs(p[0] - dd[0]) < 2 and 2 < abs(p[1] - dd[1]) < 9: got.add(dd) res.append((name, sorted(got))) for name, g in sorted(res, key=lambda r: r[0]): if isinstance(g, str): print(name, g) continue rows = defaultdict(list) for x, y in g: rows[round(y, 1)].append(round(x - CX)) print(name, {y: sorted(v) for y, v in sorted(rows.items(), reverse=True)})