Files
envi-code/SourceCode/Code2026/cad_mcp_aec/aec_mcp_dim_tools.cpp
T
company-desktop adab35bee4 feat: 标注支持被标注对象是普通 CAD 实体(LINE/POINT/CIRCLE + BIMDATA 扩展数据)
核查 8 个标注命令:选择过滤器本来就允许普通实体
(PL_DKBZ 是 AEC_X3DPART,LINE,POINT,CIRCLE;PL_TGBZ 去掉 CIRCLE;PL_YHBZ 只要 LINE;
PL_XHBZ 用 acedEntSel 不过滤),对非仓库实体走的是
  AecDbObjXDataMap xdata; xdata.Read(pEnt, BIMDATA_APP);
再读 KEY_NAME / _T("nShape") / dRad·dL·dW / KEY_ORGPT / vtDir / KEY_CENEL(中心标高)
· KEY_BOTMEL(洞底标高);对 AcDbPoint 还把点位置当拾取点。

工具这边:ComposeDimText 分两条路 ——
1) AecDbX3dPartEntity::cast 成功 -> GetInterface(0)(原逻辑)
2) 失败 -> 读 BIMDATA 扩展数据,按上面那些键还原 AecInterface
   (套管的类型 KEY_PIPETYPE、壁厚 PIPE_THICK 或 dT 也从 xdata 兜底)
翼环(PIPE_TOD/PIPE_YMS)、阀门(KEY_NAME)、序号(KEY_BOMNO) 同样在专业数据读不到时
回退到 BIMDATA 扩展数据,与 PL_XHBZ 的做法一致。
target 只按 handle 打开、不限类型,所以任何实体都能当被标注对象。

实测(R230 Debug,被标注对象用普通 LINE):
- 无 xdata 又没传文字 -> 明确报错要求传 text
- 传了 text/text_down -> 正常标注
- kind=seq 且实体没有 KEY_BOMNO -> 明确报错
("普通实体 + BIMDATA 扩展数据"这条路代码已对齐,MCP 侧造不出该 xdata,未端到端实测。)

R230/Z2026 都编过。
2026-10-31 11:33:35 +08:00

