Files
company-desktop 22d9ce2af6 feat: 多段楼梯(PL_DDLT)+ 立管固架(PL_LGGJ);老虎窗卡住未做
一、多段楼梯 AecDbMRPLineStair
CMRLTDlg 填 MRPLineStair(各跑踏步数在 nSteeps[20]),两点定基准曲线。
add/update_aec_mrp_stair:add 给踏步高宽 + steep_counts 数组(最多 20 跑)+ 梯段宽/底标高/
平台/扶手;update 收全套(加强筋、侧梁、扶手栏杆截面与形状、材质)。

二、立管固架 AecDbHolder(PL_LGGJ)
选立管 + 选墙,位置按命令算法算:立管中点投影到墙中心线 -> 沿墙切向挪 (管外径+角钢宽)/2
-> 再沿墙法向挪半墙厚;底标高缺省 = min(管两端 z) + 管长/3。
add_aec_vertical_pipe_holder(pipe, wall, ...),参数同 XHolder,改参数用现成的 update_aec_holder。

三、老虎窗 AecDbXDormerWindow(PL_JLHC)—— 卡住,没做出来
- 几何算对了:把屋顶 ExploreToFaces 分解成面,挑"XY 内含拾取点、法向倾斜、z 最高"的坡屋面
  (dry_run 实测底面点 (4000,11000,4732)、朝向 (0,-0.866,0.5))。顺带发现命令用的
  AecDbXRoof::HitGetPlane 是按分解顺序取第一个含点的面,常常先命中水平底板,不能照抄。
- 但 new AecDbXDormerWindow() 之后一调 SetDormerPar 就把 CAD 带崩(MCP 连接直接断)。
  同一段代码在命令里由 CJLHCDlg + CDormerJig 驱动、在 jig 拖拽绘制过程中执行。
  查过 SetDormerPar 只是把参数转成 XDormerPar 交给 impl(图层创建那段源码里是注释掉的),
  所以崩点应在 XDormerWindow 实现里。
- 为了不发出会崩 CAD 的工具,这两个工具先撤掉了;配置里没有它们。

实测(R230 Debug,空图):
- 多段楼梯(8/6/9 级、踏步 160/250、梯段宽 1200)-> 311,bbox 0~8600 x -1200~0;
  改成 8/8/8 + 侧梁 80 回读正确
- 立管固架(墙 8000x4000 厚 240、立管 DN100 从 -500 到 3500)-> 位置 (3025,120,833),
  正好等于"墙厚中点 + 半个(外径+角钢宽)"和"管底 + 管长/3"

工具总数 92 -> 95(本插件 64 -> 67),R230/Z2026 都编过。
2026-10-31 10:48:07 +08:00

