Initial commit: cad-building-showcase skill
Agent-led CAD-drawing → 3D web showcase pipeline. Inspects CAD reports, maps layers/floors, extracts a normalized building spec (with block-probed glazing and audit gates), builds Blender/trimesh GLB variants, and assembles a Three.js web viewer. Includes XREF-shell detection and a regression test. Large inputs/outputs (work/, outputs/, source_cad/, renders) are gitignored. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
505
scripts/audit_showcase_extraction.py
Executable file
505
scripts/audit_showcase_extraction.py
Executable file
@@ -0,0 +1,505 @@
|
||||
#!/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()
|
||||
|
||||
|
||||
def iter_block_contents(entity, max_depth: int, depth: int = 0):
|
||||
"""Yield every leaf entity inside an INSERT, regardless of extraction config."""
|
||||
if depth >= max_depth:
|
||||
return
|
||||
try:
|
||||
from ezdxf import explode as ezdxf_explode
|
||||
|
||||
virtual_entities = list(ezdxf_explode.virtual_block_reference_entities(entity))
|
||||
except Exception:
|
||||
return
|
||||
for virtual_entity in virtual_entities:
|
||||
if virtual_entity.dxftype() == "INSERT":
|
||||
yield from iter_block_contents(virtual_entity, max_depth, depth + 1)
|
||||
else:
|
||||
yield virtual_entity
|
||||
|
||||
|
||||
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)
|
||||
return {
|
||||
"layer": layer,
|
||||
"type": dxftype,
|
||||
"category": category,
|
||||
"segment_count": len(segments),
|
||||
"supported_length": supported_length,
|
||||
"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 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
|
||||
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.
|
||||
for inner in iter_block_contents(source_entity, max_block_depth):
|
||||
inner_length = sum(
|
||||
segment_length_source(segment)
|
||||
for segment in derive.entity_segments(inner, max_chord)
|
||||
)
|
||||
if inner_length <= 0:
|
||||
continue
|
||||
inner_layer = derive.entity_layer(inner)
|
||||
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 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),
|
||||
},
|
||||
"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)
|
||||
return report
|
||||
|
||||
|
||||
GATE_GLAZING_MIN_METERS = 5.0
|
||||
|
||||
|
||||
def evaluate_gates(report: 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]] = []
|
||||
|
||||
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"- 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())
|
||||
Reference in New Issue
Block a user