#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 #include #include #include 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 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":}]}} // 与既有截图工具返回结构保持一致,便于标准 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