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:
@@ -40,6 +40,26 @@ from pathlib import Path
|
||||
PLAN_TITLE = re.compile(r"平面图$")
|
||||
PLAN_EXCLUDE = re.compile(r"(示意|疏散|分区|索引|定位|总平面|放大|详图|布置|大样)")
|
||||
OTHER_TITLE = re.compile(r"(立面图|剖面图)$")
|
||||
|
||||
# AutoCAD MIF escapes (\M+5XXXX): CJK titles in pre-Unicode DWGs arrive as
|
||||
# literal escape sequences, invisible to the 平面图 regexes until decoded.
|
||||
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"}
|
||||
|
||||
|
||||
def decode_mif(text: str) -> str:
|
||||
if "\\M+" not in text:
|
||||
return text
|
||||
|
||||
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:
|
||||
return raw.decode(MIF_CODEPAGES.get(match.group(1), "gbk"))
|
||||
except (UnicodeDecodeError, LookupError):
|
||||
return match.group(0)
|
||||
|
||||
return MIF_RE.sub(repl, text)
|
||||
CN_NUM = {"一": 1, "二": 2, "三": 3, "四": 4, "五": 5, "六": 6, "七": 7, "八": 8, "九": 9, "十": 10}
|
||||
FLOOR_NAME_KEY = [
|
||||
(re.compile(r"地下(?P<n>[一二三四五六七八九十\d]+)层夹层"), lambda m: f"B{_cn(m.group('n'))}M"),
|
||||
@@ -100,9 +120,9 @@ def match_any(value: str, patterns: list[str]) -> bool:
|
||||
def entity_text(entity) -> str:
|
||||
try:
|
||||
if entity.dxftype() == "TEXT":
|
||||
return str(entity.dxf.text or "").strip()
|
||||
return decode_mif(str(entity.dxf.text or "").strip())
|
||||
if entity.dxftype() == "MTEXT":
|
||||
return str(entity.plain_text() or "").strip()
|
||||
return decode_mif(str(entity.plain_text() or "").strip())
|
||||
except Exception:
|
||||
pass
|
||||
return ""
|
||||
@@ -184,8 +204,65 @@ def main() -> int:
|
||||
plan_exclude = re.compile(args.title_exclude_regex) if args.title_exclude_regex else None
|
||||
titles, others = [], []
|
||||
lines = [] # (layer, sx, sy, ex, ey) for LINE and polyline edges
|
||||
|
||||
# Plan grids are not always modelspace lines: TArch exports may wrap each
|
||||
# frame's axis system in an anonymous block (黄桥: A$C... wrappers holding
|
||||
# hundreds of DOTE lines while msp has ~8). Expand INSERTs — but only
|
||||
# blocks that actually contain axis/wall-layer curves (memoized per block
|
||||
# name), or a sheet full of door/furniture blocks slows the scan for nothing.
|
||||
expand_patterns = axis_layers + wall_layers
|
||||
block_memo: dict[str, bool] = {}
|
||||
|
||||
def block_has_expandable(name: str, depth: int = 0) -> bool:
|
||||
if name in block_memo:
|
||||
return block_memo[name]
|
||||
if depth > 3 or name not in doc.blocks:
|
||||
return False
|
||||
block_memo[name] = False # cycle guard
|
||||
found = False
|
||||
for be in doc.blocks.get(name):
|
||||
bkind = be.dxftype()
|
||||
if bkind in {"LINE", "LWPOLYLINE"} and match_any(str(be.dxf.layer), expand_patterns):
|
||||
found = True
|
||||
break
|
||||
if bkind == "INSERT" and block_has_expandable(str(be.dxf.name), depth + 1):
|
||||
found = True
|
||||
break
|
||||
block_memo[name] = found
|
||||
return found
|
||||
|
||||
def collect_insert_lines(insert, depth: int = 0) -> None:
|
||||
if depth > 3:
|
||||
return
|
||||
try:
|
||||
virtual = list(insert.virtual_entities())
|
||||
except Exception:
|
||||
return
|
||||
for ve in virtual:
|
||||
vkind = ve.dxftype()
|
||||
if vkind == "LINE":
|
||||
d = ve.dxf
|
||||
if match_any(str(d.layer), expand_patterns):
|
||||
lines.append((str(d.layer), float(d.start.x), float(d.start.y), float(d.end.x), float(d.end.y)))
|
||||
elif vkind == "LWPOLYLINE":
|
||||
layer = str(ve.dxf.layer)
|
||||
if not match_any(layer, expand_patterns):
|
||||
continue
|
||||
try:
|
||||
pts = [(float(px), float(py)) for px, py in ve.get_points("xy")]
|
||||
except Exception:
|
||||
continue
|
||||
for a, b in zip(pts, pts[1:]):
|
||||
lines.append((layer, a[0], a[1], b[0], b[1]))
|
||||
elif vkind == "INSERT":
|
||||
collect_insert_lines(ve, depth + 1)
|
||||
|
||||
for entity in msp:
|
||||
kind = entity.dxftype()
|
||||
if kind == "INSERT":
|
||||
if block_has_expandable(str(entity.dxf.name)):
|
||||
collect_insert_lines(entity)
|
||||
continue
|
||||
if kind in {"TEXT", "MTEXT"}:
|
||||
content = entity_text(entity)
|
||||
if not content or len(content) > 40:
|
||||
|
||||
Reference in New Issue
Block a user