Files
envi-code/SourceCode/Code2026/cad_mcp_aec/aec_mcp_stair_tools.cpp
T
company-desktop 92daf4033d feat: 第6项第一批——直线钢梯(PL_PTLT)与护栏(PL_FZHN/PL_QXHN)工具
新增 cad_mcp_aec/aec_mcp_stair_tools.cpp(4 个工具):
- add/update_aec_line_stair:直线钢梯 AecDbLineStair,参数照 CPTLTDlg(梯宽/踏步/梯梁/
  栏管/材质编号,缺省值抄对话框构造函数),图层 LAY_CSTAIR
- add/update_aec_guard_bar:护栏 AecDbGuardBar,points 自己画(PL_FZHN)或 curve 给已有
  曲线 handle(PL_QXHN),图层 LAY_GBAR

三个坑:
1. SetBaseCurve() 会清专业数据。命令里的顺序是 SetLineStairData -> SetSection ->
   SetBaseCurve,刚写的材质/厂家/编号被最后一步清掉(UI 里也一样)。
   工具改成 SetBaseCurve -> SetLineStairData,数据才留得住。
2. AecDbGuardBar::SetGuardBarData 把编号写进 KEY_MATERIAL(SetAt(KEY_MATERIAL, m_sNo)),
   GetGuardBarData 又从 KEY_MATERIAL 读两遍、厂家从来没存过——护栏"材质"永远显示编号。
   已修 CoreLibrary:写 KEY_MATERIAL/KEY_FACTORY/KEY_NO;读时没有 KEY_NO 就退回老位置,
   兼容旧图。
3. 钢梯的 GetLineStairData 只回填 bar 的直径/间距,材质/厂家/编号在 LineStair 自己的成员里,
   组装返回 JSON 不能从 bar 取。

实测(R230 Debug,空图):
- 钢梯 (0,0)->(4000,0),bbox x -208~4015 / z 0~4015;改 width=1200、top_height=1100 后
  材质"不锈钢"、编号"LT-01"仍在
- 护栏 points 2 点 -> bbox 含 40 管半径;改 diameter=50 后 material/no/factory 正确回读
- 护栏 curve 走一条 3 点多段线也能建(31B)

工具总数 53 -> 57(本插件 25 -> 29),R230/Z2026 都编过。
2026-10-30 23:14:23 +08:00

