Files
company-desktop de72320ee6 feat: 管线专业数据补齐(对比 test_pipe.dwg 后从 12 个字段补到 80 个)
用户给了 E:\桌面\test_pipe.dwg:一条 MCP 建的长管 291、一条 PL_DHGD 手画的短管 31C。
新增调试工具 dump_aec_xdata(handle),吐 AecDbObjXDataMap::List() 的全部 key/value。
注意管线派生自 AecDbCurveBase 而不是 AecDbEntityBase,取专业数据要两边都 cast。

对比:MCP 建的 12 个字段,手画的 70 个。缺的 59 个分五类:
1) 管线自身参数(PIPE_D/PIPE_TDN/TOD/Ratio/KEY_RadRatio/MATERIALFT/KEY_NO/InsThick/
   PIDD/DrawDir/EApp/Single/BWeldDot/FLGF/Gasket/IFD/IFD2/IFL/IFL2)
   -> AecDbEwPipe::SetPipeParam(PipeParam*),命令 PL_wpipedlg 走 TDbPipeJig 也是它。
2) 图纸级参数(PipeMaxDia/PipeMinDia/PerElbowDia/Trade/Project/Unit/Ratio...)
   -> FillInInitXDataMap:把 KT_PIPEINIT 整份拷到实体。
3) 图层/线型/颜色(CentLay/SingLay/OutDlay/COLOR/PIPE_LINELAYER*/PIPE_LINETYPE*)
   -> AecDbEwPipe::SetLayerByXDataMap(),按 PIPE_SYS 查系统图层。
4) 常量(RRADFLG=1/PIPCMD/Z2DRAW/IFL3/UnitNo)-> TDbPipeLine::FillConfigXDataMap 里写死。
5) 管子规格(ODNo/STDNO/SHTNO、PIPE_OD=57、PIPE_Thick=3.5)-> 查 sys\Plumbing.db 的管子规格表。
   另有出图线宽字段和水暖关联 &FOR&WEIR&(不需要)。

本次补了 1~4,字段数 12 -> 80(手画 70)。踩的坑:
- GetPipeParam() 返回 new 出来的指针,用完要 delete;
- PipeParam 的 szSys/szMertial/szLevel 是 TCHAR[255],只能用 _tcscpy_s 赋值。

仍缺(下一步):管子规格查库(OD/壁厚/STDNO/SHTNO/ODNo)和几个出图线宽字段。
2026-11-01 20:43:22 +08:00

