import json,pathlib,collections,math ROOT=pathlib.Path(__file__).resolve().parents[1] raw=(ROOT/'public/drawings/광터지하차도_일반도(BLOCK7).dxf').read_text(encoding='utf8',errors='replace').splitlines() pairs=[(int(raw[i].strip()),raw[i+1].strip()) for i in range(0,len(raw)-1,2)] entities=[]; current=None; section=''; block=None; blocks={} for code,value in pairs: if code==0: if current: if section=='ENTITIES': entities.append(current) elif section=='BLOCKS' and block and current['type'] not in ('BLOCK','ENDBLK'): blocks.setdefault(block,[]).append(current) current={'type':value,'data':[]} if value=='ENDBLK': block=None elif current: current['data'].append((code,value)) if current['type']=='SECTION' and code==2: section=value if current['type']=='BLOCK' and code==2: block=value def get(e,c,d=None): return next((v for k,v in e['data'] if k==c),d) def point(e,c=10): return [float(get(e,c,0)),float(get(e,c+10,0))] def layer(e): return get(e,8,'') out=ROOT/'docs/extracted';out.mkdir(exist_ok=True) texts=[{'text':get(e,1,''),'p':point(e),'layer':layer(e)} for e in entities if e['type'] in ('TEXT','MTEXT')] lines=[{'a':point(e),'b':point(e,11),'layer':layer(e),'handle':get(e,5)} for e in entities if e['type']=='LINE'] for name,data in [('texts',texts),('lines',lines),('blocks',blocks),('inserts',[{'name':get(e,2),'p':point(e),'sx':get(e,41,'1'),'sy':get(e,42,'1'),'rotation':get(e,50,'0'),'layer':layer(e)} for e in entities if e['type']=='INSERT'])]: (out/(name+'.json')).write_text(json.dumps(data,ensure_ascii=False,indent=2),encoding='utf8') print('Entities',collections.Counter(e['type'] for e in entities)) for t in texts: if any(s in t['text'] for s in ('BLOCK7','106.675','107.307','101.636','101.069','102.436','101.869','299.010','318.990','선형','측구','20.100')): print(t) for name,top in [('A',-74271),('B',-91211)]: selected=[l for l in lines if l['layer'] in ('CS-CONC-MAJR','CS-CONC-MINR') and all(1118000{l["handle"]} / {l["layer"]}') for t in texts: x,y=t['p'] if x0{html.escape(t["text"])}') # 공동구 삽입 블록의 실제 선 포함 for e in entities: if e['type']=='INSERT' and get(e,2)==ductName: x,y=point(e);sx=float(get(e,41,1));sy=float(get(e,42,1)) if x0') svg=f''+''.join(content)+'' (previewDir/(title+'.svg')).write_text(svg,encoding='utf8') print('모델 추출:',len(parts),'객체. 단면 원본 선 114쌍, 공동구 20쌍 × 좌우.')