"""Read-only DWG template analysis. Every conversion and report stays in 도면작성.""" from collections import Counter import hashlib import json from pathlib import Path import subprocess import sys ROOT = Path(__file__).resolve().parent PROJECT = ROOT.parent sys.path.insert(0, str(PROJECT / 'pierzainer_ground')) from pierzainer_ground.dxf_terrain import read_source_document from ezdxf import bbox CACHE = ROOT / '.cache' CACHE.mkdir(exist_ok=True) exe = PROJECT / 'pierzainer_ground/tools/libredwg/dwgread.exe' results = [] for source in sorted(ROOT.glob('*.dwg')): out = CACHE / (source.stem + '.dxf') if not out.exists(): process = subprocess.run([str(exe), '-O', 'DXF', '-o', str(out), str(source)], capture_output=True) (CACHE / (source.stem + '.conversion.log')).write_bytes(process.stdout + process.stderr) if process.returncode or not out.exists(): raise RuntimeError('DWG conversion failed: ' + source.name) doc, _ = read_source_document(out, CACHE) texts = [] dimensions = [] for entity in doc.modelspace(): if entity.dxftype() in ('TEXT', 'MTEXT'): text = entity.plain_text() if entity.dxftype() == 'MTEXT' else entity.dxf.text texts.append(dict(text=text, layer=entity.dxf.layer, height=float(entity.dxf.char_height if entity.dxftype() == 'MTEXT' else entity.dxf.height), point=list(entity.dxf.insert), rotation=float(entity.dxf.get('rotation', 0)))) if entity.dxftype() == 'DIMENSION': dimensions.append(dict(style=entity.dxf.dimstyle, text=entity.dxf.get('text', ''), type=int(entity.dxf.dimtype))) extents = bbox.extents(doc.modelspace(), fast=True) result = dict(file=source.name, sha256=hashlib.sha256(source.read_bytes()).hexdigest(), units=doc.header.get('$INSUNITS'), entity_types=dict(Counter(e.dxftype() for e in doc.modelspace())), extent=[list(extents.extmin), list(extents.extmax)], texts=texts, dimensions=dimensions, layers=[dict(name=l.dxf.name, color=l.dxf.color, linetype=l.dxf.linetype, lineweight=l.dxf.lineweight) for l in doc.layers], dimstyles=[dict(name=d.dxf.name, text_height=d.dxf.get('dimtxt'), arrow_size=d.dxf.get('dimasz'), scale=d.dxf.get('dimscale'), factor=d.dxf.get('dimlfac')) for d in doc.dimstyles], layouts=[dict(name=l.name, attributes=l.dxf.all_existing_dxf_attribs()) for l in doc.layouts]) results.append(result) print(source.name, result['entity_types'], 'texts', len(texts), 'dims', len(dimensions)) print('HEADINGS', [t for t in texts if any(w in t['text'] for w in ('정면', '측면', '평면', '단면', '일반', '축척', 'S=', 'S =', 'EL.', 'A1', 'A3'))][:35]) (ROOT / 'reference-analysis.json').write_text(json.dumps(results, ensure_ascii=False, indent=2, default=str), encoding='utf-8')