// ============================================================================ // aec_mcp_dim_tools.cpp // cad_mcp_aec 的标注工具(工作计划第 6 项) // * add_aec_pipe_dim / update_aec_pipe_dim —— AecDbEwPipeDim(纯 2D,没有 3D 显示) // // 8 个标注命令(PL_XHBZ 序号 / PL_XHPB 序号批标 / PL_DKBZ 洞口 / PL_TGBZ 套管 / // PL_YHBZ 翼环 / PL_FMBZ 阀门 / IS_BZFM 标注阀门 / IS_GZGJ 标注管件)用的都是同一个类, // 创建序列也一致(见 PipelineCmd/command.cpp PL_XHBZCmdFnc): // new AecDbEwPipeDim // AddAecDbWEntity(被标注实体) // 关联 // SetDimStyle(n) / SetDimDir(0) / SetDimTextUporDownFlag(1) // setLayer(LAY_PP3D_DIM) + SetTextStyleId + SetTextLayerId // SetPScale(getPScale()) / SetTextHeight(3.0) / SetTextWidthScale(0.75) // AddDimText(上文字, 下文字) // AppendPoint(引出起点) / AppendPoint(文字位置) // 入库 // 差别只在 SetDimStyle 的取值和文字怎么拼(命令是按被标注实体的专业数据拼的)。 // 工具这版:样式/方向/上下标记/字高/字宽比/图层都可给,文字由调用方直接传 // (`text` 上、`text_down` 下)——自动拼文字没做,命令里那套按实体数据拼的逻辑要逐类搬。 // ============================================================================ #include "StdAfx.h" #include "KTCad.h" #include "aec_mcp_common.h" #include "mcp_plugin_api.h" #include "AecDbObjXDataMap.h" #include "AecDbEwPipeDim.h" #include "AecDbObjXDataMap.h" #include "CoreTools.h" #include "KTCadUtility.h" #include "KTSetting.h" using namespace aec_mcp; namespace { const TCHAR* kDimLayer = _T("LAY_PP3D_DIM"); // PipelineTools::gLayer_Pipe_Dim const TCHAR* kDimTextStyle = _T("Pipe_Dim"); // PipelineTools::gTextStyle_Pipe_Dim std::string HandleOfId(const AcDbObjectId& id) { if (id.isNull()) return std::string(); AcDbObject* pObj = NULL; if (Acad::eOk != acdbOpenObject(pObj, id, AcDb::kForRead)) return std::string(); const std::string s = handleOf(pObj); pObj->close(); return s; } // 8 个命令各自的 dim style(命令里写死的取值,没给的用 3=序号标注) int StyleOfKind(const std::wstring& sKind) { if (sKind == L"seq") return 3; // PL_XHBZ 序号标注 if (sKind == L"seq_batch") return 3; // PL_XHPB 序号批标 if (sKind == L"opening") return 3; // PL_DKBZ 洞口 if (sKind == L"casing") return 3; // PL_TGBZ 套管 if (sKind == L"ring") return 3; // PL_YHBZ 翼环 if (sKind == L"valve") return 3; // PL_FMBZ 阀门 if (sKind == L"iso_valve") return 3; // IS_BZFM if (sKind == L"iso_fitting") return 3; // IS_GZGJ return 3; } void AppendPoints(AecDbEwPipeDim* pDim, const nlohmann::json& a, bool& bOk) { bOk = false; if (!hasKey(a, "points") || !a["points"].is_array() || a["points"].size() < 2) return; for (size_t i = 0; i < a["points"].size() && i < 2; ++i) { const nlohmann::json& pt = a["points"][i]; AcGePoint3d ptOn(0.0, 0.0, 0.0); if (!pointOf(pt, ptOn)) return; pDim->AppendPoint(ptOn); } bOk = true; } } // --------------------------------------------------------------------------- // add_aec_pipe_dim —— 管线标注(8 个标注命令共用) // --------------------------------------------------------------------------- class ToolAddPipeDim : public mcp_Tool { public: std::string private_name() const override { return "add_aec_pipe_dim"; } std::string private_description() const override { return "建管线标注(`AecDbEwPipeDim`,纯 2D 无 3D 显示)。覆盖 8 个标注命令:\n" " seq=PL_XHBZ 序号 / seq_batch=PL_XHPB 序号批标 / opening=PL_DKBZ 洞口 / casing=PL_TGBZ 套管 /\n" " ring=PL_YHBZ 翼环 / valve=PL_FMBZ 阀门 / iso_valve=IS_BZFM 标注阀门 / iso_fitting=IS_GZGJ 标注管件\n" "给被标注实体的 handle、引出起点+文字位置两个点、上/下文字,样式/方向/字高/字宽比可选。\n" "注意:命令里文字是按被标注实体的专业数据自动拼的(如\"预留D300洞口\")," "这版需要调用方自己把 text 传进来。"; } nlohmann::json private_input_schema() const override { return nlohmann::json::parse(R"({ "type": "object", "properties": { "target": { "type": "string", "description": "被标注实体的 handle(管/洞/套管/管件…)" }, "kind": { "type": "string", "enum": [ "seq", "seq_batch", "opening", "casing", "ring", "valve", "iso_valve", "iso_fitting" ], "description": "标注种类(决定 dim style 与语义),缺省 seq" }, "points": { "type": "array", "items": { "type": "array", "items": { "type": "number" } }, "description": "两个点:[[引出起点],[文字位置]]" }, "text": { "type": "string", "description": "上排文字(序号/洞洞尺寸等)" }, "text_down": { "type": "string", "description": "下排文字(如中心标高)" }, "dim_style": { "type": "integer", "description": "标注样式编号,缺省按 kind" }, "dim_dir": { "type": "integer", "description": "标注方向,缺省 0" }, "text_up_down": { "type": "integer", "description": "上下标记:1 上 / -1 下,缺省 1" }, "text_height": { "type": "number", "description": "字高,缺省 3.0(命令里 2.5×dimscale)" }, "text_width_scale": { "type": "number", "description": "字宽比,缺省 0.75" }, "p_scale": { "type": "number", "description": "出图比例,缺省 100" }, "layer": { "type": "string", "description": "图层,缺省 LAY_PP3D_DIM" }, "text_style": { "type": "string", "description": "文字样式,缺省 Pipe_Dim" }, "color": { "type": "integer" } }, "required": [ "target", "points", "text" ] })"); } nlohmann::json Execute(const nlohmann::json& args) override { if (workingDb() == NULL) return errJson(kErrNoDocument, L"没有活动图纸"); DocLock lock; if (!lock.isLocked()) return errJson(kErrInternal, L"拿不到图纸写锁(有别的命令在跑?)"); if (!hasKey(args, "target")) return errJson(kErrBadParam, L"缺少 target(被标注实体的 handle)"); const std::string sTarget = args["target"].get(); AcDbEntity* pTarget = openByHandle(sTarget, AcDb::kForRead); if (pTarget == NULL) return errJson(kErrNotFound, L"按 handle 打不开被标注实体:" + utf8ToWstr(sTarget)); const AcDbObjectId idTarget = pTarget->objectId(); pTarget->close(); AecDbEwPipeDim* pDim = new AecDbEwPipeDim(); std::wstring sLay = hasKey(args, "layer") ? wstrOr(args, "layer", L"") : L""; if (sLay.empty()) sLay = kDimLayer; AcDbObjectId idLay = KTSetting::InitLayer(sLay.c_str()); std::wstring sTxtStyle = hasKey(args, "text_style") ? wstrOr(args, "text_style", L"") : L""; if (sTxtStyle.empty()) sTxtStyle = kDimTextStyle; AcDbObjectId idTSty = CKTCadUtility::InitTextStyle(sTxtStyle.c_str()); pDim->AddAecDbWEntity(idTarget); pDim->SetDimStyle((short)(hasKey(args, "dim_style") ? intOr(args, "dim_style", 3) : StyleOfKind(wstrOr(args, "kind", L"seq")))); pDim->SetDimDir((short)intOr(args, "dim_dir", 0)); pDim->SetDimTextUporDownFlag((short)intOr(args, "text_up_down", 1)); if (!idLay.isNull()) pDim->setLayer(idLay); if (!idTSty.isNull()) { pDim->SetTextStyleId(idTSty); pDim->SetTextLayerId(idLay); } pDim->SetPScale(numOr(args, "p_scale", 100.0)); pDim->SetTextHeight(numOr(args, "text_height", 3.0)); pDim->SetTextWidthScale(numOr(args, "text_width_scale", 0.75)); if (hasKey(args, "color")) pDim->setColorIndex((Adesk::UInt16)intOr(args, "color", 256)); const std::wstring sUp = wstrOr(args, "text", L""); const std::wstring sDown = hasKey(args, "text_down") ? wstrOr(args, "text_down", L"") : std::wstring(); pDim->AddDimText(sUp.c_str(), sDown.empty() ? NULL : sDown.c_str()); bool bPointsOk = false; AppendPoints(pDim, args, bPointsOk); if (!bPointsOk) { delete pDim; return errJson(kErrBadParam, L"points 要给两个点:[[引出起点],[文字位置]]"); } AcDbObjectId idNew; Acad::ErrorStatus es = addToSpace(pDim, &idNew); if (es != Acad::eOk) { delete pDim; return errJson(kErrInternal, std::wstring(L"加入模型空间失败,err=") + std::to_wstring((int)es)); } touchEntity(pDim); nlohmann::json o; o["target"] = sTarget; o["kind"] = acharToUtf8(wstrOr(args, "kind", L"seq").c_str()); o["text"] = acharToUtf8(sUp.c_str()); o["text_down"] = acharToUtf8(sDown.c_str()); o["dim_style"] = (int)pDim->GetDimStyle(); o["dim_dir"] = (int)pDim->GetDimDir(); o["text_height"] = pDim->GetTextHeight(); o["text_width_scale"] = pDim->GetTextWidthScale(); nlohmann::json body; body["ok"] = true; body["type"] = "pipe_dim"; body["params"] = o; pDim->close(); body["handle"] = HandleOfId(idNew); return okJson(body); } }; EXPORT_MCP_TOOL(ToolAddPipeDim, AddAecPipeDimTool) // --------------------------------------------------------------------------- // update_aec_pipe_dim —— 改标注文字/样式/字高 // --------------------------------------------------------------------------- class ToolUpdatePipeDim : public mcp_Tool { public: std::string private_name() const override { return "update_aec_pipe_dim"; } std::string private_description() const override { return "改管线标注(AecDbEwPipeDim):上/下文字、样式、方向、上下标记、字高、字宽比、出图比例," "只传要改的字段。位置点(引出点/文字点)目前不能改,要挪就删了重建。"; } nlohmann::json private_input_schema() const override { return nlohmann::json::parse(R"({ "type": "object", "properties": { "handle": { "type": "string", "description": "标注实体 handle" }, "text": { "type": "string", "description": "新上排文字" }, "text_down": { "type": "string", "description": "新下排文字" }, "dim_style": { "type": "integer" }, "dim_dir": { "type": "integer" }, "text_up_down": { "type": "integer" }, "text_height": { "type": "number" }, "text_width_scale": { "type": "number" }, "p_scale": { "type": "number" }, "color": { "type": "integer" } }, "required": [ "handle" ] })"); } nlohmann::json Execute(const nlohmann::json& args) override { if (workingDb() == NULL) return errJson(kErrNoDocument, L"没有活动图纸"); DocLock lock; if (!lock.isLocked()) return errJson(kErrInternal, L"拿不到图纸写锁(有别的命令在跑?)"); if (!hasKey(args, "handle")) return errJson(kErrBadParam, L"缺少 handle"); const std::string sHandle = args["handle"].get(); AcDbEntity* pEnt = openByHandle(sHandle, AcDb::kForWrite); if (pEnt == NULL) return errJson(kErrNotFound, L"按 handle 打不开实体:" + utf8ToWstr(sHandle)); AecDbEwPipeDim* pDim = AecDbEwPipeDim::cast(pEnt); if (pDim == NULL) { const std::string sCls = classNameOf(pEnt); pEnt->close(); return errJson(kErrWrongType, L"实体 " + utf8ToWstr(sCls) + L" 不是管线标注(AecDbEwPipeDim)"); } if (hasKey(args, "dim_style")) pDim->SetDimStyle((short)intOr(args, "dim_style", 3)); if (hasKey(args, "dim_dir")) pDim->SetDimDir((short)intOr(args, "dim_dir", 0)); if (hasKey(args, "text_up_down")) pDim->SetDimTextUporDownFlag((short)intOr(args, "text_up_down", 1)); if (hasKey(args, "text_height")) pDim->SetTextHeight(numOr(args, "text_height", 3.0)); if (hasKey(args, "text_width_scale")) pDim->SetTextWidthScale(numOr(args, "text_width_scale", 0.75)); if (hasKey(args, "p_scale")) pDim->SetPScale(numOr(args, "p_scale", 100.0)); if (hasKey(args, "color")) pDim->setColorIndex((Adesk::UInt16)intOr(args, "color", 256)); if (hasKey(args, "text") || hasKey(args, "text_down")) { // 文字整组换掉:先清上/下,再写新的 CString sUpOld, sDownOld; pDim->GetDimText(0, sUpOld, sDownOld); CString sUp = hasKey(args, "text") ? CString(wstrOr(args, "text", L"").c_str()) : sUpOld; CString sDown = hasKey(args, "text_down") ? CString(wstrOr(args, "text_down", L"").c_str()) : sDownOld; pDim->RemoveAll(1); pDim->RemoveAll(-1); pDim->AddDimText((LPCTSTR)sUp, sDown.IsEmpty() ? NULL : (LPCTSTR)sDown); } pDim->UpdateFromParam(); pDim->ReCreateDraw3d(); touchEntity(pDim); CString sUp, sDown; pDim->GetDimText(0, sUp, sDown); nlohmann::json o; o["text"] = acharToUtf8((LPCTSTR)sUp); o["text_down"] = acharToUtf8((LPCTSTR)sDown); o["dim_style"] = (int)pDim->GetDimStyle(); o["dim_dir"] = (int)pDim->GetDimDir(); o["text_height"] = pDim->GetTextHeight(); o["text_width_scale"] = pDim->GetTextWidthScale(); nlohmann::json body; body["ok"] = true; body["type"] = "pipe_dim"; body["handle"] = sHandle; body["params"] = o; pEnt->close(); return okJson(body); } }; EXPORT_MCP_TOOL(ToolUpdatePipeDim, UpdateAecPipeDimTool)