601 lines
24 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// ============================================================================
// aec_mcp_stair_tools.cpp
// cad_mcp_aec 的楼梯 / 护栏工具(工作计划第 6 项)
// * PL_PTLT 直线钢梯 -> AecDbLineStair(AecDbBaseStair 派生)
// * PL_FZHN 放置护栏 / PL_QXHN 曲线护栏 -> AecDbGuardBar(AecDbEwPipeBase 派生)
//
// 两个命令的建模序列(Pool3dCmd/pool3dcmdCmd.cpp):
// 建实体 -> setLayer(LAY_CSTAIR / LAY_GBAR) -> SetLineStairData / SetGuardBarData
// -> SetSection(...) -> SetBaseCurve(&line / &polyline) -> KEY_2DSTYLE=0 -> 入库
// 参数全部来自各自的对话框(CPTLTDlg / CFZHNDlg),缺省值照抄对话框构造函数,
// 尺寸用图纸单位。
// ============================================================================
#include "StdAfx.h"
#include "KTCad.h"
#include "aec_mcp_common.h"
#include "mcp_plugin_api.h"
#include "AecDbLineStair.h"
#include "AecDbGuardBar.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;
}
nlohmann::json PointJson(const AcGePoint3d& pt)
{
nlohmann::json o;
o["x"] = pt.x;
o["y"] = pt.y;
o["z"] = pt.z;
return o;
}
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;
}
// ---------------------------------------------------------------- 护栏
// GuardBar 的缺省值照抄 CFZHNDlg 构造函数
void InitGuardBar(GuardBar& bar)
{
::memset(&bar, 0, sizeof(GuardBar));
bar.dD = 40.0; // 管材直径
bar.dt = 2.5; // 管材厚(命令里写死 2.5)
bar.dBomEl = 0.0; // 底标高
bar.dH1 = 650.0; // 底层横杆高
bar.dH = 1080.0; // 顶部扶手高
bar.dP = 1200.0; // 立柱间距
bar.bNoBomLever = FALSE;
bar.bIsRectLever = FALSE;
bar.bVerDraw = FALSE;
CopyStr(bar.m_sMaterial, 256, L"镜亮不锈钢型材");
CopyStr(bar.m_sFactory, 256, L"厂家自制");
CopyStr(bar.m_sNo, 256, L"HL-01");
}
void GuardBarFromArgs(GuardBar& bar, const nlohmann::json& a, bool bMerge)
{
if (!bMerge) InitGuardBar(bar);
if (hasKey(a, "diameter")) bar.dD = numOr(a, "diameter", bar.dD);
if (hasKey(a, "bar_thickness")) bar.dt = numOr(a, "bar_thickness", bar.dt);
if (hasKey(a, "bottom_elevation")) bar.dBomEl = numOr(a, "bottom_elevation", bar.dBomEl);
if (hasKey(a, "top_height")) bar.dH = numOr(a, "top_height", bar.dH);
if (hasKey(a, "first_height")) bar.dH1 = numOr(a, "first_height", bar.dH1);
if (hasKey(a, "post_space")) bar.dP = numOr(a, "post_space", bar.dP);
if (hasKey(a, "rect_post")) bar.bIsRectLever = intOr(a, "rect_post", 0) ? TRUE : FALSE;
if (hasKey(a, "no_bottom_rail")) bar.bNoBomLever = intOr(a, "no_bottom_rail", 0) ? TRUE : FALSE;
if (hasKey(a, "vertical_draw")) bar.bVerDraw = intOr(a, "vertical_draw", 0) ? TRUE : FALSE;
if (hasKey(a, "material")) CopyStr(bar.m_sMaterial, 256, wstrOr(a, "material", L""));
if (hasKey(a, "factory")) CopyStr(bar.m_sFactory, 256, wstrOr(a, "factory", L""));
if (hasKey(a, "no")) CopyStr(bar.m_sNo, 256, wstrOr(a, "no", L""));
}
nlohmann::json GuardBarToJson(const GuardBar& bar)
{
nlohmann::json o;
o["diameter"] = bar.dD;
o["bar_thickness"] = bar.dt;
o["bottom_elevation"] = bar.dBomEl;
o["top_height"] = bar.dH;
o["first_height"] = bar.dH1;
o["post_space"] = bar.dP;
o["rect_post"] = (bar.bIsRectLever != FALSE);
o["no_bottom_rail"] = (bar.bNoBomLever != FALSE);
o["vertical_draw"] = (bar.bVerDraw != FALSE);
o["material"] = acharToUtf8(bar.m_sMaterial);
o["factory"] = acharToUtf8(bar.m_sFactory);
o["no"] = acharToUtf8(bar.m_sNo);
return o;
}
// 二维点串 -> 多段线(护栏的基准曲线,elevation 固定 0,与命令一致)
AcDbPolyline* PolylineOfPoints(const nlohmann::json& a, std::wstring& err)
{
if (!hasKey(a, "points") || !a["points"].is_array() || a["points"].size() < 2)
{
err = L"需要 points:[[x,y],...](至少 2 点)";
return NULL;
}
AcDbPolyline* pPl = new AcDbPolyline((int)a["points"].size());
int nIdx = 0;
for (size_t i = 0; i < a["points"].size(); ++i)
{
const nlohmann::json& pt = a["points"][i];
if (!pt.is_array() || pt.size() < 2) continue;
pPl->addVertexAt(nIdx++, AcGePoint2d(pt[0].get<double>(), pt[1].get<double>()), 0.0, 0.0, 0.0);
}
if (nIdx < 2)
{
delete pPl;
err = L"points 有效点少于 2 个";
return NULL;
}
pPl->setElevation(0.0); // 命令里是"视图 elevation=0 时绘制",这里固定 0
return pPl;
}
// 已有曲线(圆弧/圆/多段线/样条)-> 多段线,供 PL_QXHN 那种"沿已有曲线"的用法
AcDbPolyline* PolylineOfHandle(const std::wstring& sHandle, std::wstring& err)
{
AcDbEntity* pEnt = openByHandle(acharToUtf8(sHandle.c_str()), AcDb::kForRead);
if (pEnt == NULL)
{
err = L"按 handle 打不开曲线:" + sHandle;
return NULL;
}
AcDbCurve* pCrv = AcDbCurve::cast(pEnt);
if (pCrv == NULL)
{
pEnt->close();
err = L"curve 必须是曲线类实体(直线/圆弧/圆/多段线/样条)";
return NULL;
}
AcDbPolyline* pPl = AcDbPolyline::cast(pEnt) ? (AcDbPolyline*)pCrv->clone()
: CoreTools::MakePolyline(pCrv);
pEnt->close();
if (pPl == NULL)
{
err = L"曲线转多段线失败";
return NULL;
}
pPl->setElevation(0.0);
return pPl;
}
// ---------------------------------------------------------------- 直线钢梯
// LineStair 的缺省值照抄 CPTLTDlg 构造函数
void InitLineStair(LineStair& data)
{
::memset(&data, 0, sizeof(LineStair));
InitGuardBar(data.bar);
data.bar.dD = 30.0; // 钢梯的栏管直径缺省 30
data.bar.dH = 1080.0;
data.bar.dH1 = 650.0;
data.bar.dP = 1200.0;
data.bar.dt = 2.0;
data.dSteepH = 200.0; // 踏步高
data.dSteept = 8.0; // 踏步厚
data.dSteepW = 400.0; // 踏步宽
data.dSideW = 200.0; // 梯梁宽
data.dSidet = 40.0; // 梯梁厚
data.dW = 1000.0; // 梯宽
data.dBomEl = 0.0;
data.dTopEl = 4000.0;
CopyStr(data.m_sMaterial, 256, L"不锈钢");
CopyStr(data.m_sFactory, 256, L"厂家自制");
}
void LineStairFromArgs(LineStair& data, const nlohmann::json& a, bool bMerge)
{
if (!bMerge) InitLineStair(data);
GuardBarFromArgs(data.bar, a, true);
if (hasKey(a, "width")) data.dW = numOr(a, "width", data.dW);
if (hasKey(a, "steep_height")) data.dSteepH = numOr(a, "steep_height", data.dSteepH);
if (hasKey(a, "steep_width")) data.dSteepW = numOr(a, "steep_width", data.dSteepW);
if (hasKey(a, "steep_thickness")) data.dSteept = numOr(a, "steep_thickness", data.dSteept);
if (hasKey(a, "side_width")) data.dSideW = numOr(a, "side_width", data.dSideW);
if (hasKey(a, "side_thickness")) data.dSidet = numOr(a, "side_thickness", data.dSidet);
if (hasKey(a, "bottom_elevation")) data.dBomEl = numOr(a, "bottom_elevation", data.dBomEl);
if (hasKey(a, "top_elevation")) data.dTopEl = numOr(a, "top_elevation", data.dTopEl);
if (hasKey(a, "with_rail")) data.bGuardBar = intOr(a, "with_rail", 0) ? TRUE : FALSE;
if (hasKey(a, "material")) CopyStr(data.m_sMaterial, 256, wstrOr(a, "material", L""));
if (hasKey(a, "factory")) CopyStr(data.m_sFactory, 256, wstrOr(a, "factory", L""));
if (hasKey(a, "no")) CopyStr(data.m_sNo, 256, wstrOr(a, "no", L""));
}
}
// ---------------------------------------------------------------------------
// add_aec_line_stair —— 直线钢梯(PL_PTLT)
// ---------------------------------------------------------------------------
class ToolAddLineStair : public mcp_Tool
{
public:
std::string private_name() const override { return "add_aec_line_stair"; }
std::string private_description() const override
{
return "建直线钢梯(菜单 PL_PTLT 直线钢梯,实体 AecDbLineStair):"
"给起点/终点平面位置 + 底/顶标高,再给梯宽、踏步、梯梁、栏管参数(都有缺省,照抄 CPTLTDlg)。"
"尺寸用图纸单位;起点 z 取 bottom_elevation、终点 z 取 top_elevation。图层 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](z 用 bottom_elevation)" },
"end": { "type": "array", "items": { "type": "number" }, "description": "终点 [x,y](z 用 top_elevation)" },
"bottom_elevation": { "type": "number", "description": "底标高,缺省 0" },
"top_elevation": { "type": "number", "description": "顶标高,缺省 4000" },
"width": { "type": "number", "description": "梯宽,缺省 1000" },
"steep_height": { "type": "number", "description": "踏步高,缺省 200" },
"steep_width": { "type": "number", "description": "踏步宽,缺省 400" },
"steep_thickness": { "type": "number", "description": "踏步厚,缺省 8" },
"side_width": { "type": "number", "description": "梯梁宽,缺省 200" },
"side_thickness": { "type": "number", "description": "梯梁厚,缺省 40" },
"diameter": { "type": "number", "description": "栏管直径(也是钢梯截面),缺省 30" },
"top_height": { "type": "number", "description": "扶手高,缺省 1080" },
"first_height": { "type": "number", "description": "底层横杆高,缺省 650" },
"post_space": { "type": "number", "description": "立柱间距,缺省 1200" },
"with_rail": { "type": "boolean", "description": "是否带护栏" },
"material": { "type": "string", "description": "材质,缺省 不锈钢" },
"factory": { "type": "string", "description": "厂家,缺省 厂家自制" },
"no": { "type": "string", "description": "编号" },
"layer": { "type": "string", "description": "图层,缺省 LAY_CSTAIR" },
"color": { "type": "integer", "description": "ACI 颜色号,256=ByLayer" }
},
"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"拿不到图纸写锁(有别的命令在跑?)");
AcGePoint3d ptS(0.0, 0.0, 0.0), ptE(0.0, 0.0, 0.0);
if (!readPoint(args, "start", ptS) || !readPoint(args, "end", ptE))
return errJson(kErrBadParam, L"需要 start / end([x,y] 或 [x,y,z])");
if (ptS.distanceTo(ptE) < 1.0)
return errJson(kErrBadParam, L"start 与 end 距离太近(命令里 <1 也拒绝)");
LineStair data;
LineStairFromArgs(data, args, false);
ptS.z = data.dBomEl;
ptE.z = data.dTopEl;
AcDbLine ln(ptS, ptE);
AecDbLineStair* pStair = new AecDbLineStair();
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));
// 顺序有讲究:SetBaseCurve() 内部会重算截面/清专业数据,所以必须放在
// SetLineStairData 之前;命令里是反的(Data -> SetSection -> SetBaseCurve),
// 结果材质/编号会被 SetBaseCurve 清掉。
pStair->SetBaseCurve(&ln);
pStair->SetLineStairData(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);
pStair->close();
nlohmann::json body;
body["ok"] = true;
body["type"] = "line_stair";
body["handle"] = HandleOfId(idNew);
body["start"] = PointJson(ptS);
body["end"] = PointJson(ptE);
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolAddLineStair, AddAecLineStairTool)
// ---------------------------------------------------------------------------
// update_aec_line_stair —— 改直线钢梯
// ---------------------------------------------------------------------------
class ToolUpdateLineStair : public mcp_Tool
{
public:
std::string private_name() const override { return "update_aec_line_stair"; }
std::string private_description() const override
{
return "改直线钢梯:起终点、梯宽/踏步/梯梁/栏管尺寸、材质编号,只传要改的字段。"
"改尺寸会重建几何;不传 start/end 就保持原来的走向。";
}
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" }, "description": "新起点 [x,y]" },
"end": { "type": "array", "items": { "type": "number" }, "description": "新终点 [x,y]" },
"bottom_elevation": { "type": "number" }, "top_elevation": { "type": "number" },
"width": { "type": "number" }, "steep_height": { "type": "number" },
"steep_width": { "type": "number" }, "steep_thickness": { "type": "number" },
"side_width": { "type": "number" }, "side_thickness": { "type": "number" },
"diameter": { "type": "number" }, "top_height": { "type": "number" },
"first_height": { "type": "number" }, "post_space": { "type": "number" },
"with_rail": { "type": "boolean" },
"material": { "type": "string" }, "factory": { "type": "string" }, "no": { "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));
AecDbLineStair* pStair = AecDbLineStair::cast(pEnt);
if (pStair == NULL)
{
const std::string sCls = classNameOf(pEnt);
pEnt->close();
return errJson(kErrWrongType, L"实体 " + utf8ToWstr(sCls) + L" 不是直线钢梯(AecDbLineStair)");
}
LineStair data;
::memset(&data, 0, sizeof(LineStair));
pStair->GetLineStairData(data);
AcGePoint3d ptS(0.0, 0.0, 0.0), ptE(0.0, 0.0, 0.0);
AcDbCurve* pCrv = AcDbCurve::cast(pEnt);
bool bHasCurve = (pCrv != NULL && pCrv->getStartPoint(ptS) == Acad::eOk &&
pCrv->getEndPoint(ptE) == Acad::eOk);
LineStairFromArgs(data, args, true);
if (readPoint(args, "start", ptS)) bHasCurve = bHasCurve || true;
if (readPoint(args, "end", ptE)) bHasCurve = bHasCurve || true;
ptS.z = data.dBomEl;
ptE.z = data.dTopEl;
if (bHasCurve && ptS.distanceTo(ptE) > 1.0)
{
AcDbLine ln(ptS, ptE);
pStair->SetBaseCurve(&ln);
}
pStair->SetLineStairData(data); // 必须在 SetBaseCurve 之后(它会清专业数据)
if (hasKey(args, "color")) pStair->setColorIndex((Adesk::UInt16)intOr(args, "color", 256));
pStair->ReCreateDraw3d();
touchEntity(pStair);
pStair->close();
nlohmann::json body;
body["ok"] = true;
body["type"] = "line_stair";
body["handle"] = sHandle;
body["start"] = PointJson(ptS);
body["end"] = PointJson(ptE);
// 注意:材质/厂家/编号在 LineStair 自己的成员里,不在 bar 里
// (GetLineStairData 只回填 bar 的直径/间距那些)
body["params"] = GuardBarToJson(data.bar);
body["params"]["material"] = acharToUtf8(data.m_sMaterial);
body["params"]["factory"] = acharToUtf8(data.m_sFactory);
body["params"]["no"] = acharToUtf8(data.m_sNo);
body["params"]["width"] = data.dW;
body["params"]["steep_height"] = data.dSteepH;
body["params"]["steep_width"] = data.dSteepW;
body["params"]["steep_thickness"] = data.dSteept;
body["params"]["side_width"] = data.dSideW;
body["params"]["side_thickness"] = data.dSidet;
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolUpdateLineStair, UpdateAecLineStairTool)
// ---------------------------------------------------------------------------
// add_aec_guard_bar —— 护栏(PL_FZHN 直接绘制 / PL_QXHN 沿已有曲线)
// ---------------------------------------------------------------------------
class ToolAddGuardBar : public mcp_Tool
{
public:
std::string private_name() const override { return "add_aec_guard_bar"; }
std::string private_description() const override
{
return "建护栏(菜单 PL_FZHN 放置护栏 / PL_QXHN 曲线护栏,实体 AecDbGuardBar):"
"轮廓给 points([[x,y],...],自己画的)或 curve(已有曲线实体的 handle,圆弧/圆/多段线都行)。"
"栏管直径/扶手高/立柱间距等缺省照抄 CFZHNDlg。护栏按视图 elevation=0 绘制,基准曲线标高固定 0,"
"实际高度由 bottom_elevation 决定。图层 LAY_GBAR。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"points": { "type": "array", "items": { "type": "array", "items": { "type": "number" } },
"description": "护栏走向 [[x,y],...](至少 2 点)" },
"curve": { "type": "string", "description": "或者给一条已有曲线的 handle(PL_QXHN 的做法)" },
"diameter": { "type": "number", "description": "栏管直径,缺省 40" },
"bar_thickness": { "type": "number", "description": "栏管壁厚,缺省 2.5" },
"bottom_elevation": { "type": "number", "description": "底标高,缺省 0" },
"top_height": { "type": "number", "description": "扶手高,缺省 1080" },
"first_height": { "type": "number", "description": "底层横杆高,缺省 650" },
"post_space": { "type": "number", "description": "立柱间距,缺省 1200" },
"rect_post": { "type": "boolean", "description": "立柱是否方管" },
"no_bottom_rail": { "type": "boolean", "description": "是否没有底部栏杆" },
"vertical_draw": { "type": "boolean", "description": "是否竖向出图" },
"material": { "type": "string", "description": "材质,缺省 镜亮不锈钢型材" },
"factory": { "type": "string", "description": "厂家,缺省 厂家自制" },
"no": { "type": "string", "description": "编号,缺省 HL-01" },
"layer": { "type": "string", "description": "图层,缺省 LAY_GBAR" },
"color": { "type": "integer", "description": "ACI 颜色号,256=ByLayer" }
}
})");
}
nlohmann::json Execute(const nlohmann::json& args) override
{
if (workingDb() == NULL) return errJson(kErrNoDocument, L"没有活动图纸");
DocLock lock;
if (!lock.isLocked()) return errJson(kErrInternal, L"拿不到图纸写锁(有别的命令在跑?)");
std::wstring err;
AcDbPolyline* pPl = NULL;
if (hasKey(args, "curve"))
pPl = PolylineOfHandle(wstrOr(args, "curve", L""), err);
else
pPl = PolylineOfPoints(args, err);
if (pPl == NULL) return errJson(kErrBadParam, err);
GuardBar bar;
GuardBarFromArgs(bar, args, false);
AecDbGuardBar* pBar = new AecDbGuardBar();
std::wstring sLay = hasKey(args, "layer") ? wstrOr(args, "layer", L"") : L"";
if (sLay.empty()) sLay = L"LAY_GBAR";
AcDbObjectId idLay = KTSetting::InitLayer(sLay.c_str());
if (!idLay.isNull()) pBar->setLayer(idLay);
// 命令里:方管立柱 SetSection(dD, dD, 0),其它 SetSection(dD, 0)
if (bar.bIsRectLever)
pBar->SetSection(bar.dD, bar.dD, 0.0);
else
pBar->SetSection(bar.dD, 0.0);
pBar->SetBaseCurve(pPl);
pBar->SetGuardBarData(bar);
pBar->GetSpecialtyData()->SetAt(KEY_2DSTYLE, 0);
if (hasKey(args, "color")) pBar->setColorIndex((Adesk::UInt16)intOr(args, "color", 256));
delete pPl; // SetBaseCurve 会自己 clone
AcDbObjectId idNew;
Acad::ErrorStatus es = addToSpace(pBar, &idNew);
if (es != Acad::eOk)
{
delete pBar;
return errJson(kErrInternal, std::wstring(L"加入模型空间失败,err=") + std::to_wstring((int)es));
}
touchEntity(pBar);
pBar->close();
nlohmann::json body;
body["ok"] = true;
body["type"] = "guard_bar";
body["handle"] = HandleOfId(idNew);
body["params"] = GuardBarToJson(bar);
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolAddGuardBar, AddAecGuardBarTool)
// ---------------------------------------------------------------------------
// update_aec_guard_bar —— 改护栏
// ---------------------------------------------------------------------------
class ToolUpdateGuardBar : public mcp_Tool
{
public:
std::string private_name() const override { return "update_aec_guard_bar"; }
std::string private_description() const override
{
return "改护栏:栏管直径/高度/立柱间距/材质编号,只传要改的字段;给 points 或 curve 可以换走向。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"handle": { "type": "string", "description": "护栏实体 handle" },
"points": { "type": "array", "items": { "type": "array", "items": { "type": "number" } },
"description": "新的护栏走向(可选)" },
"curve": { "type": "string", "description": "或者给一条新曲线的 handle(可选)" },
"diameter": { "type": "number" }, "bar_thickness": { "type": "number" },
"bottom_elevation": { "type": "number" }, "top_height": { "type": "number" },
"first_height": { "type": "number" }, "post_space": { "type": "number" },
"rect_post": { "type": "boolean" }, "no_bottom_rail": { "type": "boolean" },
"vertical_draw": { "type": "boolean" },
"material": { "type": "string" }, "factory": { "type": "string" }, "no": { "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));
AecDbGuardBar* pBar = AecDbGuardBar::cast(pEnt);
if (pBar == NULL)
{
const std::string sCls = classNameOf(pEnt);
pEnt->close();
return errJson(kErrWrongType, L"实体 " + utf8ToWstr(sCls) + L" 不是护栏(AecDbGuardBar)");
}
GuardBar bar;
::memset(&bar, 0, sizeof(GuardBar));
if (!pBar->GetGuardBarData(bar)) InitGuardBar(bar);
GuardBarFromArgs(bar, args, true);
std::wstring err;
AcDbPolyline* pPl = NULL;
if (hasKey(args, "curve"))
pPl = PolylineOfHandle(wstrOr(args, "curve", L""), err);
else if (hasKey(args, "points"))
pPl = PolylineOfPoints(args, err);
if (!err.empty())
{
pEnt->close();
return errJson(kErrBadParam, err);
}
if (pPl != NULL)
{
pBar->SetBaseCurve(pPl);
delete pPl;
}
if (bar.bIsRectLever)
pBar->SetSection(bar.dD, bar.dD, 0.0);
else
pBar->SetSection(bar.dD, 0.0);
pBar->SetGuardBarData(bar);
if (hasKey(args, "color")) pBar->setColorIndex((Adesk::UInt16)intOr(args, "color", 256));
pBar->ReCreateDraw3d();
touchEntity(pBar);
pBar->close();
nlohmann::json body;
body["ok"] = true;
body["type"] = "guard_bar";
body["handle"] = sHandle;
body["params"] = GuardBarToJson(bar);
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolUpdateGuardBar, UpdateAecGuardBarTool)