#!/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 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", ) 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) -> 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) 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.", } return { "name": "floors_aligned", "status": "PASS", "detail": "Wall-bbox centers of same-stack floors coincide.", } def evaluate_gates(report: dict[str, Any], spec: dict[str, Any], config: dict[str, Any]) -> 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]] = [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": "WARN" 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." ), } ) return gates 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']}`, " f"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']}`, " f"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")) report = audit(Path(args.dxf_dir), config, spec, Path(args.vendor_dir)) 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())