"""Stage 1: convert source DWG drawings to DXF with ODA File Converter. Mirrors the audit record of the box project's dwg_conversion.py (source/output SHA-256, entity count, drawing_verified=False). RAG sources are read only; existing DXF outputs are never overwritten. """ import hashlib import json import shutil import subprocess import sys import tempfile from pathlib import Path import ezdxf ROOT = Path(__file__).resolve().parents[2] SOURCE_DIR = ROOT / "RAG" OUT_DIR = Path(__file__).resolve().parent / "입력DXF" ODA = Path(r"C:\Program Files\ODA\ODAFileConverter 27.1.0\ODAFileConverter.exe") PATTERN = "C0050205-02*-운곡교 교각도*.dwg" def sha256(path): return hashlib.sha256(path.read_bytes()).hexdigest() def main(): sources = sorted(SOURCE_DIR.glob(PATTERN)) if len(sources) != 3: sys.exit(f"expected 3 source DWG, found {len(sources)}") OUT_DIR.mkdir(exist_ok=True) audit = [] with tempfile.TemporaryDirectory() as tmp: tmp_in, tmp_out = Path(tmp, "in"), Path(tmp, "out") tmp_in.mkdir(), tmp_out.mkdir() # Copy so the converter never touches RAG originals. for src in sources: shutil.copy2(src, tmp_in / src.name) subprocess.run([str(ODA), str(tmp_in), str(tmp_out), "ACAD2018", "DXF", "0", "1", "*.DWG"], check=True, timeout=600) for src in sources: produced = tmp_out / (src.stem + ".dxf") target = OUT_DIR / produced.name if not produced.is_file(): sys.exit(f"conversion failed: {src.name}") if target.exists(): sys.exit(f"existing DXF must not be overwritten: {target.name}") shutil.copy2(produced, target) doc = ezdxf.readfile(target) audit.append(dict(source=str(src.relative_to(ROOT)), engine="ODA File Converter 27.1.0", source_sha256=sha256(src), output=str(target.relative_to(ROOT)), output_sha256=sha256(target), dxf_version=doc.dxfversion, modelspace_entity_count=len(doc.modelspace()), layer_count=len(doc.layers), drawing_verified=False)) (OUT_DIR / "conversion_audit.json").write_text(json.dumps(audit, ensure_ascii=False, indent=2), encoding="utf-8") print(json.dumps(audit, ensure_ascii=False, indent=2)) if __name__ == "__main__": main()