2731 lines
98 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_entity_tools.cpp
// cad_mcp_aec 的"建/改"工具:管道、墙、板、柱、梁
//
// 参数约定:
// * 坐标与建筑尺寸(墙厚/墙高/板厚/柱高/梁宽…)用**图纸单位**;
// * 管道的公称直径 / 外径 / 壁厚用 **mm**,内部按 AS_MUNIT 转米
// (与 BZGDDlg、AecPipeLineTool 的既有写法一致)。
//
// 属性 key 用的是 EarThwormDef.h 里的 PIPE_DN / PIPE_OD / PIPE_THICK 等
// (AecDbCurveBase.h 里包含的就是它;注意 inc/AecWPipe.h 里有同名但值不同的宏,
// 本文件不要包含 AecWPipe.h,否则写进去的 key 会变)。
// ============================================================================
#include "StdAfx.h"
#include "KTCad.h"
#include "CoreTools.h"
#include "KTModelLayoutXData.h"
#include "aec_mcp_common.h"
#include "mcp_plugin_api.h"
#include "AecDbEwPipe.h"
#include "AecDbWall.h"
#include "AecDbSlab.h"
#include "AecDbColumn.h"
#include "AecDbBeam.h"
#include "AecDbClimbStair.h"
#include "AecDbEwFitting.h"
#include "AecDbX3dPartEntity.h"
#include "AecDbSlab.h"
#include "AecDbObjXDataMap.h"
#include "KTSetting.h"
using namespace aec_mcp;
namespace
{
// id -> 句柄字符串(公共层只有"对象->句柄",这里给刚入库的对象用)
std::string handleOfId(const AcDbObjectId& id)
{
if (id.isNull()) return std::string();
AcDbObject* pObj = NULL;
if (acdbOpenObject(pObj, id, AcDb::kForRead) != Acad::eOk || pObj == NULL)
return std::string();
AcDbHandle h;
pObj->getAcDbHandle(h);
pObj->close();
ACHAR szBuf[64] = { 0 };
h.getIntoAsciiBuffer(szBuf, 64);
return acharToUtf8(szBuf);
}
}
namespace
{
// ---------------------------------------------------------------------------
// 参数应用(建/改共用)
// ---------------------------------------------------------------------------
// 管道:至少要有起终点与公称直径。其余参数缺省时保留原值(改的时候)。
// 照 PipelineTools::isDoubleLine:DOC_PIPEMAXDIA 存的是当前图纸单位(米),
// DN 也换成米再比。读不到参数就按产品默认(AecDbEwPipe 内部缺省 2=双线)当双线。
static bool isDoubleLinePipe(double dDnMm)
{
AecDbObjXDataMap x;
if (!KTModelLayoutXData::ReadModelXData(KT_PIPEINIT, x)) return true;
double dMax = 0.0;
if (!x.GetAt(DOC_PIPEMAXDIA, dMax) || dMax <= 0.0) return true;
return aec_mcp::mmToDwg(dDnMm) >= dMax; // 两边都是图纸单位
}
bool applyPipe(AecDbEwPipe* p, const nlohmann::json& a, bool bCreate, std::wstring& err)
{
AcGePoint3d ptS, ptE;
bool bHasS = readPoint(a, "start", ptS);
bool bHasE = readPoint(a, "end", ptE);
if (bCreate && (!bHasS || !bHasE))
{
err = L"add_aec_pipe 需要 start / end({\"x\",\"y\",\"z\"} 或 [x,y,z])";
return false;
}
// 改的时候只给一个端点:另一个沿用实体现有端点,别把管拉回原点
if (!bCreate && (bHasS != bHasE))
{
const AcDbCurve* pCrv = AcDbCurve::cast(p);
AcGePoint3d ptCurS, ptCurE;
if (pCrv != NULL && pCrv->getStartPoint(ptCurS) == Acad::eOk
&& pCrv->getEndPoint(ptCurE) == Acad::eOk)
{
if (!bHasS) { ptS = ptCurS; bHasS = true; }
if (!bHasE) { ptE = ptCurE; bHasE = true; }
}
}
if (bHasS && bHasE)
{
if (ptS.distanceTo(ptE) < 1e-9)
{
err = L"start 与 end 重合,管道长度为 0";
return false;
}
p->SetBaseCurve(ptS, ptE);
}
AecDbObjXDataMap* pX = p->GetSpecialtyData();
if (pX == NULL)
{
err = L"取专业数据失败(GetSpecialtyData 返回空)";
return false;
}
// 和 PL_DHGD / PL_wpipedrawEx 一样:系统没初始化(没跑过 pl_cssz)就没法建正确的管道,
// 硬建出来的管 DN/等级/系统都是空的(特性面板显示 DN=0)。
if (bCreate && !aec_mcp::ensurePipeInit(err)) // 没有 KT_PIPEINIT 就写一份 pl_cssz 缺省值
return false;
// 现有的截面参数(内部存的是米),下面按"传了就用新的、没传就保持"合并后统一重写:
// SetSection() 会把 PIPE_THICK/PIPE_OD 之类的派生字段清掉,所以必须先取再补。
double dDnOld = 0.0, dOdOld = 0.0, dThOld = 0.0;
pX->GetAt(PIPE_DN, dDnOld);
pX->GetAt(PIPE_OD, dOdOld);
pX->GetAt(PIPE_THICK, dThOld);
const double dDn = hasKey(a, "dn_mm") ? numOr(a, "dn_mm", 0.0) : aec_mcp::dwgToMm(dDnOld);
const double dOd = hasKey(a, "od_mm") ? numOr(a, "od_mm", 0.0) : aec_mcp::dwgToMm(dOdOld);
const double dTh = hasKey(a, "wall_thickness_mm") ? numOr(a, "wall_thickness_mm", 0.0) : aec_mcp::dwgToMm(dThOld);
if (bCreate || hasKey(a, "dn_mm"))
{
if (dDn <= 0.0)
{
err = L"add_aec_pipe 需要 dn_mm(公称直径,mm)";
return false;
}
// 与 BZGDDlg 一致:截面圆用 DN(米制)
p->SetSection(aec_mcp::mmToDwg(dDn), 0.0);
}
// 单线/双线:命令里是 KEY_2DSTYLE = PipelineTools::isDoubleLine(m_dDN) ? 2 : 1,
// 即 DN(米) >= 图纸参数里 DOC_PIPEMAXDIA 就双线。之前这里硬写 1,所以画出来永远一根线。
pX->SetAt(KEY_2DSTYLE, isDoubleLinePipe(dDn > 0.0 ? dDn : aec_mcp::dwgToMm(dDnOld)) ? 2 : 1);
// 统一重写三项:SetSection 之后无论传没传都要落到专业数据里
if (dDn > 0.0) pX->SetAt(PIPE_DN, aec_mcp::mmToDwg(dDn));
// shortcut: 双线画法按 PIPE_OD 生成两条线,没给 od_mm 就用 DN 兜底(真实外径应查管子库)
double dOdSet = (dOd <= 0.0) ? dDn : dOd;
if (dOdSet > 0.0) pX->SetAt(PIPE_OD, aec_mcp::mmToDwg(dOdSet));
pX->SetAt(PIPE_THICK, aec_mcp::mmToDwg(dTh));
if (hasKey(a, "system"))
pX->SetAt(PIPE_SYS, wstrOr(a, "system", L"").c_str());
if (hasKey(a, "material"))
pX->SetAt(PIPE_MATERIAL, wstrOr(a, "material", L"").c_str());
// 用产品自己的 SetPipeParam 把"管线应有的那套字段"补齐(命令 PL_DHGD 就是走这条):
// TDbPipeJig::DoIt -> AecDbEwPipe::SetPipeParam(PipeParam*),一次写 ~30 个专业数据字段
// (PIPE_EAPP/PIPE_DRAWDIR/KEY_SINGLE/PIPE_D/PIPE_TDN/PIPE_TOD/PIPE_RATIO/KEY_RadRatio/
// PIPE_MATERIALFT/KEY_NO/PIPE_INSTHICK/PIPE_PIDD/PIPE_CONNECT/PIPE_INTERFACED*...)。
// 不加这一步,MCP 建的管比手画的少几十个字段。
{
WPipe::PipeParam* pPrm = p->GetPipeParam(); // 返回的是 new 出来的,要自己删
if (pPrm == NULL) pPrm = new WPipe::PipeParam();
if (dDn > 0.0) pPrm->m_dDN = aec_mcp::mmToDwg(dDn);
if (dOdSet > 0.0) pPrm->m_dOD = aec_mcp::mmToDwg(dOdSet);
pPrm->m_dThick = aec_mcp::mmToDwg(dTh);
if (hasKey(a, "system")) _tcscpy_s(pPrm->szSys, wstrOr(a, "system", L"").c_str());
if (hasKey(a, "material")) _tcscpy_s(pPrm->szMertial, wstrOr(a, "material", L"").c_str());
if (hasKey(a, "level")) _tcscpy_s(pPrm->szLevel, wstrOr(a, "level", L"").c_str());
p->SetPipeParam(pPrm);
delete pPrm;
// 按管线系统分图层/线型/颜色(CentLay/SingLay/OutDlay/COLOR/PIPE_LINELAYER* 这一批
// 都是 SetLayerByXDataMap 填的,产品画管时也调它)
p->SetLayerByXDataMap();
// 几个常量字段:命令那边在 FillConfigXDataMap 里固定这么写,SetPipeParam 不管它们
pX->SetAt(PIPE_RRADFLG, 1); // 弯头曲率用输入值
pX->SetAt(PIPE_PIPCMD, 0);
pX->SetAt(PIPE_Z2DDRAW, 0);
pX->SetAt(PIPE_INTERFACEL3, 0.0);
pX->SetAt(PIPE_UNITNO, _T(""));
}
// 再把图纸级参数(KT_PIPEINIT 里的 PipeMaxDia/PipeMinDia/PerElbowDia/Trade/Project...)
// 整份拷到实体上 —— 命令那边是 FillInInitXDataMap 干的,这里按 key 逐个复制等价。
{
AecDbObjXDataMap xInit;
if (KTModelLayoutXData::ReadModelXData(KT_PIPEINIT, xInit))
{
std::vector<CString> lst;
xInit.List(lst);
for (size_t i = 0; i + 1 < lst.size(); i += 2)
{
const CString& sKey = lst[i];
const CString& sVal = lst[i + 1];
// 只补"实体上没有的",免得盖掉刚写的管线自身参数
CString sOld;
if (!pX->GetAt(sKey, sOld)) pX->SetAt(sKey, (LPCTSTR)sVal);
}
}
}
// 颜色:与 cad_mcp_plugins 的既有工具统一用 color(ACI,256=ByLayer),
// 同时兼容旧写法 color_index
if (hasKey(a, "color"))
pX->SetAt(_T("COLOR"), intOr(a, "color", 256));
else if (hasKey(a, "color_index"))
pX->SetAt(_T("COLOR"), intOr(a, "color_index", 256));
return true;
}
// 墙:起点-终点 + 厚/高/底标高(+ 对齐、材质)
bool applyWall(AecDbXWall* p, const nlohmann::json& a, bool bCreate, std::wstring& err)
{
AcGePoint3d ptS, ptE;
const bool bHasS = readPoint(a, "start", ptS);
const bool bHasE = readPoint(a, "end", ptE);
if (bCreate && (!bHasS || !bHasE))
{
err = L"add_aec_wall 需要 start / end";
return false;
}
if (bHasS && bHasE)
{
if (ptS.distanceTo(ptE) < 1e-9)
{
err = L"start 与 end 重合,墙长为 0";
return false;
}
p->SetBaseCurve(ptS, ptE);
}
WallData data;
::memset(&data, 0, sizeof(WallData));
p->GetWallData(data);
const double dThick = numOr(a, "thickness", bCreate ? 0.0 : data.dThick);
const double dH = numOr(a, "height", bCreate ? 0.0 : data.dH);
const double dElev = numOr(a, "bottom_elevation", bCreate ? 0.0 : data.dBomEl);
if (dThick <= 0.0 || dH <= 0.0)
{
err = L"墙需要 thickness(厚)与 height(高),且都 > 0";
return false;
}
EAlign eAlign = data.kAlign;
if (hasKey(a, "align"))
{
const std::wstring s = wstrOr(a, "align", L"center");
if (s == L"left") eAlign = kLeft;
else if (s == L"right") eAlign = kRight;
else eAlign = kMid;
}
p->SetWallData(dThick, dH, dElev, eAlign);
if (hasKey(a, "material"))
p->GetSpecialtyData()->SetAt(KEY_MATERIAL, wstrOr(a, "material", L"").c_str());
return true;
}
// 板:闭合点串 + 板厚 + 板底标高
bool applySlab(AecDbXSlab* p, const nlohmann::json& a, bool bCreate, std::wstring& err)
{
AcGePoint2dArray arPts;
const bool bHasPts = readPoint2dArray(a, "points", arPts);
if (bCreate && !bHasPts)
{
err = L"add_aec_slab 需要 points:[[x,y],...](至少 3 个点,可不闭合)";
return false;
}
if (bHasPts && arPts.length() < 3)
{
err = L"points 至少要有 3 个点";
return false;
}
// SetRegionSlab() 内部用 CreatePolygonRegionOnly() 连线,而后者只连
// [0, n-2] 段、不自动闭合(闭合点只写进了 KEY_POINTS 字符串)。
// 所以这里必须自己把首点补到末尾,否则 region 建不出来、直接返回 false。
if (bHasPts && arPts.first().distanceTo(arPts.last()) > 1e-9)
arPts.append(arPts.first());
const double dThick = numOr(a, "thickness", 0.0);
const double dElev = numOr(a, "bottom_elevation", 0.0);
if (bCreate && dThick <= 0.0)
{
err = L"add_aec_slab 需要 thickness(板厚)> 0";
return false;
}
if (bHasPts)
{
// 中心点缺省取各点平均
AcGePoint3d ptCen(0.0, 0.0, 0.0);
if (!readPoint(a, "center", ptCen))
{
for (int i = 0; i < arPts.length(); ++i)
{
ptCen.x += arPts.at(i).x;
ptCen.y += arPts.at(i).y;
}
ptCen.x /= (double)arPts.length();
ptCen.y /= (double)arPts.length();
ptCen.z = 0.0;
}
if (!p->SetRegionSlab(arPts, dThick, dElev, ptCen))
{
err = L"SetRegionSlab 失败(点串可能自交或退化)";
return false;
}
}
else if (!bCreate)
{
// 只改板厚/标高:SetRegionSlab 的数组重载必须重新给轮廓,
// 所以这里把存盘时记在 KEY_POINTS 里的轮廓串取出来,走字符串重载。
const AecDbObjXDataMap* pX = p->GetSpecialtyData(AcDb::kForRead);
CString sPts;
AcGePoint3d ptOrg(0.0, 0.0, 0.0);
if (pX != NULL)
{
pX->GetAt(KEY_POINTS, sPts);
pX->GetAt(KEY_ORGPT, ptOrg);
}
if (sPts.IsEmpty())
{
err = L"这块板没有记录轮廓(KEY_POINTS 为空),无法在不给 points 的情况下改尺寸";
return false;
}
double dThickNow = dThick;
p->GetAt(KEY_BOMTHICK, dThickNow);
const double dThNew = hasKey(a, "thickness") ? dThick : (dThickNow > 0.0 ? dThickNow : dThick);
const double dElevNew = hasKey(a, "bottom_elevation") ? dElev : ptOrg.z;
AcGePoint3d ptCen = ptOrg;
readPoint(a, "center", ptCen);
if (!p->SetRegionSlab((LPCTSTR)sPts, dThNew, dElevNew, ptCen))
{
err = L"按已有轮廓重建板失败(轮廓串可能已损坏)";
return false;
}
}
if (hasKey(a, "material"))
p->GetSpecialtyData()->SetAt(KEY_MATERIAL, wstrOr(a, "material", L"").c_str());
return true;
}
// 柱:位置 + 高 + 截面(长 x 宽)+ 旋转
bool applyColumn(AecDbColumn* p, const nlohmann::json& a, bool bCreate, std::wstring& err)
{
AcGePoint3d ptPos;
const bool bHasPos = readPoint(a, "position", ptPos);
ColumnParam data;
::memset(&data, 0, sizeof(ColumnParam));
p->GetColumnData(data);
const bool bAny = bHasPos || hasKey(a, "height") || hasKey(a, "length")
|| hasKey(a, "width") || hasKey(a, "rotation_deg");
if (bCreate && !bHasPos)
{
err = L"add_aec_column 需要 position(柱底中心点)";
return false;
}
if (!bAny)
return true; // 只改了材质之类的场景
const AcGePoint3d ptBase = bHasPos ? ptPos : p->GetPosition();
const double dH = numOr(a, "height", bCreate ? 0.0 : p->GetHeight());
const AcGePoint2d rc = p->GetRect();
const double dL = numOr(a, "length", rc.x);
const double dW = numOr(a, "width", rc.y);
const double dRot = hasKey(a, "rotation_deg")
? numOr(a, "rotation_deg", 0.0) * PI / 180.0
: p->GetRotation();
if (dH <= 0.0 || dL <= 0.0 || dW <= 0.0)
{
err = L"柱需要 height / length / width,且都 > 0";
return false;
}
p->SetColumnData(ptBase, dL, dW, dH, dRot);
if (hasKey(a, "material"))
p->GetSpecialtyData()->SetAt(KEY_MATERIAL, wstrOr(a, "material", L"").c_str());
return true;
}
// 梁:起点-终点 + 截面宽 x 高
bool applyBeam(AecDbBeam* p, const nlohmann::json& a, bool bCreate, std::wstring& err)
{
AcGePoint3d ptS, ptE;
const bool bHasS = readPoint(a, "start", ptS);
const bool bHasE = readPoint(a, "end", ptE);
if (bCreate && (!bHasS || !bHasE))
{
err = L"add_aec_beam 需要 start / end";
return false;
}
if (bHasS && bHasE)
{
if (ptS.distanceTo(ptE) < 1e-9)
{
err = L"start 与 end 重合,梁长为 0";
return false;
}
p->SetBaseCurve(ptS, ptE);
}
ColumnParam data;
::memset(&data, 0, sizeof(ColumnParam));
p->GetBeamData(data);
// GetBeamData 把截面尺寸放在 m_dSizes[0](宽)/ m_dSizes[1](高)
const double dW = numOr(a, "width", bCreate ? 0.0 : data.m_dSizes[0]);
const double dH = numOr(a, "height", bCreate ? 0.0 : data.m_dSizes[1]);
if (dW <= 0.0 || dH <= 0.0)
{
err = L"梁需要 width(宽)与 height(高),且都 > 0";
return false;
}
p->SetBeamData(dW, dH);
if (hasKey(a, "material"))
p->GetSpecialtyData()->SetAt(KEY_MATERIAL, wstrOr(a, "material", L"").c_str());
return true;
}
// ---------------------------------------------------------------------------
// 共用收尾:设图层、加入空间、回包
// ---------------------------------------------------------------------------
nlohmann::json finishCreate(AcDbEntity* pEnt, const nlohmann::json& a, const char* pszType)
{
if (hasKey(a, "layer"))
{
const std::wstring sLayer = wstrOr(a, "layer", L"");
if (!sLayer.empty())
{
AcDbObjectId idLay = layerIdByName(sLayer);
if (!idLay.isNull())
pEnt->setLayer(idLay);
}
}
// space:与 cad_mcp_plugins 的 add_* 工具一致,缺省模型空间
std::wstring sSpace;
if (hasKey(a, "space"))
sSpace = wstrOr(a, "space", L"");
AcDbObjectId idNew;
Acad::ErrorStatus es = addToSpace(pEnt, &idNew, sSpace);
// 句柄要**入图之后**才有值:append 之前 pEnt 还没 handle,取出来是 "0"
const std::string sHandle = (es == Acad::eOk) ? handleOf(pEnt) : std::string();
pEnt->close();
if (es != Acad::eOk)
{
delete pEnt;
return errJson(kErrInternal, std::wstring(L"加入模型空间失败,err=") + std::to_wstring((int)es));
}
nlohmann::json body;
body["type"] = pszType;
body["handle"] = sHandle;
return okJson(body);
}
// 按 handle 打开并确认类型,失败时直接给出可返回的错误
template <typename T>
T* openTyped(const nlohmann::json& a, const wchar_t* pszExpect, nlohmann::json& errOut)
{
if (!hasKey(a, "handle"))
{
errOut = errJson(kErrBadParam, L"缺少 handle(实体句柄,十六进制字符串)");
return NULL;
}
const std::string sHandle = a["handle"].get<std::string>();
AcDbEntity* pEnt = openByHandle(sHandle, AcDb::kForWrite);
if (pEnt == NULL)
{
errOut = errJson(kErrNotFound, std::wstring(L"按 handle 打不开实体:") + utf8ToWstr(sHandle));
return NULL;
}
T* pTyped = T::cast(pEnt);
if (pTyped == NULL)
{
const std::string sCls = classNameOf(pEnt);
pEnt->close();
errOut = errJson(kErrWrongType,
std::wstring(L"handle ") + utf8ToWstr(sHandle) + L" 的实体是 " +
utf8ToWstr(sCls) + L",不是 " + pszExpect);
return NULL;
}
return pTyped;
}
nlohmann::json finishUpdate(AcDbObject* pObj, const std::string& sHandle, const char* pszType)
{
touchEntity(AcDbEntity::cast(pObj));
const std::string sH = sHandle.empty() ? handleOf(pObj) : sHandle;
pObj->close();
nlohmann::json body;
body["type"] = pszType;
body["handle"] = sH;
body["updated"] = true;
return okJson(body);
}
} // namespace
// ===========================================================================
// 管道
// ===========================================================================
class ToolCreatePipe : public mcp_Tool
{
public:
std::string private_name() const override { return "add_aec_pipe"; }
std::string private_description() const override
{
return "在当前图纸新建一根 Envi3D 管道(AecDbEwPipe)。"
"坐标用图纸单位,dn_mm/od_mm/wall_thickness_mm 用毫米。"
"返回新实体的 handle,后续可用 update_aec_pipe 修改。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"start": { "type": "object", "description": "起点 {\"x\",\"y\",\"z\"},图纸单位;也可用 [x,y,z]" },
"end": { "type": "object", "description": "终点,格式同 start" },
"dn_mm": { "type": "number", "description": "公称直径 DN,毫米" },
"od_mm": { "type": "number", "description": "外径,毫米(缺省不改)" },
"wall_thickness_mm": { "type": "number", "description": "壁厚,毫米(缺省不改)" },
"system": { "type": "string", "description": "管道系统名,如 给水/排水" },
"material": { "type": "string", "description": "材质名" },
"color": { "type": "integer", "description": "ACI 颜色号,256=ByLayer" },
"layer": { "type": "string", "description": "图层名,留空用当前图层" }
},
"required": [ "start", "end", "dn_mm" ]
})");
}
nlohmann::json Execute(const nlohmann::json& args) override
{
if (workingDb() == NULL) return errJson(kErrNoDocument, L"没有活动图纸");
DocLock lock;
if (!lock.isLocked()) return errJson(kErrInternal, L"拿不到图纸写锁(有别的命令在跑?)");
AecDbEwPipe* pPipe = new AecDbEwPipe();
std::wstring err;
if (!applyPipe(pPipe, args, true, err))
{
delete pPipe;
return errJson(kErrBadParam, err);
}
return finishCreate(pPipe, args, "pipe");
}
};
EXPORT_MCP_TOOL(ToolCreatePipe, CreateAecPipeTool)
class ToolUpdatePipe : public mcp_Tool
{
public:
std::string private_name() const override { return "update_aec_pipe"; }
std::string private_description() const override
{
return "修改已存在的 Envi3D 管道(AecDbEwPipe)。只传要改的字段,未传的保持原值。"
"改起终点会重算几何;改 dn_mm 会重建截面。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"handle": { "type": "string", "description": "要修改的管道句柄(十六进制)" },
"start": { "type": "object", "description": "新起点(可选)" },
"end": { "type": "object", "description": "新终点(可选)" },
"dn_mm": { "type": "number", "description": "新公称直径,毫米(可选)" },
"od_mm": { "type": "number", "description": "新外径,毫米(可选)" },
"wall_thickness_mm": { "type": "number", "description": "新壁厚,毫米(可选)" },
"system": { "type": "string", "description": "管道系统名(可选)" },
"material": { "type": "string", "description": "材质(可选)" },
"color": { "type": "integer", "description": "ACI 颜色号,256=ByLayer(可选)" }
},
"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"拿不到图纸写锁(有别的命令在跑?)");
nlohmann::json err;
AecDbEwPipe* pPipe = openTyped<AecDbEwPipe>(args, L"AecDbEwPipe", err);
if (pPipe == NULL) return err;
std::wstring msg;
if (!applyPipe(pPipe, args, false, msg))
{
pPipe->close();
return errJson(kErrBadParam, msg);
}
return finishUpdate(pPipe, args["handle"].get<std::string>(), "pipe");
}
};
EXPORT_MCP_TOOL(ToolUpdatePipe, UpdateAecPipeTool)
// ===========================================================================
// 墙
// ===========================================================================
class ToolCreateWall : public mcp_Tool
{
public:
std::string private_name() const override { return "add_aec_wall"; }
std::string private_description() const override
{
return "新建 Envi3D 墙体(AecDbXWall)。沿 start→end 建墙,尺寸用图纸单位,"
"bottom_elevation 是墙底标高;align 决定墙厚在基线哪一侧。返回 handle。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"start": { "type": "object", "description": "墙基线起点 {x,y,z}" },
"end": { "type": "object", "description": "墙基线终点 {x,y,z}" },
"thickness": { "type": "number", "description": "墙厚,图纸单位" },
"height": { "type": "number", "description": "墙高,图纸单位" },
"bottom_elevation": { "type": "number", "description": "墙底标高,图纸单位,缺省 0" },
"align": { "type": "string", "enum": [ "left", "center", "right" ], "description": "对齐方式,缺省 center" },
"material": { "type": "string", "description": "材质" },
"layer": { "type": "string", "description": "图层名" }
},
"required": [ "start", "end", "thickness", "height" ]
})");
}
nlohmann::json Execute(const nlohmann::json& args) override
{
if (workingDb() == NULL) return errJson(kErrNoDocument, L"没有活动图纸");
DocLock lock;
if (!lock.isLocked()) return errJson(kErrInternal, L"拿不到图纸写锁(有别的命令在跑?)");
AecDbXWall* pWall = new AecDbXWall();
std::wstring err;
if (!applyWall(pWall, args, true, err))
{
delete pWall;
return errJson(kErrBadParam, err);
}
return finishCreate(pWall, args, "wall");
}
};
EXPORT_MCP_TOOL(ToolCreateWall, CreateAecWallTool)
class ToolUpdateWall : public mcp_Tool
{
public:
std::string private_name() const override { return "update_aec_wall"; }
std::string private_description() const override
{
return "修改已存在的 Envi3D 墙体(AecDbXWall)。只传要改的字段。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"handle": { "type": "string", "description": "墙体句柄" },
"start": { "type": "object", "description": "新起点(可选)" },
"end": { "type": "object", "description": "新终点(可选)" },
"thickness": { "type": "number", "description": "新墙厚(可选)" },
"height": { "type": "number", "description": "新墙高(可选)" },
"bottom_elevation": { "type": "number", "description": "新墙底标高(可选)" },
"align": { "type": "string", "enum": [ "left", "center", "right" ], "description": "对齐(可选)" },
"material": { "type": "string", "description": "材质(可选)" }
},
"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"拿不到图纸写锁(有别的命令在跑?)");
nlohmann::json err;
AecDbXWall* pWall = openTyped<AecDbXWall>(args, L"AecDbXWall", err);
if (pWall == NULL) return err;
std::wstring msg;
if (!applyWall(pWall, args, false, msg))
{
pWall->close();
return errJson(kErrBadParam, msg);
}
return finishUpdate(pWall, args["handle"].get<std::string>(), "wall");
}
};
EXPORT_MCP_TOOL(ToolUpdateWall, UpdateAecWallTool)
// ===========================================================================
// 板
// ===========================================================================
class ToolCreateSlab : public mcp_Tool
{
public:
std::string private_name() const override { return "add_aec_slab"; }
std::string private_description() const override
{
return "新建 Envi3D 板/底板(AecDbXSlab)。points 是水平轮廓点串(可不闭合),"
"thickness 是板厚,bottom_elevation 是板底标高,均用图纸单位。返回 handle。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"points": { "type": "array", "description": "轮廓点 [[x,y],...],至少 3 个",
"items": { "type": "array", "items": { "type": "number" } } },
"thickness": { "type": "number", "description": "板厚,图纸单位" },
"bottom_elevation": { "type": "number", "description": "板底标高,缺省 0" },
"center": { "type": "object", "description": "板位置参考点 {x,y,z},缺省取点串平均值" },
"material": { "type": "string", "description": "材质" },
"layer": { "type": "string", "description": "图层名" }
},
"required": [ "points", "thickness" ]
})");
}
nlohmann::json Execute(const nlohmann::json& args) override
{
if (workingDb() == NULL) return errJson(kErrNoDocument, L"没有活动图纸");
DocLock lock;
if (!lock.isLocked()) return errJson(kErrInternal, L"拿不到图纸写锁(有别的命令在跑?)");
AecDbXSlab* pSlab = new AecDbXSlab();
std::wstring err;
if (!applySlab(pSlab, args, true, err))
{
delete pSlab;
return errJson(kErrBadParam, err);
}
return finishCreate(pSlab, args, "slab");
}
};
EXPORT_MCP_TOOL(ToolCreateSlab, CreateAecSlabTool)
class ToolUpdateSlab : public mcp_Tool
{
public:
std::string private_name() const override { return "update_aec_slab"; }
std::string private_description() const override
{
return "修改已存在的 Envi3D 板(AecDbXSlab)。传 points 会重做轮廓,"
"否则只改板厚/标高/材质。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"handle": { "type": "string", "description": "板句柄" },
"points": { "type": "array", "description": "新轮廓点(可选)",
"items": { "type": "array", "items": { "type": "number" } } },
"thickness": { "type": "number", "description": "新板厚(可选)" },
"bottom_elevation": { "type": "number", "description": "新板底标高(可选)" },
"center": { "type": "object", "description": "参考点(可选)" },
"material": { "type": "string", "description": "材质(可选)" }
},
"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"拿不到图纸写锁(有别的命令在跑?)");
nlohmann::json err;
AecDbXSlab* pSlab = openTyped<AecDbXSlab>(args, L"AecDbXSlab", err);
if (pSlab == NULL) return err;
std::wstring msg;
if (!applySlab(pSlab, args, false, msg))
{
pSlab->close();
return errJson(kErrBadParam, msg);
}
return finishUpdate(pSlab, args["handle"].get<std::string>(), "slab");
}
};
EXPORT_MCP_TOOL(ToolUpdateSlab, UpdateAecSlabTool)
// ===========================================================================
// 柱
// ===========================================================================
class ToolCreateColumn : public mcp_Tool
{
public:
std::string private_name() const override { return "add_aec_column"; }
std::string private_description() const override
{
return "新建矩形柱(AecDbColumn)。position 是柱底中心点,height 是柱高,"
"length/width 是截面两个方向尺寸,rotation_deg 是绕 Z 轴转角(度)。尺寸用图纸单位。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"position": { "type": "object", "description": "柱底中心点 {x,y,z}" },
"height": { "type": "number", "description": "柱高,图纸单位" },
"length": { "type": "number", "description": "截面长(X 向),图纸单位" },
"width": { "type": "number", "description": "截面宽(Y 向),图纸单位" },
"rotation_deg": { "type": "number", "description": "绕 Z 轴旋转角度(度),缺省 0" },
"material": { "type": "string", "description": "材质" },
"layer": { "type": "string", "description": "图层名" }
},
"required": [ "position", "height", "length", "width" ]
})");
}
nlohmann::json Execute(const nlohmann::json& args) override
{
if (workingDb() == NULL) return errJson(kErrNoDocument, L"没有活动图纸");
DocLock lock;
if (!lock.isLocked()) return errJson(kErrInternal, L"拿不到图纸写锁(有别的命令在跑?)");
AecDbColumn* pCol = new AecDbColumn();
std::wstring err;
if (!applyColumn(pCol, args, true, err))
{
delete pCol;
return errJson(kErrBadParam, err);
}
return finishCreate(pCol, args, "column");
}
};
EXPORT_MCP_TOOL(ToolCreateColumn, CreateAecColumnTool)
class ToolUpdateColumn : public mcp_Tool
{
public:
std::string private_name() const override { return "update_aec_column"; }
std::string private_description() const override
{
return "修改已存在的矩形柱(AecDbColumn)。只传要改的字段。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"handle": { "type": "string", "description": "柱句柄" },
"position": { "type": "object", "description": "新柱底中心点(可选)" },
"height": { "type": "number", "description": "新柱高(可选)" },
"length": { "type": "number", "description": "新截面长(可选)" },
"width": { "type": "number", "description": "新截面宽(可选)" },
"rotation_deg": { "type": "number", "description": "新旋转角度(可选)" },
"material": { "type": "string", "description": "材质(可选)" }
},
"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"拿不到图纸写锁(有别的命令在跑?)");
nlohmann::json err;
AecDbColumn* pCol = openTyped<AecDbColumn>(args, L"AecDbColumn", err);
if (pCol == NULL) return err;
std::wstring msg;
if (!applyColumn(pCol, args, false, msg))
{
pCol->close();
return errJson(kErrBadParam, msg);
}
return finishUpdate(pCol, args["handle"].get<std::string>(), "column");
}
};
EXPORT_MCP_TOOL(ToolUpdateColumn, UpdateAecColumnTool)
// ===========================================================================
// 梁
// ===========================================================================
class ToolCreateBeam : public mcp_Tool
{
public:
std::string private_name() const override { return "add_aec_beam"; }
std::string private_description() const override
{
return "新建梁(AecDbBeam)。沿 start→end 建梁,width/height 是截面尺寸,用图纸单位。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"start": { "type": "object", "description": "梁起点 {x,y,z}" },
"end": { "type": "object", "description": "梁终点 {x,y,z}" },
"width": { "type": "number", "description": "梁宽,图纸单位" },
"height": { "type": "number", "description": "梁高,图纸单位" },
"material": { "type": "string", "description": "材质" },
"layer": { "type": "string", "description": "图层名" }
},
"required": [ "start", "end", "width", "height" ]
})");
}
nlohmann::json Execute(const nlohmann::json& args) override
{
if (workingDb() == NULL) return errJson(kErrNoDocument, L"没有活动图纸");
DocLock lock;
if (!lock.isLocked()) return errJson(kErrInternal, L"拿不到图纸写锁(有别的命令在跑?)");
AecDbBeam* pBeam = new AecDbBeam();
std::wstring err;
if (!applyBeam(pBeam, args, true, err))
{
delete pBeam;
return errJson(kErrBadParam, err);
}
return finishCreate(pBeam, args, "beam");
}
};
EXPORT_MCP_TOOL(ToolCreateBeam, CreateAecBeamTool)
class ToolUpdateBeam : public mcp_Tool
{
public:
std::string private_name() const override { return "update_aec_beam"; }
std::string private_description() const override
{
return "修改已存在的梁(AecDbBeam)。只传要改的字段。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"handle": { "type": "string", "description": "梁句柄" },
"start": { "type": "object", "description": "新起点(可选)" },
"end": { "type": "object", "description": "新终点(可选)" },
"width": { "type": "number", "description": "新梁宽(可选)" },
"height": { "type": "number", "description": "新梁高(可选)" },
"material": { "type": "string", "description": "材质(可选)" }
},
"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"拿不到图纸写锁(有别的命令在跑?)");
nlohmann::json err;
AecDbBeam* pBeam = openTyped<AecDbBeam>(args, L"AecDbBeam", err);
if (pBeam == NULL) return err;
std::wstring msg;
if (!applyBeam(pBeam, args, false, msg))
{
pBeam->close();
return errJson(kErrBadParam, msg);
}
return finishUpdate(pBeam, args["handle"].get<std::string>(), "beam");
}
};
EXPORT_MCP_TOOL(ToolUpdateBeam, UpdateAecBeamTool)
// ===========================================================================
// 爬梯(AecDbClimbStair)
// 对应菜单命令 PL_ZXPT 直爬梯 / PL_YHZT 圆护爬梯(pool3dcmdCmd.cpp:5742)。
// 建模序列照抄命令实现:setLayer -> SetSection(踏管直径) -> SetBaseCurve(底->顶)
// -> SetClimbStairData(参数体) -> 专业数据 2dStyle/朝向。
// ===========================================================================
namespace
{
// 爬梯参数体:没传的字段用"现有值",新建时用命令对话框里的缺省量级
bool ApplyClimbStair(AecDbClimbStair* p, const nlohmann::json& a, bool bCreate, std::wstring& err)
{
AcGePoint3d ptS, ptE;
bool bHasS = readPoint(a, "start", ptS);
bool bHasE = readPoint(a, "end", ptE);
if (bCreate && (!bHasS || !bHasE))
{
err = L"add_aec_climb_stair 需要 start(梯段底部点) 与 end(顶部点)";
return false;
}
ClimbStair data;
::memset(&data, 0, sizeof(ClimbStair));
p->GetClimbStairData(data);
if (bCreate)
{
data.nType = hasKey(a, "type") ? intOr(a, "type", 0) : 0; // 0 圆护直梯 1 圆护凸爬
data.dSegSp = 300.0;
data.dSteepH = 300.0;
data.dSpWall = 180.0;
data.dD = 700.0;
data.dW = 450.0;
data.dd = 20.0;
data.dt = 2.5;
}
if (hasKey(a, "type")) data.nType = intOr(a, "type", data.nType);
if (hasKey(a, "cage_diameter")) data.dD = numOr(a, "cage_diameter", data.dD);
if (hasKey(a, "width")) data.dW = numOr(a, "width", data.dW);
if (hasKey(a, "height")) data.dH = numOr(a, "height", data.dH);
if (hasKey(a, "bottom_elevation")) data.dBomEl = numOr(a, "bottom_elevation", data.dBomEl);
if (hasKey(a, "step_height")) data.dSteepH = numOr(a, "step_height", data.dSteepH);
if (hasKey(a, "step_spacing")) data.dSegSp = numOr(a, "step_spacing", data.dSegSp);
if (hasKey(a, "wall_distance")) data.dSpWall = numOr(a, "wall_distance", data.dSpWall);
if (hasKey(a, "bar_diameter")) data.dd = numOr(a, "bar_diameter", data.dd);
if (hasKey(a, "bar_thickness")) data.dt = numOr(a, "bar_thickness", data.dt);
if (hasKey(a, "material"))
{
CString sMat(wstrOr(a, "material", L"").c_str());
_tcsncpy_s(data.m_sMaterial, 256, sMat, _TRUNCATE);
}
if (data.dW <= 0.0 || data.dD <= 0.0)
{
err = L"爬梯需要 width 与 cage_diameter,且都 > 0";
return false;
}
// 梯段基线:命令里是按"放置点+朝向"算出来的,工具直接收两个端点(更直接)
if (bHasS && bHasE)
{
if (ptS.distanceTo(ptE) < 1e-9)
{
err = L"start 与 end 重合,梯段长度为 0";
return false;
}
if (data.dH <= 1e-9)
data.dH = ptE.z - ptS.z;
if (data.dH <= 1e-9)
data.dH = 3000.0;
p->SetSection(data.dd, 0.0);
p->SetBaseCurve(ptS, ptE);
}
p->SetClimbStairData(data);
// 朝向:命令里存的是"人面对的方向",缺省给 +Y
AcGeVector3d vtFace(0.0, 1.0, 0.0);
{
nlohmann::json jFace;
if (a.is_object() && a.contains("facing"))
{
AcGePoint3d ptFace;
if (readPoint(a, "facing", ptFace))
vtFace = ptFace.asVector();
}
vtFace.normalize();
}
p->GetSpecialtyData()->SetAt(KEY_2DSTYLE, 0);
p->GetSpecialtyData()->SetAt(KEY_MAJBR, vtFace);
return true;
}
}
class ToolAddClimbStair : public mcp_Tool
{
public:
std::string private_name() const override { return "add_aec_climb_stair"; }
std::string private_description() const override
{
return "新建爬梯(AecDbClimbStair,对应菜单命令 直爬梯/圆护爬梯)。"
"给梯段底部点与顶部点,以及梯宽、护笼直径、总高;其余按对话框缺省。尺寸用图纸单位。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"start": { "type": "array", "items": { "type": "number" }, "description": "梯段底部点 [x,y] 或 [x,y,z]" },
"end": { "type": "array", "items": { "type": "number" }, "description": "梯段顶部点" },
"width": { "type": "number", "description": "梯宽,图纸单位(缺省 450)" },
"cage_diameter": { "type": "number", "description": "护笼总直径(缺省 700)" },
"height": { "type": "number", "description": "总高,不给则用 end.z - start.z" },
"bottom_elevation": { "type": "number", "description": "底标高,缺省 0" },
"type": { "type": "integer", "description": "0=圆护直梯(缺省)1=圆护凸爬" },
"bar_diameter": { "type": "number", "description": "踏管直径(缺省 20)" },
"step_height": { "type": "number", "description": "踏步高(缺省 300)" },
"step_spacing": { "type": "number", "description": "踏棍间距(缺省 300)" },
"wall_distance": { "type": "number", "description": "距墙距离(缺省 180)" },
"facing": { "type": "array", "items": { "type": "number" }, "description": "人面对的方向 [x,y],缺省 +Y" },
"material": { "type": "string", "description": "材质" },
"layer": { "type": "string", "description": "图层,缺省 LAY_CSTAIR" }
},
"required": [ "start", "end" ]
})");
}
nlohmann::json Execute(const nlohmann::json& args) override
{
if (workingDb() == NULL) return errJson(kErrNoDocument, L"没有活动图纸");
DocLock lock;
if (!lock.isLocked()) return errJson(kErrInternal, L"拿不到图纸写锁(有别的命令在跑?)");
AecDbClimbStair* pStair = new AecDbClimbStair();
std::wstring err;
if (!ApplyClimbStair(pStair, args, true, err))
{
delete pStair;
return errJson(kErrBadParam, err);
}
// 命令里固定用 LAY_CSTAIR 图层;工具允许覆盖
if (!hasKey(args, "layer"))
{
AcDbObjectId idLay = layerIdByName(L"LAY_CSTAIR");
if (!idLay.isNull())
pStair->setLayer(idLay);
}
return finishCreate(pStair, args, "climb_stair");
}
};
EXPORT_MCP_TOOL(ToolAddClimbStair, AddAecClimbStairTool)
class ToolUpdateClimbStair : public mcp_Tool
{
public:
std::string private_name() const override { return "update_aec_climb_stair"; }
std::string private_description() const override
{
return "修改已有爬梯:只传要改的字段(端点、梯宽、护笼直径、总高、踏步、材质…)。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"handle": { "type": "string", "description": "爬梯句柄" },
"start": { "type": "array", "items": { "type": "number" }, "description": "新起点(可选)" },
"end": { "type": "array", "items": { "type": "number" }, "description": "新终点(可选)" },
"width": { "type": "number" }, "cage_diameter": { "type": "number" },
"height": { "type": "number" }, "bottom_elevation": { "type": "number" },
"type": { "type": "integer" }, "bar_diameter": { "type": "number" },
"step_height": { "type": "number" }, "step_spacing": { "type": "number" },
"wall_distance": { "type": "number" }, "facing": { "type": "array", "items": { "type": "number" } },
"material": { "type": "string" }
},
"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"拿不到图纸写锁(有别的命令在跑?)");
nlohmann::json err;
AecDbClimbStair* pStair = openTyped<AecDbClimbStair>(args, L"AecDbClimbStair", err);
if (pStair == NULL) return err;
std::wstring msg;
if (!ApplyClimbStair(pStair, args, false, msg))
{
pStair->close();
return errJson(kErrBadParam, msg);
}
return finishUpdate(pStair, args["handle"].get<std::string>(), "climb_stair");
}
};
EXPORT_MCP_TOOL(ToolUpdateClimbStair, UpdateAecClimbStairTool)
// ===========================================================================
// 管件(AecDbEwFitting)
// 对应菜单命令 PL_DJWT 等(弯头/三通/大小头/法兰/堵头…,都是同一个实体类,
// 靠专业数据 KEY_RUNTIME 区分)。参数集照 TDbPipeLine::FillConfigXDataMap
// (PipelineCmd/AecPipeLine.cpp:3204)的最小可用子集,接口尺寸里那几项要查库的
// 先不填,绘制时会按 DN/OD/壁厚/曲率自己算。
// ===========================================================================
namespace
{
// 类型名 -> EFittingType
bool FittingTypeFromName(const std::wstring& s, int& nType)
{
if (s == L"elbow") { nType = eElbow; return true; }
if (s == L"reduce_elbow") { nType = eReduceElbow; return true; }
if (s == L"reduce") { nType = eReduce; return true; }
if (s == L"tee") { nType = eTee; return true; }
if (s == L"cross") { nType = eCross; return true; }
if (s == L"lateral") { nType = eLateral; return true; }
if (s == L"wye") { nType = eWye; return true; }
if (s == L"flange") { nType = eFlange; return true; }
if (s == L"cap") { nType = eWcap; return true; }
if (s == L"coupling") { nType = eFlxJoin; return true; }
return false;
}
bool ApplyFitting(AecDbEwFitting* p, const nlohmann::json& a, bool bCreate, std::wstring& err)
{
AecDbObjXDataMap* pX = p->GetSpecialtyData();
if (pX == NULL)
{
err = L"取专业数据失败";
return false;
}
// 类型
int nType = eElbow;
if (bCreate)
{
const std::wstring sType = hasKey(a, "type") ? wstrOr(a, "type", L"elbow") : L"elbow";
if (!FittingTypeFromName(sType, nType))
{
err = L"type 只支持 elbow/reduce_elbow/reduce/tee/cross/lateral/wye/flange/cap/coupling";
return false;
}
}
else if (hasKey(a, "type"))
{
const std::wstring sType = wstrOr(a, "type", L"elbow");
if (!FittingTypeFromName(sType, nType))
{
err = L"type 只支持 elbow/reduce_elbow/reduce/tee/cross/lateral/wye/flange/cap/coupling";
return false;
}
pX->SetAt(KEY_RUNTIME, nType);
}
else
{
int nCur = eElbow;
pX->GetAt(KEY_RUNTIME, nCur);
nType = nCur;
}
if (bCreate)
pX->SetAt(KEY_RUNTIME, nType);
// 位置与方向
AcGePoint3d ptMid;
if (readPoint(a, "position", ptMid))
{
pX->SetAt(KEY_MID, ptMid);
// 弯头/三通还要一条中线做 2D 表达,缺省给"主管方向"
}
else if (bCreate)
{
err = L"add_aec_fitting 需要 position(管件中心点)";
return false;
}
AcGePoint3d ptDir, ptBr;
AcGeVector3d vtMaj(1.0, 0.0, 0.0), vtMin(0.0, 1.0, 0.0);
if (readPoint(a, "direction", ptDir))
vtMaj = ptDir.asVector();
if (readPoint(a, "branch_direction", ptBr))
vtMin = ptBr.asVector();
vtMaj.z = 0.0;
vtMin.z = 0.0;
if (vtMaj.length() < 1e-9) vtMaj = AcGeVector3d::kXAxis;
if (vtMin.length() < 1e-9) vtMin = AcGeVector3d::kYAxis;
vtMaj.normalize();
vtMin.normalize();
pX->SetAt(KEY_MAJBR, vtMaj);
pX->SetAt(KEY_MINBR, vtMin);
// 口径(mm -> 米,与管道一致)
double dDn = 0.0, dOd = 0.0, dTh = 0.0;
pX->GetAt(PIPE_DN, dDn);
pX->GetAt(PIPE_OD, dOd);
pX->GetAt(PIPE_THICK, dTh);
if (hasKey(a, "dn_mm")) dDn = AS_MUNIT(numOr(a, "dn_mm", 0.0));
if (hasKey(a, "od_mm")) dOd = AS_MUNIT(numOr(a, "od_mm", 0.0));
if (hasKey(a, "wall_thickness_mm")) dTh = AS_MUNIT(numOr(a, "wall_thickness_mm", 0.0));
if (dDn <= 0.0)
{
err = L"管件需要 dn_mm(公称直径,mm)> 0";
return false;
}
if (dOd <= 0.0)
dOd = dDn * 1.1; // 没给外径时给个常见量级,绘制时按 DN 走
pX->SetAt(PIPE_DN, dDn);
pX->SetAt(PIPE_OD, dOd);
pX->SetAt(PIPE_TDN, dDn);
pX->SetAt(PIPE_TOD, dOd);
pX->SetAt(PIPE_D, dDn);
pX->SetAt(PIPE_THICK, dTh);
if (hasKey(a, "ratio")) pX->SetAt(PIPE_RATIO, numOr(a, "ratio", 1.0));
else if (bCreate) pX->SetAt(PIPE_RATIO, 1.0);
if (hasKey(a, "connect")) pX->SetAt(PIPE_CONNECT, intOr(a, "connect", 0));
else if (bCreate) pX->SetAt(PIPE_CONNECT, 0);
if (hasKey(a, "color")) pX->SetAt(PIPE_COLOR, intOr(a, "color", 256));
if (hasKey(a, "system")) pX->SetAt(PIPE_SYS, wstrOr(a, "system", L"").c_str());
if (hasKey(a, "material")) pX->SetAt(PIPE_MATERIAL, wstrOr(a, "material", L"").c_str());
if (bCreate)
{
pX->SetAt(KEY_2DSTYLE, 1); // 双线
pX->SetAt(KEY_SINGLE, 0);
pX->SetAt(PIPE_RRADFLG, 1);
pX->SetAt(KEY_RadRatio, 1);
pX->SetAt(PIPE_DRAWDIR, (int)WPipe::eDN);
}
return true;
}
}
class ToolAddFitting : public mcp_Tool
{
public:
std::string private_name() const override { return "add_aec_fitting"; }
std::string private_description() const override
{
return "新建管件(AecDbEwFitting:弯头/三通/大小头/四通/法兰/堵头/箍)。"
"给中心点、主管方向(可选支管方向)与口径;接口尺寸按 DN/外径/壁厚自算。"
"坐标用图纸单位,dn_mm/od_mm/wall_thickness_mm 用毫米。返回新实体 handle。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"type": { "type": "string",
"enum": ["elbow","reduce_elbow","reduce","tee","cross","lateral","wye","flange","cap","coupling"],
"description": "管件类型:弯头/变径弯头/大小头/三通/四通/L通/Y通/法兰/堵头/箍,缺省 elbow" },
"position": { "type": "array", "items": { "type": "number" }, "description": "管件中心点 [x,y] 或 [x,y,z]" },
"direction": { "type": "array", "items": { "type": "number" }, "description": "主管方向 [x,y],缺省 +X" },
"branch_direction": { "type": "array", "items": { "type": "number" }, "description": "支管方向 [x,y](弯头/三通的另一侧),缺省 +Y" },
"dn_mm": { "type": "number", "description": "公称直径,毫米" },
"od_mm": { "type": "number", "description": "外径,毫米(缺省按 DN 估算)" },
"wall_thickness_mm": { "type": "number", "description": "壁厚,毫米" },
"ratio": { "type": "number", "description": "曲率半径倍数(DN 的倍数),缺省 1.0" },
"connect": { "type": "integer", "description": "连接方式 0 焊接 / 1 承插 / 2 螺纹 / 3 法兰,缺省 0" },
"system": { "type": "string", "description": "管道系统名" },
"material": { "type": "string", "description": "材质" },
"color": { "type": "integer", "description": "ACI 颜色号,256=ByLayer" },
"layer": { "type": "string", "description": "图层名,缺省用当前图层" },
"space": { "type": "string", "description": "空间:Model 或布局名,缺省 Model" }
},
"required": [ "position", "dn_mm" ]
})");
}
nlohmann::json Execute(const nlohmann::json& args) override
{
if (workingDb() == NULL) return errJson(kErrNoDocument, L"没有活动图纸");
DocLock lock;
if (!lock.isLocked()) return errJson(kErrInternal, L"拿不到图纸写锁(有别的命令在跑?)");
AecDbEwFitting* pFit = new AecDbEwFitting();
std::wstring err;
if (!ApplyFitting(pFit, args, true, err))
{
delete pFit;
return errJson(kErrBadParam, err);
}
return finishCreate(pFit, args, "fitting");
}
};
EXPORT_MCP_TOOL(ToolAddFitting, AddAecFittingTool)
class ToolUpdateFitting : public mcp_Tool
{
public:
std::string private_name() const override { return "update_aec_fitting"; }
std::string private_description() const override
{
return "修改已有管件:只传要改的字段(类型、中心点、方向、口径、曲率、连接方式、材质…)。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"handle": { "type": "string", "description": "管件句柄" },
"type": { "type": "string",
"enum": ["elbow","reduce_elbow","reduce","tee","cross","lateral","wye","flange","cap","coupling"] },
"position": { "type": "array", "items": { "type": "number" }, "description": "新中心点(可选)" },
"direction": { "type": "array", "items": { "type": "number" }, "description": "新主管方向(可选)" },
"branch_direction": { "type": "array", "items": { "type": "number" }, "description": "新支管方向(可选)" },
"dn_mm": { "type": "number" }, "od_mm": { "type": "number" },
"wall_thickness_mm": { "type": "number" }, "ratio": { "type": "number" },
"connect": { "type": "integer" }, "system": { "type": "string" },
"material": { "type": "string" }, "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"拿不到图纸写锁(有别的命令在跑?)");
nlohmann::json err;
AecDbEwFitting* pFit = openTyped<AecDbEwFitting>(args, L"AecDbEwFitting", err);
if (pFit == NULL) return err;
std::wstring msg;
if (!ApplyFitting(pFit, args, false, msg))
{
pFit->close();
return errJson(kErrBadParam, msg);
}
return finishUpdate(pFit, args["handle"].get<std::string>(), "fitting");
}
};
EXPORT_MCP_TOOL(ToolUpdateFitting, UpdateAecFittingTool)
// ===========================================================================
// 开洞(AecDbX3dPartEntity + 母体布尔)
// 对应菜单命令 PL_CDKD 平板开洞(Pool3dCmd/pool3dcmdCmd.cpp:14763)。
// 序列照命令实现:new AecDbX3dPartEntity -> SetAsHole(中心,尺寸,板厚,法向)
// -> KEY_ASSOIC 记母体 -> LAY_HOLE 图层 -> 入库 -> 给母体加关联件 + 持久反应器。
// 洞是"长在母体上的 part",所以工具收 host(板的 handle)+ 洞口参数。
// ===========================================================================
namespace
{
const TCHAR* kHoleCenKey = _T("AECMCP_HOLE_CEN"); // 记洞口中心,供 update 复用
const TCHAR* kHoleDirKey = _T("AECMCP_HOLE_DIR"); // 记洞口法向(墙厚方向)
// 墙的放样中心:基线中点(墙没有 ClosestPointToCen)
void WallCenter(AecDbXWall* pWall, AcGePoint3d& ptCen)
{
if (pWall == NULL) return;
AcDbCurve* pBase = pWall->GetBaseCurve(false);
if (pBase == NULL) return;
AcGePoint3d ptS, ptE;
if (pBase->getStartPoint(ptS) == Acad::eOk && pBase->getEndPoint(ptE) == Acad::eOk)
ptCen = AcGePoint3d((ptS.x + ptE.x) * 0.5, (ptS.y + ptE.y) * 0.5, ptCen.z);
delete pBase;
}
// 井的四种形态(对应菜单命令 方井 PL_JFZJ / 圆井 PL_JYZJ / 方圆井 PL_JFYJ / 异形井 PL_JYXJ)
enum WellShape
{
kWellRect = 0, //方台井:矩形上下口
kWellCircle, //圆台井:圆形上下口
kWellRectCircle, //方圆井:上口矩形、下口圆
kWellPolygon, //异形偏心井:上下口都是点串
kWellSump //集水坑井:矩形井 + 放坡角
};
bool ParseWellShape(const nlohmann::json& a, WellShape& eShape, std::wstring& err)
{
const std::wstring s = hasKey(a, "shape") ? wstrOr(a, "shape", L"rect") : L"rect";
if (s == L"rect") { eShape = kWellRect; return true; }
if (s == L"circle") { eShape = kWellCircle; return true; }
if (s == L"rect_circle") { eShape = kWellRectCircle; return true; }
if (s == L"polygon") { eShape = kWellPolygon; return true; }
if (s == L"sump") { eShape = kWellSump; return true; }
err = L"shape 只支持 rect(方井)/ circle(圆井)/ rect_circle(方圆井)/ polygon(异形井)/ sump(集水坑)";
return false;
}
// update 时按实体自己的名字判断它属于哪种井(名字由 SetAsWell 写进实体)
bool ShapeFromWellName(const CString& sName, WellShape& eShape)
{
if (sName.CompareNoCase(_T("方台井")) == 0) { eShape = kWellRect; return true; }
if (sName.CompareNoCase(_T("圆台井")) == 0) { eShape = kWellCircle; return true; }
if (sName.CompareNoCase(_T("方圆井")) == 0) { eShape = kWellRectCircle; return true; }
if (sName.CompareNoCase(_T("异形偏心井")) == 0) { eShape = kWellPolygon; return true; }
if (sName.CompareNoCase(_T("集水坑井")) == 0) { eShape = kWellSump; return true; }
return false;
}
// 点串 [[x,y],...] -> "x,y@bulge,..."(闭合),与 CoreTools::PolylineToString 一致
bool PointsToScript(const nlohmann::json& arr, const nlohmann::json& bulges,
CString& sOut, AcGePoint3d& ptCen)
{
if (!arr.is_array() || arr.size() < 3) return false;
double dMinX = 1e300, dMinY = 1e300, dMaxX = -1e300, dMaxY = -1e300;
double dFirstX = 0.0, dFirstY = 0.0, dLastX = 0.0, dLastY = 0.0;
CString sAll;
for (size_t i = 0; i < arr.size(); ++i)
{
const nlohmann::json& e = arr[i];
if (!e.is_array() || e.size() < 2) return false;
const double x = e[0].get<double>();
const double y = e[1].get<double>();
double b = 0.0;
if (bulges.is_array() && i < bulges.size() && bulges[i].is_number())
b = bulges[i].get<double>();
CString sOne;
sOne.Format(_T("%.9f,%.9f@%.9f,"), x, y, b);
sAll += sOne;
if (x < dMinX) dMinX = x;
if (y < dMinY) dMinY = y;
if (x > dMaxX) dMaxX = x;
if (y > dMaxY) dMaxY = y;
if (i == 0) { dFirstX = x; dFirstY = y; }
dLastX = x; dLastY = y;
}
if (fabs(dLastX - dFirstX) > 1e-9 || fabs(dLastY - dFirstY) > 1e-9)
{
CString sOne;
sOne.Format(_T("%.9f,%.9f@0.0,"), dFirstX, dFirstY);
sAll += sOne;
}
sAll.TrimRight(_T(','));
ptCen.set((dMinX + dMaxX) * 0.5, (dMinY + dMaxY) * 0.5, 0.0);
sOut = sAll;
return !sOut.IsEmpty();
}
// 从实体专业数据里取一个 double(update 时没传参数就沿用原值)
double XDataD(const AecDbObjXDataMap* pX, const TCHAR* pszKey, double dDef)
{
double d = 0.0;
if (pX != NULL && pX->GetAt(pszKey, d))
return d;
return dDef;
}
// 井:先开洞、再叠井体。序列与 DBL_AEC_X3DPART 里各井的修改分支一致:
// 洞口中心 = 板底标高 + 半板厚;井体原点 = 板底标高。
bool ApplyWell(AecDbX3dPartEntity* pPart, WellShape eShape, const AcGePoint3d& ptWellOrg,
double dSlabT, double dSlabBomEl, const nlohmann::json& a, bool bCreate, std::wstring& err)
{
if (dSlabT <= 0.0)
{
err = L"取不到板厚,请显式给 slab_thickness";
return false;
}
// 没传就沿用实体里已有的(update 场景);新建时必须给
const AecDbObjXDataMap* pX = pPart->GetSpecialtyData(AcDb::kForRead);
const double dH = numOr(a, "height", XDataD(pX, _T("dH"), 0.0));
const double dT = numOr(a, "wall_thickness",
XDataD(pX, _T("dt"), XDataD(pX, _T("dThick"),
XDataD(pX, _T("dBomThick"), 0.0))));
if (dH <= 0.0 || dT <= 0.0)
{
err = L"井需要 height 与 wall_thickness,且都 > 0";
return false;
}
if (bCreate)
pPart->GetSpecialtyData()->SetAt(KEY_SETASWELL, TRUE);
AcGeVector3d vtZ = AcGeVector3d::kZAxis;
AcGePoint3d ptHole = ptWellOrg;
ptHole.z = dSlabBomEl + 0.5 * dSlabT;
AcGeVector2d vtOffset(0.0, 0.0);
if (hasKey(a, "offset_x") || hasKey(a, "offset_y"))
vtOffset.set(numOr(a, "offset_x", 0.0), numOr(a, "offset_y", 0.0));
if (eShape == kWellRect)
{
const double dL = numOr(a, "length", XDataD(pX, _T("dL"), 0.0));
const double dW = numOr(a, "width", XDataD(pX, _T("dW"), 0.0));
const double dL1 = numOr(a, "bottom_length", XDataD(pX, _T("dL1"), dL));
const double dW1 = numOr(a, "bottom_width", XDataD(pX, _T("dW1"), dW));
if (dL <= 0.0 || dW <= 0.0)
{
err = L"方井需要 length 与 width,且都 > 0";
return false;
}
pPart->SetAsHole(ptHole, dL, dW, 1.1 * dSlabT, vtZ);
pPart->SetAsWell(ptWellOrg, dL, dW, dL1, dW1, dH, dT, dSlabT, vtOffset, true);
}
else if (eShape == kWellCircle)
{
const double dD = numOr(a, "diameter", 2.0 * XDataD(pX, _T("dR"), 0.0));
const double dd = numOr(a, "bottom_diameter", 2.0 * XDataD(pX, _T("dr"), 0.5 * dD));
if (dD <= 0.0)
{
err = L"圆井需要 diameter > 0";
return false;
}
pPart->SetAsHole(ptHole, 0.5 * dD, 1.1 * dSlabT, vtZ);
pPart->SetAsWell(ptWellOrg, 0.5 * dD, 0.5 * dd, dH, dT, dSlabT, vtOffset, true);
}
else if (eShape == kWellRectCircle)
{
const double dL = numOr(a, "length", XDataD(pX, _T("dL"), 0.0));
const double dW = numOr(a, "width", XDataD(pX, _T("dW"), 0.0));
const double dD = numOr(a, "diameter", numOr(a, "bottom_diameter",
2.0 * XDataD(pX, _T("dr"), 0.0)));
if (dL <= 0.0 || dW <= 0.0 || dD <= 0.0)
{
err = L"方圆井需要 length / width / diameter(下口圆直径),且都 > 0";
return false;
}
// 命令里方圆井的洞按 1.2 倍板厚,且偏移只用 X 方向一个值
pPart->SetAsHole(ptHole, dL, dW, 1.2 * dSlabT, vtZ);
pPart->SetAsWell(ptWellOrg, dL, dW, 0.5 * dD, dH, dT, dSlabT,
numOr(a, "offset_x", 0.0), true);
}
else if (eShape == kWellSump)
{
// 集水坑:矩形井 + 放坡角;命令里 SetAsWell(ptCen, dL, dW, dH, 板厚, 壁厚, 弧度, true)
const double dL = numOr(a, "length", XDataD(pX, _T("dL"), 0.0));
const double dW = numOr(a, "width", XDataD(pX, _T("dW"), 0.0));
if (dL <= 0.0 || dW <= 0.0)
{
err = L"集水坑需要 length 与 width,且都 > 0";
return false;
}
const double dAng = hasKey(a, "angle_deg")
? numOr(a, "angle_deg", 0.0) * PI / 180.0
: XDataD(pX, _T("Ang"), 22.5 * PI / 180.0);
pPart->SetAsHole(ptHole, dL, dW, 1.1 * dSlabT, vtZ);
pPart->SetAsWell(ptWellOrg, dL, dW, dH, (float)dSlabT, dT, dAng, true);
}
else
{
// 异形井:上下口都是点串;没给就用实体里已经存的(修改时不改截面)
CString sTop, sBom;
AcGePoint3d ptTmp;
const nlohmann::json jBulg = hasKey(a, "bulges") ? a["bulges"] : nlohmann::json();
if (hasKey(a, "top_points"))
{
if (!PointsToScript(a["top_points"], jBulg, sTop, ptTmp))
{
err = L"top_points 至少 3 个点,格式 [[x,y],...]";
return false;
}
}
else
{
pPart->GetSpecialtyData()->GetAt(_T("TopScr"), sTop);
}
if (hasKey(a, "bottom_points"))
{
if (!PointsToScript(a["bottom_points"], jBulg, sBom, ptTmp))
{
err = L"bottom_points 至少 3 个点,格式 [[x,y],...]";
return false;
}
}
else
{
pPart->GetSpecialtyData()->GetAt(_T("BomScr"), sBom);
}
if (sTop.IsEmpty() || sBom.IsEmpty())
{
err = L"异形井需要 top_points 与 bottom_points(新建时必填)";
return false;
}
pPart->SetAsHole(ptHole, (LPCTSTR)sTop, 1.1 * dSlabT, vtZ);
pPart->SetAsWell((LPCTSTR)sTop, (LPCTSTR)sBom, dH, dT, dSlabT, dSlabBomEl, true);
}
if (hasKey(a, "no"))
pPart->GetSpecialtyData()->SetAt(KEY_NO, wstrOr(a, "no", L"").c_str());
if (hasKey(a, "remark"))
pPart->GetSpecialtyData()->SetAt(KEY_REM, wstrOr(a, "remark", L"").c_str());
if (hasKey(a, "purpose"))
pPart->GetSpecialtyData()->SetAt(KEY_PURPOSE, wstrOr(a, "purpose", L"").c_str());
return true;
}
// [[x,y],...] + 可选 bulges -> "x,y@b,x,y@b,...,x,y@0"(首尾闭合,同 PolylineToScript)
bool BuildPointScript(const nlohmann::json& a, CString& sOut, AcGePoint3d& ptCen)
{
if (!hasKey(a, "points") || !a["points"].is_array()) return false;
const nlohmann::json& arr = a["points"];
if (arr.size() < 3) return false;
std::vector<double> bulges;
if (hasKey(a, "bulges") && a["bulges"].is_array())
{
for (size_t i = 0; i < a["bulges"].size(); ++i)
bulges.push_back(a["bulges"][i].get<double>());
}
double dMinX = 1e300, dMinY = 1e300, dMaxX = -1e300, dMaxY = -1e300;
double dFirstX = 0.0, dFirstY = 0.0, dLastX = 0.0, dLastY = 0.0;
CString sAll;
for (size_t i = 0; i < arr.size(); ++i)
{
const nlohmann::json& e = arr[i];
if (!e.is_array() || e.size() < 2) return false;
const double x = e[0].get<double>();
const double y = e[1].get<double>();
const double b = (i < bulges.size()) ? bulges[i] : 0.0;
CString sOne;
sOne.Format(_T("%.9f,%.9f@%.9f,"), x, y, b);
sAll += sOne;
if (x < dMinX) dMinX = x;
if (y < dMinY) dMinY = y;
if (x > dMaxX) dMaxX = x;
if (y > dMaxY) dMaxY = y;
if (i == 0) { dFirstX = x; dFirstY = y; }
dLastX = x; dLastY = y;
}
if (fabs(dLastX - dFirstX) > 1e-9 || fabs(dLastY - dFirstY) > 1e-9)
{
CString sOne;
sOne.Format(_T("%.9f,%.9f@0.0,"), dFirstX, dFirstY);
sAll += sOne;
}
sAll.TrimRight(_T(','));
ptCen.set((dMinX + dMaxX) * 0.5, (dMinY + dMaxY) * 0.5, 0.0);
sOut = sAll;
return !sOut.IsEmpty();
}
// 墙厚方向:在墙基线上取最近点,把切线绕 Z 转 90 度(与 PL_QTKDCmdFnc 的算法一致)
bool WallNormalAt(AecDbXWall* pWall, const AcGePoint3d& ptOn, AcGeVector3d& vtNormal)
{
if (pWall == NULL) return false;
AcDbCurve* pBase = pWall->GetBaseCurve(false);
if (pBase == NULL) return false;
AcGePoint3d ptWall;
bool bOk = (pBase->getClosestPointTo(ptOn, ptWall, Adesk::kFalse) == Acad::eOk);
if (bOk)
{
AcGeVector3d vt;
bOk = (pBase->getFirstDeriv(ptWall, vt) == Acad::eOk);
if (bOk)
{
vt.rotateBy(PI / 2.0, AcGeVector3d::kZAxis);
vt.z = 0.0;
if (vt.length() > 1e-9)
vt.normalize();
else
bOk = false;
vtNormal = vt;
}
}
delete pBase;
return bOk;
}
bool ApplyHole(AecDbX3dPartEntity* pPart, AcDbEntity* pHost,
const AcGePoint3d& ptCen, double dDepth, const AcGeVector3d& vtDir,
const nlohmann::json& a, bool bCreate, std::wstring& err)
{
// 形状:circle / rect
int nShape = 0; // 0 圆 1 矩形 2 异形
if (hasKey(a, "shape"))
{
const std::wstring s = wstrOr(a, "shape", L"circle");
if (s == L"rect") nShape = 1;
else if (s == L"circle") nShape = 0;
else if (s == L"polygon") nShape = 2;
else
{
err = L"shape 只支持 circle / rect / polygon";
return false;
}
}
const double dDia = numOr(a, "diameter", 0.0);
const double dW = numOr(a, "width", 0.0);
const double dH = numOr(a, "height", 0.0);
const bool bNozz = hasKey(a, "nozzle") ? (intOr(a, "nozzle", 0) != 0) : false;
AcGeVector3d vtZ = vtDir;
if (vtZ.length() < 1e-9) vtZ = AcGeVector3d::kZAxis;
vtZ.normalize();
AcGePoint3d ptStore = ptCen; // 实际用于记录的洞口中心(异形洞会换成点集中心)
if (nShape == 2)
{
// 异形洞:点串格式与 PolylineToScript 一致,中心取点集包围盒中点
CString sPts;
AcGePoint3d ptPolyCen(0.0, 0.0, 0.0);
if (!BuildPointScript(a, sPts, ptPolyCen))
{
err = L"异形洞需要 points:[[x,y],...](至少 3 个点),可选 bulges";
return false;
}
ptPolyCen.z = ptCen.z;
if (!pPart->SetAsHole(ptPolyCen, (LPCTSTR)sPts, dDepth, vtZ))
{
err = L"SetAsHole(异形) 失败";
return false;
}
ptStore = ptPolyCen;
}
else if (nShape == 0)
{
if (dDia <= 0.0)
{
err = L"圆洞需要 diameter > 0";
return false;
}
if (!pPart->SetAsHole(ptCen, 0.5 * dDia, dDepth, vtZ, bNozz))
{
err = L"SetAsHole(圆) 失败";
return false;
}
}
else
{
if (dW <= 0.0 || dH <= 0.0)
{
err = L"矩形洞需要 width 与 height,且都 > 0";
return false;
}
if (!pPart->SetAsHole(ptCen, dW, dH, dDepth, vtZ, bNozz))
{
err = L"SetAsHole(矩形) 失败";
return false;
}
}
// 记录中心,方便后续 update(SetAsHole 自己会把 KEY_NOPOS 挪到侧边)
pPart->GetSpecialtyData()->SetAt(kHoleCenKey, ptStore);
pPart->GetSpecialtyData()->SetAt(kHoleDirKey, vtZ);
if (hasKey(a, "no"))
pPart->GetSpecialtyData()->SetAt(KEY_NO, wstrOr(a, "no", L"").c_str());
if (hasKey(a, "for_use"))
pPart->GetSpecialtyData()->SetAt(KEY_FORUSE, wstrOr(a, "for_use", L"").c_str());
if (bCreate)
{
pPart->GetSpecialtyData()->SetAt(KEY_ASSOIC, pHost->objectId());
}
return true;
}
}
// 母体沿某方向的尺寸 = 洞深缺省值。
// 3D 缓存没建时 entityBBox 拿不到,就当场要一份 3D solid 来量(不挂缓存,量完删掉)。
bool HostSizeAlong(AecDbBDPipeBase* pBase, const AcGeVector3d& vtDir, double& dOut)
{
dOut = 0.0;
if (pBase == NULL) return false;
AcGeVector3d vtN = vtDir;
vtN.normalize();
AcGePoint3d ptMin(0.0, 0.0, 0.0), ptMax(0.0, 0.0, 0.0);
if (entityBBox(pBase, ptMin, ptMax))
{
dOut = fabs((ptMax - ptMin).dotProduct(vtN));
if (dOut > 1e-9) return true;
}
AcDb3dSolid* pSolid = pBase->GetDraw3dSolid(false, 0, false); // 不扣洞,量原始体
if (pSolid == NULL) return false;
AcDbExtents ext;
const bool bOk = (pSolid->getGeomExtents(ext) == Acad::eOk);
delete pSolid;
if (!bOk) return false;
dOut = fabs((ext.maxPoint() - ext.minPoint()).dotProduct(vtN));
return dOut > 1e-9;
}
// 套管:同一类 AecDbX3dPartEntity,但用 SetAsPipe 而不是 SetAsHole。
// 命令里(PL_CDKD / PL_QTKD 的 dlg.m_bHasCase 分支):
// SetAsPipe(ptOrg, nShape, dlg.m_dDL, dlg.m_dW, dlg.m_dT, 套管长, bExt, dEx1, dEx2, 法向)
// 再写 MATERIAL / PIPETYPE / PIPE_TOD(翼环外径) / PIPE_YMS(翼环数量),图层 LAY_CASEPIPE
// 注意 SetAsPipe 的矩形分支里参数是"反着存"的:传 (dH=高, dW=宽) → xdata 存 dL=宽、dW=高。
bool ApplyCasing(AecDbX3dPartEntity* pPart, const AcGePoint3d& ptCen, const AcGeVector3d& vtDirIn,
double dDepthDefault, const nlohmann::json& a, bool bCreate, std::wstring& err)
{
const AecDbObjXDataMap* pX = pPart->GetSpecialtyData(AcDb::kForRead);
// 形状:SetAsPipe 按 0=circle / 1=rect 分派(对话框约定);
// 而 xdata 里存的 nShape 是 ESection(eRect=1 / eCircle=2),读回来要换算。
bool bRect = false;
if (hasKey(a, "shape"))
{
const std::wstring s = wstrOr(a, "shape", L"circle");
if (s == L"rect") bRect = true;
else if (s == L"circle") bRect = false;
else
{
err = L"套管只支持 circle / rect(异形洞不能做套管)";
return false;
}
}
else if (pX != NULL)
{
int nOld = eCircle;
pX->GetAt(_T("nShape"), nOld);
bRect = (nOld == eRect);
}
const int nShape = bRect ? 1 : 0;
AcGeVector3d vtDir = vtDirIn;
if (vtDir.length() < 1e-9)
vtDir = AcGeVector3d::kZAxis;
// 尺寸:没传就沿用实体里已有的(update 场景)
double dLen = numOr(a, "casing_length", XDataD(pX, _T("dH"), dDepthDefault));
if (dLen <= 0.0 && !hasKey(a, "casing_length"))
dLen = XDataD(pX, _T("m_dT"), 0.0); // 洞转套管:洞的 m_dT 存的是洞深
const double dT = numOr(a, "casing_wall_thickness", XDataD(pX, _T("dT"), 0.0));
const double dExt1 = numOr(a, "casing_extend1", XDataD(pX, _T("dEx1"), 0.0));
const double dExt2 = numOr(a, "casing_extend2", XDataD(pX, _T("dEx2"), 0.0));
const BOOL bExt = hasKey(a, "casing_extend") ? (intOr(a, "casing_extend", 0) != 0)
: (XDataD(pX, _T("bExt"), 0.0) != 0.0);
if (dLen <= 0.0 || dT <= 0.0)
{
err = L"套管需要 casing_length(套管长)与 casing_wall_thickness(壁厚),且都 > 0";
return false;
}
if (bCreate)
pPart->GetSpecialtyData()->SetAt(KEY_SETASWELL, TRUE);
bool bOk = false;
if (bRect)
{
const double dW = numOr(a, "width", XDataD(pX, _T("dL"), 0.0));
const double dH = numOr(a, "height", XDataD(pX, _T("dW"), 0.0));
if (dW <= 0.0 || dH <= 0.0)
{
err = L"矩形套管需要 width 与 height,且都 > 0";
return false;
}
bOk = pPart->SetAsPipe(ptCen, nShape, dH, dW, dT, dLen, bExt, dExt1, dExt2, vtDir);
}
else
{
double dD = numOr(a, "diameter", XDataD(pX, _T("dD"), 0.0));
if (dD <= 0.0 && !hasKey(a, "diameter"))
dD = 2.0 * XDataD(pX, _T("m_dL"), 0.0); // 洞转套管:洞的 m_dL 存的是半径
if (dD <= 0.0)
{
err = L"圆套管需要 diameter > 0";
return false;
}
bOk = pPart->SetAsPipe(ptCen, nShape, dD, 0.0, dT, dLen, bExt, dExt1, dExt2, vtDir);
}
if (!bOk)
{
err = L"SetAsPipe 失败(参数或几何不合法)";
return false;
}
if (hasKey(a, "material"))
pPart->GetSpecialtyData()->SetAt(KEY_MATERIAL, wstrOr(a, "material", L"").c_str());
if (hasKey(a, "pipe_type"))
pPart->GetSpecialtyData()->SetAt(KEY_PIPETYPE, wstrOr(a, "pipe_type", L"").c_str());
if (hasKey(a, "ring_od")) // 翼环外径(命令里存字符串)
pPart->GetSpecialtyData()->SetAt(PIPE_TOD, wstrOr(a, "ring_od", L"").c_str());
if (hasKey(a, "ring_count")) // 翼环数量
pPart->GetSpecialtyData()->SetAt(PIPE_YMS, intOr(a, "ring_count", 0));
if (hasKey(a, "no"))
pPart->GetSpecialtyData()->SetAt(KEY_NO, wstrOr(a, "no", L"").c_str());
return true;
}
class ToolAddOpening : public mcp_Tool
{
public:
std::string private_name() const override { return "add_aec_opening"; }
std::string private_description() const override
{
return "在板或墙上开洞(对应菜单命令 平板开洞 PL_CDKD / 墙体开洞 PL_CBKD / 阵列开洞 PL_ZLKD / 穿管开洞 PL_YXCG),"
"也可以做成套管:给母体的 handle 与洞口参数,生成 AecDbX3dPartEntity 并自动与母体做布尔、挂到母体下"
"(洞=LAY_HOLE / 套管=LAY_CASEPIPE)。尺寸用图纸单位;depth 缺省取母体厚度(板厚/墙厚);"
"洞口方向自动取板法向或墙厚方向。阵列给 rows/columns + spacing_x/spacing_y;"
"穿管开洞给 source=管线 handle(圆管道自动取外径);casing=true 走 SetAsPipe。"
"返回洞口/套管实体 handle,阵列时另给 handles 数组。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"host": { "type": "string", "description": "母体 handle:板/墙,或任意 AecDbBDPipeBase 派生实体(异形构筑物/柱/梁/堰);后几种必须给 direction" },
"shape": { "type": "string", "enum": [ "circle", "rect" ], "description": "洞口形状,缺省 circle" },
"position": { "type": "array", "items": { "type": "number" },
"description": "洞口位置 [x,y] 或 [x,y,z]:普通洞是洞在厚度方向的起点(面/底面),穿管洞(source)直接是洞心;不给则用母体中心" },
"diameter": { "type": "number", "description": "圆洞直径,图纸单位" },
"width": { "type": "number", "description": "矩形洞宽(X 向)" },
"height": { "type": "number", "description": "矩形洞高(Y 向)" },
"depth": { "type": "number", "description": "洞深:板缺省=板厚、墙缺省=墙厚、其它母体缺省=沿 direction 的实体尺寸" },
"nozzle": { "type": "boolean", "description": "是否要管嘴(缺省否)" },
"no": { "type": "string", "description": "洞口编号" },
"for_use":{ "type": "string", "description": "洞口用途" },
"layer": { "type": "string", "description": "图层,缺省 洞=LAY_HOLE / 套管=LAY_CASEPIPE" },
"source": { "type": "string", "description": "穿管开洞(PL_YXCG):管线 handle(管道/风管/桥架),洞口中心取管线轴线与母体中面的交点;圆管道自动取外径" },
"rows": { "type": "integer", "description": "阵列开洞(PL_ZLKD)行数,缺省 1" },
"columns": { "type": "integer", "description": "阵列开洞列数,缺省 1" },
"spacing_x": { "type": "number", "description": "阵列列间距(墙=沿墙方向 / 板=X 方向)" },
"spacing_y": { "type": "number", "description": "阵列行间距(墙=竖向 / 板=Y 方向)" },
"casing": { "type": "boolean", "description": "true=做成套管(SetAsPipe,只支持 circle/rect);缺省否" },
"casing_length": { "type": "number", "description": "套管长度(缺省取母体厚度)" },
"casing_wall_thickness": { "type": "number", "description": "套管壁厚" },
"casing_extend": { "type": "boolean", "description": "套管两端是否外伸" },
"casing_extend1": { "type": "number", "description": "外伸长度 1" },
"casing_extend2": { "type": "number", "description": "外伸长度 2" },
"material": { "type": "string", "description": "套管材质" },
"pipe_type": { "type": "string", "description": "套管类型" },
"ring_od": { "type": "string", "description": "翼环外径" },
"ring_count": { "type": "integer", "description": "翼环数量" }
},
"required": [ "host" ]
})");
}
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, "host")) return errJson(kErrBadParam, L"缺少 host(母体板的 handle)");
// 局部副本:穿管开洞(source)要往里补缺省直径
nlohmann::json a(args);
const std::string sHost = a["host"].get<std::string>();
AcDbEntity* pEnt = openByHandle(sHost, AcDb::kForWrite);
if (pEnt == NULL)
return errJson(kErrNotFound, L"按 handle 打不开母体:" + utf8ToWstr(sHost));
// 母体:板(Z 方向开洞)、墙(沿墙厚方向)或任意 AecDbBDPipeBase
// (异形构筑物/柱/梁/堰/设备)——后几种没有"厚度"概念,必须给 position/direction
AecDbXSlab* pSlab = AecDbXSlab::cast(pEnt);
AecDbXWall* pWall = (pSlab == NULL) ? AecDbXWall::cast(pEnt) : NULL;
AecDbBDPipeBase* pBase = AecDbBDPipeBase::cast(pEnt);
if (pBase == NULL)
{
const std::string sCls = classNameOf(pEnt);
pEnt->close();
return errJson(kErrWrongType, L"handle " + utf8ToWstr(sHost) + L" 是 " +
utf8ToWstr(sCls) + L",不能开洞(需要板/墙/异形构筑物等 AecDbBDPipeBase 派生实体)");
}
// 洞口中心:没给就用母体中心(板=板中心,墙=基线中点)
AcGePoint3d ptCen(0.0, 0.0, 0.0);
const bool bHasPos = readPoint(a, "position", ptCen);
AcGeVector3d vtDir = AcGeVector3d::kZAxis; // 洞口法向 = 深度方向
double dDepth = numOr(a, "depth", 0.0);
WallData wd;
::memset(&wd, 0, sizeof(WallData));
if (pWall != NULL)
pWall->GetWallData(wd);
if (pSlab != NULL)
{
if (!bHasPos)
pSlab->ClosestPointToCen(ptCen);
if (dDepth <= 0.0)
dDepth = pSlab->GetThick();
if (dDepth <= 0.0)
{
pEnt->close();
return errJson(kErrBadParam, L"板厚取不到,请显式给 depth");
}
}
else if (pWall != NULL)
{
if (dDepth <= 0.0)
dDepth = wd.dThick;
if (dDepth <= 0.0)
{
pEnt->close();
return errJson(kErrBadParam, L"墙厚取不到,请显式给 depth");
}
if (!bHasPos)
{
// 墙基线中点 + 墙底标高 + 半高
WallCenter(pWall, ptCen);
ptCen.z = wd.dBomEl + 0.5 * wd.dH;
}
// 法向 = 墙厚方向;给了 direction 就用给的
AcGePoint3d ptGiven;
if (readPoint(a, "direction", ptGiven))
vtDir = ptGiven.asVector();
else if (!WallNormalAt(pWall, ptCen, vtDir))
vtDir = AcGeVector3d::kZAxis;
}
else
{
// 其它 AecDbBDPipeBase(异形构筑物/柱/梁/堰…):没有"厚度",得靠参数
AcGePoint3d ptGiven;
if (readPoint(a, "direction", ptGiven) && ptGiven.asVector().length() > 1e-9)
vtDir = ptGiven.asVector();
else
{
pEnt->close();
return errJson(kErrBadParam, L"这种母体没有固定厚度/法向,请给 direction(洞口法向)");
}
if (!bHasPos)
{
AcGePoint3d ptMin, ptMax;
if (!entityBBox(pEnt, ptMin, ptMax))
{
pEnt->close();
return errJson(kErrBadParam, L"取不到母体中心,请给 position");
}
ptCen = AcGePoint3d(0.5 * (ptMin.x + ptMax.x), 0.5 * (ptMin.y + ptMax.y), 0.5 * (ptMin.z + ptMax.z));
}
// 洞深:没给就用母体沿法向的尺寸(打穿整个实体)
if (dDepth <= 0.0)
HostSizeAlong(pBase, vtDir, dDepth);
if (dDepth <= 0.0)
{
pEnt->close();
return errJson(kErrBadParam, L"算不出洞深,请给 depth");
}
}
// 命令里是把中心挪到厚度中点。
// 但穿管开洞(PL_YXCG)例外:命令直接用管线与实体交点的中点当洞心,
// 这时 position 给的就是"洞心",不能再挪半深,否则洞会贴在面上。
AcGeVector3d vtUnit = vtDir;
vtUnit.normalize();
if (!hasKey(a, "source"))
ptCen += vtUnit * 0.5 * dDepth;
// 穿管开洞(PL_YXCG):source 给管线 handle,洞口中心取"管线轴线与母体中面"的交点,
// 直径缺省取管道外径(PIPE_OD 已是图纸单位)。
// shortcut: 这里只对圆管道自动取外径,风管/桥架请自己给 diameter 或 width+height。
std::string sSrc;
double dSrcDia = 0.0;
if (hasKey(a, "source"))
{
const std::string sIn = a["source"].get<std::string>();
AcDbEntity* pSrc = openByHandle(sIn, AcDb::kForRead);
AcDbCurve* pCur = (pSrc != NULL) ? AcDbCurve::cast(pSrc) : NULL;
if (pCur == NULL)
{
if (pSrc != NULL) pSrc->close();
pEnt->close();
return errJson(kErrWrongType, L"source 必须是曲线类管线(管道/风管/桥架),handle=" + utf8ToWstr(sIn));
}
AcGePoint3d ptS(0.0, 0.0, 0.0), ptE(0.0, 0.0, 0.0);
pCur->getStartPoint(ptS);
pCur->getEndPoint(ptE);
AecDbEwPipe* pPipe = AecDbEwPipe::cast(pSrc);
if (pPipe != NULL)
{
const AecDbObjXDataMap* pXPipe = pPipe->GetSpecialtyData(AcDb::kForRead);
if (pXPipe != NULL)
{
double dOd = 0.0, dDn = 0.0;
pXPipe->GetAt(PIPE_OD, dOd);
pXPipe->GetAt(PIPE_DN, dDn);
dSrcDia = (dOd > 0.0) ? dOd : dDn; // 没写外径就用公称直径
}
}
// 轴线与"过 ptCen、法向 vtUnit"的平面求交
const AcGeVector3d vtAxis = ptE - ptS;
const double dDen = vtAxis.dotProduct(vtUnit);
if (fabs(dDen) > 1e-9)
ptCen = ptS + vtAxis * ((ptCen - ptS).dotProduct(vtUnit) / dDen);
sSrc = sIn;
pSrc->close();
if (dSrcDia > 0.0 && !hasKey(a, "diameter") && !hasKey(a, "width"))
a["diameter"] = dSrcDia;
}
// 套管:给了 casing / casing_length / casing_wall_thickness 就走 SetAsPipe 那条
const bool bCasing = hasKey(a, "casing")
? (intOr(a, "casing", 0) != 0)
: (hasKey(a, "casing_length") || hasKey(a, "casing_wall_thickness"));
std::wstring err;
std::string sFail;
// 单个洞/套管:阵列时同一套参数只换中心
auto makeOne = [&](const AcGePoint3d& pt) -> std::string
{
AecDbX3dPartEntity* pPart = new AecDbX3dPartEntity();
const bool bApplied = bCasing
? ApplyCasing(pPart, pt, vtDir, dDepth, a, true, err)
: ApplyHole(pPart, pEnt, pt, dDepth, vtDir, a, true, err);
if (!bApplied)
{
delete pPart;
return std::string();
}
// 母体关联 + 中心/法向:update 与"洞↔套管互转"都要用
pPart->GetSpecialtyData()->SetAt(KEY_ASSOIC, pEnt->objectId());
pPart->GetSpecialtyData()->SetAt(kHoleCenKey, pt);
pPart->GetSpecialtyData()->SetAt(kHoleDirKey, vtDir);
// 图层:缺省 洞=LAY_HOLE / 套管=LAY_CASEPIPE,与命令一致
std::wstring sLay;
if (hasKey(a, "layer"))
sLay = wstrOr(a, "layer", L"");
if (sLay.empty())
sLay = bCasing ? L"LAY_CASEPIPE" : L"LAY_HOLE";
AcDbObjectId idLay = KTSetting::InitLayer(sLay.c_str()); // 图层不存在就建,与命令一致
if (!idLay.isNull())
pPart->setLayer(idLay);
AcDbObjectId idHole;
Acad::ErrorStatus es = addToSpace(pPart, &idHole);
if (es != Acad::eOk)
{
delete pPart;
sFail = "加入模型空间失败,err=" + std::to_string((int)es);
return std::string();
}
// 与母体建立关联:持久反应器 + 母体的关联件表(绘制时母体据此做布尔)
pPart->addPersistentReactor(pEnt->objectId());
pBase->AppendAssocPart(idHole);
pBase->ReCreateDraw3d(); // 母体 3D 缓存要重建(命令里是 regen)
pPart->close();
return handleOfId(idHole);
};
// 阵列开洞(PL_ZLKD):rows/columns + spacing_x(墙=沿墙、板=X)/ spacing_y(墙=竖向、板=Y)
int nRows = intOr(a, "rows", 1);
int nCols = intOr(a, "columns", 1);
if (nRows < 1) nRows = 1;
if (nCols < 1) nCols = 1;
const double dSpX = numOr(a, "spacing_x", 0.0);
const double dSpY = numOr(a, "spacing_y", 0.0);
AcGeVector3d vtColDir = AcGeVector3d::kXAxis;
AcGeVector3d vtRowDir = AcGeVector3d::kYAxis;
if (pWall != NULL)
{
// 墙:列沿墙方向(法向绕 Z 转 -90°),行沿 +Z
if (WallNormalAt(pWall, ptCen, vtColDir))
vtColDir.rotateBy(-PI / 2.0, AcGeVector3d::kZAxis);
else
vtColDir = AcGeVector3d::kXAxis;
vtRowDir = AcGeVector3d::kZAxis;
}
nlohmann::json arHandles = nlohmann::json::array();
for (int i = 0; i < nRows; ++i)
{
for (int j = 0; j < nCols; ++j)
{
const AcGePoint3d pt = ptCen + vtColDir * (j * dSpX) + vtRowDir * (i * dSpY);
const std::string sH = makeOne(pt);
if (sH.empty())
{
pEnt->close();
return errJson(kErrBadParam, !err.empty() ? err : utf8ToWstr(sFail));
}
arHandles.push_back(sH);
}
}
pEnt->close();
nlohmann::json body;
body["type"] = bCasing ? "casing" : "opening";
body["host"] = sHost;
body["handle"] = arHandles[0];
body["count"] = (int)arHandles.size();
body["handles"] = arHandles;
if (!sSrc.empty())
{
body["source"] = sSrc;
if (dSrcDia > 0.0) body["source_diameter"] = dSrcDia;
}
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolAddOpening, AddAecOpeningTool)
class ToolUpdateOpening : public mcp_Tool
{
public:
std::string private_name() const override { return "update_aec_opening"; }
std::string private_description() const override
{
return "修改已有洞口或套管:改形状/直径/宽高/深度/管嘴/编号/用途;也可以 casing=true/false 在洞口与套管之间切换。改尺寸会重建几何。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"handle": { "type": "string", "description": "洞口实体的 handle" },
"shape": { "type": "string", "enum": [ "circle", "rect" ] },
"position": { "type": "array", "items": { "type": "number" }, "description": "新中心(可选)" },
"diameter": { "type": "number" }, "width": { "type": "number" },
"height": { "type": "number" }, "depth": { "type": "number" },
"nozzle": { "type": "boolean" }, "no": { "type": "string" },
"for_use": { "type": "string" },
"casing": { "type": "boolean", "description": "true=改成套管 / false=改回洞口(缺省保持原样)" },
"casing_length": { "type": "number" }, "casing_wall_thickness": { "type": "number" },
"casing_extend": { "type": "boolean" }, "casing_extend1": { "type": "number" },
"casing_extend2": { "type": "number" }, "material": { "type": "string" },
"pipe_type": { "type": "string" }, "ring_od": { "type": "string" },
"ring_count": { "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"拿不到图纸写锁(有别的命令在跑?)");
nlohmann::json err;
AecDbX3dPartEntity* pPart = openTyped<AecDbX3dPartEntity>(args, L"AecDbX3dPartEntity", err);
if (pPart == NULL) return err;
// 母体与中心/法向:都从创建时记下的数据里取,拿不到再按母体重算
AcDbObjectId idHost;
pPart->GetSpecialtyData()->GetAt(KEY_ASSOIC, idHost);
AcDbEntity* pHost = NULL;
if (!idHost.isNull())
acdbOpenObject(pHost, idHost, AcDb::kForRead);
AecDbXSlab* pSlab = (pHost != NULL) ? AecDbXSlab::cast(pHost) : NULL;
AecDbXWall* pWall = (pSlab == NULL && pHost != NULL) ? AecDbXWall::cast(pHost) : NULL;
AcGePoint3d ptCen(0.0, 0.0, 0.0);
if (!readPoint(args, "position", ptCen) &&
!pPart->GetSpecialtyData()->GetAt(kHoleCenKey, ptCen))
{
if (pSlab != NULL) pSlab->ClosestPointToCen(ptCen);
else if (pWall != NULL) WallCenter(pWall, ptCen);
}
// 现在是不是套管:按实体名字判断(SetAsPipe 写的是"套管")
const CString sCurName = pPart->GetName();
const bool bCasingNow = (sCurName.CompareNoCase(_T("套管")) == 0);
const bool bCasing = hasKey(args, "casing") ? (intOr(args, "casing", 0) != 0) : bCasingNow;
double dDepth = numOr(args, "depth", 0.0);
if (dDepth <= 0.0 && pSlab != NULL)
dDepth = pSlab->GetThick();
if (dDepth <= 0.0 && pWall != NULL)
{
WallData wd;
::memset(&wd, 0, sizeof(WallData));
pWall->GetWallData(wd);
dDepth = wd.dThick;
}
if (dDepth <= 0.0)
pPart->GetSpecialtyData()->GetAt(_T("m_dT"), dDepth); // 洞口建的时候存的深度
if (dDepth <= 0.0 && !bCasing) // 套管用 casing_length,不依赖 depth
{
if (pHost != NULL) pHost->close();
pPart->close();
return errJson(kErrBadParam, L"取不到深度,请显式给 depth");
}
// 法向:优先用创建时记下的;没有就按母体重算(墙=墙厚方向,板=Z)
AcGeVector3d vtDir = AcGeVector3d::kZAxis;
AcGePoint3d ptDirGiven;
if (readPoint(args, "direction", ptDirGiven))
vtDir = ptDirGiven.asVector();
else if (!pPart->GetSpecialtyData()->GetAt(kHoleDirKey, vtDir))
{
if (pWall != NULL && !WallNormalAt(pWall, ptCen, vtDir))
vtDir = AcGeVector3d::kZAxis;
}
std::wstring msg;
const bool bApplied = bCasing
? ApplyCasing(pPart, ptCen, vtDir, dDepth, args, false, msg)
: ApplyHole(pPart, pHost, ptCen, dDepth, vtDir, args, false, msg);
if (!bApplied)
{
if (pHost != NULL) pHost->close();
pPart->close();
return errJson(kErrBadParam, msg);
}
// 洞 / 套管 切换后图层要跟着换(命令里就是 LAY_HOLE / LAY_CASEPIPE)
if (!hasKey(args, "layer"))
{
AcDbObjectId idLay = KTSetting::InitLayer(bCasing ? L"LAY_CASEPIPE" : L"LAY_HOLE");
if (!idLay.isNull()) pPart->setLayer(idLay);
}
if (pHost != NULL) pHost->close();
return finishUpdate(pPart, args["handle"].get<std::string>(), bCasing ? "casing" : "opening");
}
};
EXPORT_MCP_TOOL(ToolUpdateOpening, UpdateAecOpeningTool)
class ToolAddWell : public mcp_Tool
{
public:
std::string private_name() const override { return "add_aec_well"; }
std::string private_description() const override
{
return "在池底板上加井(对应菜单命令 方井 PL_JFZJ / 圆井 PL_JYZJ / 方圆井 PL_JFYJ / "
"异形井 PL_JYXJ / 集水坑 PL_JJSK):给母体板 handle、井位与尺寸,内部先开洞再叠井体"
"(AecDbX3dPartEntity),自动挂到板下(LAY_HOLE)。"
"shape=rect 方井 / circle 圆井 / rect_circle 方圆井 / polygon 异形井 / sump 集水坑。"
"集水坑在方井基础上多一个放坡角 angle_deg(缺省 22.5°,0~75)与板厚 slab_thickness。尺寸用图纸单位。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"host": { "type": "string", "description": "母体板(池底板)的 handle" },
"shape": { "type": "string", "enum": [ "rect", "circle", "rect_circle", "polygon", "sump" ],
"description": "rect=方井(缺省,上口矩形/下口矩形)/ circle=圆井 / rect_circle=方圆井(上口矩形、下口圆)/ polygon=异形井(上下口点串)/ sump=集水坑(矩形井 + 放坡角)" },
"position": { "type": "array", "items": { "type": "number" }, "description": "井位 [x,y]" },
"length": { "type": "number", "description": "上口长(X 向,rect / rect_circle)" },
"width": { "type": "number", "description": "上口宽(Y 向,rect / rect_circle)" },
"bottom_length": { "type": "number", "description": "下口长(缺省=length,rect)" },
"bottom_width": { "type": "number", "description": "下口宽(缺省=width,rect)" },
"diameter": { "type": "number", "description": "圆井直径(circle)/ 方圆井下口圆直径(rect_circle)" },
"bottom_diameter": { "type": "number", "description": "圆井下口直径(缺省=diameter,circle)" },
"top_points": { "type": "array", "items": { "type": "array", "items": { "type": "number" } },
"description": "异形井上口轮廓 [[x,y],...](polygon,至少 3 点)" },
"bottom_points": { "type": "array", "items": { "type": "array", "items": { "type": "number" } },
"description": "异形井下口轮廓 [[x,y],...](polygon,至少 3 点)" },
"bulges": { "type": "array", "items": { "type": "number" }, "description": "异形井轮廓的凸度(可选)" },
"angle_deg": { "type": "number", "description": "集水坑放坡角(度,0~75),缺省 22.5" },
"height": { "type": "number", "description": "井深" },
"wall_thickness": { "type": "number", "description": "井壁厚" },
"slab_thickness": { "type": "number", "description": "板厚(缺省取母体板厚)" },
"offset_x": { "type": "number", "description": "井心偏移 X(缺省 0)" },
"offset_y": { "type": "number", "description": "井心偏移 Y(缺省 0;方圆井只用 X)" },
"no": { "type": "string", "description": "井编号" },
"remark": { "type": "string", "description": "备注(方圆井/方井/圆井)" },
"purpose": { "type": "string", "description": "用途(异形井)" },
"layer": { "type": "string", "description": "图层,缺省 LAY_HOLE" }
},
"required": [ "host", "position", "height", "wall_thickness" ]
})");
}
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, "host")) return errJson(kErrBadParam, L"缺少 host(母体板的 handle)");
const std::string sHost = args["host"].get<std::string>();
AcDbEntity* pEnt = openByHandle(sHost, AcDb::kForWrite);
if (pEnt == NULL)
return errJson(kErrNotFound, L"按 handle 打不开母体:" + utf8ToWstr(sHost));
AecDbXSlab* pSlab = AecDbXSlab::cast(pEnt);
if (pSlab == NULL)
{
const std::string sCls = classNameOf(pEnt);
pEnt->close();
return errJson(kErrWrongType, L"handle " + utf8ToWstr(sHost) + L" 是 " +
utf8ToWstr(sCls) + L",不是板(AecDbXSlab)");
}
WellShape eShape = kWellRect;
std::wstring err;
if (!ParseWellShape(args, eShape, err))
{
pSlab->close();
return errJson(kErrBadParam, err);
}
AcGePoint3d ptWellOrg;
if (!readPoint(args, "position", ptWellOrg))
{
pSlab->close();
return errJson(kErrBadParam, L"缺少 position(井位 [x,y])");
}
// 板底标高与板厚:命令里用 GetBasinSlabData
PoolData data;
::memset(&data, 0, sizeof(PoolData));
double dSlabT = numOr(args, "slab_thickness", pSlab->GetThick());
double dSlabBomEl = ptWellOrg.z;
if (pSlab->GetBasinSlabData(data))
{
if (!hasKey(args, "slab_thickness") && data.m_dBomThick > 1e-9)
dSlabT = data.m_dBomThick;
dSlabBomEl = data.m_dBomEl;
}
ptWellOrg.z = dSlabBomEl;
AecDbX3dPartEntity* pPart = new AecDbX3dPartEntity();
if (!ApplyWell(pPart, eShape, ptWellOrg, dSlabT, dSlabBomEl, args, true, err))
{
delete pPart;
pSlab->close();
return errJson(kErrBadParam, err);
}
if (hasKey(args, "layer"))
{
const std::wstring sLay = wstrOr(args, "layer", L"");
if (!sLay.empty())
{
AcDbObjectId idLay = layerIdByName(sLay);
if (!idLay.isNull()) pPart->setLayer(idLay);
}
}
else
{
AcDbObjectId idLay = layerIdByName(L"LAY_HOLE");
if (!idLay.isNull()) pPart->setLayer(idLay);
}
pPart->GetSpecialtyData()->SetAt(KEY_ASSOIC, pSlab->objectId());
AcDbObjectId idWell;
Acad::ErrorStatus es = addToSpace(pPart, &idWell);
if (es != Acad::eOk)
{
delete pPart;
pSlab->close();
return errJson(kErrInternal, std::wstring(L"加入模型空间失败,err=") + std::to_wstring((int)es));
}
pPart->addPersistentReactor(pSlab->objectId());
pSlab->AppendAssocPart(idWell);
pPart->close();
pSlab->close();
nlohmann::json body;
body["type"] = "well";
body["handle"] = handleOfId(idWell);
body["host"] = sHost;
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolAddWell, AddAecWellTool)
class ToolUpdateWell : public mcp_Tool
{
public:
std::string private_name() const override { return "update_aec_well"; }
std::string private_description() const override
{
return "修改已有井(方井/圆井/方圆井/异形井/集水坑):只传要改的字段。"
"按实体自身的类型重建井体(对应 DBL_AEC_X3DPART 里各井的修改分支),"
"改完会顺带刷新母体板的 3D。异形井不给 top_points/bottom_points 就沿用原截面。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"handle": { "type": "string", "description": "井实体的 handle" },
"position": { "type": "array", "items": { "type": "number" }, "description": "新井位 [x,y](可选,z 自动按板底标高)" },
"length": { "type": "number" }, "width": { "type": "number" },
"bottom_length": { "type": "number" }, "bottom_width": { "type": "number" },
"diameter": { "type": "number" }, "bottom_diameter": { "type": "number" },
"top_points": { "type": "array", "items": { "type": "array", "items": { "type": "number" } } },
"bottom_points": { "type": "array", "items": { "type": "array", "items": { "type": "number" } } },
"bulges": { "type": "array", "items": { "type": "number" } },
"height": { "type": "number" }, "wall_thickness": { "type": "number" },
"slab_thickness": { "type": "number" },
"angle_deg": { "type": "number", "description": "集水坑放坡角(度,0~75)" },
"offset_x": { "type": "number" }, "offset_y": { "type": "number" },
"no": { "type": "string" }, "remark": { "type": "string" }, "purpose": { "type": "string" }
},
"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"拿不到图纸写锁(有别的命令在跑?)");
nlohmann::json err;
AecDbX3dPartEntity* pPart = openTyped<AecDbX3dPartEntity>(args, L"AecDbX3dPartEntity", err);
if (pPart == NULL) return err;
// 类型由实体自己的名字决定(SetAsWell 写进去的)
const CString sName = pPart->GetName();
WellShape eShape = kWellRect;
if (!ShapeFromWellName(sName, eShape))
{
pPart->close();
return errJson(kErrWrongType, L"这个实体不是井(名字:" + utf8ToWstr(acharToUtf8(sName)) + L")");
}
// 井心:命令里是 GetOrgPoint(true),并先 ResetMove()
AcGePoint3d ptWellOrg = pPart->GetOrgPoint(true);
pPart->ResetMove();
if (readPoint(args, "position", ptWellOrg))
{
// 用户给了新位置,z 稍后用板底标高覆盖
}
// 母体板:拿板厚与板底标高(跟命令一致)
AcDbObjectId idHost;
pPart->GetSpecialtyData()->GetAt(KEY_ASSOIC, idHost);
AecDbXSlab* pSlabR = NULL;
AecDbXSlab* pSlabW = NULL;
PoolData data;
::memset(&data, 0, sizeof(PoolData));
double dSlabT = numOr(args, "slab_thickness", 0.0);
if (!idHost.isNull())
{
if (acdbOpenObject(pSlabR, idHost, AcDb::kForRead) == Acad::eOk && pSlabR != NULL)
{
if (pSlabR->GetBasinSlabData(data))
{
ptWellOrg.z = data.m_dBomEl;
if (dSlabT <= 0.0) dSlabT = data.m_dBomThick;
}
}
}
if (dSlabT <= 0.0)
{
// 板读不到就退回实体存的板厚(SetAsWell 会写 dBomt/dBomT/dBomThick)
pPart->GetSpecialtyData()->GetAt(_T("dBomt"), dSlabT);
if (dSlabT <= 0.0) pPart->GetSpecialtyData()->GetAt(_T("dBomT"), dSlabT);
if (dSlabT <= 0.0) pPart->GetSpecialtyData()->GetAt(_T("dBomThick"), dSlabT);
}
std::wstring msg;
if (!ApplyWell(pPart, eShape, ptWellOrg, dSlabT, ptWellOrg.z, args, false, msg))
{
if (pSlabR != NULL) pSlabR->close();
pPart->close();
return errJson(kErrBadParam, msg);
}
// 井变了,母体板的洞也要重画(命令里是 ReCreateDraw3d)
if (!idHost.isNull() && acdbOpenObject(pSlabW, idHost, AcDb::kForWrite) == Acad::eOk && pSlabW != NULL)
{
pSlabW->ReCreateDraw3d();
pSlabW->close();
}
if (pSlabR != NULL) pSlabR->close();
return finishUpdate(pPart, args["handle"].get<std::string>(), "well");
}
};
EXPORT_MCP_TOOL(ToolUpdateWell, UpdateAecWellTool)