feat: cad_mcp_aec 增加平板开洞工具(add/update_aec_opening)

对应菜单命令 PL_CDKD 平板开洞(Pool3dCmd/pool3dcmdCmd.cpp:14763)。
洞不是普通实体,是"长在母体上的 part",所以工具收 host(板的 handle):

- add_aec_opening:host + shape(circle/rect) + position(缺省取板中心)
  + diameter 或 width/height + depth(缺省取板厚)+ nozzle/no/for_use/layer。
  内部照命令的序列:new AecDbX3dPartEntity -> SetAsHole(中心,尺寸,板厚,法向)
  -> KEY_ASSOIC 记母体 -> LAY_HOLE 图层 -> 入库 -> 给母体 addPersistentReactor
  + AppendAssocPart(绘制时母体据此做布尔)。
- update_aec_opening:改形状/直径/宽高/深度/管嘴/编号/用途;改尺寸会重建几何。
  洞口中心在创建时另存一个 AECMCP_HOLE_CEN 键(SetAsHole 自己会把 KEY_NOPOS 挪到侧边),
  更新时就靠它 / 或母体中心兜底。
- query_aec_entities / get_aec_entity_detail 支持 opening(读回 host/no/for_use/position)。

实测(AutoCAD 2020 / R230 Debug,pcf_1.dwg):
- tools/list = 46;
- 先 add_aec_slab 建 6x5m/厚200 的板(3A0),再 add_aec_opening(DN600 圆洞, 中心 2000,2000)
  返回洞口 3A1、host=3A0;
- get_aec_entity_detail 读回 host=3A0 / no=H-1 / position=(2000,2000,100)
  (z=100 正是板厚中点,说明命令里那步"挪到厚度中点"生效);
- update_aec_opening 改直径成功;query(type=opening) 命中 1 条。

限制(写进文档):目前只支持平板 + 圆/矩形洞;墙体开洞(母体是墙)
和异形洞(点串脚本格式)还没接。
This commit is contained in:
company-desktop committed 2026-10-30 08:48:02 +08:00
1 parent 2704a663ae
commit 3013ceb4f7
3 files changed
+448 -2

No files matched your search

