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>
This commit is contained in:
@@ -95,12 +95,24 @@ def make_xref_shell_dxf(path: Path) -> None:
|
||||
doc.saveas(path)
|
||||
|
||||
|
||||
def make_multiframe_dxf(path: Path) -> None:
|
||||
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.
|
||||
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"):
|
||||
@@ -123,6 +135,8 @@ def make_multiframe_dxf(path: Path) -> None:
|
||||
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, "一层平面图")
|
||||
@@ -130,6 +144,43 @@ def make_multiframe_dxf(path: Path) -> None:
|
||||
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)
|
||||
@@ -466,6 +517,224 @@ def main() -> int:
|
||||
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}")
|
||||
|
||||
Reference in New Issue
Block a user