Files
envi-code/SourceCode/Code2026/cad_mcp_plugins/dwg_review_common.h
T
company-desktop ecb8436720 feat: 移植 cad_mcp_plugins,并对齐中望平台的符号与依赖
插件工程在 CadProject/src/cad_mcp_plugins(8 个 cpp/h),本次一并整合进 code2026。

移植改动:
- 源码拷到 SourceCode/Code2026/cad_mcp_plugins/;cadsdk.h -> KTCad.h
- 输出是普通 DLL(.dll),由 cad_mcp_frame 运行时 LoadLibrary,不注册 ARX 命令
- 新增 cad_mcp_plugins/cmakelists.txt:按 code2026 惯例
  (target_set_name_and_suffix ".dll" / target_link_objectarx / 预编译头 / SUB_ARX)
- mcp_plugin_api.h 从 cad_mcp_frame/inc 上移到 SourceCode/Code2026/inc:
  frame 与 plugins 共用,且在 ShsGlobalIncludes 的 include 路径里两边都可见;
  frame 的 CMake 去掉已删除的本地 inc 目录
- 顶层在同一个 C++11+ 条件里挂载 add_custom_module_2(cad_mcp_plugins CadMcpPlugins)
- 中望适配:源码里的 #pragma comment(lib,"rxapi.lib") 在中望下没有这个库(叫 ZwRx.lib),
  该 pragma 与导出名都按 #ifdef ZWCAD 分开(中望导出 zcrxGetApiVersion);
  frame 里 GetProcAddress 查插件版本号的函数名同样按平台分

验证:
- R230:cad_mcp_frame.arx 826 KB + cad_mcp_plugins.dll 866 KB,已自动部署
- Z2026:cad_mcp_frame.zrx 825 KB + cad_mcp_plugins.dll 864 KB,已自动部署
- 配置里 16 个 c++_dll 工具的工厂函数(CreateXxxTool)全部出现在 dll 导出表里
- 所有 .cpp/.h 为 UTF-8 with BOM

未做:没有在 CAD 里实跑。另外配置文件本身没有随构建部署(set_debug_post_build 只拷产物),
需要把 cad-mcp-frame.json(或改名 mcp_config.json)与 LISP 目录放到部署目录,
否则 frame 起来后 LoadAndRegister 失败会直接自关。详见 cad_mcp_frame-review.md。
2026-10-29 00:16:37 +08:00

91 lines
3.7 KiB
C++

#pragma once
// ============================================================================
// dwg_review_common.h
// 图纸审查工具集 - 公共辅助层
//
// 供 dwg_review_overview / dwg_review_query / dwg_review_frame /
// dwg_review_check / dwg_review_view 共用,避免各工具重复实现:
// - 字符串/句柄/图层/实体类型/包围盒 等基础读取
// - 通配符匹配、扫描范围过滤
// - 通用查询参数解析 (space / layer_filter / window / limit / offset)
// - 空间定位与统一实体遍历 forEachEntity
// - 分页信封、错误体、坐标序列化
// ============================================================================
#include "KTCad.h"
#include <string>
#include <vector>
#include <functional>
#include <nlohmann/json.hpp>
namespace dwg_review
{
// 审查工具统一错误码区间
enum ReviewErrorCode
{
kErrBadParam = -32001, // 参数非法
kErrNotFound = -32002, // 实体/图层等未找到
kErrNoDocument = -32003, // 无活动文档
kErrInternal = -32603 // 内部错误
};
// 通用查询参数(所有"读"类工具共用)
struct CommonQuery
{
std::string space = "Model"; // "Model" 或布局名
std::string layerFilter; // 图层通配符,空 = 不过滤
bool useWindow = false; // 是否限定扫描范围
double minX = 0.0, minY = 0.0, maxX = 0.0, maxY = 0.0;
int limit = 200; // 单次最大返回,上限 1000
int offset = 0; // 分页偏移
};
// 包围盒
struct BBox
{
double minX = 0.0, minY = 0.0, maxX = 0.0, maxY = 0.0;
bool valid = false;
};
// 解析通用查询参数
CommonQuery parseCommonQuery(const nlohmann::json& args);
// ---------- 基础读取 ----------
std::string wstrToUtf8(const std::wstring& w);
std::string acharToUtf8(const ACHAR* s);
std::string handleOf(AcDbEntity* pEnt); // 实体句柄(大写十六进制)
std::string layerOf(AcDbEntity* pEnt); // 实体所在图层名
std::string entityTypeName(AcDbEntity* pEnt); // DXF 风格类型名,如 "LINE"
bool entityBBox(AcDbEntity* pEnt, BBox& out); // 取几何包围盒(含异常保护)
std::string blockNameOf(AcDbBlockReference* pRef); // 取块参照的块名(经块表记录)
bool objectIdFromHandle(const std::string& handleStr, AcDbObjectId& out); // 句柄字符串 -> ObjectId
// ---------- 匹配 / 过滤 ----------
bool wildcardMatch(const std::string& name, const std::string& pattern);
bool bboxInWindow(const BBox& b, const CommonQuery& q);
bool layerAccepted(AcDbEntity* pEnt, const CommonQuery& q);
// ---------- 遍历 ----------
// 打开指定空间的块表记录(成功返回的对象需调用者 close)
AcDbBlockTableRecord* openSpace(AcDbDatabase* pDb, const std::string& space);
// 实体访问器:返回 false 可提前终止遍历
typedef std::function<bool(AcDbEntity*)> EntityVisitor;
// 遍历指定空间实体:自动处理图层过滤、范围过滤、对象关闭。
// 注意:实体以 kForRead 打开,visitor 内只做读取;需要写操作请自行按句柄重开。
void forEachEntity(const CommonQuery& q, const EntityVisitor& visitor);
// ---------- 结果封装 ----------
nlohmann::json pageResult(int total, int returned, int offset, const nlohmann::json& items);
nlohmann::json makeError(int code, const std::string& msg);
// 把数据包装为 MCP 文本结果信封:{"result":{"content":[{"type":"text","text":<json>}]}}
// 与既有截图工具返回结构保持一致,便于标准 MCP 客户端 / agent 统一读取 result.content。
nlohmann::json makeJsonResult(const nlohmann::json& data);
nlohmann::json point2Json(double x, double y);
nlohmann::json point3Json(double x, double y, double z);
nlohmann::json bboxJson(const BBox& b);
} // namespace dwg_review