#!/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 make_multiframe_dxf(path: Path) -> None: """Chinese sheet-wall pattern: two floor-plan frames stacked 98100 apart. Reproduces the 贝昂 failure mode: one DWG holds every floor; frames keep their sheet positions, so extraction without per-frame offsets scatters floors diagonally. """ doc = ezdxf.new("R2010", setup=True) for layer in ("WALL", "DOTE", "PUB_TEXT"): doc.layers.add(layer) msp = doc.modelspace() pitch = 98100.0 def draw_frame(oy: float, title: str) -> None: # walls: 40m x 20m outline + interior partitions (>=10 lines for clip refine) outline = [((0, 0), (40000, 0)), ((40000, 0), (40000, 20000)), ((40000, 20000), (0, 20000)), ((0, 20000), (0, 0))] interior = [((8000 * i, 0), (8000 * i, 20000)) for i in range(1, 5)] interior += [((0, 10000), (40000, 10000)), ((12000, 5000), (28000, 5000)), ((20000, 0), (20000, 10000)), ((30000, 10000), (30000, 20000))] for (sx, sy), (ex, ey) in outline + interior: msp.add_line((sx, sy + oy), (ex, ey + oy), dxfattribs={"layer": "WALL"}) for i in range(6): # vertical grid lines on DOTE (TArch convention) x = i * 8000 msp.add_line((x, -3000 + oy), (x, 26000 + oy), dxfattribs={"layer": "DOTE"}) for j in range(3): # horizontal grid lines y = j * 10000 msp.add_line((-3000, y + oy), (46000, y + oy), dxfattribs={"layer": "DOTE"}) msp.add_text(title, height=800, dxfattribs={"layer": "PUB_TEXT"}).set_placement((18000, oy - 6000)) draw_frame(0.0, "一层平面图") draw_frame(-pitch, "二层平面图") doc.saveas(path) def make_rowframe_dxf(path: Path) -> None: """Row layout (3#辅房 pattern): frames side by side, pitch on X, titles same row.""" doc = ezdxf.new("R2010", setup=True) for layer in ("WALL", "DOTE", "PUB_TEXT"): doc.layers.add(layer) msp = doc.modelspace() pitch = 21000.0 def draw_frame(ox: float, title: str) -> None: outline = [((0, 0), (8000, 0)), ((8000, 0), (8000, 16000)), ((8000, 16000), (0, 16000)), ((0, 16000), (0, 0))] interior = [((0, 2000 * i), (8000, 2000 * i)) for i in range(1, 8)] interior += [((4000, 0), (4000, 16000)), ((2000, 4000), (6000, 4000)), ((2000, 12000), (6000, 12000))] for (sx, sy), (ex, ey) in outline + interior: msp.add_line((sx + ox, sy), (ex + ox, ey), dxfattribs={"layer": "WALL"}) for i in range(3): x = i * 4000 msp.add_line((x + ox, -2500), (x + ox, 18500), dxfattribs={"layer": "DOTE"}) for j in range(4): y = j * 5000 msp.add_line((-2500 + ox, y), (10500 + ox, y), dxfattribs={"layer": "DOTE"}) msp.add_text(title, height=700, dxfattribs={"layer": "PUB_TEXT"}).set_placement((2500 + ox, -5200)) draw_frame(0.0, "一层平面图") draw_frame(pitch, "二层平面图") 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)) # --- multi-frame sheet: detect frames, grid-align, alignment gate --- mf_path = dxf_dir / "multiframe.dxf" make_multiframe_dxf(mf_path) proc = run([ sys.executable, "scripts/cad_dxf_report.py", "--input", str(dxf_dir), "--work", str(WORK / "report_work2"), "--output", str(WORK / "read_report2"), "--vendor-dir", str(VENDOR), ]) report2 = json.loads((WORK / "read_report2" / "cad_read_report.json").read_text(encoding="utf-8")) mf_item = {item["file"]: item for item in report2["files"]}.get("multiframe.dxf", {}) check("multi_frame_sheet warning raised", any("multi_frame_sheet" in w for w in mf_item.get("warnings", [])), str(mf_item.get("warnings"))) proc = run([ sys.executable, "scripts/detect_sheet_frames.py", "--dxf", str(mf_path), "--out-config", str(WORK / "frames.json"), "--out-report", str(WORK / "frames.md"), "--vendor-dir", str(VENDOR), ]) check("detect_sheet_frames runs", proc.returncode == 0, proc.stderr[-800:]) frames = json.loads((WORK / "frames.json").read_text(encoding="utf-8")) floors = frames["drawings"][0]["floors"] keys = [floor["key"] for floor in floors] check("two plan frames found", sorted(keys) == ["F1", "F2"], str(keys)) off = {floor["key"]: floor["offset"] for floor in floors} ok_offset = abs(off["F2"][0] - 0.0) <= 120 and abs(off["F2"][1] - 98100.0) <= 120 check("grid alignment recovers frame offset", ok_offset, str(off)) frames.update({"scale": 0.001, "layers": {"walls": ["WALL"], "grid": ["DOTE"]}}) aligned_cfg = WORK / "config_frames.json" aligned_cfg.write_text(json.dumps(frames, ensure_ascii=False), encoding="utf-8") aligned_spec = WORK / "spec_frames.json" proc = run([ sys.executable, "scripts/derive_showcase_spec.py", "--dxf-dir", str(dxf_dir), "--config", str(aligned_cfg), "--out", str(aligned_spec), "--vendor-dir", str(VENDOR), ]) check("derive multi-frame runs", proc.returncode == 0, proc.stderr[-800:]) proc = run([ sys.executable, "scripts/audit_showcase_extraction.py", "--dxf-dir", str(dxf_dir), "--config", str(aligned_cfg), "--spec", str(aligned_spec), "--out", str(WORK / "audit_frames"), "--vendor-dir", str(VENDOR), ]) audit_frames = json.loads((WORK / "audit_frames" / "extraction_audit.json").read_text(encoding="utf-8")) gates_frames = {gate["name"]: gate["status"] for gate in audit_frames["gates"]} check("floors_aligned gate PASS with offsets", gates_frames.get("floors_aligned") == "PASS", str(gates_frames)) # Same clips but WITHOUT offsets -> the diagonal-staircase bug -> gate FAIL. bad = json.loads(aligned_cfg.read_text(encoding="utf-8")) for floor in bad["drawings"][0]["floors"]: floor["offset"] = [0.0, 0.0] bad_cfg = WORK / "config_frames_bad.json" bad_cfg.write_text(json.dumps(bad, ensure_ascii=False), encoding="utf-8") bad_spec = WORK / "spec_frames_bad.json" run([ sys.executable, "scripts/derive_showcase_spec.py", "--dxf-dir", str(dxf_dir), "--config", str(bad_cfg), "--out", str(bad_spec), "--vendor-dir", str(VENDOR), ]) proc = run([ sys.executable, "scripts/audit_showcase_extraction.py", "--dxf-dir", str(dxf_dir), "--config", str(bad_cfg), "--spec", str(bad_spec), "--out", str(WORK / "audit_frames_bad"), "--vendor-dir", str(VENDOR), ]) check("misaligned floors exit 3", proc.returncode == 3, f"rc={proc.returncode}") audit_bad = json.loads((WORK / "audit_frames_bad" / "extraction_audit.json").read_text(encoding="utf-8")) gates_bad = {gate["name"]: gate["status"] for gate in audit_bad["gates"]} check("floors_aligned gate FAIL without offsets", gates_bad.get("floors_aligned") == "FAIL", str(gates_bad)) cov = audit_frames["coverage"] check("clip-scoped coverage reported", cov.get("clipped_supported_length_coverage") is not None, str(cov)) # --- horizontal-row frames (3#辅房 pattern) --- row_dir = WORK / "dxf_row" row_dir.mkdir(parents=True, exist_ok=True) make_rowframe_dxf(row_dir / "rowframes.dxf") proc = run([ sys.executable, "scripts/detect_sheet_frames.py", "--dxf", str(row_dir / "rowframes.dxf"), "--out-config", str(WORK / "frames_row.json"), "--out-report", str(WORK / "frames_row.md"), "--vendor-dir", str(VENDOR), ]) check("detect handles row layout", proc.returncode == 0, proc.stderr[-600:]) frames_row = json.loads((WORK / "frames_row.json").read_text(encoding="utf-8")) row_floors = frames_row["drawings"][0]["floors"] row_off = {f["key"]: f["offset"] for f in row_floors} check("row layout: two floors", sorted(row_off) == ["F1", "F2"], str(row_off)) ok_row = "F2" in row_off and abs(row_off["F2"][0] + 21000.0) <= 120 and abs(row_off["F2"][1]) <= 120 check("row layout: x-pitch offset recovered", ok_row, str(row_off)) # --- merge_wall_solids: shapely union of wall segments (spec_to_glb) --- sys.path.insert(0, str(ROOT / "scripts")) import spec_to_glb # noqa: E402 l_walls = [ {"start": [0, 0], "end": [10, 0], "width": 0.24, "layer": "WALL"}, {"start": [10, 0], "end": [10, 8], "width": 0.24, "layer": "WALL"}, ] check("spec_to_glb exposes merged_wall_polygons", hasattr(spec_to_glb, "merged_wall_polygons")) if hasattr(spec_to_glb, "merged_wall_polygons"): polys = spec_to_glb.merged_wall_polygons(l_walls) check("L walls union to a single polygon", len(polys) == 1, f"{len(polys)} polygons") if len(polys) == 1: corner_count = len(list(polys[0].exterior.coords)) - 1 # A seamless right-angle corner is exactly 6 corners; 8 means the # w/2 x w/2 notch at the shared endpoint was left open. check("L union corner is seamless (6 corners)", corner_count == 6, f"{corner_count} corners") minx, miny, maxx, maxy = polys[0].bounds check( "free wall ends are not extended", abs(minx - 0.0) < 1e-6 and abs(maxy - 8.0) < 1e-6, f"bounds={polys[0].bounds}", ) wall_spec = { "project": {"name": "wall-merge-test"}, "units": "meters", "categories": {"walls": {"label": "墙体", "color": "#d4cbb8"}}, "source": {"config": {}}, "floors": { "F1": { "name": "首层", "z": 0.0, "height": 4.0, "bounds": [0, 0, 12, 10], "segments": {"walls": list(l_walls)}, } }, } def build_glb(tag: str, config: dict) -> tuple[bytes, dict]: spec = json.loads(json.dumps(wall_spec)) spec["source"]["config"] = config spec_file = WORK / f"wall_spec_{tag}.json" spec_file.write_text(json.dumps(spec, ensure_ascii=False), encoding="utf-8") out_file = WORK / f"wall_{tag}.glb" proc = run([ sys.executable, "scripts/spec_to_glb.py", "--spec", str(spec_file), "--out", str(out_file), "--name", tag, ]) check(f"spec_to_glb ({tag}) runs", proc.returncode == 0, proc.stderr[-400:]) info = json.loads(proc.stdout.strip().splitlines()[-1]) if proc.returncode == 0 else {} return (out_file.read_bytes() if out_file.exists() else b""), info default_bytes, default_info = build_glb("default", {}) off_bytes, _ = build_glb("off", {"merge_wall_solids": False}) on_bytes, on_info = build_glb("on", {"merge_wall_solids": True}) check("merge_wall_solids default matches explicit off byte-for-byte", default_bytes == off_bytes and len(default_bytes) > 0) check("merge_wall_solids on changes the output", on_bytes not in (b"", default_bytes)) check( "merged walls keep one node with valid triangles", on_info.get("nodes") == default_info.get("nodes") and on_info.get("triangles", 0) > 0, f"default={default_info} on={on_info}", ) # --- glazing_mullions: panel grid over merged glazing bands --- check("spec_to_glb exposes glazing_mullion_segments", hasattr(spec_to_glb, "glazing_mullion_segments")) if hasattr(spec_to_glb, "glazing_mullion_segments"): band = [{"start": [0, 0], "end": [6, 0], "width": 0.06, "layer": "CURTWALL"}] msegs = spec_to_glb.glazing_mullion_segments(band, 0.0, 4.0, panel_w=1.5) verticals = [s for s in msegs if "_z0" not in s] transoms = [s for s in msegs if "_z0" in s] check("6m band at 1.5m pitch gives 5 vertical mullions", len(verticals) == 5, f"{len(verticals)} verticals") check("band gets top+bottom transoms", len(transoms) == 2, f"{len(transoms)} transoms") if transoms: zs = sorted(float(s["_z0"]) for s in transoms) check("transoms sit at the floor lines", abs(zs[0] - 0.0) < 0.15 and abs(zs[-1] - 4.0) < 0.15, str(zs)) glaze_spec = { "project": {"name": "mullion-test"}, "units": "meters", "categories": { "walls": {"label": "墙体", "color": "#d4cbb8"}, "glazing": {"label": "玻璃", "color": "#79b8c9"}, }, "source": {"config": {}}, "floors": { "F1": { "name": "首层", "z": 0.0, "height": 4.0, "bounds": [0, 0, 8, 4], "segments": {"glazing": [ {"start": [0, 0], "end": [6, 0], "width": 0.06, "layer": "CURTWALL"}, ]}, } }, } def build_glaze_glb(tag: str, config: dict) -> tuple[bytes, dict]: spec = json.loads(json.dumps(glaze_spec)) spec["source"]["config"] = config spec_file = WORK / f"glaze_spec_{tag}.json" spec_file.write_text(json.dumps(spec, ensure_ascii=False), encoding="utf-8") out_file = WORK / f"glaze_{tag}.glb" proc = run([ sys.executable, "scripts/spec_to_glb.py", "--spec", str(spec_file), "--out", str(out_file), "--name", tag, ]) check(f"spec_to_glb ({tag}) runs", proc.returncode == 0, proc.stderr[-400:]) info = json.loads(proc.stdout.strip().splitlines()[-1]) if proc.returncode == 0 else {} return (out_file.read_bytes() if out_file.exists() else b""), info gdefault_bytes, gdefault_info = build_glaze_glb("default", {}) goff_bytes, _ = build_glaze_glb("off", {"glazing_mullions": False}) gon_bytes, gon_info = build_glaze_glb("on", {"glazing_mullions": True}) check("glazing_mullions default matches explicit off byte-for-byte", gdefault_bytes == goff_bytes and len(gdefault_bytes) > 0) check("glazing_mullions on changes the output", gon_bytes not in (b"", gdefault_bytes)) check( "mullions add a walls node and keep the glass pane", gon_info.get("nodes", 0) == gdefault_info.get("nodes", 0) + 1 and gon_info.get("triangles", 0) > gdefault_info.get("triangles", 0), f"default={gdefault_info} on={gon_info}", ) 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())