Agent-led CAD-drawing → 3D web showcase pipeline. Inspects CAD reports, maps layers/floors, extracts a normalized building spec (with block-probed glazing and audit gates), builds Blender/trimesh GLB variants, and assembles a Three.js web viewer. Includes XREF-shell detection and a regression test. Large inputs/outputs (work/, outputs/, source_cad/, renders) are gitignored. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
208 lines
9.4 KiB
Python
208 lines
9.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Regression test for the CAD extraction pipeline improvements.
|
|
|
|
Reproduces the failure modes found in the 主辅楼 run with synthetic DXF files:
|
|
- curtain-wall glazing locked inside INSERT blocks (incl. wrapper blocks and
|
|
blocks whose CJK names were mangled by DWG->DXF conversion)
|
|
- annotation layers (*-IDEN) matching broad category patterns like A-DOOR*
|
|
- floor-plan sheets exported as near-empty XREF shells
|
|
|
|
Run: python3 tests/test_extraction_pipeline.py
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
WORK = ROOT / "work" / "pipeline_test"
|
|
VENDOR = ROOT / "work" / "vendor"
|
|
|
|
sys.path.insert(0, str(VENDOR.resolve()))
|
|
import ezdxf # noqa: E402
|
|
|
|
FAILURES: list[str] = []
|
|
|
|
|
|
def check(name: str, condition: bool, detail: str = "") -> None:
|
|
status = "ok" if condition else "FAIL"
|
|
print(f"[{status}] {name}" + (f" — {detail}" if detail and not condition else ""))
|
|
if not condition:
|
|
FAILURES.append(name)
|
|
|
|
|
|
def make_plan_dxf(path: Path) -> None:
|
|
doc = ezdxf.new("R2010", setup=True)
|
|
for layer in (
|
|
"I-WALL", "A-GLAZ-CURT", "S-GRID-IDEN", "A-ANNO-DIMS",
|
|
"A-DOOR-IDEN", "A-DOOR", "A-DETL",
|
|
):
|
|
doc.layers.add(layer)
|
|
msp = doc.modelspace()
|
|
|
|
# Walls: building outline 40m x 20m (mm units).
|
|
for start, end in [
|
|
((0, 0), (40000, 0)), ((40000, 0), (40000, 20000)),
|
|
((40000, 20000), (0, 20000)), ((0, 20000), (0, 0)),
|
|
]:
|
|
msp.add_line(start, end, dxfattribs={"layer": "I-WALL"})
|
|
|
|
# Filler detail lines so the sheet is not "near empty" (>60 entities).
|
|
for i in range(70):
|
|
msp.add_line((1000 + i * 100, 5000), (1000 + i * 100, 5500), dxfattribs={"layer": "A-DETL"})
|
|
|
|
# Case 1: glazing block whose CJK name was mangled in conversion
|
|
# ("系统嵌板" -> "系统嵌"); matched by the widened GLAZING_BLOCK_PATTERNS.
|
|
mangled = doc.blocks.new(name="系统嵌 - SZ_ _偏100_-407498-平图")
|
|
mangled.add_line((0, 0), (3000, 0), dxfattribs={"layer": "A-GLAZ-CURT"})
|
|
for i in range(4):
|
|
msp.add_blockref("系统嵌 - SZ_ _偏100_-407498-平图", (i * 4000, 0))
|
|
|
|
# Case 2: glazing nested inside a wrapper block whose name matches nothing.
|
|
panel = doc.blocks.new(name="PNL-407498")
|
|
panel.add_line((0, 0), (0, 3000), dxfattribs={"layer": "A-GLAZ-CURT"})
|
|
wrapper = doc.blocks.new(name="SHEET-WRAP")
|
|
for i in range(3):
|
|
wrapper.add_blockref("PNL-407498", (40000, i * 5000))
|
|
msp.add_blockref("SHEET-WRAP", (0, 0))
|
|
|
|
# Annotation block content that must stay out of the spec.
|
|
axis = doc.blocks.new(name="轴号-01")
|
|
axis.add_circle((0, 0), 400, dxfattribs={"layer": "S-GRID-IDEN"})
|
|
axis.add_line((0, 0), (0, 2000), dxfattribs={"layer": "A-ANNO-DIMS"})
|
|
for i in range(5):
|
|
msp.add_blockref("轴号-01", (i * 8000, 22000))
|
|
|
|
# Door tag lines (annotation) vs a real door swing arc.
|
|
msp.add_line((5000, 0), (5800, 0), dxfattribs={"layer": "A-DOOR-IDEN"})
|
|
msp.add_arc((10000, 0), 900, 0, 90, dxfattribs={"layer": "A-DOOR"})
|
|
|
|
doc.saveas(path)
|
|
|
|
|
|
def make_xref_shell_dxf(path: Path) -> None:
|
|
doc = ezdxf.new("R2010")
|
|
doc.layers.add("A-ANNO-SYMB")
|
|
doc.add_xref_def(filename="../XREF/roof_geometry.dwg", name="XREF-ROOF")
|
|
msp = doc.modelspace()
|
|
msp.add_text("屋顶平面图", dxfattribs={"layer": "A-ANNO-SYMB"})
|
|
for i in range(4):
|
|
msp.add_line((i * 1000, 0), (i * 1000, 500), dxfattribs={"layer": "A-ANNO-SYMB"})
|
|
doc.saveas(path)
|
|
|
|
|
|
def run(cmd: list[str]) -> subprocess.CompletedProcess:
|
|
return subprocess.run(cmd, cwd=ROOT, text=True, capture_output=True)
|
|
|
|
|
|
def main() -> int:
|
|
if WORK.exists():
|
|
shutil.rmtree(WORK)
|
|
dxf_dir = WORK / "dxf"
|
|
dxf_dir.mkdir(parents=True)
|
|
make_plan_dxf(dxf_dir / "plan.dxf")
|
|
make_xref_shell_dxf(dxf_dir / "roof.dxf")
|
|
|
|
# --- cad_dxf_report: XREF shell must be flagged, plan must not ---
|
|
proc = run([
|
|
sys.executable, "scripts/cad_dxf_report.py",
|
|
"--input", str(dxf_dir), "--work", str(WORK / "report_work"),
|
|
"--output", str(WORK / "read_report"), "--vendor-dir", str(VENDOR),
|
|
])
|
|
check("cad_dxf_report runs", proc.returncode == 0, proc.stderr[-800:])
|
|
report = json.loads((WORK / "read_report" / "cad_read_report.json").read_text(encoding="utf-8"))
|
|
by_file = {item["file"]: item for item in report["files"]}
|
|
roof_warnings = " ".join(by_file.get("roof.dxf", {}).get("warnings", []))
|
|
check("roof.dxf flagged as XREF shell", "likely_xref_shell" in roof_warnings, roof_warnings)
|
|
check("plan.dxf has no warnings", not by_file.get("plan.dxf", {}).get("warnings"), str(by_file.get("plan.dxf", {}).get("warnings")))
|
|
md = (WORK / "read_report" / "cad_read_report.md").read_text(encoding="utf-8")
|
|
check("markdown has warnings section", "Warnings (resolve before modeling)" in md)
|
|
|
|
# --- derive with default config: glazing captured from blocks ---
|
|
config = {
|
|
"scale": 0.001,
|
|
"drawings": [{"file": "plan.dxf", "floors": [{"key": "1F", "name": "首层", "z": 0.0, "height": 4.2}]}],
|
|
}
|
|
config_path = WORK / "config.json"
|
|
config_path.write_text(json.dumps(config, ensure_ascii=False), encoding="utf-8")
|
|
spec_path = WORK / "building_spec.json"
|
|
proc = run([
|
|
sys.executable, "scripts/derive_showcase_spec.py",
|
|
"--dxf-dir", str(dxf_dir), "--config", str(config_path),
|
|
"--out", str(spec_path), "--vendor-dir", str(VENDOR),
|
|
])
|
|
check("derive runs", proc.returncode == 0, proc.stderr[-800:])
|
|
spec = json.loads(spec_path.read_text(encoding="utf-8"))
|
|
segments = spec["floors"]["1F"]["segments"]
|
|
glazing_layers = {seg.get("layer") for seg in segments.get("glazing", [])}
|
|
check("glazing captured from blocks", len(segments.get("glazing", [])) >= 2, str(segments.get("glazing")))
|
|
check("glazing came from A-GLAZ-CURT", glazing_layers <= {"A-GLAZ-CURT"}, str(glazing_layers))
|
|
# Both capture paths must contribute: 4 mangled-name inserts are horizontal
|
|
# (y const), 3 probe-only nested panels are vertical (x const at 40m).
|
|
has_vertical = any(
|
|
abs(seg["start"][0] - seg["end"][0]) < 1e-6 for seg in segments.get("glazing", [])
|
|
)
|
|
check("probe path captured nested wrapper glazing", has_vertical)
|
|
check("walls captured", len(segments.get("walls", [])) >= 4)
|
|
check("door swing arc classified as doors", len(segments.get("doors", [])) > 0)
|
|
door_layers = {seg.get("layer") for seg in segments.get("doors", [])}
|
|
check("A-DOOR-IDEN excluded from doors", "A-DOOR-IDEN" not in door_layers, str(door_layers))
|
|
all_layers = {seg.get("layer") for segs in segments.values() for seg in segs}
|
|
check("S-GRID-IDEN/A-ANNO-DIMS not extracted", not ({"S-GRID-IDEN", "A-ANNO-DIMS"} & all_layers), str(all_layers))
|
|
|
|
# --- audit with default config: gates pass, exit 0 ---
|
|
proc = run([
|
|
sys.executable, "scripts/audit_showcase_extraction.py",
|
|
"--dxf-dir", str(dxf_dir), "--config", str(config_path), "--spec", str(spec_path),
|
|
"--out", str(WORK / "audit_ok"), "--vendor-dir", str(VENDOR),
|
|
])
|
|
check("audit (glazing captured) exits 0", proc.returncode == 0, proc.stderr[-800:])
|
|
audit = json.loads((WORK / "audit_ok" / "extraction_audit.json").read_text(encoding="utf-8"))
|
|
gates = {gate["name"]: gate["status"] for gate in audit["gates"]}
|
|
check("glazing gate PASS", gates.get("glazing_captured") == "PASS", str(gates))
|
|
check("annotation gate PASS", gates.get("no_annotation_layers_classified") == "PASS", str(gates))
|
|
locked = audit["coverage"]["block_locked_length_meters"]
|
|
check("block-locked length reported", locked > 0, str(audit["coverage"]))
|
|
|
|
# --- derive+audit with expansion disabled: glazing gate must FAIL (exit 3) ---
|
|
config_off = dict(config)
|
|
config_off.update({"classify_block_contents": False, "expand_glazing_blocks": False})
|
|
config_off_path = WORK / "config_off.json"
|
|
config_off_path.write_text(json.dumps(config_off, ensure_ascii=False), encoding="utf-8")
|
|
spec_off_path = WORK / "building_spec_off.json"
|
|
proc = run([
|
|
sys.executable, "scripts/derive_showcase_spec.py",
|
|
"--dxf-dir", str(dxf_dir), "--config", str(config_off_path),
|
|
"--out", str(spec_off_path), "--vendor-dir", str(VENDOR),
|
|
])
|
|
check("derive (expansion off) runs", proc.returncode == 0, proc.stderr[-800:])
|
|
spec_off = json.loads(spec_off_path.read_text(encoding="utf-8"))
|
|
check(
|
|
"expansion off reproduces glazing=0",
|
|
len(spec_off["floors"]["1F"]["segments"].get("glazing", [])) == 0,
|
|
)
|
|
proc = run([
|
|
sys.executable, "scripts/audit_showcase_extraction.py",
|
|
"--dxf-dir", str(dxf_dir), "--config", str(config_off_path), "--spec", str(spec_off_path),
|
|
"--out", str(WORK / "audit_fail"), "--vendor-dir", str(VENDOR),
|
|
])
|
|
check("audit gate failure exits 3", proc.returncode == 3, f"rc={proc.returncode} {proc.stderr[-400:]}")
|
|
audit_fail = json.loads((WORK / "audit_fail" / "extraction_audit.json").read_text(encoding="utf-8"))
|
|
gates_fail = {gate["name"]: gate["status"] for gate in audit_fail["gates"]}
|
|
check("glazing gate FAIL when blocks unexpanded", gates_fail.get("glazing_captured") == "FAIL", str(gates_fail))
|
|
|
|
print()
|
|
if FAILURES:
|
|
print(f"{len(FAILURES)} check(s) failed: {FAILURES}")
|
|
return 1
|
|
print("All checks passed.")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|