555 lines
21 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// ============================================================================
// 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 "AecDbX3dPartEntity.h"
#include "AecDbBDPipeBase.h"
#include "AecDbCurveBase.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;
}
// 普通 CAD 实体(LINE/POINT/CIRCLE…)上的 BIMDATA 扩展数据。
// 命令里对这类实体就是这么取数据的:AecDbObjXDataMap xdata; xdata.Read(pEnt, BIMDATA_APP);
// 再 GetAt(KEY_NAME / _T("nShape") / _T("dRad") / _T("dL") / _T("dW") / KEY_ORGPT /
// _T("vtDir") / KEY_CENEL / KEY_BOTMEL …)
bool XDataOfPlain(AcDbEntity* pEnt, AecDbObjXDataMap& xdata)
{
if (pEnt == NULL) return false;
return xdata.Read(pEnt, BIMDATA_APP) == TRUE;
}
// ------------------------------------------------------------------------
// 标注文字的"自动拼":按 kind 从被标注实体的专业数据里算
// 对照命令里的格式(PL_DKBZ / PL_TGBZ / PL_YHBZ / PL_FMBZ / PL_XHBZ):
// 洞口: "预留D%d洞口"(圆, +0.2) / "预留%dx%d洞口"(矩形, +0.5)
// 套管: "预埋<类型>" + "D%dx%.1f 中心标高 %.3f" 或 "%.1fx%.1f t=%.1f 中心标高 %.3f"
// 翼环: "翼环D%s 数量%d个"(PIPE_TOD / PIPE_YMS)
// 阀门: 实体专业数据里的 KEY_NAME
// 单位:command 里 m_bUnitM=是就用米(/=1000),这里用 unit 参数控;dElvBase 是标高基准。
// 返回 false 表示这条 kind 拼不出来(比如序号类要人给),由调用方回退到 text 参数。
bool ComposeDimText(AcDbEntity* pTarget, const std::wstring& sKind, bool bUnitM, double dElvBase,
std::wstring& sUp, std::wstring& sDown)
{
sUp.clear();
sDown.clear();
if (pTarget == NULL) return false;
const int nUnit = bUnitM ? 1000 : 1;
// 洞口 / 套管:优先用 AecDbX3dPartEntity 的接口数据;
// 不是仓库实体(比如炸开后的 LINE/POINT/CIRCLE)就读它的 BIMDATA 扩展数据
if (sKind == L"opening" || sKind == L"casing")
{
AecDbX3dPartEntity* pPart = AecDbX3dPartEntity::cast(pTarget);
AecInterface inf;
::memset(&inf, 0, sizeof(AecInterface));
std::wstring sTypeName; // 套管类型(xdata 里没有就留空)
double dThickX = 0.0; // xdata 里的壁厚
double dElX = 0.0; // xdata 里的绝对标高(中心/洞底)
std::wstring sElLabel = L"中心标高";
if (pPart != NULL)
{
if (!pPart->GetInterface(0, inf)) return false;
}
else
{
// 普通实体:从 BIMDATA 扩展数据还原出一份接口数据
AecDbObjXDataMap xdata;
if (!XDataOfPlain(pTarget, xdata)) return false;
CString sName;
xdata.GetAt(KEY_NAME, sName);
if (!(sName.CompareNoCase(_T("开洞")) == 0 || sName.CompareNoCase(_T("套管")) == 0))
return false;
int nShape = 0;
xdata.GetAt(_T("nShape"), nShape);
if (nShape == (int)eCircle)
{
double dRad = 0.0;
xdata.GetAt(_T("dRad"), dRad);
inf.nSectType = (short)eCircle;
inf.dSize1 = 2.0 * dRad;
inf.dSize2 = 2.0 * dRad;
}
else if (nShape == (int)eRect)
{
double dL = 0.0, dW = 0.0;
xdata.GetAt(_T("dL"), dL);
xdata.GetAt(_T("dW"), dW);
inf.nSectType = (short)eRect;
inf.dSize1 = dL;
inf.dSize2 = dW;
}
else
{
return false; // 异形洞不拼
}
AcGePoint3d ptCen(0.0, 0.0, 0.0);
xdata.GetAt(KEY_ORGPT, ptCen);
AcGeVector3d vtZ(0.0, 0.0, 1.0);
xdata.GetAt(_T("vtDir"), vtZ);
inf.ptPos = ptCen;
inf.vtDir = vtZ;
// 标高:先看中心标高,再看洞底标高
if (xdata.GetAt(KEY_CENEL, dElX)) sElLabel = L"中心标高";
else if (xdata.GetAt(KEY_BOTMEL, dElX)) { sElLabel = L"洞底标高"; }
else
{
dElX = ptCen.z;
sElLabel = L"中心标高";
}
// 套管的类型/壁厚也可能在 xdata 里
CString sTmp;
if (xdata.GetAt(KEY_PIPETYPE, sTmp)) sTypeName = std::wstring((LPCTSTR)sTmp);
if (!xdata.GetAt(PIPE_THICK, dThickX) || dThickX <= 0.0)
xdata.GetAt(_T("dT"), dThickX);
}
const bool bCircle = (inf.nSectType == (short)eCircle);
// 普通实体:xdata 里的标高是绝对值(命令里也是直接 /nUnit),
// 仓库实体:用接口点的 z 加基准
const double dEl = (pPart != NULL)
? (inf.ptPos.z / nUnit + dElvBase / nUnit)
: (dElX / nUnit + dElvBase / nUnit);
CString cUp, cDwn;
if (sKind == L"opening")
{
if (bCircle)
cUp.Format(_T("预留D%d洞口"), (int)(inf.dSize1 + 0.2));
else
cUp.Format(_T("预留%dx%d洞口"), (int)(inf.dSize1 + 0.5), (int)(inf.dSize2 + 0.5));
}
else
{
// 套管:类型名取专业数据里的 PIPE_TYPE,没有就用"套管"
CString sType;
if (pPart != NULL)
{
if (!pPart->GetSpecialtyData(AcDb::kForRead)->GetAt(KEY_PIPETYPE, sType) || sType.IsEmpty())
sType = _T("套管");
}
else
{
sType = sTypeName.empty() ? CString(_T("套管")) : CString(sTypeName.c_str());
}
cUp.Format(_T("预埋%s"), (LPCTSTR)sType);
double dT = dThickX;
if (pPart != NULL && dT <= 0.0)
{
if (!pPart->GetSpecialtyData(AcDb::kForRead)->GetAt(PIPE_THICK, dT) || dT <= 0.0)
pPart->GetSpecialtyData(AcDb::kForRead)->GetAt(_T("dT"), dT); // 套管的壁厚存在 dT
}
if (bCircle)
// 厚度我们按图纸单位存(不是 mm),所以不再除 nUnit
cDwn.Format(_T("D%dx%.1f 中心标高 %.3f"), (int)(inf.dSize1 + 0.2), dT, dEl);
else
cDwn.Format(_T("%.1fx%.1f t=%.1f 中心标高 %.3f"), inf.dSize1, inf.dSize2, dT, dEl);
}
if (sKind == L"opening")
cDwn.Format(_T("%s %.3f"), (LPCTSTR)sElLabel.c_str(), dEl);
sUp = std::wstring((LPCTSTR)cUp);
sDown = std::wstring((LPCTSTR)cDwn);
return !sUp.empty();
}
// 翼环:PIPE_TOD 外径(字符串)+ PIPE_YMS 数量(int)
if (sKind == L"ring")
{
AecDbBDPipeBase* pBase = AecDbBDPipeBase::cast(pTarget);
AecDbEntityBase* pEntBase = AecDbEntityBase::cast(pTarget);
if (pBase == NULL && pEntBase == NULL) return false;
AecDbObjXDataMap* pMap = (pBase != NULL) ? pBase->GetSpecialtyData(AcDb::kForRead)
: pEntBase->GetSpecialtyData(AcDb::kForRead);
if (pMap == NULL)
{
// 普通实体:读 BIMDATA 扩展数据
static AecDbObjXDataMap sX;
if (!XDataOfPlain(pTarget, sX)) return false;
pMap = &sX;
}
CString sD2;
int nD2 = 0;
if (!pMap->GetAt(PIPE_TOD, sD2)) return false;
pMap->GetAt(PIPE_YMS, nD2);
CString cUp;
cUp.Format(_T("翼环D%s 数量%d个"), (LPCTSTR)sD2, nD2);
sUp = std::wstring((LPCTSTR)cUp);
return !sUp.empty();
}
// 阀门 / 标注管件:库里存的 KEY_NAME
if (sKind == L"valve" || sKind == L"iso_valve" || sKind == L"iso_fitting")
{
AecDbCurveBase* pCur = AecDbCurveBase::cast(pTarget);
AecDbEntityBase* pEntBase = AecDbEntityBase::cast(pTarget);
AecDbObjXDataMap* pMap = NULL;
if (pCur != NULL) pMap = pCur->GetSpecialtyData(AcDb::kForRead);
else if (pEntBase != NULL) pMap = pEntBase->GetSpecialtyData(AcDb::kForRead);
if (pMap == NULL)
{
static AecDbObjXDataMap sX2;
if (!XDataOfPlain(pTarget, sX2)) return false;
pMap = &sX2;
}
CString sName;
if (!pMap->GetAt(KEY_NAME, sName) || sName.IsEmpty()) return false;
sUp = std::wstring((LPCTSTR)sName);
return true;
}
// 序号类:实体上可能存着 KEY_BOMNO,有就用它当默认;没有就只能让调用方给 text
if (sKind == L"seq" || sKind == L"seq_batch")
{
AecDbCurveBase* pCur = AecDbCurveBase::cast(pTarget);
AecDbEntityBase* pEntBase = AecDbEntityBase::cast(pTarget);
AecDbObjXDataMap* pMap = NULL;
if (pCur != NULL) pMap = pCur->GetSpecialtyData(AcDb::kForRead);
else if (pEntBase != NULL) pMap = pEntBase->GetSpecialtyData(AcDb::kForRead);
if (pMap == NULL)
{
// 命令里对普通实体也是先读专业数据、读不到再读 BIMDATA 扩展数据
static AecDbObjXDataMap sX3;
if (!XDataOfPlain(pTarget, sX3)) return false;
pMap = &sX3;
}
CString sBomNo;
if (!pMap->GetAt(KEY_BOMNO, sBomNo) || sBomNo.IsEmpty()) return false;
sUp = std::wstring((LPCTSTR)sBomNo);
return true;
}
return false;
}
// 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": "上排文字;不给就按 kind 从被标注实体自动拼(与命令一致)" },
"text_down": { "type": "string", "description": "下排文字;不给就自动拼" },
"unit": { "type": "string", "enum": [ "m", "mm" ], "description": "尺寸/标高的单位,缺省 m(命令里 m_bUnitM)" },
"elevation_base": { "type": "number", "description": "标高基准(命令里的 dElv),缺省 0" },
"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" ]
})");
}
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<std::string>();
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));
// 文字:传了就用传的;没传就按 kind 从被标注实体自动拼(与命令一致)
std::wstring sUp, sDown;
if (hasKey(args, "text"))
{
sUp = wstrOr(args, "text", L"");
if (hasKey(args, "text_down")) sDown = wstrOr(args, "text_down", L"");
}
else
{
const bool bUnitM = (wstrOr(args, "unit", L"m") != L"mm");
AcDbEntity* pT = openByHandle(sTarget, AcDb::kForRead);
const bool bComposed = ComposeDimText(pT, wstrOr(args, "kind", L"seq"), bUnitM,
numOr(args, "elevation_base", 0.0), sUp, sDown);
if (pT != NULL) pT->close();
if (!bComposed)
{
delete pDim;
return errJson(kErrBadParam,
L"这个 kind 拼不出文字(序号类要实体上有编号,其它类要实体数据齐全),请传 text / text_down");
}
}
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<std::string>();
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)