Files
cad-building-showcase/tests/test_extraction_pipeline.py
zhengwr 221626b1c7 Generalization hardening from test3d-7 round 1 (黄桥文体中心): 8 gap fixes
Conversion/encoding chain (libredwg on Chinese TArch DWGs):
- run_dwg2dxf: capture bytes, decode leniently (GBK console output crashed
  the batch under subprocess text=True)
- sanitize_dxf_comments: byte-level, code/value pair walk — the UTF-8
  errors="ignore" round-trip mangled every GBK CJK string ("一层平面图"
  -> "一平图") and eating XRECORD "999" VALUE lines corrupted files
- read_doc: recover fallback also on ValueError; scrub_surrogates keeps one
  corrupt drawing from killing the whole report
- decode_mif in report + frame detector: \M+5XXXX escapes (whole 黄桥 set)
  restored so plan-title detection works at all
- ODA auto-fallback: reconvert DWGs whose libredwg DXF fails inspection or
  reads an empty modelspace (黄桥 plans: 0 entities via dwg2dxf, 21k via
  ODA); env ODA_CONVERTER override, fake-converter regression test

Frame detection:
- detect_sheet_frames expands INSERTs (memoized, depth<=3) when collecting
  axis/wall lines — 黄桥 wraps each frame's grid in anonymous A$C... blocks
  (msp had 8 DOTE lines, blocks held 700); all 7 frames went from WEAK
  clip-corner fallback to grid-aligned votes x=14

