#pragma once // ============================================================================ // aec_mcp_common.h // cad_mcp_aec 插件的公共辅助层 // // 与 cad_mcp_plugins 的 dwg_review_common 定位相同,但面向"编辑自定义实体": // 它要能 new / 改仓库的 AecDb* 实体,所以链接 CoreCommand.arx。 // // 约定(写进工具描述里,供 LLM 读): // * 所有**坐标**与**建筑尺寸**(墙厚/墙高/板厚/柱高/梁宽…)用**图纸单位**; // * 管道/圆管的**公称直径、外径、壁厚**用 **mm**,内部按 AS_MUNIT 转成米, // 与仓库里所有管道对话框一致(BZGDDlg / AecPipeLineTool)。 // ============================================================================ #include "KTCad.h" #include #include namespace aec_mcp { // 错误码区间(与 dwg_review 保持一致的风格) enum ErrCode { kErrBadParam = -32001, // 参数非法 kErrNotFound = -32002, // 实体/图层未找到 kErrNoDocument = -32003, // 没有活动图纸 kErrWrongType = -32004, // handle 指向的类型不对(比如传了柱子却调 update_pipe) kErrInternal = -32603 // 内部错误 }; // ---------- 结果封装 ---------- // okJson:把业务 json 塞进 MCP 的 text content(LLM 直接读到结构化结果) nlohmann::json okJson(const nlohmann::json& body); nlohmann::json errJson(int code, const std::wstring& msg); // ---------- 参数读取 ---------- bool hasKey(const nlohmann::json& a, const char* key); double numOr(const nlohmann::json& a, const char* key, double defVal); int intOr(const nlohmann::json& a, const char* key, int defVal); std::wstring wstrOr(const nlohmann::json& a, const char* key, const wchar_t* defVal); // 点:接受 {"x":..,"y":..,"z":..} 或 [x, y, z] 两种写法;z 缺省 0 bool pointOf(const nlohmann::json& v, AcGePoint3d& pt); bool readPoint(const nlohmann::json& a, const char* key, AcGePoint3d& pt); // 点串:[[x,y],...] 或 [{"x":..,"y":..},...] bool readPoint2dArray(const nlohmann::json& a, const char* key, AcGePoint2dArray& pts); // ---------- 图纸 / 对象 ---------- AcDbDatabase* workingDb(); // 写数据库必须先拿文档锁:MCP 工具是在主线程的消息里跑的,不在命令上下文中, // 不锁会拿到 Acad::eLockViolation(0x140)。 // 构造即加锁,析构解锁;嵌套调用安全(lockDocument 可重复加锁)。 class DocLock { public: DocLock(); ~DocLock(); bool isLocked() const { return m_bLocked; } private: DocLock(const DocLock&); DocLock& operator=(const DocLock&); bool m_bLocked; AcApDocument* m_pDoc; }; // 按 handle 打开实体(调用方负责 close)。失败返回 NULL。 AcDbEntity* openByHandle(const std::string& handleStr, AcDb::OpenMode mode, AcDbObjectId* pIdOut = nullptr); // 实体句柄字符串(大写十六进制),失败返回空串 std::string handleOf(AcDbObject* pObj); // 加入当前空间(默认模型空间),成功后 id 通过 pIdOut 返回 Acad::ErrorStatus addToSpace(AcDbEntity* pEnt, AcDbObjectId* pIdOut, const std::wstring& space = L""); // 图层名 -> ObjectId,找不到返回 kNull(不新建图层,避免污染图纸) AcDbObjectId layerIdByName(const std::wstring& name); // ---------- 其它 ---------- std::string acharToUtf8(const ACHAR* s); std::wstring utf8ToWstr(const std::string& s); // 实体类名(不含 AecDb 前缀),如 "EwPipe" std::string classNameOf(AcDbObject* pObj); // 包围盒(失败返回 false) bool entityBBox(AcDbEntity* pEnt, AcGePoint3d& ptMin, AcGePoint3d& ptMax); // 修改后让显示/3D 缓存跟着更新 void touchEntity(AcDbEntity* pEnt); } // namespace aec_mcp