Files
cad-building-showcase/scripts/audit_showcase_extraction.py

902 lines
39 KiB
Python
Executable File

#!/usr/bin/env python3
"""Audit extraction coverage so the agent can judge what may be missing."""
from __future__ import annotations
import argparse
import json
import math
import sys
from collections import Counter, defaultdict
from pathlib import Path
from typing import Any
sys.path.insert(0, str(Path(__file__).resolve().parent))
import derive_showcase_spec as derive # noqa: E402
import showcase_schema # noqa: E402
HIGH_RISK_TYPES = {
"INSERT",
"HATCH",
"SOLID",
"3DFACE",
"REGION",
"ACAD_PROXY_ENTITY",
"DIMENSION",
"TEXT",
"MTEXT",
"IMAGE",
"WIPEOUT",
}
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--dxf-dir", required=True, help="Sanitized DXF folder")
parser.add_argument("--config", required=True, help="Extraction config JSON")
parser.add_argument("--spec", required=True, help="Generated building_spec.json")
parser.add_argument("--out", default="work/cad_showcase/extraction_audit", help="Output path stem or folder")
parser.add_argument("--vendor-dir", default="work/vendor")
parser.add_argument(
"--no-strict-gates",
action="store_true",
help="Report gate failures without a nonzero exit code",
)
parser.add_argument(
"--frames",
nargs="*",
default=None,
help="detect_sheet_frames out-config JSON(s): enables the frame-ledger gate "
"(every detected plan frame must be in the extraction config or listed "
"in config discarded_frames with a reason)",
)
return parser.parse_args()
_BLOCK_LENGTHS_CACHE: dict[tuple[int, str], dict[str, float]] = {}
def block_layer_unit_lengths(doc, name: str, max_chord: float, max_depth: int, depth: int = 0) -> dict[str, float]:
"""Per-layer supported length inside a block DEFINITION (unit scale).
Exploding every INSERT for accounting is O(inserts x block size) and times
out on real Chinese sheet sets (hundreds of identical axis/door/window
blocks). Walking the definition once per block name and multiplying by the
insert scale gives the same length totals (rotation preserves length;
non-uniform scales are approximated by the mean of |sx|,|sy|).
"""
key = (id(doc), name)
cached = _BLOCK_LENGTHS_CACHE.get(key)
if cached is not None:
return cached
totals: dict[str, float] = defaultdict(float)
try:
block = doc.blocks.get(name)
except Exception:
block = None
if block is not None:
for inner in block:
if inner.dxftype() == "INSERT" and depth < max_depth:
inner_name = str(inner.dxf.name) if inner.dxf.hasattr("name") else ""
if not inner_name:
continue
sx = abs(float(inner.dxf.get("xscale", 1.0) or 1.0))
sy = abs(float(inner.dxf.get("yscale", 1.0) or 1.0))
factor = (sx + sy) / 2.0
for layer, length in block_layer_unit_lengths(doc, inner_name, max_chord, max_depth, depth + 1).items():
totals[layer] += length * factor
else:
length = sum(segment_length_source(segment) for segment in derive.entity_segments(inner, max_chord))
if length > 0:
totals[derive.entity_layer(inner)] += length
result = dict(totals)
_BLOCK_LENGTHS_CACHE[key] = result
return result
def output_paths(output: Path) -> tuple[Path, Path]:
if output.suffix:
return output.with_suffix(".json"), output.with_suffix(".md")
output.mkdir(parents=True, exist_ok=True)
return output / "extraction_audit.json", output / "extraction_audit.md"
def segment_length_source(segment: tuple[tuple[float, float], tuple[float, float]]) -> float:
(sx, sy), (ex, ey) = segment
return math.hypot(ex - sx, ey - sy)
def inspect_entity(
entity,
patterns: dict[str, list[str]],
config: dict[str, Any],
max_chord: float,
fallback_layer: str = "",
block_name: str = "",
) -> dict[str, Any]:
layer = derive.entity_layer(entity)
dxftype = entity.dxftype()
category = derive.category_for_entity(
entity,
patterns,
config,
fallback_layer=fallback_layer,
block_name=block_name,
)
segments = derive.entity_segments(entity, max_chord)
supported_length = sum(segment_length_source(segment) for segment in segments)
sample = None
if segments:
(sx, sy), (ex, ey) = segments[0]
sample = ((sx + ex) / 2.0, (sy + ey) / 2.0)
return {
"layer": layer,
"type": dxftype,
"category": category,
"segment_count": len(segments),
"supported_length": supported_length,
"sample": sample,
"high_risk": dxftype in HIGH_RISK_TYPES,
"insert_name": block_name
or (str(entity.dxf.name) if dxftype == "INSERT" and entity.dxf.hasattr("name") else ""),
}
def config_clips(config: dict[str, Any]) -> list[list[float]]:
clips = []
for drawing in config.get("drawings", []) or []:
base = drawing.get("clip")
for floor in drawing.get("floors", []) or []:
clip = floor.get("clip", base)
if clip:
x0, y0, x1, y1 = clip
clips.append([min(x0, x1), min(y0, y1), max(x0, x1), max(y0, y1)])
return clips
def source_files_from_config(dxf_dir: Path, config: dict[str, Any]) -> list[Path]:
drawings = config.get("drawings")
if drawings:
return [dxf_dir / drawing["file"] for drawing in drawings if (dxf_dir / drawing["file"]).exists()]
return sorted(dxf_dir.glob("*.dxf"))
def spec_counts(spec: dict[str, Any]) -> dict[str, dict[str, int]]:
categories = [key for key in spec.get("categories", {}) if key != "slab"]
result = {}
for floor_key, floor in spec.get("floors", {}).items():
result[floor_key] = {category: len(floor.get("segments", {}).get(category, [])) for category in categories}
return result
def audit(
dxf_dir: Path,
config: dict[str, Any],
spec: dict[str, Any],
vendor_dir: Path,
frames: list[dict[str, Any]] | None = None,
) -> dict[str, Any]:
ezdxf, recover = derive.import_ezdxf(vendor_dir)
patterns = derive.layer_patterns(config)
max_chord = float(config.get("curve_chord_source_length", 750.0))
scale = float(config.get("scale", config.get("scale_to_meters", 0.001)))
file_reports = []
global_entity_types = Counter()
global_layer_counts = Counter()
global_high_risk = Counter()
global_insert_names = Counter()
layer_lengths: dict[str, dict[str, Any]] = defaultdict(
lambda: {
"entity_count": 0,
"supported_length_source": 0.0,
"classified_length_source": 0.0,
"categories": Counter(),
"entity_types": Counter(),
"high_risk_types": Counter(),
}
)
total_supported = 0.0
classified_supported = 0.0
captured_from_blocks = 0.0
clips = config_clips(config)
clipped_total = 0.0
clipped_classified = 0.0
excludes = derive.exclude_layer_patterns(config)
max_block_depth = int(config.get("expand_blocks_max_depth", 2))
block_layer_lengths: dict[str, dict[str, Any]] = defaultdict(
lambda: {"supported_length_source": 0.0, "entity_count": 0, "category": None}
)
for path in source_files_from_config(dxf_dir, config):
doc = derive.read_doc(path, ezdxf, recover)
file_total_supported = 0.0
file_classified_supported = 0.0
file_entity_types = Counter()
file_high_risk = Counter()
file_layers = Counter()
file_insert_names = Counter()
for source_entity in doc.modelspace():
if source_entity.dxftype() == "INSERT":
insert_name = str(source_entity.dxf.name) if source_entity.dxf.hasattr("name") else ""
global_high_risk["INSERT"] += 1
file_high_risk["INSERT"] += 1
if insert_name:
global_insert_names[insert_name] += 1
file_insert_names[insert_name] += 1
# Config-independent accounting of geometry locked inside blocks,
# so missing block expansion cannot hide behind a high coverage number.
if insert_name:
sx = abs(float(source_entity.dxf.get("xscale", 1.0) or 1.0))
sy = abs(float(source_entity.dxf.get("yscale", 1.0) or 1.0))
factor = (sx + sy) / 2.0
unit_lengths = block_layer_unit_lengths(doc, insert_name, max_chord, max_block_depth)
for inner_layer, unit_length in unit_lengths.items():
inner_length = unit_length * factor
if inner_length <= 0:
continue
record = block_layer_lengths[inner_layer]
record["supported_length_source"] += inner_length
record["entity_count"] += 1
if record["category"] is None:
record["category"] = derive.match_category(inner_layer, patterns, excludes) or ""
for entity, fallback_layer, block_name in derive.iter_model_entities(source_entity, config):
item = inspect_entity(entity, patterns, config, max_chord, fallback_layer, block_name)
layer = item["layer"]
dxftype = item["type"]
length = float(item["supported_length"])
category = item["category"]
global_entity_types[dxftype] += 1
global_layer_counts[layer] += 1
file_entity_types[dxftype] += 1
file_layers[layer] += 1
layer_lengths[layer]["entity_count"] += 1
layer_lengths[layer]["supported_length_source"] += length
layer_lengths[layer]["entity_types"][dxftype] += 1
if category:
layer_lengths[layer]["categories"][category] += 1
layer_lengths[layer]["classified_length_source"] += length
classified_supported += length
file_classified_supported += length
if block_name:
captured_from_blocks += length
total_supported += length
file_total_supported += length
if clips and item["sample"] is not None:
px, py = item["sample"]
if any(c[0] <= px <= c[2] and c[1] <= py <= c[3] for c in clips):
clipped_total += length
if category:
clipped_classified += length
if item["high_risk"]:
# Top-level INSERTs were already counted above.
if not (dxftype == "INSERT" and entity is source_entity):
global_high_risk[dxftype] += 1
file_high_risk[dxftype] += 1
layer_lengths[layer]["high_risk_types"][dxftype] += 1
if item["insert_name"] and entity is not source_entity:
global_insert_names[item["insert_name"]] += 1
file_insert_names[item["insert_name"]] += 1
file_reports.append(
{
"file": path.name,
"entity_types": dict(file_entity_types.most_common()),
"top_layers": dict(file_layers.most_common(20)),
"high_risk_types": dict(file_high_risk.most_common()),
"insert_names": dict(file_insert_names.most_common(20)),
"supported_length_meters": round(file_total_supported * scale, 3),
"classified_supported_length_meters": round(file_classified_supported * scale, 3),
"supported_length_coverage": round(file_classified_supported / file_total_supported, 4)
if file_total_supported
else None,
}
)
unclassified_layers = []
classified_layers = []
for layer, data in layer_lengths.items():
record = {
"layer": layer,
"entity_count": data["entity_count"],
"supported_length_meters": round(data["supported_length_source"] * scale, 3),
"classified_length_meters": round(data["classified_length_source"] * scale, 3),
"categories": dict(data["categories"].most_common()),
"entity_types": dict(data["entity_types"].most_common(10)),
"high_risk_types": dict(data["high_risk_types"].most_common()),
}
if data["classified_length_source"] > 0:
classified_layers.append(record)
else:
unclassified_layers.append(record)
unclassified_layers.sort(key=lambda item: (-item["supported_length_meters"], -item["entity_count"], item["layer"]))
classified_layers.sort(key=lambda item: (-item["classified_length_meters"], item["layer"]))
block_locked_layers = []
block_locked_total = 0.0
block_locked_classifiable = 0.0
for layer, data in block_layer_lengths.items():
length = float(data["supported_length_source"])
block_locked_total += length
if data["category"]:
block_locked_classifiable += length
block_locked_layers.append(
{
"layer": layer,
"category": data["category"] or None,
"entity_count": data["entity_count"],
"supported_length_meters": round(length * scale, 3),
}
)
block_locked_layers.sort(key=lambda item: -item["supported_length_meters"])
report = {
"dxf_dir": str(dxf_dir),
"coverage": {
"supported_length_meters": round(total_supported * scale, 3),
"classified_supported_length_meters": round(classified_supported * scale, 3),
"supported_length_coverage": round(classified_supported / total_supported, 4) if total_supported else None,
"block_locked_length_meters": round(block_locked_total * scale, 3),
"block_locked_classifiable_length_meters": round(block_locked_classifiable * scale, 3),
"block_captured_classified_length_meters": round(captured_from_blocks * scale, 3),
# Clip-scoped view: with multi-frame clips, elevations/sections/details
# inflate the file-global denominator; judge extraction quality by THIS.
"clipped_supported_length_meters": round(clipped_total * scale, 3) if clips else None,
"clipped_supported_length_coverage": round(clipped_classified / clipped_total, 4)
if clips and clipped_total
else None,
},
"spec_counts": spec_counts(spec),
"files": file_reports,
"classified_layers": classified_layers,
"unclassified_layers": unclassified_layers[:80],
"block_locked_layers": block_locked_layers[:40],
"high_risk_entity_types": dict(global_high_risk.most_common()),
"top_insert_names": dict(global_insert_names.most_common(50)),
"entity_types": dict(global_entity_types.most_common()),
"top_layers": dict(global_layer_counts.most_common(50)),
"agent_questions": agent_questions(unclassified_layers, global_high_risk, global_insert_names),
}
report["gates"] = evaluate_gates(report, spec, config, frames)
return report
GATE_GLAZING_MIN_METERS = 5.0
GATE_ALIGN_CENTER_FACTOR = 0.35
def floors_alignment_gate(spec: dict[str, Any], config: dict[str, Any]) -> dict[str, Any]:
"""FAIL when floors of one stack are horizontally scattered.
The classic multi-frame failure mode:每层保留了图框在图纸墙上的位置,
模型里楼层沿图框间距斜着排开. Wall-bbox centers of floors in the same
cap-group must agree to within GATE_ALIGN_CENTER_FACTOR x plan size.
"""
groups: dict[str, list[tuple[str, float, float, float]]] = defaultdict(list)
for fk, floor in spec.get("floors", {}).items():
walls = floor.get("segments", {}).get("walls") or []
if len(walls) < 10:
continue
xs = [v for s in walls for v in (s["start"][0], s["end"][0])]
ys = [v for s in walls for v in (s["start"][1], s["end"][1])]
size = max(max(xs) - min(xs), max(ys) - min(ys))
groups[derive._cap_floor_group(fk, config)].append((fk, (min(xs) + max(xs)) / 2, (min(ys) + max(ys)) / 2, size))
worst: tuple[float, str] | None = None
for grp, items in groups.items():
if len(items) < 2:
continue
limit = GATE_ALIGN_CENTER_FACTOR * max(item[3] for item in items)
for i in range(len(items)):
for j in range(i + 1, len(items)):
dist = math.hypot(items[i][1] - items[j][1], items[i][2] - items[j][2])
if dist > limit and (worst is None or dist > worst[0]):
worst = (
dist,
f"{grp}: {items[i][0]} vs {items[j][0]} centers {round(dist, 1)} m apart (limit {round(limit, 1)})",
)
if worst:
return {
"name": "floors_aligned",
"status": "FAIL",
"detail": worst[1] + ". Floors are not stacked — per-frame offsets are missing or wrong; "
"run detect_sheet_frames.py and use its clip/offset config.",
}
# Cross-floor datum: envelope centers are blind to "self-consistent but
# misplaced" floors (13号 F3 sat one tower-slot off — 85.8 m — and PASSed:
# the merged twin-tower envelope hid it). Correlate each floor's wall
# profile against the group's reference floor: a strong off-zero peak
# means the whole floor sits N meters from the shared wall datum.
try:
sys.path.insert(0, str(Path(__file__).resolve().parent))
from detect_sheet_frames import correlate_shift, wall_profile
except Exception:
correlate_shift = wall_profile = None # type: ignore
if correlate_shift is not None:
wall_segs: dict[str, list[tuple[float, float, float, float]]] = {}
for fk, floor in spec.get("floors", {}).items():
segs = floor.get("segments", {}).get("walls") or []
if len(segs) >= 10:
wall_segs[fk] = [(s["start"][0], s["start"][1], s["end"][0], s["end"][1]) for s in segs]
by_group: dict[str, list[str]] = defaultdict(list)
for fk in wall_segs:
by_group[derive._cap_floor_group(fk, config)].append(fk)
for grp, keys in by_group.items():
if len(keys) < 2:
continue
ref_key = max(keys, key=lambda k: sum(math.hypot(s[2] - s[0], s[3] - s[1]) for s in wall_segs[k]))
ref_x = wall_profile(wall_segs[ref_key], 0, axis_tol=0.02, min_len=0.4)
ref_y = wall_profile(wall_segs[ref_key], 1, axis_tol=0.02, min_len=0.4)
def zero_cov(ref_prof, prof, tol: float = 0.25) -> float:
total = sum(w for _, w in prof)
if total <= 0:
return 0.0
matched = sum(w for p, w in prof if any(abs(p - rp) <= tol for rp, _ in ref_prof))
return matched / total
for fk in keys:
if fk == ref_key:
continue
for axis, ref_prof in ((0, ref_x), (1, ref_y)):
prof = wall_profile(wall_segs[fk], axis, axis_tol=0.02, min_len=0.4)
shift, cov, ratio = correlate_shift(ref_prof, prof, coarse=0.5, fine=0.05)
# a floor already matching IN PLACE is not misplaced, however
# attractive some shifted peak looks (dense podium references
# let any small profile "match" somewhere: 27号 1#A PH)
if shift is not None and zero_cov(ref_prof, prof) >= 0.7 * cov:
continue
if shift is not None and cov >= 0.45 and ratio >= 1.5 and abs(shift) > 1.0:
return {
"name": "floors_aligned",
"status": "FAIL",
"detail": (
f"cross_floor_datum {grp}: {fk} wall profile matches {ref_key} "
f"only when shifted {round(shift, 1)} m in {'xy'[axis]} "
f"(coverage {cov:.2f}, peak ratio {min(ratio, 99):.1f}) — the floor is "
"self-consistent but misplaced (wrong frame offset / wrong tower slot, "
"13号 F3 错槽 pattern). Re-check that frame's offset against the shared datum."
),
}
return {
"name": "floors_aligned",
"status": "PASS",
"detail": "Wall-bbox centers of same-stack floors coincide; cross-floor wall-profile datum consistent.",
}
def evaluate_gates(
report: dict[str, Any], spec: dict[str, Any], config: dict[str, Any], frames: list[dict[str, Any]] | None = None
) -> list[dict[str, Any]]:
"""Hard checks that must pass before 3D generation.
A gate FAIL means the extraction is known-incomplete in a way that high
coverage numbers cannot excuse; fix the config (block expansion, layer
mapping, source re-export) and re-run instead of accepting the model.
"""
gates: list[dict[str, Any]] = [
{
"name": "all_config_drawings_present",
"status": "PASS",
"detail": "Every configured source drawing exists and passed config validation.",
},
{
"name": "unique_floor_keys",
"status": "PASS",
"detail": "Configured floor keys are unique.",
},
{
"name": "every_floor_nonempty",
"status": "PASS",
"detail": "Every floor contains segments, footprint polygons, or caps.",
},
floors_alignment_gate(spec, config),
]
spec_glazing_segments = sum(counts.get("glazing", 0) for counts in report["spec_counts"].values())
locked_glazing_meters = sum(
item["supported_length_meters"] for item in report["block_locked_layers"] if item["category"] == "glazing"
)
glazing_named_blocks = [
name for name in report["top_insert_names"] if derive.match_patterns(name, derive.GLAZING_BLOCK_PATTERNS)
]
glazing_evidence = locked_glazing_meters >= GATE_GLAZING_MIN_METERS or bool(glazing_named_blocks)
if not glazing_evidence:
status, detail = "SKIP", "No curtain-wall/glazing evidence in blocks or block names."
elif spec_glazing_segments > 0:
status = "PASS"
detail = (
f"Spec contains {spec_glazing_segments} glazing segments; "
f"~{round(locked_glazing_meters, 1)} m of glazing-layer geometry found inside blocks."
)
else:
status = "FAIL"
detail = (
f"Glazing evidence exists (~{round(locked_glazing_meters, 1)} m on glazing layers inside blocks; "
f"block names: {', '.join(glazing_named_blocks[:3]) or 'none'}) but the spec has 0 glazing segments. "
"Enable block expansion / classify_block_contents or fix glazing layer mapping, then re-run."
)
gates.append({"name": "glazing_captured", "status": status, "detail": detail})
annotation_classified = [
item["layer"]
for item in report["classified_layers"]
if derive.match_patterns(item["layer"], ["*-IDEN", "*ANNO*", "*-DIMS"])
]
gates.append(
{
"name": "no_annotation_layers_classified",
"status": "FAIL" if annotation_classified else "PASS",
"detail": (
f"Annotation/identification layers classified as geometry: {', '.join(annotation_classified[:5])}. "
"These are tags, not elements; exclude them."
if annotation_classified
else "No annotation/identification layers classified."
),
}
)
coverage = report["coverage"]
locked_classifiable = float(coverage["block_locked_classifiable_length_meters"])
captured = float(coverage["block_captured_classified_length_meters"])
if locked_classifiable > 0 and captured < 0.5 * locked_classifiable:
gates.append(
{
"name": "block_locked_geometry_captured",
"status": "WARN",
"detail": (
f"~{round(locked_classifiable, 1)} m of classifiable geometry sits inside blocks but only "
f"~{round(captured, 1)} m was captured. Review block expansion settings."
),
}
)
else:
gates.append(
{
"name": "block_locked_geometry_captured",
"status": "PASS",
"detail": (
f"Captured ~{round(captured, 1)} m of ~{round(locked_classifiable, 1)} m "
"classifiable block-locked geometry."
),
}
)
gates.append(frame_ledger_gate(config, frames))
gates.append(vertical_storey_continuity_gate(spec))
gates.append(declared_area_gate(spec, config))
gates.append(storey_count_gate(spec, config))
return gates
def _rect_overlap_ratio(a: list[float], b: list[float]) -> float:
"""Intersection area / area of rect a (a = the detected frame clip)."""
ax0, ay0, ax1, ay1 = min(a[0], a[2]), min(a[1], a[3]), max(a[0], a[2]), max(a[1], a[3])
bx0, by0, bx1, by1 = min(b[0], b[2]), min(b[1], b[3]), max(b[0], b[2]), max(b[1], b[3])
iw = min(ax1, bx1) - max(ax0, bx0)
ih = min(ay1, by1) - max(ay0, by0)
if iw <= 0 or ih <= 0:
return 0.0
area_a = max((ax1 - ax0) * (ay1 - ay0), 1e-9)
return (iw * ih) / area_a
def frame_ledger_gate(config: dict[str, Any], frames: list[dict[str, Any]] | None) -> dict[str, Any]:
"""Every DETECTED plan frame must be in the config or explicitly discarded.
Frames the Agent silently ignores are exactly where whole floors (or whole
wings) go missing on a new project. Pass detect_sheet_frames' out-config
via --frames; account for unused frames in config
`"discarded_frames": [{"name": ..., "reason": ...}]`.
"""
if not frames:
return {"name": "frame_ledger", "status": "SKIP", "detail": "No --frames given; frame ledger not checked."}
discarded = {str(d.get("name", "")) for d in config.get("discarded_frames", [])}
cfg_by_file: dict[str, list[dict[str, Any]]] = {}
for drawing in config.get("drawings", []) or []:
cfg_by_file.setdefault(str(drawing.get("file", "")), []).extend(drawing.get("floors", []) or [])
unaccounted: list[str] = []
total = 0
for fcfg in frames:
for drawing in fcfg.get("drawings", []) or []:
file = str(drawing.get("file", ""))
cfg_floors = cfg_by_file.get(file, [])
for frame in drawing.get("floors", []) or []:
total += 1
name = str(frame.get("name", ""))
clip = frame.get("clip")
if name in discarded:
continue
if clip and any(fl.get("clip") and _rect_overlap_ratio(clip, fl["clip"]) >= 0.3 for fl in cfg_floors):
continue
unaccounted.append(f"{file}: {frame.get('key', '?')} {name}")
if unaccounted:
shown = "; ".join(unaccounted[:12]) + (" …" if len(unaccounted) > 12 else "")
return {
"name": "frame_ledger",
"status": "FAIL",
"detail": (
f"{len(unaccounted)}/{total} detected plan frames are neither used by the config "
f"nor listed in discarded_frames: {shown}. Silently ignored frames are where "
"floors/wings vanish — use them or discard them WITH a reason."
),
}
return {
"name": "frame_ledger",
"status": "PASS",
"detail": f"All {total} detected plan frames are used or explicitly discarded.",
}
def declared_area_gate(spec: dict[str, Any], config: dict[str, Any]) -> dict[str, Any]:
"""Model footprint areas must match declared areas (经济技术指标表/标注).
Cross-evidence beats self-consistency: 1#B passed every internal gate with
half the building missing — the site plan exposed it. Config:
`"declared_areas_m2": {"F1": 812, ...}`, optional
`"declared_area_tolerance"` (default 0.15).
"""
declared = config.get("declared_areas_m2")
if not declared:
return {
"name": "area_declared",
"status": "SKIP",
"detail": "No declared_areas_m2 in config; area cross-check not requested.",
}
tol = float(config.get("declared_area_tolerance", 0.15))
problems = []
for key, want in declared.items():
want = float(want)
floor = spec.get("floors", {}).get(str(key))
if floor is None:
problems.append(f"{key}: not in spec")
continue
got = float((floor.get("footprint") or {}).get("area") or 0.0)
if got <= 0:
problems.append(f"{key}: no footprint area in spec")
elif abs(got - want) / want > tol:
problems.append(
f"{key}: model {round(got, 1)} m² vs declared {round(want, 1)} m² "
f"({round(100 * (got - want) / want):+d}%)"
)
if problems:
return {
"name": "area_declared",
"status": "FAIL",
"detail": (
"Footprint area disagrees with the declared evidence: "
+ "; ".join(problems)
+ f" (tolerance ±{round(tol * 100)}%). A missing wing / swallowed neighbour "
"sheet moves the area far more than balconies do."
),
}
return {
"name": "area_declared",
"status": "PASS",
"detail": f"{len(declared)} floor area(s) within ±{round(tol * 100)}% of declared values.",
}
def vertical_storey_continuity_gate(spec: dict[str, Any]) -> dict[str, Any]:
"""A final full-building model may not jump from F2 directly to F16.
Representative typical-floor drawings are source evidence, not permission
to leave the repeated storeys absent from the delivered building. The
repeated floors must be instantiated and labelled as derived repetitions.
"""
import re as _re
numbered: list[tuple[int, str, dict[str, Any]]] = []
for key, floor in spec.get("floors", {}).items():
match = _re.fullmatch(r"F(\d+)", str(key))
if match:
numbered.append((int(match.group(1)), str(key), floor))
numbered.sort()
if len(numbered) < 2:
return {
"name": "vertical_storey_continuity",
"status": "PASS",
"detail": f"{len(numbered)} numbered storey; no internal continuity gap to test.",
}
missing: list[str] = []
z_gaps: list[str] = []
for (left_no, left_key, left), (right_no, right_key, right) in zip(numbered, numbered[1:]):
if right_no != left_no + 1:
missing.append(f"{left_key}->{right_key}")
continue
left_z = float(left.get("z", 0.0))
right_z = float(right.get("z", 0.0))
nominal = max(float(left.get("height", 0.0)), float(right.get("height", 0.0)), 1.0)
if right_z - left_z > max(8.0, nominal * 2.0):
z_gaps.append(f"{left_key}->{right_key}={round(right_z - left_z, 2)}m")
if missing or z_gaps:
parts = []
if missing:
parts.append("missing numbered ranges " + ", ".join(missing))
if z_gaps:
parts.append("unexplained elevation gaps " + ", ".join(z_gaps))
return {
"name": "vertical_storey_continuity",
"status": "FAIL",
"detail": (
"; ".join(parts)
+ ". Instantiate source-supported repeated typical floors; do not deliver separated floor islands."
),
}
return {
"name": "vertical_storey_continuity",
"status": "PASS",
"detail": f"Numbered storeys are contiguous from {numbered[0][1]} to {numbered[-1][1]}.",
}
def storey_count_gate(spec: dict[str, Any], config: dict[str, Any]) -> dict[str, Any]:
"""Above-ground storey count must match the declared count (总图 22F 标签 / 标高梯级数).
Config: `"expected_storeys_above_ground": 22`. Counts spec floors keyed
F<n> (mezzanines F<n>M, PH, RF, basements excluded).
"""
expected = config.get("expected_storeys_above_ground")
if expected is None:
return {
"name": "storey_count",
"status": "SKIP",
"detail": "No expected_storeys_above_ground in config; count cross-check not requested.",
}
import re as _re
actual = sum(1 for k in spec.get("floors", {}) if _re.fullmatch(r"F\d+", str(k)))
if actual != int(expected):
return {
"name": "storey_count",
"status": "FAIL",
"detail": (
f"Spec has {actual} above-ground storeys (F<n> keys) but the declared "
f"count is {expected} (总图 label / elevation ladder). A silently dropped "
"floor_order entry or an unexpanded multi-floor frame is the usual cause."
),
}
return {
"name": "storey_count",
"status": "PASS",
"detail": f"{actual} above-ground storeys match the declared count.",
}
def agent_questions(unclassified_layers: list[dict], high_risk: Counter, insert_names: Counter) -> list[str]:
questions = []
if unclassified_layers:
top = [item for item in unclassified_layers if item["supported_length_meters"] > 0][:8]
if top:
names = ", ".join(f"`{item['layer']}`" for item in top[:5])
questions.append(f"Review unclassified geometry layers with significant length: {names}.")
if high_risk.get("INSERT", 0):
questions.append("Decide whether INSERT/block geometry must be recursively expanded before modeling.")
if high_risk.get("HATCH", 0):
questions.append("Check whether hatches represent walls/slabs/columns that line extraction missed.")
if insert_names:
names = ", ".join(f"`{name}`" for name, _count in insert_names.most_common(5))
questions.append(f"Inspect frequent block names: {names}.")
questions.append("Compare SVG overlays against CAD intent before accepting the model.")
return questions
def markdown(report: dict[str, Any]) -> str:
coverage = report["coverage"]
lines = [
"# Extraction Audit",
"",
"Use this as evidence for Agent judgment. High coverage does not prove correctness; low coverage or high-risk entities require review.",
"",
"## Gates",
"",
]
for gate in report.get("gates", []):
lines.append(f"- **{gate['status']}** `{gate['name']}`: {gate['detail']}")
lines.extend(
[
"",
"## Coverage",
"",
f"- Supported vector length: `{coverage['supported_length_meters']}` m",
f"- Classified supported length: `{coverage['classified_supported_length_meters']}` m",
f"- Supported-length coverage: `{coverage['supported_length_coverage']}` "
"(modelspace linework only — block-locked geometry is NOT in this denominator)",
*(
[
f"- CLIP-SCOPED coverage: `{coverage['clipped_supported_length_coverage']}` over "
f"`{coverage['clipped_supported_length_meters']}` m inside floor clips — "
"on multi-frame sheets judge extraction by THIS number, not the file-global one"
]
if coverage.get("clipped_supported_length_coverage") is not None
else []
),
f"- Block-locked geometry: `{coverage['block_locked_length_meters']}` m total, "
f"`{coverage['block_locked_classifiable_length_meters']}` m classifiable, "
f"`{coverage['block_captured_classified_length_meters']}` m captured into the spec",
"",
"## Agent Questions",
"",
]
)
for question in report["agent_questions"]:
lines.append(f"- {question}")
lines.extend(["", "## Block-Locked Geometry By Layer", ""])
for item in report.get("block_locked_layers", [])[:15]:
lines.append(
f"- `{item['layer']}` (category: {item['category'] or 'unclassified'}): "
f"entities `{item['entity_count']}`, length `{item['supported_length_meters']}` m"
)
lines.extend(["", "## Spec Counts", ""])
for floor_key, counts in report["spec_counts"].items():
parts = ", ".join(f"{key}: {value}" for key, value in counts.items())
lines.append(f"- `{floor_key}`: {parts}")
lines.extend(["", "## High Risk Entity Types", ""])
for key, value in report["high_risk_entity_types"].items():
lines.append(f"- `{key}`: {value}")
lines.extend(["", "## Top Unclassified Geometry Layers", ""])
for layer in report["unclassified_layers"][:20]:
lines.append(
f"- `{layer['layer']}`: entities `{layer['entity_count']}`, "
f"supported length `{layer['supported_length_meters']}` m, "
f"types `{layer['entity_types']}`"
)
lines.extend(["", "## Classified Layers", ""])
for layer in report["classified_layers"][:30]:
lines.append(
f"- `{layer['layer']}`: `{layer['categories']}`, classified length `{layer['classified_length_meters']}` m"
)
lines.extend(["", "## Files", ""])
for item in report["files"]:
lines.append(
f"- `{item['file']}`: coverage `{item['supported_length_coverage']}`, high-risk `{item['high_risk_types']}`"
)
return "\n".join(lines)
def main() -> int:
args = parse_args()
config = derive.read_json(Path(args.config))
spec = json.loads(Path(args.spec).read_text(encoding="utf-8"))
try:
config = showcase_schema.validate_config(config, Path(args.dxf_dir))
showcase_schema.validate_spec(spec)
except showcase_schema.ValidationFailure as exc:
print(str(exc), file=sys.stderr)
return 3
frames = None
if args.frames:
frames = [json.loads(Path(p).read_text(encoding="utf-8")) for p in args.frames]
report = audit(Path(args.dxf_dir), config, spec, Path(args.vendor_dir), frames)
json_path, md_path = output_paths(Path(args.out))
json_path.parent.mkdir(parents=True, exist_ok=True)
md_path.parent.mkdir(parents=True, exist_ok=True)
json_path.write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8")
md_path.write_text(markdown(report), encoding="utf-8")
failed_gates = [gate for gate in report.get("gates", []) if gate["status"] == "FAIL"]
print(
json.dumps(
{
"json": str(json_path),
"markdown": str(md_path),
"gates": {gate["name"]: gate["status"] for gate in report.get("gates", [])},
},
ensure_ascii=False,
)
)
if failed_gates and not args.no_strict_gates:
for gate in failed_gates:
print(f"GATE FAIL [{gate['name']}]: {gate['detail']}", file=sys.stderr)
return 3
return 0
if __name__ == "__main__":
raise SystemExit(main())