Files
cad-agent/dwg_review/cad_tools.py
T
gjm d4a9442e30 feat(review): 几何干涉检查 find_collisions(BVH 粗筛 + 容器过滤)
C++(cad_mcp_plugins):
- 新增 bvh.h:header-only 2D BVH(质心中位分割,叶<=4,SelfPairs 交叉对枚举)
- 新增 find_collisions.cpp:粗筛 AABB 相交实体对,附重叠盒 + 逐轴重叠比 ratio
  (= 逐轴重叠长/较短长 之积,退化轴取 1.0),按 ratio 降序 + 分页;
  narrow=true 只对返回页跑 intersectWith 细筛(附 exact)
- 容器过滤(关键):图框块/分区边界会与一切相交,用重叠【度】判定
  deg >= max(exclude_container_deg=8, exclude_container_frac=0.25 * 其他实体数),
  与图幅/缩放无关;被剔除者在 excluded_containers 里如实报告
  (初版用面积判据对多图框并排失效:3 框只占并集 27/13/7% 全部漏过,
   全模型 163 对噪声;改度判据后图框 deg=35/25/23 vs 其余 deg<=3,分离干净)
- cmakelists.txt + 两个 mcp_config.json 注册(28 工具)

agent 侧(Python):
- cad_tools.READ_ONLY_TOOLS += find_collisions(受控子循环可调用)
- checks.check_collisions + run_deterministic_checks(受 CHECK_COLLISIONS 开关,默认关)
- config += CHECK_COLLISIONS=False / COLLISION_MIN_OVERLAP=0.5
- checklist.py / review_checklist.json += 「干涉/碰撞」类别(high)+ 提示词协议
  (先 find_collisions 拿候选 → inspect_region 目视确认;AABB 相交 != 真干涉)

验证:
- verify_collisions.py 17/17 PASS:全模型候选 163→80(全部 ratio=0.000),
  min_overlap=0.5 → 0 对;逐图框 0 对;narrow top-5 中 4 条 exact=true;
  --inject 注入深重叠矩形报出 ratio=0.902 并清理 2/2
- pytest 355 passed(新增 12 项)
- 端到端:LLM 主动调用 find_collisions{min_overlap:0.2,narrow:true,window:<图框>},未误报
2026-10-16 19:46:44 +08:00

82 lines
2.5 KiB
Python

"""工具表:从 tools/list 动态构建,新增工具无需改代码。"""
from .mcp_client import McpClient
# 只读工具白名单:审查阶段的受控子循环只允许调用这些(不做任何写库操作)
READ_ONLY_TOOLS = frozenset(
{
"query_entities",
"get_text_content",
"get_dimensions",
"get_block_references",
"get_polylines_info",
"get_entity_detail",
"get_layers",
"get_layouts",
"get_drawing_info",
"detect_drawing_frames",
"find_duplicate_entities",
"find_zero_length",
"find_collisions",
"zoom_to_window",
"zoom_to_entity",
"get_viewport_screenshot",
}
)
# 写类工具前缀/名称:调用时不重试,且默认禁止 LLM 在审查阶段触发
_WRITE_PREFIXES = ("add_", "delete_", "draw_", "set_")
_WRITE_EXACT = {"set_entity_properties", "highlight_entities"}
def is_write_tool(name: str) -> bool:
return name.startswith(_WRITE_PREFIXES) or name in _WRITE_EXACT
class CadTools:
"""工具注册表 + 调用代理。"""
def __init__(self, client=None):
self.client = client or McpClient()
self._by_name = {}
self.refresh()
def refresh(self):
tools = self.client.list_tools(use_cache=False)
self._by_name = {t["name"]: t for t in tools}
return tools
@property
def names(self):
return sorted(self._by_name)
def has(self, name):
return name in self._by_name
def describe(self, name):
return self._by_name.get(name)
def openai_tools(self, names=None):
"""转换为 OpenAI function 工具格式;names 为可选的名称白名单集合。"""
out = []
for name, tool in self._by_name.items():
if names is not None and name not in names:
continue
out.append(
{
"type": "function",
"function": {
"name": name,
"description": tool.get("description", ""),
"parameters": tool.get("inputSchema")
or {"type": "object", "properties": {}},
},
}
)
return out
def call(self, name, arguments=None, *, is_write=None):
if is_write is None:
is_write = is_write_tool(name)
return self.client.call_tool(name, arguments, is_write=is_write)