feat: harden CAD showcase skill and delivery pipeline
This commit is contained in:
@@ -8,6 +8,7 @@ import glob
|
||||
import fnmatch
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
@@ -24,7 +25,17 @@ def parse_args() -> argparse.Namespace:
|
||||
parser.add_argument("--work", default="work/cad_showcase", help="Intermediate work folder")
|
||||
parser.add_argument("--output", default="outputs/cad_read_report", help="Output path stem or folder")
|
||||
parser.add_argument("--vendor-dir", default="work/vendor", help="Optional Python vendor dir containing ezdxf")
|
||||
parser.add_argument("--no-convert", action="store_true", help="Skip DWG conversion and only read existing DXF files")
|
||||
parser.add_argument(
|
||||
"--no-convert", action="store_true", help="Skip DWG conversion and only read existing DXF files"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--converter",
|
||||
choices=("auto", "libredwg", "oda"),
|
||||
default="auto",
|
||||
help="auto=libredwg then ODA fallback; choose oda explicitly for 3DSOLID/ACIS drawings",
|
||||
)
|
||||
parser.add_argument("--allow-partial", action="store_true", help="Write a partial report instead of failing")
|
||||
parser.add_argument("--partial-reason", help="Required justification when --allow-partial is used")
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
@@ -36,8 +47,7 @@ def import_ezdxf(vendor_dir: Path):
|
||||
from ezdxf import bbox, recover # type: ignore
|
||||
except ImportError as exc:
|
||||
raise SystemExit(
|
||||
"Missing dependency: ezdxf. Install locally with:\n"
|
||||
f" python3 -m pip install --target {vendor_dir} ezdxf"
|
||||
f"Missing dependency: ezdxf. Install locally with:\n python3 -m pip install --target {vendor_dir} ezdxf"
|
||||
) from exc
|
||||
return ezdxf, bbox, recover
|
||||
|
||||
@@ -54,10 +64,7 @@ def safe_name(path: Path, used: set[str]) -> str:
|
||||
|
||||
|
||||
def find_cad_files(input_dir: Path) -> list[Path]:
|
||||
return sorted(
|
||||
path for path in input_dir.rglob("*")
|
||||
if path.is_file() and path.suffix.lower() in CAD_SUFFIXES
|
||||
)
|
||||
return sorted(path for path in input_dir.rglob("*") if path.is_file() and path.suffix.lower() in CAD_SUFFIXES)
|
||||
|
||||
|
||||
def run_dwg2dxf(source: Path, dest: Path) -> dict:
|
||||
@@ -129,7 +136,8 @@ def run_oda_batch(sources: list[Path], work_dir: Path) -> dict[Path, Path]:
|
||||
try:
|
||||
subprocess.run(
|
||||
[exe, str(in_dir.resolve()), str(out_dir.resolve()), "ACAD2018", "DXF", "0", "1", "*.dwg"],
|
||||
capture_output=True, timeout=1800,
|
||||
capture_output=True,
|
||||
timeout=1800,
|
||||
)
|
||||
except (subprocess.TimeoutExpired, OSError):
|
||||
return {}
|
||||
@@ -240,15 +248,13 @@ def xref_definitions(doc) -> list[dict]:
|
||||
return xrefs
|
||||
|
||||
|
||||
import re as _re
|
||||
|
||||
PLAN_TITLE_RE = _re.compile(r"平面图$")
|
||||
PLAN_TITLE_EXCLUDE_RE = _re.compile(r"(示意|疏散|分区|索引|定位|总平面|放大|详图|布置|大样)")
|
||||
PLAN_TITLE_RE = re.compile(r"平面图$")
|
||||
PLAN_TITLE_EXCLUDE_RE = re.compile(r"(示意|疏散|分区|索引|定位|总平面|放大|详图|布置|大样)")
|
||||
|
||||
# AutoCAD MIF escapes (\M+5XXXX): CJK text in pre-Unicode DWGs survives
|
||||
# DXF conversion as literal escape sequences, so title matching must decode
|
||||
# them first. The digit selects the codepage; XXXX is the hex byte pair.
|
||||
MIF_RE = _re.compile(r"\\M\+([1-5])([0-9A-Fa-f]{4})")
|
||||
MIF_RE = re.compile(r"\\M\+([1-5])([0-9A-Fa-f]{4})")
|
||||
MIF_CODEPAGES = {"1": "cp932", "2": "big5", "3": "cp949", "4": "johab", "5": "gbk"}
|
||||
|
||||
|
||||
@@ -256,7 +262,7 @@ def decode_mif(text: str) -> str:
|
||||
if "\\M+" not in text:
|
||||
return text
|
||||
|
||||
def repl(match: "_re.Match[str]") -> str:
|
||||
def repl(match: "re.Match[str]") -> str:
|
||||
value = int(match.group(2), 16)
|
||||
raw = value.to_bytes(2, "big") if value > 0xFF else value.to_bytes(1, "big")
|
||||
try:
|
||||
@@ -330,11 +336,14 @@ def file_warnings(item: dict) -> list[str]:
|
||||
# pipeline has never seen — the layer mapping must be built consciously,
|
||||
# or extraction silently captures nothing (patterns age, invariants don't).
|
||||
wall_dialects = ("*WALL*", "*墙*", "*QIANG*")
|
||||
layer_names = [l["name"] for l in item.get("layers", []) if l.get("entity_count", 0) > 0]
|
||||
if (plan_titles >= 1 and (entities or 0) >= 400
|
||||
and layer_names
|
||||
and not any(fnmatch.fnmatch(n.upper(), p.upper()) for n in layer_names for p in wall_dialects)):
|
||||
tops = ", ".join(l["name"] for l in item.get("top_layers", [])[:8])
|
||||
layer_names = [layer["name"] for layer in item.get("layers", []) if layer.get("entity_count", 0) > 0]
|
||||
if (
|
||||
plan_titles >= 1
|
||||
and (entities or 0) >= 400
|
||||
and layer_names
|
||||
and not any(fnmatch.fnmatch(n.upper(), p.upper()) for n in layer_names for p in wall_dialects)
|
||||
):
|
||||
tops = ", ".join(layer["name"] for layer in item.get("top_layers", [])[:8])
|
||||
warnings.append(
|
||||
f"unknown_layer_dialect: {plan_titles} plan title(s) but no wall-like layer "
|
||||
f"(*WALL*/*墙*) among {len(layer_names)} populated layers — an unfamiliar design-institute "
|
||||
@@ -380,11 +389,7 @@ def inspect_file(path: Path, ezdxf, bbox, recover) -> dict:
|
||||
msp = doc.modelspace()
|
||||
entities = list(msp)
|
||||
type_counts = Counter(entity.dxftype() for entity in entities)
|
||||
layer_counts = Counter(
|
||||
str(entity.dxf.layer)
|
||||
for entity in entities
|
||||
if entity.dxf.hasattr("layer")
|
||||
)
|
||||
layer_counts = Counter(str(entity.dxf.layer) for entity in entities if entity.dxf.hasattr("layer"))
|
||||
try:
|
||||
geometry_bbox = bbox_tuple(bbox.extents(entities, fast=True))
|
||||
except Exception as exc: # bbox can fail on unsupported proxy geometry.
|
||||
@@ -415,10 +420,7 @@ def inspect_file(path: Path, ezdxf, bbox, recover) -> dict:
|
||||
"modelspace_entities": len(entities),
|
||||
"plan_title_count": count_plan_titles(entities),
|
||||
"entity_types": dict(type_counts.most_common()),
|
||||
"top_layers": [
|
||||
{"name": name, "entity_count": count}
|
||||
for name, count in layer_counts.most_common(30)
|
||||
],
|
||||
"top_layers": [{"name": name, "entity_count": count} for name, count in layer_counts.most_common(30)],
|
||||
"layer_count": len(layers),
|
||||
"layers": sorted(layers, key=lambda item: (-item["entity_count"], item["name"])),
|
||||
"bbox": geometry_bbox,
|
||||
@@ -445,12 +447,14 @@ def markdown_report(results: list[dict], conversions: list[dict], input_dir: Pat
|
||||
for file_name, warning in all_warnings:
|
||||
lines.append(f"- `{file_name}`: {warning}")
|
||||
lines.append("")
|
||||
lines.extend([
|
||||
"## Files",
|
||||
"",
|
||||
"| File | Status | Entities | Layers | Size X | Size Y | Unit Guess | Main Types |",
|
||||
"| --- | --- | ---: | ---: | ---: | ---: | --- | --- |",
|
||||
])
|
||||
lines.extend(
|
||||
[
|
||||
"## Files",
|
||||
"",
|
||||
"| File | Status | Entities | Layers | Size X | Size Y | Unit Guess | Main Types |",
|
||||
"| --- | --- | ---: | ---: | ---: | ---: | --- | --- |",
|
||||
]
|
||||
)
|
||||
for item in results:
|
||||
bbox_data = item.get("bbox") or {}
|
||||
size = bbox_data.get("size", ["", "", ""])
|
||||
@@ -516,21 +520,43 @@ def main() -> int:
|
||||
|
||||
if not input_dir.exists():
|
||||
raise SystemExit(f"Input folder not found: {input_dir}")
|
||||
if args.allow_partial and not (args.partial_reason or "").strip():
|
||||
raise SystemExit("--allow-partial requires --partial-reason")
|
||||
|
||||
# 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)
|
||||
shutil.rmtree(clean_dir, ignore_errors=True)
|
||||
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] = []
|
||||
for source in find_cad_files(input_dir):
|
||||
dest = raw_dir / safe_name(source, used_names)
|
||||
assigned = [(source, raw_dir / safe_name(source, used_names)) for source in cad_files]
|
||||
oda_outputs: dict[Path, Path] = {}
|
||||
if args.converter == "oda" and not args.no_convert:
|
||||
oda_outputs = run_oda_batch(
|
||||
[source for source, _dest in assigned if source.suffix.lower() == ".dwg"],
|
||||
work_dir,
|
||||
)
|
||||
for source, dest in assigned:
|
||||
if source.suffix.lower() == ".dwg":
|
||||
if args.no_convert:
|
||||
continue
|
||||
conversion = run_dwg2dxf(source, dest)
|
||||
if args.converter == "oda":
|
||||
produced = oda_outputs.get(source)
|
||||
if produced:
|
||||
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy2(produced, dest)
|
||||
conversion = {"source": str(source), "dest": str(dest), "status": "oda_ok"}
|
||||
else:
|
||||
conversion = {"source": str(source), "dest": str(dest), "status": "oda_failed"}
|
||||
else:
|
||||
conversion = run_dwg2dxf(source, dest)
|
||||
conversions.append(conversion)
|
||||
if conversion["status"] == "ok":
|
||||
if conversion["status"] in {"ok", "oda_ok"}:
|
||||
raw_dxfs.append(dest)
|
||||
else:
|
||||
if source.resolve() != dest.resolve():
|
||||
@@ -565,7 +591,7 @@ def main() -> int:
|
||||
if c.get("status") in {"failed", "missing_dwg2dxf"} 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:
|
||||
if retry and args.converter == "auto":
|
||||
produced = run_oda_batch(list(retry.values()), work_dir)
|
||||
for name, src in retry.items():
|
||||
out = produced.get(src)
|
||||
@@ -592,17 +618,58 @@ def main() -> int:
|
||||
json_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
md_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
payload = {
|
||||
"input": str(input_dir),
|
||||
"work": str(work_dir),
|
||||
"dxf_sanitized": str(clean_dir),
|
||||
"report_version": 1,
|
||||
"input": str(input_dir.resolve()),
|
||||
"work": str(work_dir.resolve()),
|
||||
"dxf_sanitized": str(clean_dir.resolve()),
|
||||
"conversions": conversions,
|
||||
"files": results,
|
||||
}
|
||||
# --no-convert is a diagnostic convenience, not permission to silently
|
||||
# exclude DWGs from the source ledger. A mixed folder therefore fails the
|
||||
# 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"}
|
||||
}
|
||||
unresolved_dwgs = sorted(dwg_sources - successful_dwg_sources)
|
||||
failed_files = [item.get("file", "?") for item in results if item.get("status") != "ok"]
|
||||
empty_files = [
|
||||
item.get("file", "?") for item in results if item.get("status") == "ok" and item.get("modelspace_entities") == 0
|
||||
]
|
||||
issues = []
|
||||
if not cad_files:
|
||||
issues.append("input contains no DWG/DXF files")
|
||||
if unresolved_dwgs:
|
||||
issues.append("unconverted DWG files: " + ", ".join(Path(path).name for path in unresolved_dwgs))
|
||||
if failed_files:
|
||||
issues.append("unreadable DXF files: " + ", ".join(failed_files))
|
||||
if empty_files:
|
||||
issues.append("zero-entity DXF files: " + ", ".join(empty_files))
|
||||
if not results:
|
||||
issues.append("no readable DXF files were produced")
|
||||
payload["gate"] = {
|
||||
"name": "all_sources_converted_and_readable",
|
||||
"status": "WARN" if issues and args.allow_partial else ("FAIL" if issues else "PASS"),
|
||||
"detail": "; ".join(issues) if issues else f"{len(results)} DXF file(s) readable",
|
||||
"partial_reason": args.partial_reason if issues and args.allow_partial else None,
|
||||
}
|
||||
payload = scrub_surrogates(payload)
|
||||
results = payload["files"]
|
||||
json_path.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
|
||||
md_path.write_text(markdown_report(results, conversions, input_dir), encoding="utf-8")
|
||||
gate = payload["gate"]
|
||||
gate_md = (
|
||||
"## Source Gate\n\n"
|
||||
f"- **{gate['status']}** `{gate['name']}`: {gate['detail']}"
|
||||
+ (f"\n- Partial reason: {gate['partial_reason']}" if gate.get("partial_reason") else "")
|
||||
+ "\n\n"
|
||||
)
|
||||
md_path.write_text(gate_md + markdown_report(results, conversions, input_dir), encoding="utf-8")
|
||||
print(json.dumps({"json": str(json_path), "markdown": str(md_path), "files": len(results)}, ensure_ascii=False))
|
||||
if issues and not args.allow_partial:
|
||||
for issue in issues:
|
||||
print(f"GATE FAIL [all_sources_converted_and_readable]: {issue}", file=sys.stderr)
|
||||
return 3
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user