Extraction judgment rules (both loud, both default-on, both switchable):
- drop_isolated_clusters: tiny segment clusters far from the main mass are
  sheet-legend content drawn on real layers (title-block window symbols made
  every floor's footprint grow a sliver fin)
- drop_unsupported_roof_lines: roof outlines with no footprint/cap plate
  within 2m beneath are hatch-only-support canopies that extrude into
  floating ribbons (F3 SW 坡屋面, B1 ground canopy at -5.85m)

Tests: 64 -> 75 checks, incl. GBK/MIF synthetic DXFs, block-wrapped grid
alignment (non-uniform bays — uniform grids make subset correlation
ambiguous), legend-cluster and unsupported-roof fixtures.
references/generalization_test_log.md tracks the test protocol, per-project
gate table, and every gap with its regression coverage.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-02 21:23:20 +08:00

748 lines
37 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 to_mif(text: str) -> str:
"""Encode CJK chars as AutoCAD MIF escapes (\\M+5XXXX, codepage 936)."""
out = []
for ch in text:
if ord(ch) > 127:
out.append("\\M+5%04X" % int.from_bytes(ch.encode("gbk"), "big"))
else:
out.append(ch)
return "".join(out)
def make_multiframe_dxf(path: Path, mif_titles: bool = False) -> 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. mif_titles=True writes the plan titles as MIF escapes
(\\M+5....), the 黄桥文体中心 pattern that plain text matching misses.
"""
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"})
if mif_titles:
title = to_mif(title)
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_blockgrid_dxf(path: Path) -> None:
"""黄桥 pattern: plan grids live inside anonymous wrapper blocks (A$C...),
NOT in modelspace, and floor wall bboxes differ (F2 covers half the
building). Clip-corner fallback then mis-aligns by 20 m in x; only
grid-line correlation via INSERT expansion recovers the true offset."""
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(index: int, oy: float, title: str, full_walls: bool) -> None:
wall_x0 = 0 if full_walls else 20000
outline = [((wall_x0, 0), (40000, 0)), ((40000, 0), (40000, 20000)),
((40000, 20000), (wall_x0, 20000)), ((wall_x0, 20000), (wall_x0, 0))]
interior = [((8000 * i, 0), (8000 * i, 20000)) for i in range(3 if full_walls else 4, 5)]
interior += [((wall_x0, 10000), (40000, 10000)), ((30000, 10000), (30000, 20000)),
((wall_x0 + 2000, 5000), (wall_x0 + 8000, 5000)),
((wall_x0 + 5000, 0), (wall_x0 + 5000, 10000)),
((34000, 0), (34000, 20000)), ((wall_x0 + 1000, 15000), (38000, 15000))]
for (sx, sy), (ex, ey) in outline + interior:
msp.add_line((sx, sy + oy), (ex, ey + oy), dxfattribs={"layer": "WALL"})
block = doc.blocks.new(name=f"A$CGRID{index}")
# real structural grids have varied bays; a perfectly uniform grid makes
# subset correlation ambiguous (any multiple of the pitch ties)
for x in (0, 8000, 19000, 26000, 37000, 40000):
block.add_line((x, -3000), (x, 26000), dxfattribs={"layer": "DOTE"})
for y in (0, 6000, 10000, 20000):
block.add_line((-3000, y), (46000, y), dxfattribs={"layer": "DOTE"})
msp.add_blockref(f"A$CGRID{index}", (0, oy))
msp.add_text(title, height=800, dxfattribs={"layer": "PUB_TEXT"}).set_placement((18000, oy - 6000))
draw_frame(1, 0.0, "一层平面图", True)
draw_frame(2, -pitch, "二层平面图", False)
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}",
)
# --- GBK/ANSI_936 DXF survives sanitize + report (libredwg dwg2dxf output) ---
gbk_dir = WORK / "dxf_gbk"
gbk_dir.mkdir(parents=True, exist_ok=True)
gbk_lines = [
"999", "天正注释行 should be stripped",
"0", "SECTION", "2", "HEADER",
"9", "$ACADVER", "1", "AC1009",
"9", "$DWGCODEPAGE", "3", "ANSI_936",
"0", "ENDSEC",
"0", "SECTION", "2", "ENTITIES",
"0", "TEXT", "8", "PUB_TEXT",
"10", "0.0", "20", "0.0", "40", "500.0",
"1", "一层平面图",
"0", "ENDSEC",
"0", "EOF", "",
]
(gbk_dir / "gbk_plan.dxf").write_bytes("\r\n".join(gbk_lines).encode("gbk"))
proc = run([
sys.executable, "scripts/cad_dxf_report.py",
"--input", str(gbk_dir), "--work", str(WORK / "gbk_work"),
"--output", str(WORK / "gbk_report"), "--vendor-dir", str(VENDOR),
])
check("report survives GBK/ANSI_936 DXF", proc.returncode == 0, proc.stderr[-600:])
gbk_report = WORK / "gbk_report" / "cad_read_report.json"
check("GBK report json parses", gbk_report.exists() and bool(json.loads(gbk_report.read_text(encoding="utf-8"))))
sanitized_gbk = WORK / "gbk_work" / "dxf_sanitized" / "gbk_plan.dxf"
if sanitized_gbk.exists():
doc = ezdxf.readfile(sanitized_gbk)
texts = [e.dxf.text for e in doc.modelspace() if e.dxftype() == "TEXT"]
check("sanitize preserves GBK CJK text", texts == ["一层平面图"], str(texts))
raw_bytes = sanitized_gbk.read_bytes()
check("sanitize still strips 999 comments", b"999" not in raw_bytes.split(b"ENTITIES")[0] or b"should be stripped" not in raw_bytes)
else:
check("sanitized GBK dxf exists", False, str(sanitized_gbk))
# --- MIF-escaped plan titles (\M+5....): 黄桥文体中心 pattern ---
mif_dir = WORK / "dxf_mif"
mif_dir.mkdir(parents=True, exist_ok=True)
make_multiframe_dxf(mif_dir / "mifframes.dxf", mif_titles=True)
proc = run([
sys.executable, "scripts/detect_sheet_frames.py",
"--dxf", str(mif_dir / "mifframes.dxf"), "--out-config", str(WORK / "frames_mif.json"),
"--out-report", str(WORK / "frames_mif.md"), "--vendor-dir", str(VENDOR),
])
check("detect handles MIF titles", proc.returncode == 0, proc.stderr[-600:])
if proc.returncode == 0:
frames_mif = json.loads((WORK / "frames_mif.json").read_text(encoding="utf-8"))
mif_keys = sorted(f["key"] for f in frames_mif["drawings"][0]["floors"])
check("MIF titles decoded to two plan frames", mif_keys == ["F1", "F2"], str(mif_keys))
proc = run([
sys.executable, "scripts/cad_dxf_report.py",
"--input", str(mif_dir), "--work", str(WORK / "mif_work"),
"--output", str(WORK / "mif_report"), "--vendor-dir", str(VENDOR),
])
mif_report = json.loads((WORK / "mif_report" / "cad_read_report.json").read_text(encoding="utf-8"))
mif_item = {i["file"]: i for i in mif_report["files"]}.get("mifframes.dxf", {})
check(
"multi_frame_sheet warning fires on MIF titles",
any("multi_frame_sheet" in w for w in mif_item.get("warnings", [])),
str(mif_item.get("warnings")),
)
# --- block-wrapped plan grids (黄桥 A$C... wrapper blocks) ---
bg_dir = WORK / "dxf_blockgrid"
bg_dir.mkdir(parents=True, exist_ok=True)
make_blockgrid_dxf(bg_dir / "blockgrid.dxf")
proc = run([
sys.executable, "scripts/detect_sheet_frames.py",
"--dxf", str(bg_dir / "blockgrid.dxf"), "--out-config", str(WORK / "frames_bg.json"),
"--out-report", str(WORK / "frames_bg.md"), "--vendor-dir", str(VENDOR),
])
check("detect handles block-wrapped grids", proc.returncode == 0, proc.stderr[-600:])
if proc.returncode == 0:
frames_bg = json.loads((WORK / "frames_bg.json").read_text(encoding="utf-8"))
bg_off = {f["key"]: f["offset"] for f in frames_bg["drawings"][0]["floors"]}
ok_bg = "F2" in bg_off and abs(bg_off["F2"][0]) <= 120 and abs(bg_off["F2"][1] - 98100.0) <= 120
check("block-grid correlation beats clip-corner fallback", ok_bg, str(bg_off))
# --- isolated legend clusters (黄桥图签窗图例 -> footprint 细三角飘板) ---
iso_dir = WORK / "dxf_isolated"
iso_dir.mkdir(parents=True, exist_ok=True)
iso_doc = ezdxf.new("R2010", setup=True)
for layer in ("I-WALL", "A-GLAZ-CURT"):
iso_doc.layers.add(layer)
iso_msp = iso_doc.modelspace()
for start, end in [
((0, 0), (40000, 0)), ((40000, 0), (40000, 20000)),
((40000, 20000), (0, 20000)), ((0, 20000), (0, 0)),
]:
iso_msp.add_line(start, end, dxfattribs={"layer": "I-WALL"})
iso_msp.add_line((0, 0), (20000, 0), dxfattribs={"layer": "A-GLAZ-CURT"}) # real facade band
for i in range(3): # window legend in the title block, 60 m east of the building
iso_msp.add_line((100000, 30000 + i * 400), (101000, 30000 + i * 400), dxfattribs={"layer": "A-GLAZ-CURT"})
iso_doc.saveas(iso_dir / "plan.dxf")
iso_config = {
"scale": 0.001,
"drawings": [{"file": "plan.dxf", "floors": [{"key": "1F", "name": "首层", "z": 0.0, "height": 4.2}]}],
}
def derive_iso(tag: str, extra: dict) -> tuple[dict, str]:
cfg = dict(iso_config, **extra)
cfg_path = WORK / f"iso_config_{tag}.json"
cfg_path.write_text(json.dumps(cfg, ensure_ascii=False), encoding="utf-8")
out_path = WORK / f"iso_spec_{tag}.json"
proc = run([
sys.executable, "scripts/derive_showcase_spec.py",
"--dxf-dir", str(iso_dir), "--config", str(cfg_path),
"--out", str(out_path), "--vendor-dir", str(VENDOR),
])
check(f"derive isolated ({tag}) runs", proc.returncode == 0, proc.stderr[-400:])
return json.loads(out_path.read_text(encoding="utf-8")), proc.stderr
iso_spec, iso_err = derive_iso("default", {})
glz = iso_spec["floors"]["1F"]["segments"].get("glazing", [])
far = [s for s in glz if max(s["start"][0], s["end"][0]) > 60]
check("isolated legend cluster dropped by default", len(glz) > 0 and not far, f"glazing={len(glz)} far={len(far)}")
check("isolated-cluster drop warns loudly", "isolated" in iso_err.lower(), iso_err[-200:])
check(
"bounds shrink to the real building",
iso_spec["floors"]["1F"]["bounds"][2] <= 45.0,
str(iso_spec["floors"]["1F"]["bounds"]),
)
iso_spec_off, _ = derive_iso("off", {"drop_isolated_clusters": False})
glz_off = iso_spec_off["floors"]["1F"]["segments"].get("glazing", [])
check(
"legend kept when drop_isolated_clusters=false",
any(max(s["start"][0], s["end"][0]) > 60 for s in glz_off),
f"glazing={len(glz_off)}",
)
# --- unsupported roof outlines (黄桥坡屋面: canopy with hatch-only posts) ---
uns_dir = WORK / "dxf_unsupported"
uns_dir.mkdir(parents=True, exist_ok=True)
uns_doc = ezdxf.new("R2010", setup=True)
for layer in ("I-WALL", "A-ROOF"):
uns_doc.layers.add(layer)
uns_msp = uns_doc.modelspace()
for start, end in [
((0, 0), (40000, 0)), ((40000, 0), (40000, 20000)),
((40000, 20000), (0, 20000)), ((0, 20000), (0, 0)),
]:
uns_msp.add_line(start, end, dxfattribs={"layer": "I-WALL"})
uns_msp.add_line((5000, 10000), (35000, 10000), dxfattribs={"layer": "A-ROOF"}) # over the building
uns_msp.add_line((80000, 50000), (95000, 50000), dxfattribs={"layer": "A-ROOF"}) # over nothing
uns_doc.saveas(uns_dir / "plan.dxf")
uns_config = {
"scale": 0.001,
"generate_footprints": True,
"drop_isolated_clusters": False, # keep the far roof line so THIS rule is what's tested
"drawings": [{"file": "plan.dxf", "floors": [{"key": "1F", "name": "首层", "z": 0.0, "height": 4.2}]}],
}
def derive_uns(tag: str, extra: dict) -> tuple[dict, str]:
cfg = dict(uns_config, **extra)
cfg_path = WORK / f"uns_config_{tag}.json"
cfg_path.write_text(json.dumps(cfg, ensure_ascii=False), encoding="utf-8")
out_path = WORK / f"uns_spec_{tag}.json"
proc = run([
sys.executable, "scripts/derive_showcase_spec.py",
"--dxf-dir", str(uns_dir), "--config", str(cfg_path),
"--out", str(out_path), "--vendor-dir", str(VENDOR),
])
check(f"derive unsupported-roof ({tag}) runs", proc.returncode == 0, proc.stderr[-400:])
return json.loads(out_path.read_text(encoding="utf-8")), proc.stderr
uns_spec, uns_err = derive_uns("default", {})
roof_segs = uns_spec["floors"]["1F"]["segments"].get("roof", [])
check(
"unsupported roof line dropped, supported kept",
len(roof_segs) == 1 and max(roof_segs[0]["start"][0], roof_segs[0]["end"][0]) <= 45,
f"roof={[(s['start'], s['end']) for s in roof_segs]}",
)
check("unsupported-roof drop warns loudly", "unsupported_roof" in uns_err, uns_err[-200:])
uns_spec_off, _ = derive_uns("off", {"drop_unsupported_roof_lines": False})
check(
"roof lines kept when drop_unsupported_roof_lines=false",
len(uns_spec_off["floors"]["1F"]["segments"].get("roof", [])) == 2,
)
# --- ODA auto-fallback: broken libredwg conversion reconverts via ODA ---
# A garbage .dwg makes dwg2dxf fail (or be absent); with ODA_CONVERTER
# pointing at a fake directory-converter the report must recover the file
# instead of shipping a failed/empty record (黄桥: libredwg emits broken
# DXF for large TArch files while ODA reads back 21k entities).
oda_dir = WORK / "dxf_oda"
oda_dir.mkdir(parents=True, exist_ok=True)
(oda_dir / "broken.dwg").write_bytes(b"NOT-A-REAL-DWG" * 64)
good_template = WORK / "oda_template.dxf"
shutil.copy2(dxf_dir / "plan.dxf", good_template)
fake_conv = WORK / "fake_oda.sh"
fake_conv.write_text(
"#!/bin/sh\n"
"for f in \"$1\"/*.dwg; do\n"
" name=$(basename \"$f\" .dwg)\n"
f" cp \"{good_template}\" \"$2/$name.dxf\"\n"
"done\n",
encoding="utf-8",
)
fake_conv.chmod(0o755)
import os as _os
env = dict(_os.environ, ODA_CONVERTER=str(fake_conv))
proc = subprocess.run(
[sys.executable, "scripts/cad_dxf_report.py",
"--input", str(oda_dir), "--work", str(WORK / "oda_work"),
"--output", str(WORK / "oda_report"), "--vendor-dir", str(VENDOR)],
cwd=ROOT, text=True, capture_output=True, env=env,
)
check("report with ODA fallback runs", proc.returncode == 0, proc.stderr[-600:])
oda_json = json.loads((WORK / "oda_report" / "cad_read_report.json").read_text(encoding="utf-8"))
oda_convs = [c for c in oda_json["conversions"] if str(c.get("status", "")).startswith("oda_fallback")]
check("oda_fallback conversion recorded", any(c["status"] == "oda_fallback_ok" for c in oda_convs), str(oda_convs))
broken_item = {i["file"]: i for i in oda_json["files"]}.get("broken.dxf", {})
check(
"broken DWG recovered via ODA fallback",
broken_item.get("status") != "failed" and broken_item.get("modelspace_entities", 0) > 0,
str({k: broken_item.get(k) for k in ("status", "modelspace_entities")}),
)
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())