Files
company-desktop b951f985f9 feat: 标注补上「在线/线上/线下」的自动定位(placement)与上下排间距
按 AecPlumbingDrawWDimonX 的规则把两点定位搬进工具:
  管轴向 vtDir(按象限规整,第2/3象限旋转或取反到第1/4象限)
  法向 vtV = 轴向绕 Z 转 90°
  第1点 = pick 在管轴上的投影 + vtV × DN/2
  第2点 = 第1点 + vtDir × lead_length(命令里是 500)
  SetDimStyle(1) + SetDimupdownSpace(DN)

新增参数:
- placement = on_line(PL_ZXBZ 在线,上下排都写) / above(PL_XSBZ 线上,只上排)
  / below(PL_XXBZ 线下,只下排) / custom(用调用方给的 points,原行为)
- pick       = 在管上点一个位置(自动投影到管轴)
- lead_length= 沿轴引出长度,缺省 500
- dim_updown_space = 上下排间距,缺省按放置方式(在线=管径)
返回值多给一个 points 数组(算出来的两点),方便核对。

顺带确认:引出标注/引出上标、垂直标注/垂直侧标**不用搬** —— 它们本身就是 acedGetPoint
让用户点第二点,差别只在 SetDimStyle(0)、SetDimDir(0/1)、SetDimupdownSpace(0),
现有 dim_style / dim_dir / dim_updown_space / text_up_down 参数已经能等价表达。

实测(R230 Debug,空图):DN100 管沿 +X、placement=on_line、pick=(3000,0,1000)
-> 返回两点 (3000,50,1000) 与 (3500,50,1000)(法向偏 DN/2=50、沿线 +500);
above 只剩上排 "100"、below 只剩下排 "114x4.0-<标高>1.000m"。

R230/Z2026 都编过。
2026-10-31 12:28:45 +08:00

