fix: detect Revit floor frames deterministically
This commit is contained in:
@@ -11,7 +11,7 @@ import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
from collections import Counter
|
||||
from collections import Counter, defaultdict
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
@@ -244,6 +244,16 @@ import re as _re
|
||||
|
||||
PLAN_TITLE_RE = _re.compile(r"平面图$")
|
||||
PLAN_TITLE_EXCLUDE_RE = _re.compile(r"(示意|疏散|分区|索引|定位|总平面|放大|详图|布置|大样)")
|
||||
PLAN_TITLE_EXCLUDE_LAYERS = ("*图签*", "*PUB_TAB*", "*TTLB*", "*TK*", "*图框*", "*目录*")
|
||||
CAD_TEXT_FORMAT_RE = _re.compile(r"\\[LlOoKk]|%%[UuOoKk]")
|
||||
FLOOR_TOKEN_RE = _re.compile(
|
||||
r"机房小屋面|机房屋面|"
|
||||
r"地下[一二三四五六七八九十\d]+层夹层|"
|
||||
r"地下[一二三四五六七八九十\d]+层|"
|
||||
r"[一二三四五六七八九十\d]+层夹层|"
|
||||
r"[一二三四五六七八九十\d]+层|"
|
||||
r"首层|底层|夹层|屋顶层|屋顶|屋面|机房层|电梯机房"
|
||||
)
|
||||
|
||||
# AutoCAD MIF escapes (\M+5XXXX): CJK text in pre-Unicode DWGs survives
|
||||
# DXF conversion as literal escape sequences, so title matching must decode
|
||||
@@ -267,27 +277,78 @@ def decode_mif(text: str) -> str:
|
||||
return MIF_RE.sub(repl, text)
|
||||
|
||||
|
||||
def normalize_cad_text(text: str) -> str:
|
||||
text = CAD_TEXT_FORMAT_RE.sub("", decode_mif(text))
|
||||
return text.replace("\\P", " ").strip()
|
||||
|
||||
|
||||
def floor_identities(label: str) -> list[tuple[str, str]]:
|
||||
"""``(building qualifier, floor)`` identities from labels/INSERT names."""
|
||||
label = normalize_cad_text(label).replace("_", "、")
|
||||
if label.endswith(("机房小屋面", "机房屋面")):
|
||||
return [("", "PH")]
|
||||
if not label.endswith("平面图") or len(label) > 50:
|
||||
return []
|
||||
head = label[:-3]
|
||||
matches = list(FLOOR_TOKEN_RE.finditer(head))
|
||||
tokens = [match.group(0) for match in matches]
|
||||
qualifier = _re.sub(r"[、,,\s]+$", "", head[:matches[0].start()]) if matches else ""
|
||||
if len(qualifier) > 12:
|
||||
qualifier = ""
|
||||
out = []
|
||||
for token in tokens:
|
||||
if token in {"屋顶层", "屋顶", "屋面"}:
|
||||
out.append("RF")
|
||||
elif token in {"机房小屋面", "机房屋面", "机房层", "电梯机房"}:
|
||||
out.append("PH")
|
||||
elif token == "首层":
|
||||
out.append("F1")
|
||||
elif token == "底层":
|
||||
out.append("B1")
|
||||
elif token == "夹层":
|
||||
out.append("F1M")
|
||||
else:
|
||||
out.append(token)
|
||||
return [(qualifier, identity) for identity in out] or [("", label)]
|
||||
|
||||
|
||||
def count_plan_titles(entities) -> int:
|
||||
"""Distinct plan-frame titles in modelspace (multi-frame sheet detector)."""
|
||||
seen = set()
|
||||
"""Distinct plan floors from visible titles and Revit INSERT-name suffixes."""
|
||||
floor_qualifiers: dict[str, set[str]] = defaultdict(set)
|
||||
for entity in entities:
|
||||
kind = entity.dxftype()
|
||||
if kind == "INSERT":
|
||||
try:
|
||||
suffix = str(entity.dxf.name).rsplit("-", 1)[-1]
|
||||
except Exception:
|
||||
continue
|
||||
for qualifier, floor in floor_identities(suffix):
|
||||
floor_qualifiers[floor].add(qualifier)
|
||||
continue
|
||||
if kind not in {"TEXT", "MTEXT"}:
|
||||
continue
|
||||
try:
|
||||
layer = str(entity.dxf.layer)
|
||||
except Exception:
|
||||
layer = ""
|
||||
if any(fnmatch.fnmatch(layer.upper(), pattern.upper()) for pattern in PLAN_TITLE_EXCLUDE_LAYERS):
|
||||
continue
|
||||
try:
|
||||
text = str(entity.dxf.text if kind == "TEXT" else entity.plain_text() or "").strip()
|
||||
except Exception:
|
||||
continue
|
||||
text = decode_mif(text)
|
||||
text = normalize_cad_text(text)
|
||||
if not text or len(text) > 40:
|
||||
continue
|
||||
if PLAN_TITLE_RE.search(text) and not PLAN_TITLE_EXCLUDE_RE.search(text):
|
||||
try:
|
||||
pos = entity.dxf.insert
|
||||
seen.add((text, round(float(pos.x) / 5000), round(float(pos.y) / 5000)))
|
||||
except Exception:
|
||||
seen.add((text, 0, 0))
|
||||
return len(seen)
|
||||
for qualifier, floor in floor_identities(text):
|
||||
floor_qualifiers[floor].add(qualifier)
|
||||
# Revit repeats the same floor suffix across hundreds of family INSERTs;
|
||||
# a sheet-title block may add ``主楼`` while families remain unqualified.
|
||||
# Count that as one frame, but retain two genuinely different qualified
|
||||
# buildings that both contain (say) an F1 plan in the same modelspace.
|
||||
return sum(max(1, len({q for q in qualifiers if q}))
|
||||
for qualifiers in floor_qualifiers.values())
|
||||
|
||||
|
||||
def insert_block_content(doc, entities) -> list[dict]:
|
||||
|
||||
Reference in New Issue
Block a user