549 lines
22 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_extra_tools.cpp
// cad_mcp_aec 的补遗工具(工作计划第 6 项收尾)
// * add/update_aec_mrp_stair 多段楼梯 AecDbMRPLineStair(PL_DDLT)
// * add_aec_vertical_pipe_holder 立管固架 AecDbHolder(PL_LGGJ)
// * add/update_aec_dormer_window 老虎窗 AecDbXDormerWindow(PL_JLHC)
//
// 三个命令的出处:
// PL_DDLT Pool3dCmd/pool3dcmdCmd.cpp:5841 CMRLTDlg + MRPLineStair + SetBaseCurve(两点)
// PL_LGGJ Pool3dCmd/pool3dcmdCmd.cpp:12121 选立管 + 选墙,算位置后建一个 AecDbHolder
// PL_JLHC Pool3dCmd/pool3dcmdCmd.cpp:5194 选屋顶 + CDormerJig 点插入点,
// jig 的 update() 用 AecDbXRoof::HitGetPlane 算出屋面点与朝向
// ============================================================================
#include "StdAfx.h"
#include "KTCad.h"
#include "aec_mcp_common.h"
#include "mcp_plugin_api.h"
#include "AecDbMRPLineStair.h"
#include "AecDbHolder.h"
#include "AecDbEwPipe.h"
#include "AecDbWall.h"
#include "AecDbObjXDataMap.h"
#include "CoreTools.h"
#include "KTSetting.h"
using namespace aec_mcp;
namespace
{
std::string HandleOfId(const AcDbObjectId& id)
{
if (id.isNull()) return std::string();
AcDbObject* pObj = NULL;
if (Acad::eOk != acdbOpenObject(pObj, id, AcDb::kForRead)) return std::string();
const std::string s = handleOf(pObj);
pObj->close();
return s;
}
// 两个平面求交线(AcGeLine3d)
bool PlaneIntersectPlane(const AcGePlane& p1, const AcGePlane& p2, AcGeLine3d& ln)
{
AcGeVector3d vtDir = p1.normal().crossProduct(p2.normal());
if (vtDir.length() < 1e-9) return false; // 平行
vtDir.normalize();
// 解 n1·x = d1, n2·x = d2(n1、n2 单位向量):x = a*n1 + b*n2
const AcGeVector3d n1 = p1.normal(), n2 = p2.normal();
const AcGePoint3d pOn1 = p1.pointOnPlane();
const AcGePoint3d pOn2 = p2.pointOnPlane();
const double d1 = n1.x * pOn1.x + n1.y * pOn1.y + n1.z * pOn1.z;
const double d2 = n2.x * pOn2.x + n2.y * pOn2.y + n2.z * pOn2.z;
const double det = n1.dotProduct(n2);
const double den = 1.0 - det * det;
if (fabs(den) < 1e-12) return false;
const double a = (d1 - d2 * det) / den;
const double b = (d2 - d1 * det) / den;
const AcGePoint3d pt(n1.x * a + n2.x * b, n1.y * a + n2.y * b, n1.z * a + n2.z * b);
ln.set(pt, vtDir);
return true;
}
void CopyStr(TCHAR* pszDst, int nSize, const std::wstring& sSrc)
{
if (pszDst == NULL || nSize <= 0) return;
_tcsncpy(pszDst, sSrc.c_str(), nSize - 1);
pszDst[nSize - 1] = 0;
}
}
// ===========================================================================
// 多段楼梯(PL_DDLT,AecDbMRPLineStair)
// ===========================================================================
namespace
{
// CMRLTDlg 构造函数里的缺省值
void InitMrpStair(MRPLineStair& d)
{
::memset(&d, 0, sizeof(MRPLineStair));
d.nKind = 0;
d.dSteepW = 250.0;
d.dSteepH = 160.0;
d.dStairW = 1200.0;
d.dBomEl = 0.0;
d.nSteepSize = 2;
d.nSteeps[0] = 10;
d.nSteeps[1] = 10;
d.bHasResetPLF = TRUE;
d.dRestPtW = 1200.0;
d.dStiffnW = 250.0;
d.dStiffnH = 286.0;
d.dSbThick = 150.0;
d.bHasGarBar = TRUE;
d.nGarBarLR = 0;
d.bHasStiffn = FALSE;
d.bGBResetPLF = FALSE;
d.dSideBeamW = 60.0;
d.dSideBeamH = 60.0;
d.dBarH = 923.0;
d.dGarSize[0] = 40.0;
d.dGarSize[1] = 60.0;
d.dGarExt = 60.0;
d.nGShape = 0;
d.dBarSp = 300.0;
d.dBarSize[0] = 30.0;
d.dBarSize[1] = 30.0;
d.nBShape = 0;
CopyStr(d.szGarMat, 256, L"不锈钢");
CopyStr(d.szBarMat, 256, L"不锈钢");
}
// add 只给关键尺寸(bKeyOnly),update 收全套
void MrpStairFromArgs(MRPLineStair& d, const nlohmann::json& a, bool bMerge, bool bKeyOnly)
{
if (!bMerge) InitMrpStair(d);
if (hasKey(a, "steep_height")) d.dSteepH = numOr(a, "steep_height", d.dSteepH);
if (hasKey(a, "steep_width")) d.dSteepW = numOr(a, "steep_width", d.dSteepW);
if (hasKey(a, "stair_width")) d.dStairW = numOr(a, "stair_width", d.dStairW);
if (hasKey(a, "bottom_elevation")) d.dBomEl = numOr(a, "bottom_elevation", d.dBomEl);
if (hasKey(a, "rest_platform")) d.bHasResetPLF = intOr(a, "rest_platform", 1) ? TRUE : FALSE;
if (hasKey(a, "with_rail")) d.bHasGarBar = intOr(a, "with_rail", 1) ? TRUE : FALSE;
if (hasKey(a, "rest_platform_width")) d.dRestPtW = numOr(a, "rest_platform_width", d.dRestPtW);
if (hasKey(a, "platform_thickness")) d.dSbThick = numOr(a, "platform_thickness", d.dSbThick);
if (hasKey(a, "bar_height")) d.dBarH = numOr(a, "bar_height", d.dBarH);
if (hasKey(a, "material")) CopyStr(d.szGarMat, 256, wstrOr(a, "material", L""));
if (hasKey(a, "rail_material")) CopyStr(d.szBarMat, 256, wstrOr(a, "rail_material", L""));
// 各跑的踏步数(数组)
if (hasKey(a, "steep_counts") && a["steep_counts"].is_array())
{
int n = 0;
for (size_t i = 0; i < a["steep_counts"].size() && n < 20; ++i)
{
if (!a["steep_counts"][i].is_number()) continue;
d.nSteeps[n++] = a["steep_counts"][i].get<int>();
}
if (n > 0) d.nSteepSize = n;
}
if (bKeyOnly) return;
// ---- 精细字段 ----
if (hasKey(a, "structure_kind")) d.nKind = intOr(a, "structure_kind", d.nKind);
if (hasKey(a, "stiffener_width")) d.dStiffnW = numOr(a, "stiffener_width", d.dStiffnW);
if (hasKey(a, "stiffener_height")) d.dStiffnH = numOr(a, "stiffener_height", d.dStiffnH);
if (hasKey(a, "has_stiffener")) d.bHasStiffn = intOr(a, "has_stiffener", 0) ? TRUE : FALSE;
if (hasKey(a, "rail_side")) d.nGarBarLR = intOr(a, "rail_side", d.nGarBarLR);
if (hasKey(a, "rail_reset_platform")) d.bGBResetPLF = intOr(a, "rail_reset_platform", 0) ? TRUE : FALSE;
if (hasKey(a, "side_beam_w")) d.dSideBeamW = numOr(a, "side_beam_w", d.dSideBeamW);
if (hasKey(a, "side_beam_h")) d.dSideBeamH = numOr(a, "side_beam_h", d.dSideBeamH);
if (hasKey(a, "handrail_size_w")) d.dGarSize[0] = numOr(a, "handrail_size_w", d.dGarSize[0]);
if (hasKey(a, "handrail_size_h")) d.dGarSize[1] = numOr(a, "handrail_size_h", d.dGarSize[1]);
if (hasKey(a, "handrail_ext")) d.dGarExt = numOr(a, "handrail_ext", d.dGarExt);
if (hasKey(a, "handrail_shape")) d.nGShape = intOr(a, "handrail_shape", d.nGShape);
if (hasKey(a, "rail_space")) d.dBarSp = numOr(a, "rail_space", d.dBarSp);
if (hasKey(a, "rail_size_w")) d.dBarSize[0] = numOr(a, "rail_size_w", d.dBarSize[0]);
if (hasKey(a, "rail_size_h")) d.dBarSize[1] = numOr(a, "rail_size_h", d.dBarSize[1]);
if (hasKey(a, "rail_shape")) d.nBShape = intOr(a, "rail_shape", d.nBShape);
}
nlohmann::json MrpStairToJson(const MRPLineStair& d)
{
nlohmann::json o;
o["steep_height"] = d.dSteepH;
o["steep_width"] = d.dSteepW;
o["stair_width"] = d.dStairW;
o["bottom_elevation"] = d.dBomEl;
o["rest_platform"] = (d.bHasResetPLF != FALSE);
o["rest_platform_width"] = d.dRestPtW;
o["platform_thickness"] = d.dSbThick;
o["with_rail"] = (d.bHasGarBar != FALSE);
o["bar_height"] = d.dBarH;
o["material"] = acharToUtf8(d.szGarMat);
o["rail_material"] = acharToUtf8(d.szBarMat);
o["structure_kind"] = d.nKind;
o["stiffener_width"] = d.dStiffnW;
o["stiffener_height"] = d.dStiffnH;
o["has_stiffener"] = (d.bHasStiffn != FALSE);
o["rail_side"] = d.nGarBarLR;
o["rail_reset_platform"] = (d.bGBResetPLF != FALSE);
o["side_beam_w"] = d.dSideBeamW;
o["side_beam_h"] = d.dSideBeamH;
o["handrail_size_w"] = d.dGarSize[0];
o["handrail_size_h"] = d.dGarSize[1];
o["handrail_ext"] = d.dGarExt;
o["handrail_shape"] = d.nGShape;
o["rail_space"] = d.dBarSp;
o["rail_size_w"] = d.dBarSize[0];
o["rail_size_h"] = d.dBarSize[1];
o["rail_shape"] = d.nBShape;
nlohmann::json ar = nlohmann::json::array();
for (int i = 0; i < d.nSteepSize && i < 20; ++i)
ar.push_back(d.nSteeps[i]);
o["steep_counts"] = ar;
return o;
}
}
class ToolAddMrpStair : public mcp_Tool
{
public:
std::string private_name() const override { return "add_aec_mrp_stair"; }
std::string private_description() const override
{
return "建多段楼梯(菜单 PL_DDLT 多段楼梯,实体 AecDbMRPLineStair):给基点 + 跑步方向点,"
"再给踏步高宽、各跑踏步数(steep_counts 数组,最多 20 跑)、梯段宽、底标高、有无休息平台/扶手"
"(缺省照抄 CMRLTDlg)。其余精细字段用 update_aec_mrp_stair。图层 LAY_CSTAIR。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"start": { "type": "array", "items": { "type": "number" }, "description": "基点 [x,y]" },
"direction_point": { "type": "array", "items": { "type": "number" }, "description": "跑步方向点 [x,y]" },
"steep_counts": { "type": "array", "items": { "type": "integer" },
"description": "各跑踏步数(最多 20 个),缺省 [10,10]" },
"steep_height": { "type": "number", "description": "踏步高,缺省 160" },
"steep_width": { "type": "number", "description": "踏步宽,缺省 250" },
"stair_width": { "type": "number", "description": "梯段宽,缺省 1200" },
"bottom_elevation": { "type": "number", "description": "底标高,缺省 0" },
"rest_platform": { "type": "boolean", "description": "是否有休息平台(缺省是)" },
"rest_platform_width": { "type": "number", "description": "休息平台宽,缺省 1200" },
"platform_thickness": { "type": "number", "description": "平台板厚,缺省 150" },
"with_rail": { "type": "boolean", "description": "是否带扶手(缺省是)" },
"bar_height": { "type": "number", "description": "扶手高,缺省 923" },
"material": { "type": "string", "description": "扶手材质,缺省 不锈钢" },
"rail_material": { "type": "string", "description": "栏杆材质,缺省 不锈钢" },
"layer": { "type": "string", "description": "图层,缺省 LAY_CSTAIR" },
"color": { "type": "integer" }
},
"required": [ "start", "direction_point" ]
})");
}
nlohmann::json Execute(const nlohmann::json& args) override
{
if (workingDb() == NULL) return errJson(kErrNoDocument, L"没有活动图纸");
DocLock lock;
if (!lock.isLocked()) return errJson(kErrInternal, L"拿不到图纸写锁(有别的命令在跑?)");
AcGePoint3d ptS(0.0, 0.0, 0.0), ptE(0.0, 0.0, 0.0);
if (!readPoint(args, "start", ptS) || !readPoint(args, "direction_point", ptE))
return errJson(kErrBadParam, L"需要 start / direction_point([x,y])");
if (ptS.distanceTo(ptE) < 1.0)
return errJson(kErrBadParam, L"基点和方向点距离太近");
MRPLineStair data;
MrpStairFromArgs(data, args, false, true);
AecDbMRPLineStair* pStair = new AecDbMRPLineStair();
std::wstring sLay = hasKey(args, "layer") ? wstrOr(args, "layer", L"") : L"";
if (sLay.empty()) sLay = L"LAY_CSTAIR";
AcDbObjectId idLay = KTSetting::InitLayer(sLay.c_str());
if (!idLay.isNull()) pStair->setLayer(idLay);
if (hasKey(args, "color")) pStair->setColorIndex((Adesk::UInt16)intOr(args, "color", 256));
pStair->SetBaseCurve(ptS, ptE);
pStair->SetStairData(data);
pStair->GetSpecialtyData()->SetAt(KEY_2DSTYLE, 0);
AcDbObjectId idNew;
Acad::ErrorStatus es = addToSpace(pStair, &idNew);
if (es != Acad::eOk)
{
delete pStair;
return errJson(kErrInternal, std::wstring(L"加入模型空间失败,err=") + std::to_wstring((int)es));
}
touchEntity(pStair);
nlohmann::json body;
body["ok"] = true;
body["type"] = "mrp_stair";
body["params"] = MrpStairToJson(data);
pStair->close();
body["handle"] = HandleOfId(idNew);
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolAddMrpStair, AddAecMrpStairTool)
class ToolUpdateMrpStair : public mcp_Tool
{
public:
std::string private_name() const override { return "update_aec_mrp_stair"; }
std::string private_description() const override
{
return "精细修改多段楼梯(AecDbMRPLineStair):踏步/各跑踏步数/梯段宽/底标高/平台/扶手栏杆/"
"加强筋/侧梁/截面形状/材质,只传要改的字段;给 start+direction_point 可换走向。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"handle": { "type": "string", "description": "楼梯实体 handle" },
"start": { "type": "array", "items": { "type": "number" } },
"direction_point": { "type": "array", "items": { "type": "number" } },
"steep_counts": { "type": "array", "items": { "type": "integer" } },
"steep_height": { "type": "number" }, "steep_width": { "type": "number" },
"stair_width": { "type": "number" }, "bottom_elevation": { "type": "number" },
"rest_platform": { "type": "boolean" }, "rest_platform_width": { "type": "number" },
"platform_thickness": { "type": "number" }, "with_rail": { "type": "boolean" },
"bar_height": { "type": "number" },
"structure_kind": { "type": "integer", "description": "0 钢筋混凝土 / 1 钢" },
"stiffener_width": { "type": "number" }, "stiffener_height": { "type": "number" },
"has_stiffener": { "type": "boolean" }, "rail_side": { "type": "integer" },
"rail_reset_platform": { "type": "boolean" },
"side_beam_w": { "type": "number" }, "side_beam_h": { "type": "number" },
"handrail_size_w": { "type": "number" }, "handrail_size_h": { "type": "number" },
"handrail_ext": { "type": "number" }, "handrail_shape": { "type": "integer" },
"rail_space": { "type": "number" }, "rail_size_w": { "type": "number" },
"rail_size_h": { "type": "number" }, "rail_shape": { "type": "integer" },
"material": { "type": "string" }, "rail_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"拿不到图纸写锁(有别的命令在跑?)");
if (!hasKey(args, "handle")) return errJson(kErrBadParam, L"缺少 handle");
const std::string sHandle = args["handle"].get<std::string>();
AcDbEntity* pEnt = openByHandle(sHandle, AcDb::kForWrite);
if (pEnt == NULL) return errJson(kErrNotFound, L"按 handle 打不开实体:" + utf8ToWstr(sHandle));
AecDbMRPLineStair* pStair = AecDbMRPLineStair::cast(pEnt);
if (pStair == NULL)
{
const std::string sCls = classNameOf(pEnt);
pEnt->close();
return errJson(kErrWrongType, L"实体 " + utf8ToWstr(sCls) + L" 不是多段楼梯(AecDbMRPLineStair)");
}
MRPLineStair data;
::memset(&data, 0, sizeof(MRPLineStair));
pStair->GetStairData(data);
MrpStairFromArgs(data, args, true, false);
AcGePoint3d ptS(0.0, 0.0, 0.0), ptE(0.0, 0.0, 0.0);
if (readPoint(args, "start", ptS) && readPoint(args, "direction_point", ptE) &&
ptS.distanceTo(ptE) > 1.0)
pStair->SetBaseCurve(ptS, ptE);
pStair->SetStairData(data);
if (hasKey(args, "color")) pStair->setColorIndex((Adesk::UInt16)intOr(args, "color", 256));
pStair->ReCreateDraw3d();
touchEntity(pStair);
nlohmann::json body;
body["ok"] = true;
body["type"] = "mrp_stair";
body["handle"] = sHandle;
body["params"] = MrpStairToJson(data);
pEnt->close();
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolUpdateMrpStair, UpdateAecMrpStairTool)
// ===========================================================================
// 立管固架(PL_LGGJ,AecDbHolder)—— 选立管 + 选墙,位置自己算
// ===========================================================================
class ToolAddVerticalPipeHolder : public mcp_Tool
{
public:
std::string private_name() const override { return "add_aec_vertical_pipe_holder"; }
std::string private_description() const override
{
return "在墙上给立管加固定支架(菜单 PL_LGGJ 立管固架,实体 AecDbHolder):给立管 pipe 和墙 wall 两个 handle,"
"位置按命令的算法自动算(立管中点投影到墙中心线 → 沿墙切向挪 半个(管外径+角钢宽) → 再沿墙厚方向挪半墙厚),"
"底标高缺省取管底 + 管长/3。支架本身的参数(支柱长/臂长/角钢宽厚/材质编号)可覆盖。"
"改参数用 update_aec_holder。图层 LAY_SUPPORT。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"pipe": { "type": "string", "description": "立管(AecDbEwPipe)的 handle,必须是立管" },
"wall": { "type": "string", "description": "固定管子的墙(AecDbXWall)的 handle" },
"bottom_elevation": { "type": "number", "description": "支架底标高(缺省=管底+管长/3)" },
"count": { "type": "integer", "description": "支架行数(数据字段),缺省 3" },
"post_length": { "type": "number", "description": "支柱垂直长度,缺省 500" },
"arm_length": { "type": "number", "description": "支架臂长,缺省 800" },
"steel_width": { "type": "number", "description": "角钢宽,缺省 50" },
"steel_thickness": { "type": "number", "description": "角钢厚,缺省 3" },
"material": { "type": "string", "description": "材质,缺省 普通碳钢" },
"no": { "type": "string", "description": "编号,缺省 G-01" },
"purpose": { "type": "string", "description": "用途" },
"layer": { "type": "string", "description": "图层,缺省 LAY_SUPPORT" },
"color": { "type": "integer" }
},
"required": [ "pipe", "wall" ]
})");
}
nlohmann::json Execute(const nlohmann::json& args) override
{
if (workingDb() == NULL) return errJson(kErrNoDocument, L"没有活动图纸");
DocLock lock;
if (!lock.isLocked()) return errJson(kErrInternal, L"拿不到图纸写锁(有别的命令在跑?)");
// 立管
const std::wstring sPipe = wstrOr(args, "pipe", L"");
AcDbEntity* pPipeEnt = openByHandle(acharToUtf8(sPipe.c_str()), AcDb::kForRead);
if (pPipeEnt == NULL) return errJson(kErrNotFound, L"按 handle 打不开立管:" + sPipe);
AecDbEwPipe* pPipe = AecDbEwPipe::cast(pPipeEnt);
AcDbCurve* pPipeCrv = AcDbCurve::cast(pPipeEnt);
if (pPipe == NULL || pPipeCrv == NULL)
{
pPipeEnt->close();
return errJson(kErrWrongType, L"pipe 不是管道(AecDbEwPipe):" + sPipe);
}
AcGePoint3d ptSta(0.0, 0.0, 0.0), ptEnd(0.0, 0.0, 0.0);
pPipeCrv->getStartPoint(ptSta);
pPipeCrv->getEndPoint(ptEnd);
double dOD = 0.0;
pPipe->GetSpecialtyData(AcDb::kForRead)->GetAt(PIPE_OD, dOD);
const AcGeVector3d vtDir = pPipe->GetTailDir();
pPipeEnt->close();
// 必须是立管(方向不能平行于 XY 平面)
{
AcGeLine3d ln(AcGePoint3d::kOrigin, vtDir);
if (ln.isParallelTo(AcGePlane::kXYPlane))
return errJson(kErrBadParam, L"选择的管道不是立管");
}
// 墙
const std::wstring sWall = wstrOr(args, "wall", L"");
AcDbEntity* pWallEnt = openByHandle(acharToUtf8(sWall.c_str()), AcDb::kForRead);
if (pWallEnt == NULL) return errJson(kErrNotFound, L"按 handle 打不开墙:" + sWall);
AecDbXWall* pWall = AecDbXWall::cast(pWallEnt);
if (pWall == NULL)
{
const std::string sCls = classNameOf(pWallEnt);
pWallEnt->close();
return errJson(kErrWrongType, L"wall 是 " + utf8ToWstr(sCls) + L",不是墙");
}
WallData wd;
::memset(&wd, 0, sizeof(WallData));
pWall->GetWallData(wd);
// 位置:立管中点投影到墙基线 → 再算切线方向与到管轴的距离
AcGePoint3d ptM(0.5 * (ptSta.x + ptEnd.x), 0.5 * (ptSta.y + ptEnd.y), 0.5 * (ptSta.z + ptEnd.z));
AcGePoint3d ptOnWall(0.0, 0.0, 0.0), ptOnPipe(0.0, 0.0, 0.0);
AcGeVector3d vtT(1.0, 0.0, 0.0);
if (pWall->getClosestPointTo(ptM, ptOnWall, Adesk::kTrue) != Acad::eOk ||
pWall->getFirstDeriv(ptOnWall, vtT) != Acad::eOk)
{
pWallEnt->close();
return errJson(kErrInternal, L"墙基线投影/取切向失败");
}
vtT.normalize();
AcDbLine lnPipe(ptSta, ptEnd);
lnPipe.getClosestPointTo(ptOnWall, ptOnPipe, Adesk::kTrue);
AcGeVector3d vtL = ptOnPipe - ptOnWall;
vtL.normalize();
pWallEnt->close();
// 支架数据(CLGGJDlg 的缺省)
XHolder holder;
::memset(&holder, 0, sizeof(XHolder));
holder.N = 3;
holder.V = 500.0;
holder.L = 800.0;
holder.W = 50.0;
holder.t = 3.0;
CopyStr(holder.szMat, 33, L"普通碳钢");
CopyStr(holder.szNo, 33, L"G-01");
if (hasKey(args, "count")) holder.N = intOr(args, "count", holder.N);
if (hasKey(args, "post_length")) holder.V = numOr(args, "post_length", holder.V);
if (hasKey(args, "arm_length")) holder.L = numOr(args, "arm_length", holder.L);
if (hasKey(args, "steel_width")) holder.W = numOr(args, "steel_width", holder.W);
if (hasKey(args, "steel_thickness")) holder.t = numOr(args, "steel_thickness", holder.t);
if (hasKey(args, "material")) CopyStr(holder.szMat, 33, wstrOr(args, "material", L""));
if (hasKey(args, "no")) CopyStr(holder.szNo, 33, wstrOr(args, "no", L""));
if (hasKey(args, "purpose")) CopyStr(holder.szUsage, 33, wstrOr(args, "purpose", L""));
// 底标高缺省:管底 + 管长/3(与命令一致)
double dBomEl = numOr(args, "bottom_elevation", 0.0);
if (!hasKey(args, "bottom_elevation"))
{
dBomEl = __min(ptSta.z, ptEnd.z) + ptSta.distanceTo(ptEnd) / 3.0;
}
holder.dBomEl = dBomEl;
// 位置:沿墙切向挪 (管外径 + 角钢宽)/2,再沿墙厚方向挪 半墙厚
AcGePoint3d ptPos = ptOnWall;
ptPos += vtT * (0.5 * (dOD + holder.W));
ptPos += vtL * (0.5 * wd.dThick);
ptPos.z = dBomEl;
AecDbHolder* pHolder = new AecDbHolder();
pHolder->SetHolder(holder);
pHolder->SetPosition(ptPos, vtL);
std::wstring sLay = hasKey(args, "layer") ? wstrOr(args, "layer", L"") : L"";
if (sLay.empty()) sLay = L"LAY_SUPPORT";
AcDbObjectId idLay = KTSetting::InitLayer(sLay.c_str());
if (!idLay.isNull()) pHolder->setLayer(idLay);
if (hasKey(args, "color")) pHolder->setColorIndex((Adesk::UInt16)intOr(args, "color", 256));
AcDbObjectId idNew;
Acad::ErrorStatus es = addToSpace(pHolder, &idNew);
if (es != Acad::eOk)
{
delete pHolder;
return errJson(kErrInternal, std::wstring(L"加入模型空间失败,err=") + std::to_wstring((int)es));
}
touchEntity(pHolder);
nlohmann::json o;
o["count"] = holder.N;
o["post_length"] = holder.V;
o["arm_length"] = holder.L;
o["steel_width"] = holder.W;
o["steel_thickness"] = holder.t;
o["bottom_elevation"] = holder.dBomEl;
nlohmann::json body;
body["ok"] = true;
body["type"] = "vertical_pipe_holder";
body["position"] = { ptPos.x, ptPos.y, ptPos.z };
body["params"] = o;
pHolder->close();
body["handle"] = HandleOfId(idNew);
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolAddVerticalPipeHolder, AddAecVerticalPipeHolderTool)