889 lines
35 KiB
C++
Raw Permalink 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 "KTModelLayoutXData.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;
}
// ------------------------------------------------------------------------
// 「在线/线上/线下标注」的两点定位(照 AecPlumbingDrawWDimonX):
// 管轴向 vtDir(按象限规整,保证文字朝右上)→ 法向 vtV = 轴向绕 Z 转 90°
// 第1点 = 拾取点在管轴上的投影 + vtV * (DN/2)
// 第2点 = 第1点 + vtDir * leadLen(命令里是 500)
// DN 在库里按图纸单位存(产品的管是 mm;我们自己建的管按 AS_MUNIT 存米,
// 所以 <10 时按米换成图纸单位),与 ComposeSystemText 里的处理一致。
bool ComputeOnlinePoints(AcDbEntity* pTarget, const AcGePoint3d& ptPick, double dLeadLen,
AcGePoint3d& pt1, AcGePoint3d& pt2, double& dDnDraw, std::wstring& err)
{
AcDbCurve* pCrv = AcDbCurve::cast(pTarget);
if (pCrv == NULL) { err = L"被标注实体不是曲线(算不出管轴)"; return false; }
AcGePoint3d ptSta(0.0, 0.0, 0.0), ptEnd(0.0, 0.0, 0.0);
if (pCrv->getStartPoint(ptSta) != Acad::eOk || pCrv->getEndPoint(ptEnd) != Acad::eOk)
{
err = L"取不到被标注实体的起终点";
return false;
}
// 管径(图纸单位)
dDnDraw = 0.0;
{
AecDbObjXDataMap xObj;
AecDbCurveBase* pCurBase = AecDbCurveBase::cast(pTarget);
if (pCurBase != NULL)
xObj.CopyFrom(pCurBase->GetSpecialtyData(AcDb::kForRead));
else
XDataOfPlain(pTarget, xObj);
xObj.GetAt(PIPE_DN, dDnDraw);
if (dDnDraw > 0.0 && dDnDraw < 10.0) dDnDraw *= 1000.0; // 米 -> 图纸单位
}
// 拾取点投影到管轴:ptOn = ptSta + t*(ptEnd-ptSta)
const AcGeVector3d vtAxis = ptEnd - ptSta;
const double dLen2 = vtAxis.dotProduct(vtAxis);
if (dLen2 < 1e-18) { err = L"管轴长度为 0"; return false; }
double t = (ptPick - ptSta).dotProduct(vtAxis) / dLen2;
if (t < 0.0) t = 0.0;
if (t > 1.0) t = 1.0;
AcGePoint3d ptOn = ptSta + vtAxis * t;
// 轴向 + 法向(象限规整)
AcGeVector3d vtDir = ptEnd - ptSta;
if (vtDir.length() < 1e-9) { err = L"管轴长度为 0"; return false; }
vtDir.normalize();
if (vtDir.isParallelTo(AcGeVector3d::kYAxis)) vtDir = AcGeVector3d::kYAxis;
if (vtDir.isParallelTo(AcGeVector3d::kXAxis)) vtDir = AcGeVector3d::kXAxis;
AcGeVector3d vtV = vtDir;
if (vtDir.x < -1.0e-10 && vtDir.y > 1.0e-10) // 第 2 象限
{
vtV.rotateBy(-PI / 2.0, AcGeVector3d::kZAxis);
vtDir *= -1.0; // 转成第 4 象限
}
else if (vtDir.x < -1.0e-10 && vtDir.y < -1.0e-10) // 第 3 象限
{
vtDir.negate(); // 转成第 1 象限
vtV.rotateBy(-PI / 2.0, AcGeVector3d::kZAxis);
}
else
{
vtV.rotateBy(PI / 2.0, AcGeVector3d::kZAxis);
}
ptOn += vtV * (dDnDraw * 0.5);
pt1 = ptOn;
pt2 = ptOn + vtDir * dLeadLen;
return true;
}
// ------------------------------------------------------------------------
// 「系统标注」的字段脚本引擎(等价于 PipelineCmd 里 PipelineTools::FormatDimText)
//
// 标注配置存在模型 xdata 里(KTModelLayoutXData,AecRxBase 导出):
// ScriptT / ScriptB = 上/下排脚本,形如 "{0}{-}{1}{-}{2}{-}{3}"
// DNPRE / DPRE / ADPRE = 是否加 DN / D / 统一前缀
// ElType / ElDescr = 标高写法(0 符号 / 1 简写 / 2 自定义)
// InsTra = 保温伴热 0/1/2
// 字段表(与 FormatDimText 一致):
// {0} 公称直径 {1} 介质 {2} 管号 {3} 压力等级 {4} 外径x壁厚 {5} 标高
// {6} 类型(按图层查库,这里留空) {7} 保温伴热 {8} 弯头比率 {9} 标准(空)
// {10} 材质 {11} 区号 {12} 系统 {13} 单元号
// 区别:{6} 原版要查 SQLite 设备库(GetSystemDimkeyByLayer),本插件不碰库 → 留空。
bool ComposeSystemText(AcDbEntity* pTarget, double dEl, std::wstring& sUp, std::wstring& sDown,
std::wstring& err)
{
sUp.clear();
sDown.clear();
if (pTarget == NULL) { err = L"被标注实体为空"; return false; }
// 标注配置
AecDbObjXDataMap xCfg;
bool bUseDefaultCfg = false;
if (!KTModelLayoutXData::ReadModelXData(KT_PIPEINIT, xCfg))
{
// 图上还没做过"标注设置":用 KTModelLayoutXData::ReSet 的出厂默认脚本
// (默认上标是 "{0}{-}{1}{-}{2}{-}{3}" = 公称直径-介质-管号-压力等级)
AecDbObjXDataMap xDef;
KTModelLayoutXData::ReSet(xDef);
xCfg.CopyFrom(&xDef);
bUseDefaultCfg = true;
}
CString cUp, cDown;
xCfg.GetAt(DOC_SCRIPTT, cUp);
xCfg.GetAt(DOC_SCRIPTB, cDown);
if (cUp.IsEmpty() && cDown.IsEmpty())
{
err = L"标注设置里上/下排脚本都是空的";
return false;
}
// 实体专业数据:仓库实体读专业数据,普通实体读 BIMDATA 扩展数据
AecDbObjXDataMap xObj;
// 管线/管件是 AecDbCurveBase 派生(不是 AecDbEntityBase);其它仓库实体再试 EntityBase
AecDbCurveBase* pCurBase = AecDbCurveBase::cast(pTarget);
AecDbEntityBase* pEntBase = (pCurBase == NULL) ? AecDbEntityBase::cast(pTarget) : NULL;
if (pCurBase != NULL)
xObj.CopyFrom(pCurBase->GetSpecialtyData(AcDb::kForRead));
else if (pEntBase != NULL)
xObj.CopyFrom(pEntBase->GetSpecialtyData(AcDb::kForRead));
else if (!XDataOfPlain(pTarget, xObj))
{
err = L"取不到被标注实体的专业数据(既不是仓库实体,也没有 BIMDATA 扩展数据)";
return false;
}
BOOL bDNP = FALSE, bDP = FALSE, bADP = FALSE;
xCfg.GetAt(DOC_DNPRE, bDNP);
xCfg.GetAt(DOC_DPRE, bDP);
xCfg.GetAt(DOC_ADPRE, bADP);
CString sTemp;
// {0} 公称直径
double dDn = 0.0;
xObj.GetAt(PIPE_DN, dDn);
if (dDn > 0.0 && dDn < 10.0) dDn *= 1000.0; // 库里存米,拼文字用毫米
if (bADP) sTemp.Format(_T("%%%%c%d"), (int)dDn);
else if (bDNP) sTemp.Format(_T("DN%d"), (int)dDn);
else sTemp.Format(_T("%d"), (int)dDn);
cUp.Replace(_T("{0}"), sTemp); cDown.Replace(_T("{0}"), sTemp);
// {1} 介质 {2} 管号 {3} 压力等级
CString sVal;
xObj.GetAt(PIPE_MEDIA, sVal); cUp.Replace(_T("{1}"), sVal); cDown.Replace(_T("{1}"), sVal);
xObj.GetAt(PIPE_NO, sVal); cUp.Replace(_T("{2}"), sVal); cDown.Replace(_T("{2}"), sVal);
xObj.GetAt(PIPE_LEVEL, sVal); cUp.Replace(_T("{3}"), sVal); cDown.Replace(_T("{3}"), sVal);
// {4} 外径 x 壁厚
double dOd = dDn, dTh = 0.0;
xObj.GetAt(PIPE_OD, dOd);
xObj.GetAt(PIPE_THICK, dTh);
if (dOd > 0.0 && dOd < 10.0) dOd *= 1000.0; // 同上:米 -> 毫米
if (dTh > 0.0 && dTh < 10.0) dTh *= 1000.0;
if (bADP) sTemp.Format(_T("%%%%c%dx%.1f"), (int)dOd, dTh);
else if (bDP) sTemp.Format(_T("D%dx%.1f"), (int)dOd, dTh);
else sTemp.Format(_T("%dx%.1f"), (int)dOd, dTh);
cUp.Replace(_T("{4}"), sTemp); cDown.Replace(_T("{4}"), sTemp);
// {5} 标高(写法照 FormatDimText:ElType 0 符号 / 1 简写 / 2 自定义描述)
{
const bool bMeter = CoreTools::IsMeterUnitForPipeElevDim();
const int nPrec = CoreTools::getPrecForPipeElevDim();
CString sEl;
sEl.Format(bMeter ? _T("%.*fm") : _T("%.*fmm"), nPrec, bMeter ? dEl * 0.001 : dEl);
int nElType = 0;
xCfg.GetAt(DOC_ELTYPE, nElType);
CString sElDescr;
xCfg.GetAt(DOC_ELDESCR, sElDescr);
if (nElType == 0) sTemp.Format(_T("%s%s"), DIM_CEN_EL, (LPCTSTR)sEl);
else if (nElType == 1) sTemp.Format(_T("%s%s"), PIPE_CEN_EL, (LPCTSTR)sEl);
else
{
// 自定义:描述串用 | 分段,第 3 段是标高前缀(与 FormatDimText 一致)
CString sFirst = sElDescr;
int nBar = sElDescr.Find(KT_SEPARATOR_CHAR);
for (int i = 0; i < 2 && nBar >= 0; ++i)
{
sFirst = sElDescr.Mid(nBar + 1);
nBar = sFirst.Find(KT_SEPARATOR_CHAR);
}
if (nBar >= 0) sFirst = sFirst.Left(nBar);
sTemp.Format(_T("%s%s"), (LPCTSTR)sFirst, (LPCTSTR)sEl);
}
cUp.Replace(_T("{5}"), sTemp); cDown.Replace(_T("{5}"), sTemp);
}
// {6} 类型:原版按图层查设备库,这里留空
cUp.Replace(_T("{6}"), _T("")); cDown.Replace(_T("{6}"), _T(""));
// {7} 保温伴热
int nIns = 0;
xObj.GetAt(DOC_INSTRATAG, nIns);
sTemp = (nIns == 1) ? CString(_T("保温")) : ((nIns == 2) ? CString(_T("保温加伴热")) : CString(_T("无保温伴热")));
cUp.Replace(_T("{7}"), sTemp); cDown.Replace(_T("{7}"), sTemp);
// {8} 弯头比率
double dRatio = 1.0;
xObj.GetAt(PIPE_RATIO, dRatio);
sTemp.Format(_T("%.1f"), dRatio);
cUp.Replace(_T("{8}"), sTemp); cDown.Replace(_T("{8}"), sTemp);
// {9} 标准:原版也是空
cUp.Replace(_T("{9}"), _T("")); cDown.Replace(_T("{9}"), _T(""));
// {10} 材质(管件用 MaterialFT)
xObj.GetAt(PIPE_MATERIAL, sTemp);
{
int nRuntime = 0;
xObj.GetAt(KEY_RUNTIME, nRuntime);
if (nRuntime != ePipe) xObj.GetAt(PIPE_MATERIALFT, sTemp);
}
cUp.Replace(_T("{10}"), sTemp); cDown.Replace(_T("{10}"), sTemp);
// {11} 区号 {12} 系统(去掉"管"再加"管") {13} 单元号
xObj.GetAt(PIPE_ZONE, sTemp);
cUp.Replace(_T("{11}"), sTemp); cDown.Replace(_T("{11}"), sTemp);
xObj.GetAt(PIPE_SYS, sTemp);
sTemp.Replace(_T("管"), _T(""));
sTemp += _T("管");
cUp.Replace(_T("{12}"), sTemp); cDown.Replace(_T("{12}"), sTemp);
sTemp = _T("");
xObj.GetAt(PIPE_UNITNO, sTemp);
cUp.Replace(_T("{13}"), sTemp); cDown.Replace(_T("{13}"), sTemp);
// 收尾:剪掉两端多余的 _ 和 -,去掉没替换掉的 {} 占位
LPCTSTR aszTrim[] = { _T("_"), _T("-") };
for (int pass = 0; pass < 3; ++pass)
{
for (int i = 0; i < 2; ++i)
{
cUp.TrimRight(aszTrim[i]); cUp.TrimLeft(aszTrim[i]);
cDown.TrimRight(aszTrim[i]); cDown.TrimLeft(aszTrim[i]);
}
cUp.Replace(_T("{"), _T("")); cUp.Replace(_T("}"), _T(""));
cDown.Replace(_T("{"), _T("")); cDown.Replace(_T("}"), _T(""));
}
sUp = std::wstring((LPCTSTR)cUp);
sDown = std::wstring((LPCTSTR)cDown);
if (sUp.empty() && sDown.empty())
{
err = L"按标注设置拼出来的文字是空的(检查标注设置里的字段脚本)";
return false;
}
return 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", "system" ],
"description": "标注种类(决定 dim style 与语义),缺省 seq;system=按模型里的\"标注设置\"字段脚本拼(系统标注那 17 个命令用的就是这个)" },
"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" },
"elevation": { "type": "number", "description": "kind=system 时给管线标高(填 {5} 字段),缺省 0" },
"placement": { "type": "string", "enum": [ "on_line", "above", "below", "custom" ],
"description": "放置方式:on_line 在线(上下排都写)/ above 线上(只写上排)/ below 线下(只写下排);给了就用 pick 自动算两点,不用再给 points" },
"pick": { "type": "array", "items": { "type": "number" }, "description": "placement 用:在管上点一个位置(会自动投影到管轴)" },
"lead_length": { "type": "number", "description": "placement 用:沿管轴的引出长度,缺省 500(命令里就是这个值)" },
"dim_updown_space": { "type": "number", "description": "上下排文字间距,缺省按放置方式给(在线=管径,引出/垂直=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" ]
})");
}
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, errMsg;
if (hasKey(args, "text"))
{
sUp = wstrOr(args, "text", L"");
if (hasKey(args, "text_down")) sDown = wstrOr(args, "text_down", L"");
}
else
{
const std::wstring sKind = wstrOr(args, "kind", L"seq");
const bool bUnitM = (wstrOr(args, "unit", L"m") != L"mm");
AcDbEntity* pT = openByHandle(sTarget, AcDb::kForRead);
bool bComposed = false;
if (sKind == L"system")
{
// 「系统标注」:按模型里的标注设置(字段脚本)拼
std::wstring sErr;
bComposed = ComposeSystemText(pT, numOr(args, "elevation", 0.0), sUp, sDown, sErr);
if (!bComposed && sErr.size() > 0) errMsg = sErr;
}
else
{
bComposed = ComposeDimText(pT, sKind, bUnitM,
numOr(args, "elevation_base", 0.0), sUp, sDown);
}
if (pT != NULL) pT->close();
if (!bComposed)
{
delete pDim;
return errJson(kErrBadParam, errMsg.empty()
? std::wstring(L"这个 kind 拼不出文字(序号类要实体上有编号,其它类要实体数据齐全),请传 text / text_down")
: errMsg);
}
}
pDim->AddDimText(sUp.c_str(), sDown.empty() ? NULL : sDown.c_str());
AcGePoint3d ptPlaced1(0.0, 0.0, 0.0), ptPlaced2(0.0, 0.0, 0.0);
bool bHasPlaced = false;
// 放置方式:在线/线上/线下(照 AecPlumbingDrawWDimonX 自动算两点)
const std::wstring sPlace = hasKey(args, "placement") ? wstrOr(args, "placement", L"") : std::wstring();
double dUpdownSpace = numOr(args, "dim_updown_space", -1.0);
if (!sPlace.empty() && sPlace != L"custom")
{
AcGePoint3d ptPick(0.0, 0.0, 0.0);
if (!readPoint(args, "pick", ptPick))
{
delete pDim;
return errJson(kErrBadParam, L"placement 需要 pick(在管上点一个位置)");
}
AcDbEntity* pT = openByHandle(sTarget, AcDb::kForRead);
AcGePoint3d pt1(0.0, 0.0, 0.0), pt2(0.0, 0.0, 0.0);
double dDnDraw = 0.0;
std::wstring sErr;
const bool bOk = ComputeOnlinePoints(pT, ptPick, numOr(args, "lead_length", 500.0),
pt1, pt2, dDnDraw, sErr);
if (pT != NULL) pT->close();
if (!bOk)
{
delete pDim;
return errJson(kErrBadParam, sErr);
}
pDim->SetDimStyle((short)(hasKey(args, "dim_style") ? intOr(args, "dim_style", 1) : 1));
pDim->AppendPoint(pt1);
pDim->AppendPoint(pt2);
ptPlaced1 = pt1; // 回给调用方核对位置
ptPlaced2 = pt2;
bHasPlaced = true;
if (dUpdownSpace < 0.0) dUpdownSpace = dDnDraw; // 命令:SetDimupdownSpace(dDn)
// above=线上(只写上排)/ below=线下(只写下排)/ on_line=在线(两排都写)
if (!hasKey(args, "text") && !hasKey(args, "text_down"))
{
if (sPlace == L"above" && !sDown.empty()) sDown.clear();
if (sPlace == L"below" && !sUp.empty()) sUp.clear();
// 文字是上面拼好的,这里要重新写一遍
CString sUpAll, sDownAll;
pDim->RemoveAll(1);
pDim->RemoveAll(-1);
pDim->AddDimText(CString(sUp.c_str()), sDown.empty() ? NULL : (LPCTSTR)CString(sDown.c_str()));
}
}
else
{
bool bPointsOk = false;
AppendPoints(pDim, args, bPointsOk);
if (!bPointsOk)
{
delete pDim;
return errJson(kErrBadParam, L"points 要给两个点:[[引出起点],[文字位置]](或用 placement + pick 自动算)");
}
}
if (dUpdownSpace >= 0.0) pDim->SetDimupdownSpace(dUpdownSpace);
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;
if (bHasPlaced)
{
body["points"] = nlohmann::json::array();
body["points"].push_back({ ptPlaced1.x, ptPlaced1.y, ptPlaced1.z });
body["points"].push_back({ ptPlaced2.x, ptPlaced2.y, ptPlaced2.z });
}
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)