@@ -24,11 +24,31 @@
#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
{
@@ -1385,3 +1405,276 @@ public:
}
};
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 复用
bool ApplyHole(AecDbX3dPartEntity* pPart, AecDbXSlab* pSlab,
const AcGePoint3d& ptCen, double dDepth, const nlohmann::json& a, bool bCreate, std::wstring& err)
{
// 形状:circle / rect
int nShape = 0; // 0 圆 1 矩形
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
{
err = L"shape 只支持 circle / rect(异形洞的点串格式还没接)";
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 = AcGeVector3d::kZAxis;
vtZ.normalize();
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, ptCen);
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, pSlab->objectId());
}
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):给母体板的 handle 与洞口参数,"
"生成 AecDbX3dPartEntity 并自动与板做布尔、挂到板下(LAY_HOLE 图层)。"
"尺寸用图纸单位;depth 缺省取板厚。返回洞口实体的 handle。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"host": { "type": "string", "description": "母体板的 handle(AecDbXSlab)" },
"shape": { "type": "string", "enum": [ "circle", "rect" ], "description": "洞口形状,缺省 circle" },
"position": { "type": "array", "items": { "type": "number" },
"description": "洞口中心 [x,y] 或 [x,y,z];不给则用板中心" },
"diameter": { "type": "number", "description": "圆洞直径,图纸单位" },
"width": { "type": "number", "description": "矩形洞宽(X 向)" },
"height": { "type": "number", "description": "矩形洞高(Y 向)" },
"depth": { "type": "number", "description": "洞深,缺省取板厚" },
"nozzle": { "type": "boolean", "description": "是否要管嘴(缺省否)" },
"no": { "type": "string", "description": "洞口编号" },
"for_use":{ "type": "string", "description": "洞口用途" },
"layer": { "type": "string", "description": "图层,缺省 LAY_HOLE" }
},
"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)");
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)");
}
// 洞口中心:没给就用板中心
AcGePoint3d ptCen(0.0, 0.0, 0.0);
if (!readPoint(args, "position", ptCen))
pSlab->ClosestPointToCen(ptCen);
const double dThick = numOr(args, "depth", pSlab->GetThick());
if (dThick <= 0.0)
{
pSlab->close();
return errJson(kErrBadParam, L"板厚取不到,请显式给 depth");
}
// 命令里是把中心挪到板厚中点
ptCen += AcGeVector3d::kZAxis * 0.5 * dThick;
AecDbX3dPartEntity* pPart = new AecDbX3dPartEntity();
std::wstring err;
if (!ApplyHole(pPart, pSlab, ptCen, dThick, args, true, err))
{
delete pPart;
pSlab->close();
return errJson(kErrBadParam, err);
}
// 图层:缺省 LAY_HOLE,与命令一致
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);
}
AcDbObjectId idHole;
Acad::ErrorStatus es = addToSpace(pPart, &idHole);
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(idHole);
pPart->close();
pSlab->close();
nlohmann::json body;
body["type"] = "opening";
body["handle"] = handleOfId(idHole);
body["host"] = sHost;
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 "修改已有洞口:改形状/直径/宽高/深度/管嘴/编号/用途。改尺寸会重建洞的几何。";
}
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" }
},
"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);
AecDbXSlab* pSlab = NULL;
if (!idHost.isNull())
acdbOpenObject(pSlab, idHost, AcDb::kForRead);
AcGePoint3d ptCen(0.0, 0.0, 0.0);
if (!readPoint(args, "position", ptCen) &&
!pPart->GetSpecialtyData()->GetAt(kHoleCenKey, ptCen))
{
if (pSlab != NULL) pSlab->ClosestPointToCen(ptCen);
}
double dDepth = numOr(args, "depth", 0.0);
if (dDepth <= 0.0 && pSlab != NULL)
dDepth = pSlab->GetThick();
if (dDepth <= 0.0)
{
if (pSlab != NULL) pSlab->close();
pPart->close();
return errJson(kErrBadParam, L"取不到深度,请显式给 depth");
}
std::wstring msg;
if (!ApplyHole(pPart, pSlab, ptCen, dDepth, args, false, msg))
{
if (pSlab != NULL) pSlab->close();
pPart->close();
return errJson(kErrBadParam, msg);
}
if (pSlab != NULL) pSlab->close();
return finishUpdate(pPart, args["handle"].get<std::string>(), "opening");
}
};
EXPORT_MCP_TOOL(ToolUpdateOpening, UpdateAecOpeningTool)
@@ -19,6 +19,7 @@
#include "AecDbBeam.h"
#include "AecDbClimbStair.h"
#include "AecDbEwFitting.h"
#include "AecDbX3dPartEntity.h"
#include "AecDbObjXDataMap.h"
using namespace aec_mcp;
@@ -33,6 +34,7 @@ const char* kTypeColumn = "column";
const char* kTypeBeam = "beam";
const char* kTypeClimb = "climb_stair";
const char* kTypeFitting = "fitting";
const char* kTypeOpening = "opening";
// 实体 -> 五个受支持类型之一;不是这几类返回空串
std::string supportedTypeOf(AcDbEntity* pEnt)
@@ -47,6 +49,8 @@ std::string supportedTypeOf(AcDbEntity* pEnt)
if (AecDbBeam::cast(pEnt)) return kTypeBeam;
if (AecDbClimbStair::cast(pEnt)) return kTypeClimb;
if (AecDbEwFitting::cast(pEnt)) return kTypeFitting;
// 洞口/井/倒角都是这个类,靠专业数据区分;这里统一叫 opening
if (AecDbX3dPartEntity::cast(pEnt)) return kTypeOpening;
return std::string();
}
@@ -147,6 +151,31 @@ nlohmann::json paramsOf(AcDbEntity* pEnt, const std::string& sType)
p["width"] = rc.y;
p["rotation_deg"] = pCol->GetRotation() * 180.0 / PI;
}
else if (sType == kTypeOpening)
{
AecDbX3dPartEntity* pPart = AecDbX3dPartEntity::cast(pEnt);
if (pPart == NULL) return p;
CString s;
if (pPart->GetAt(KEY_NO, s)) p["no"] = acharToUtf8(s);
if (pPart->GetAt(KEY_FORUSE, s)) p["for_use"] = acharToUtf8(s);
AcGePoint3d pt;
if (pPart->GetAt(_T("AECMCP_HOLE_CEN"), pt))
p["position"] = pointJson(pt);
// 母体(洞挂在谁的下面)
AcDbObjectId idHost;
if (pPart->GetAt(KEY_ASSOIC, idHost) && !idHost.isNull())
{
AcDbObject* pObj = NULL;
if (acdbOpenObject(pObj, idHost, AcDb::kForRead) == Acad::eOk && pObj != NULL)
{
p["host"] = handleOf(pObj);
pObj->close();
}
}
}
else if (sType == kTypeFitting)
{
AecDbEwFitting* pFit = AecDbEwFitting::cast(pEnt);
@@ -266,7 +295,7 @@ public:
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"type": { "type": "string", "enum": [ "all", "pipe", "wall", "slab", "column", "beam", "climb_stair", "fitting" ],
"type": { "type": "string", "enum": [ "all", "pipe", "wall", "slab", "column", "beam", "climb_stair", "fitting", "opening" ],
"description": "要列出的类型,缺省 all" },
"space": { "type": "string", "description": "空间名:Model 或布局名,缺省 Model" },
"limit": { "type": "integer", "description": "最多返回多少条,缺省 200,上限 1000" },
+125 -1
View File
@@ -1137,7 +1137,8 @@
"column",
"beam",
"climb_stair",
"fitting"
"fitting",
"opening"
],
"description": "要列出的类型,缺省 all"
},
@@ -2065,6 +2066,129 @@
"dll_name": "cad_mcp_aec.dll",
"factory_function": "UpdateAecFittingTool"
}
},
{
"name": "add_aec_opening",
"description": "在板上开洞(对应菜单命令 平板开洞 PL_CDKD):给母体板的 handle 与洞口参数,生成洞口实体(AecDbX3dPartEntity)并自动与板做布尔、挂到板下(LAY_HOLE 图层)。尺寸用图纸单位;depth 缺省取板厚。返回洞口实体的 handle。",
"inputSchema": {
"type": "object",
"properties": {
"host": {
"type": "string",
"description": "母体板的 handle(AecDbXSlab)"
},
"shape": {
"type": "string",
"enum": [
"circle",
"rect"
],
"description": "洞口形状,缺省 circle"
},
"position": {
"type": "array",
"items": {
"type": "number"
},
"description": "洞口中心 [x,y] 或 [x,y,z];不给则用板中心"
},
"diameter": {
"type": "number",
"description": "圆洞直径"
},
"width": {
"type": "number",
"description": "矩形洞宽(X 向)"
},
"height": {
"type": "number",
"description": "矩形洞高(Y 向)"
},
"depth": {
"type": "number",
"description": "洞深,缺省取板厚"
},
"nozzle": {
"type": "boolean",
"description": "是否要管嘴(缺省否)"
},
"no": {
"type": "string",
"description": "洞口编号"
},
"for_use": {
"type": "string",
"description": "洞口用途"
},
"layer": {
"type": "string",
"description": "图层,缺省 LAY_HOLE"
}
},
"required": [
"host"
]
},
"backend": {
"type": "c++_dll",
"dll_name": "cad_mcp_aec.dll",
"factory_function": "AddAecOpeningTool"
}
},
{
"name": "update_aec_opening",
"description": "修改已有洞口:改形状/直径/宽高/深度/管嘴/编号/用途,改尺寸会重建洞的几何。",
"inputSchema": {
"type": "object",
"properties": {
"handle": {
"type": "string"
},
"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"
}
},
"required": [
"handle"
]
},
"backend": {
"type": "c++_dll",
"dll_name": "cad_mcp_aec.dll",
"factory_function": "UpdateAecOpeningTool"
}
}
]
}