diff --git a/SKILL.md b/SKILL.md index ab77006..359e3ec 100644 --- a/SKILL.md +++ b/SKILL.md @@ -48,7 +48,7 @@ Read only the branches needed for the current task: - Keep `屋面平面图` (`RF`) and `机房小屋面` (`PH`) as distinct source identities. One overlapping RF clip may not satisfy both; resolve their view-suffixed blocks, heights, and roof-cap roles from the drawing evidence. - Model each building separately before campus assembly. - For multi-building/campus requests, write `scope_manifest.json` before modeling. Copy every requested building ID from the user/source inventory; never shrink scope to the easiest drawing set. - - Use `--converter oda` plus `extract_3d_solids.py` for known 3DSOLID/ACIS drawings. + - Use `--converter oda` plus `extract_3d_solids.py` for known 3DSOLID/ACIS drawings. For ordinary DWG input, `--converter auto` tries native LibreDWG, then the packaged WASM bridge, then ODA; do not manually assemble the WASM commands unless the report records a converter failure. - Stop and request DWG/DXF/vector conversion for PDF-only input. 3. Write and validate an explicit config. diff --git a/package-lock.json b/package-lock.json index adf0cd7..0a8c4d1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "@gltf-transform/core": "^4.4.1", "@gltf-transform/extensions": "^4.4.1", "@gltf-transform/functions": "^4.4.1", + "@mlightcad/libredwg-web": "0.7.8", "draco3dgltf": "^1.5.7" }, "devDependencies": { @@ -532,6 +533,15 @@ "url": "https://opencollective.com/libvips" } }, + "node_modules/@mlightcad/libredwg-web": { + "version": "0.7.8", + "resolved": "https://registry.npmjs.org/@mlightcad/libredwg-web/-/libredwg-web-0.7.8.tgz", + "integrity": "sha512-3ol9nxsCr5YJarcUs8Pv5WM8MvF45H3Gi8M3/GA4o8sahwG0Ehfz8Nh3OnxuX+pxQCt+pm9n8DFCeOf7kaU81A==", + "license": "GPL-3.0", + "engines": { + "node": ">=20" + } + }, "node_modules/@types/ndarray": { "version": "1.0.14", "resolved": "https://registry.npmjs.org/@types/ndarray/-/ndarray-1.0.14.tgz", diff --git a/package.json b/package.json index 76822ff..f5a0220 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "playwright": "^1.52.0" }, "dependencies": { + "@mlightcad/libredwg-web": "0.7.8", "@gltf-transform/core": "^4.4.1", "@gltf-transform/extensions": "^4.4.1", "@gltf-transform/functions": "^4.4.1", diff --git a/references/input_formats.md b/references/input_formats.md index 1223d78..b241f02 100644 --- a/references/input_formats.md +++ b/references/input_formats.md @@ -30,7 +30,8 @@ Tool discovery is environment variable, PATH, then platform defaults: ODA requires existing absolute input/output directories. On macOS the command shape is `ODAFileConverter ACAD2004 DXF 0 1 "*.dwg"`; create both directories first. Bad arguments are often reported only through a GUI dialog, with empty stderr, and the process may return exit 0 while producing nothing. Treat “exit 0 + no DXF” as failure, check the screen for a blocked dialog, and keep the script's ASCII staging plus produced-file verification. -If native converters are unavailable, use the tested WASM bridge: +If native converters are unavailable, `--converter auto` uses the packaged WASM bridge after `npm install`. +For diagnosis or a manual one-file conversion, the equivalent commands are: ```bash npm install --prefix work/vendor-js @mlightcad/libredwg-web diff --git a/scripts/cad_dxf_report.py b/scripts/cad_dxf_report.py index 3d7bcfe..1eafed2 100755 --- a/scripts/cad_dxf_report.py +++ b/scripts/cad_dxf_report.py @@ -17,6 +17,8 @@ from pathlib import Path CAD_SUFFIXES = {".dwg", ".dxf"} +SCRIPT_DIR = Path(__file__).resolve().parent +ROOT = SCRIPT_DIR.parent def parse_args() -> argparse.Namespace: @@ -93,6 +95,91 @@ def run_dwg2dxf(source: Path, dest: Path) -> dict: } +def libredwg_wasm_module_dir() -> Path | None: + """Resolve the packaged WASM libredwg module without installing at runtime.""" + override = os.environ.get("CAD_LIBREDWG_MODULE_DIR") + candidates = [Path(override)] if override else [] + candidates.append(ROOT / "node_modules") + for candidate in candidates: + package = candidate / "@mlightcad" / "libredwg-web" + if (package / "dist" / "libredwg-web.js").exists() and (package / "wasm").is_dir(): + return candidate + return None + + +def run_wasm_libredwg(source: Path, dest: Path, work_dir: Path, vendor_dir: Path) -> dict: + """Convert one DWG through the packaged libredwg WASM bridge.""" + node = shutil.which("node") + module_dir = libredwg_wasm_module_dir() + if not node or module_dir is None: + return { + "source": str(source), + "dest": str(dest), + "status": "missing_libredwg_wasm", + "message": ( + "Install the toolchain Node dependencies with `npm install` or set " + "CAD_LIBREDWG_MODULE_DIR to a node_modules directory containing " + "@mlightcad/libredwg-web." + ), + } + + dump_dir = work_dir / "dwg_json" + dump_dir.mkdir(parents=True, exist_ok=True) + dump = dump_dir / f"{dest.stem}.json" + dest.parent.mkdir(parents=True, exist_ok=True) + try: + dump_proc = subprocess.run( + [ + node, + str(SCRIPT_DIR / "dwg_dump_json.mjs"), + str(source), + str(dump), + "--module-dir", + str(module_dir), + ], + capture_output=True, + timeout=600, + ) + if dump_proc.returncode != 0 or not dump.exists(): + return { + "source": str(source), + "dest": str(dest), + "status": "wasm_dump_failed", + "returncode": dump_proc.returncode, + "stdout": dump_proc.stdout.decode("utf-8", errors="replace")[-2000:], + "stderr": dump_proc.stderr.decode("utf-8", errors="replace")[-2000:], + } + rebuild_proc = subprocess.run( + [ + sys.executable, + str(SCRIPT_DIR / "dwgjson_to_dxf.py"), + "--json", + str(dump), + "--out", + str(dest), + "--vendor-dir", + str(vendor_dir), + ], + capture_output=True, + timeout=600, + ) + except subprocess.TimeoutExpired as exc: + return { + "source": str(source), + "dest": str(dest), + "status": "wasm_timeout", + "message": str(exc), + } + return { + "source": str(source), + "dest": str(dest), + "status": "wasm_ok" if rebuild_proc.returncode == 0 and dest.exists() else "wasm_rebuild_failed", + "returncode": rebuild_proc.returncode, + "stdout": rebuild_proc.stdout.decode("utf-8", errors="replace")[-2000:], + "stderr": rebuild_proc.stderr.decode("utf-8", errors="replace")[-2000:], + } + + def oda_converter() -> str | None: """ODA File Converter: env ODA_CONVERTER > PATH > macOS app-bundle default.""" override = os.environ.get("ODA_CONVERTER") @@ -583,6 +670,21 @@ def main() -> int: if args.allow_partial and not (args.partial_reason or "").strip(): raise SystemExit("--allow-partial requires --partial-reason") + cad_files = find_cad_files(input_dir) + generated_inputs = [] + for source in cad_files: + resolved = source.resolve() + for generated_dir in (raw_dir.resolve(), clean_dir.resolve()): + if resolved == generated_dir or generated_dir in resolved.parents: + generated_inputs.append(source) + break + if generated_inputs: + examples = ", ".join(str(path) for path in generated_inputs[:3]) + raise SystemExit( + "Input CAD files are inside generated work directories and would be deleted before inspection: " + f"{examples}. Use a separate --input directory such as out/dxf_src." + ) + # These folders are generated intermediates. Recreate them so a source # removed between runs cannot survive as a stale DXF and enter modeling. shutil.rmtree(raw_dir, ignore_errors=True) @@ -590,7 +692,6 @@ def main() -> int: raw_dir.mkdir(parents=True, exist_ok=True) clean_dir.mkdir(parents=True, exist_ok=True) - cad_files = find_cad_files(input_dir) used_names: set[str] = set() conversions: list[dict] = [] raw_dxfs: list[Path] = [] @@ -615,8 +716,10 @@ def main() -> int: conversion = {"source": str(source), "dest": str(dest), "status": "oda_failed"} else: conversion = run_dwg2dxf(source, dest) + if conversion["status"] != "ok": + conversion = run_wasm_libredwg(source, dest, work_dir, vendor_dir) conversions.append(conversion) - if conversion["status"] in {"ok", "oda_ok"}: + if conversion["status"] in {"ok", "wasm_ok", "oda_ok"}: raw_dxfs.append(dest) else: if source.resolve() != dest.resolve(): @@ -648,7 +751,7 @@ def main() -> int: if src and (item.get("status") == "failed" or item.get("modelspace_entities") == 0): retry[item["file"]] = src for c in list(conversions): - if c.get("status") in {"failed", "missing_dwg2dxf"} and str(c.get("source", "")).lower().endswith(".dwg"): + if c.get("status") not in {"ok", "wasm_ok", "oda_ok"} and str(c.get("source", "")).lower().endswith(".dwg"): name = Path(c["dest"]).name if c.get("dest") else Path(c["source"]).with_suffix(".dxf").name retry.setdefault(name, Path(c["source"])) if retry and args.converter == "auto": @@ -690,7 +793,9 @@ def main() -> int: # hard gate until every DWG is converted or the run is explicitly partial. dwg_sources = {str(path) for path in cad_files if path.suffix.lower() == ".dwg"} successful_dwg_sources = { - str(item.get("source")) for item in conversions if item.get("status") in {"ok", "oda_ok", "oda_fallback_ok"} + str(item.get("source")) + for item in conversions + if item.get("status") in {"ok", "wasm_ok", "oda_ok", "oda_fallback_ok"} } unresolved_dwgs = sorted(dwg_sources - successful_dwg_sources) failed_files = [item.get("file", "?") for item in results if item.get("status") != "ok"] diff --git a/scripts/cad_showcase.py b/scripts/cad_showcase.py index 90980ca..15c6d54 100644 --- a/scripts/cad_showcase.py +++ b/scripts/cad_showcase.py @@ -451,6 +451,14 @@ def doctor() -> int: except SystemExit: path = None checks.append({"name": executable, "status": "PASS" if path else "OPTIONAL", "path": path}) + wasm_module = ROOT / "node_modules" / "@mlightcad" / "libredwg-web" + checks.append( + { + "name": "libredwg-wasm", + "status": "PASS" if (wasm_module / "dist" / "libredwg-web.js").exists() else "OPTIONAL", + "path": str(wasm_module) if wasm_module.exists() else None, + } + ) for executable, resolver in (("ODAFileConverter", oda_converter), ("blender", default_blender)): candidate = resolver() path = candidate if candidate and Path(candidate).exists() else None diff --git a/tests/test_contracts.py b/tests/test_contracts.py index ab69e8f..4345af6 100644 --- a/tests/test_contracts.py +++ b/tests/test_contracts.py @@ -549,6 +549,34 @@ def test_no_convert_cannot_hide_dwg_sources(tmp_path: Path) -> None: assert "unconverted.dwg" in report["gate"]["detail"] +def test_report_refuses_to_delete_input_inside_generated_work_dir(tmp_path: Path) -> None: + work = tmp_path / "work" + source_dir = work / "dxf_raw" + source_dir.mkdir(parents=True) + source = source_dir / "roof.dxf" + ezdxf.new("R2010").saveas(source) + + proc = subprocess.run( + [ + sys.executable, + str(ROOT / "scripts" / "cad_dxf_report.py"), + "--input", + str(source_dir), + "--work", + str(work), + "--output", + str(tmp_path / "report"), + "--no-convert", + ], + text=True, + capture_output=True, + ) + + assert proc.returncode != 0 + assert "would be deleted before inspection" in proc.stderr + assert source.exists() + + def test_variants_manifest_is_versioned(tmp_path: Path) -> None: spec_path = tmp_path / "spec.json" spec_path.write_text(json.dumps(minimal_spec()), encoding="utf-8")