Files
envi-code/SourceCode/Code2026/cad_mcp_aec/aec_mcp_stair_tools.cpp
T
company-desktop fba0b7c285 feat: 第6项收尾——沟渠、标注(8 个命令)、楼梯 update 精细字段
一、沟渠 PL_LSJM(AecDbEwPipeBase)
断面是 CHZGQDlg::GetSection() 用 9 点现算的 U 形槽(总宽/总高/内底宽/侧壁坡度角/壁厚),
工具里照抄成 BuildDitchRegion,再 SetSection(reg,cen) + SetBaseCurve + KEY_NAME=沟渠 +
KEY_2DSTYLE=2 + KEY_CENNODRAW + KEY_W/H/V/Angle/T。update 改断面时重建 region,
只改底标高时把路径整体抬高。

二、标注 AecDbEwPipeDim(8 个命令共用,纯 2D)
创建序列一致:AddAecDbWEntity + SetDimStyle/DimDir/TextUporDownFlag + 文字样式与图层
(LAY_PP3D_DIM / Pipe_Dim)+ SetPScale + SetTextHeight(3.0) + SetTextWidthScale(0.75) +
AddDimText(上,下) + AppendPoint(引出点) + AppendPoint(文字点)。
做成 add_aec_pipe_dim + kind(seq/seq_batch/opening/casing/ring/valve/iso_valve/iso_fitting),
update_aec_pipe_dim 改文字/样式/字高。
限制:命令里文字是按被标注实体数据自动拼的(预留D%d洞口 / 中心标高:%.3f),
这版要调用方自己传 text/text_down;引出点与文字点不能改。

三、楼梯 update 精细字段(按总原则补齐)
- 钢梯/直线/圆弧(LineStair):steep_count、symbol、in_slab_3d、left_beam/right_beam、
  slope/nonslip/slope_base、bottom_at_start
- 双跑(ZLineStair):flight_count、stiffener_width/height、handrail_size_w/h、
  handrail_ext/handrail_ext_z、handrail_shape、rail_size_w/h、rail_shape、side_beam_w/h
- 顺带修:钢梯 update 的返回之前只报了 bar 的字段,改用 LineStairToJson 后精细字段能回读

实测(R230 Debug,空图):
- 沟渠 L 形走向(W800/H800/V700/T10、底标高-500)-> 290,bbox 0~6400 x -400~3000;
  改 W1000/H900/T20 生效
- 管线上序号标注(文字 1、字高 250)-> 311;改 7 + 下排"中心标高 1.000" + 字高 300 正确;
  洞口标注(预留D250洞口)-> 312
- 钢梯精修:steep_count=18/symbol=1/left_beam/right_beam/nonslip/in_slab_3d/with_rail 全回读正确
- 双跑精修:flight_count=2/side_beam/扶手栏杆截面/加强筋 全回读正确

工具总数 77 -> 81(本插件 49 -> 53),R230/Z2026 都编过。
2026-10-31 09:34:07 +08:00

1302 lines
54 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 "AecDbZLineStair.h"
#include "AecDbLineSegStair.h"
#include "AecDbArcStair.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, "steep_count")) data.nSteepS = intOr(a, "steep_count", data.nSteepS);
if (hasKey(a, "symbol")) data.nSymbol = intOr(a, "symbol", data.nSymbol);
if (hasKey(a, "in_slab_3d")) data.bShowInSlab3d = intOr(a, "in_slab_3d", 0) ? TRUE : FALSE;
if (hasKey(a, "left_beam")) data.bLBeam = intOr(a, "left_beam", 1) ? TRUE : FALSE;
if (hasKey(a, "right_beam")) data.bRBeam = intOr(a, "right_beam", 1) ? TRUE : FALSE;
if (hasKey(a, "slope")) data.bSlope = intOr(a, "slope", 0) ? TRUE : FALSE;
if (hasKey(a, "nonslip")) data.bNonslip = intOr(a, "nonslip", 0) ? TRUE : FALSE;
if (hasKey(a, "slope_base")) data.bSlopeBase = intOr(a, "slope_base", 0) ? TRUE : FALSE;
if (hasKey(a, "bottom_at_start")) data.bBomSta = intOr(a, "bottom_at_start", 1) ? 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""));
}
}
// ---------------------------------------------------------------- 双跑楼梯
// ZLineStair 的缺省值照抄 CDPLTDlg 构造函数
void InitZStair(ZLineStair& data)
{
::memset(&data, 0, sizeof(ZLineStair));
data.nZs = 1; // 梯段数
data.dSteepW = 270.0;
data.dSteepH = 150.0;
data.nSteeps1 = 11;
data.nSteeps2 = 11;
data.dStairTW = 2560.0; // 梯段总宽
data.dStairSW = 1230.0; // 单跑宽
data.dWellW = 100.0; // 梯井宽
data.nUpSide = 0; // 上跑位置 0 L / 1 R
data.dRestPtW = 1200.0; // 休息平台宽
data.dSbThick = 80.0; // 平台板厚
data.dStiffnW = 200.0;
data.dStiffnH = 180.0;
data.dBarH = 923.0; // 扶手高
data.dGarSize[0] = 40.0;
data.dGarSize[1] = 60.0;
data.dBarSp = 300.0; // 栏杆间距
data.dBarSize[0] = 30.0;
data.dBarSize[1] = 30.0;
data.nGShape = 0;
data.nBShape = 0;
data.dGarExt = 60.0;
data.dZGarExt = 60.0;
CopyStr(data.szGarMat, 256, L"不锈钢");
CopyStr(data.szBarMat, 256, L"不锈钢");
data.dBomEl = 0.0;
}
void ZStairFromArgs(ZLineStair& data, const nlohmann::json& a, bool bMerge)
{
if (!bMerge) InitZStair(data);
if (hasKey(a, "steep_count1")) data.nSteeps1 = intOr(a, "steep_count1", data.nSteeps1);
if (hasKey(a, "steep_count2")) data.nSteeps2 = intOr(a, "steep_count2", data.nSteeps2);
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, "total_width")) data.dStairTW = numOr(a, "total_width", data.dStairTW);
if (hasKey(a, "run_width")) data.dStairSW = numOr(a, "run_width", data.dStairSW);
if (hasKey(a, "well_width")) data.dWellW = numOr(a, "well_width", data.dWellW);
if (hasKey(a, "up_side_right")) data.nUpSide = intOr(a, "up_side_right", 0) ? 1 : 0;
if (hasKey(a, "bottom_elevation")) data.dBomEl = numOr(a, "bottom_elevation", data.dBomEl);
if (hasKey(a, "rest_platform_width")) data.dRestPtW = numOr(a, "rest_platform_width", data.dRestPtW);
if (hasKey(a, "platform_thickness")) data.dSbThick = numOr(a, "platform_thickness", data.dSbThick);
if (hasKey(a, "bar_height")) data.dBarH = numOr(a, "bar_height", data.dBarH);
if (hasKey(a, "bar_space")) data.dBarSp = numOr(a, "bar_space", data.dBarSp);
if (hasKey(a, "material")) CopyStr(data.szGarMat, 256, wstrOr(a, "material", L""));
if (hasKey(a, "rail_material")) CopyStr(data.szBarMat, 256, wstrOr(a, "rail_material", L""));
// ---- 精细字段(CDPLTDlg 里其余那些)----
if (hasKey(a, "flight_count")) data.nZs = intOr(a, "flight_count", data.nZs);
if (hasKey(a, "stiffener_width")) data.dStiffnW = numOr(a, "stiffener_width", data.dStiffnW);
if (hasKey(a, "stiffener_height")) data.dStiffnH = numOr(a, "stiffener_height", data.dStiffnH);
if (hasKey(a, "handrail_size_w")) data.dGarSize[0] = numOr(a, "handrail_size_w", data.dGarSize[0]);
if (hasKey(a, "handrail_size_h")) data.dGarSize[1] = numOr(a, "handrail_size_h", data.dGarSize[1]);
if (hasKey(a, "handrail_ext")) data.dGarExt = numOr(a, "handrail_ext", data.dGarExt);
if (hasKey(a, "handrail_ext_z")) data.dZGarExt = numOr(a, "handrail_ext_z", data.dZGarExt);
if (hasKey(a, "handrail_shape")) data.nGShape = intOr(a, "handrail_shape", data.nGShape);
if (hasKey(a, "rail_size_w")) data.dBarSize[0] = numOr(a, "rail_size_w", data.dBarSize[0]);
if (hasKey(a, "rail_size_h")) data.dBarSize[1] = numOr(a, "rail_size_h", data.dBarSize[1]);
if (hasKey(a, "rail_shape")) data.nBShape = intOr(a, "rail_shape", data.nBShape);
if (hasKey(a, "side_beam_w")) data.dSideBeamW = numOr(a, "side_beam_w", data.dSideBeamW);
if (hasKey(a, "side_beam_h")) data.dSideBeamH = numOr(a, "side_beam_h", data.dSideBeamH);
}
nlohmann::json ZStairToJson(const ZLineStair& d)
{
nlohmann::json o;
o["steep_height"] = d.dSteepH;
o["steep_width"] = d.dSteepW;
o["steep_count1"] = d.nSteeps1;
o["steep_count2"] = d.nSteeps2;
o["total_width"] = d.dStairTW;
o["run_width"] = d.dStairSW;
o["well_width"] = d.dWellW;
o["up_side_right"] = (d.nUpSide != 0);
o["bottom_elevation"] = d.dBomEl;
o["rest_platform_width"] = d.dRestPtW;
o["platform_thickness"] = d.dSbThick;
o["bar_height"] = d.dBarH;
o["bar_space"] = d.dBarSp;
o["material"] = acharToUtf8(d.szGarMat);
o["rail_material"] = acharToUtf8(d.szBarMat);
o["flight_count"] = d.nZs;
o["stiffener_width"] = d.dStiffnW;
o["stiffener_height"] = d.dStiffnH;
o["handrail_size_w"] = d.dGarSize[0];
o["handrail_size_h"] = d.dGarSize[1];
o["handrail_ext"] = d.dGarExt;
o["handrail_ext_z"] = d.dZGarExt;
o["handrail_shape"] = d.nGShape;
o["rail_size_w"] = d.dBarSize[0];
o["rail_size_h"] = d.dBarSize[1];
o["rail_shape"] = d.nBShape;
o["side_beam_w"] = d.dSideBeamW;
o["side_beam_h"] = d.dSideBeamH;
return o;
}
// LineStair 的材质/厂家/编号不在 bar 里,单独组装(钢梯/直线楼梯共用)
void LineStairToJson(nlohmann::json& o, const LineStair& data)
{
o["material"] = acharToUtf8(data.m_sMaterial);
o["factory"] = acharToUtf8(data.m_sFactory);
o["no"] = acharToUtf8(data.m_sNo);
o["width"] = data.dW;
o["steep_height"] = data.dSteepH;
o["steep_width"] = data.dSteepW;
o["steep_thickness"] = data.dSteept;
o["steep_count"] = data.nSteepS;
o["side_width"] = data.dSideW;
o["side_thickness"] = data.dSidet;
o["symbol"] = data.nSymbol;
o["in_slab_3d"] = (data.bShowInSlab3d != FALSE);
o["left_beam"] = (data.bLBeam != FALSE);
o["right_beam"] = (data.bRBeam != FALSE);
o["slope"] = (data.bSlope != FALSE);
o["nonslip"] = (data.bNonslip != FALSE);
o["slope_base"] = (data.bSlopeBase != FALSE);
o["with_rail"] = (data.bGuardBar != FALSE);
}
// ---------------------------------------------------------------------------
// 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 里;统一用 LineStairToJson 组装
body["params"] = nlohmann::json::object();
body["params"]["diameter"] = data.bar.dD;
body["params"]["bar_thickness"] = data.bar.dt;
body["params"]["bottom_elevation"] = data.bar.dBomEl;
body["params"]["top_height"] = data.bar.dH;
body["params"]["first_height"] = data.bar.dH1;
body["params"]["post_space"] = data.bar.dP;
LineStairToJson(body["params"], data);
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)
// ---------------------------------------------------------------------------
// add_aec_z_stair —— 双跑楼梯(PL_SPLT,AecDbZLineStair)
// ---------------------------------------------------------------------------
class ToolAddZStair : public mcp_Tool
{
public:
std::string private_name() const override { return "add_aec_z_stair"; }
std::string private_description() const override
{
return "建双跑楼梯(菜单 PL_SPLT 双跑楼梯,实体 AecDbZLineStair):给基点 + 跑步方向点,"
"再给踏步(高/宽/两跑踏步数)、梯段宽、梯井、休息平台、扶手栏杆等关键尺寸"
"(缺省照抄 CDPLTDlg,其余字段用对话框缺省)。图层 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_height": { "type": "number", "description": "踏步高,缺省 150" },
"steep_width": { "type": "number", "description": "踏步宽,缺省 270" },
"steep_count1": { "type": "integer", "description": "第一跑踏步数,缺省 11" },
"steep_count2": { "type": "integer", "description": "第二跑踏步数,缺省 11" },
"total_width": { "type": "number", "description": "梯段总宽,缺省 2560" },
"run_width": { "type": "number", "description": "单跑梯宽,缺省 1230" },
"well_width": { "type": "number", "description": "梯井宽,缺省 100" },
"up_side_right": { "type": "boolean", "description": "上跑在右侧(缺省左)" },
"bottom_elevation": { "type": "number", "description": "底标高,缺省 0" },
"rest_platform_width": { "type": "number", "description": "休息平台宽,缺省 1200" },
"platform_thickness": { "type": "number", "description": "平台板厚,缺省 80" },
"bar_height": { "type": "number", "description": "扶手高,缺省 923" },
"bar_space": { "type": "number", "description": "栏杆间距,缺省 300" },
"material": { "type": "string", "description": "扶手材质,缺省 不锈钢" },
"rail_material": { "type": "string", "description": "栏杆材质,缺省 不锈钢" },
"layer": { "type": "string", "description": "图层,缺省 LAY_CSTAIR" },
"color": { "type": "integer", "description": "ACI 颜色号,256=ByLayer" }
},
"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 pt1(0.0, 0.0, 0.0), pt2(0.0, 0.0, 0.0);
if (!readPoint(args, "start", pt1) || !readPoint(args, "direction_point", pt2))
return errJson(kErrBadParam, L"需要 start / direction_point([x,y])");
if (pt1.distanceTo(pt2) < 1.0)
return errJson(kErrBadParam, L"基点和方向点距离太近");
ZLineStair data;
ZStairFromArgs(data, args, false);
AecDbZLineStair* pStair = new AecDbZLineStair();
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->SetZLineStairData(data);
pStair->SetBaseCurve(pt1, pt2);
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"] = "z_stair";
body["handle"] = HandleOfId(idNew);
body["params"] = ZStairToJson(data);
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolAddZStair, AddAecZStairTool)
class ToolUpdateZStair : public mcp_Tool
{
public:
std::string private_name() const override { return "update_aec_z_stair"; }
std::string private_description() const override
{
return "改双跑楼梯:踏步/梯段宽/梯井/平台/扶手栏杆尺寸,只传要改的字段;"
"给 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" }, "description": "新基点 [x,y]" },
"direction_point": { "type": "array", "items": { "type": "number" }, "description": "新方向点 [x,y]" },
"steep_height": { "type": "number" }, "steep_width": { "type": "number" },
"steep_count1": { "type": "integer" }, "steep_count2": { "type": "integer" },
"total_width": { "type": "number" }, "run_width": { "type": "number" },
"well_width": { "type": "number" }, "up_side_right": { "type": "boolean" },
"bottom_elevation": { "type": "number" },
"rest_platform_width": { "type": "number" }, "platform_thickness": { "type": "number" },
"bar_height": { "type": "number" }, "bar_space": { "type": "number" },
"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));
AecDbZLineStair* pStair = AecDbZLineStair::cast(pEnt);
if (pStair == NULL)
{
const std::string sCls = classNameOf(pEnt);
pEnt->close();
return errJson(kErrWrongType, L"实体 " + utf8ToWstr(sCls) + L" 不是双跑楼梯(AecDbZLineStair)");
}
ZLineStair data;
::memset(&data, 0, sizeof(ZLineStair));
pStair->GetZLineStairData(data);
ZStairFromArgs(data, args, true);
AcGePoint3d pt1(0.0, 0.0, 0.0), pt2(0.0, 0.0, 0.0);
if (readPoint(args, "start", pt1) && readPoint(args, "direction_point", pt2) &&
pt1.distanceTo(pt2) > 1.0)
{
pStair->SetBaseCurve(pt1, pt2);
}
pStair->SetZLineStairData(data);
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"] = "z_stair";
body["handle"] = sHandle;
body["params"] = ZStairToJson(data);
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolUpdateZStair, UpdateAecZStairTool)
// ---------------------------------------------------------------------------
// add_aec_line_seg_stair —— 直线楼梯(PL_ZXLT,AecDbLineSegStair)
// ---------------------------------------------------------------------------
class ToolAddLineSegStair : public mcp_Tool
{
public:
std::string private_name() const override { return "add_aec_line_seg_stair"; }
std::string private_description() const override
{
return "建直线楼梯(菜单 PL_ZXLT 直线楼梯,实体 AecDbLineSegStair):给起点 + 放置方向点,"
"再给踏步(高/宽/个数)、梯宽、梯梁、材质等(缺省照抄 CZXLTDlg)。"
"与 add_aec_line_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]" },
"width": { "type": "number", "description": "梯宽,缺省 1200" },
"steep_height": { "type": "number", "description": "踏步高,缺省 150" },
"steep_width": { "type": "number", "description": "踏步宽,缺省 270" },
"steep_count": { "type": "integer", "description": "踏步数,缺省 20" },
"bottom_elevation": { "type": "number", "description": "底标高,缺省 0" },
"steep_thickness": { "type": "number", "description": "踏步厚,缺省 8" },
"side_width": { "type": "number", "description": "梯梁宽,缺省 200" },
"side_thickness": { "type": "number", "description": "梯梁厚,缺省 40" },
"with_rail": { "type": "boolean", "description": "是否带护栏" },
"in_slab_3d": { "type": "boolean", "description": "是否画进楼板 3D" },
"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", "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 pt1(0.0, 0.0, 0.0), pt2(0.0, 0.0, 0.0);
if (!readPoint(args, "start", pt1) || !readPoint(args, "direction_point", pt2))
return errJson(kErrBadParam, L"需要 start / direction_point([x,y])");
if (pt1.distanceTo(pt2) < 1.0)
return errJson(kErrBadParam, L"起点和方向点距离太近");
LineStair data;
InitLineStair(data);
// CZXLTDlg 的缺省与 CPTLTDlg 不同:梯宽 1200、踏步 150/270、20 级、钢筋混凝土
data.dW = 1200.0;
data.dSteepH = 150.0;
data.dSteepW = 270.0;
data.nSteepS = 20;
data.bLBeam = TRUE;
data.bRBeam = TRUE;
CopyStr(data.m_sMaterial, 256, L"钢筋混凝土");
CopyStr(data.m_sFactory, 256, L"现场浇筑");
LineStairFromArgs(data, args, true);
if (hasKey(args, "steep_count")) data.nSteepS = intOr(args, "steep_count", data.nSteepS);
pt1.z = data.dBomEl;
pt2.z = data.dBomEl; // 与命令一致:基准曲线水平,高度全靠 data 里的踏步数
AcDbLine ln(pt1, pt2);
AecDbLineSegStair* pStair = new AecDbLineSegStair();
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(&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_seg_stair";
body["handle"] = HandleOfId(idNew);
body["params"] = nlohmann::json::object();
LineStairToJson(body["params"], data);
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolAddLineSegStair, AddAecLineSegStairTool)
class ToolUpdateLineSegStair : public mcp_Tool
{
public:
std::string private_name() const override { return "update_aec_line_seg_stair"; }
std::string private_description() const override
{
return "改直线楼梯(AecDbLineSegStair):踏步/梯宽/梯梁/材质,只传要改的字段;"
"给 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" } },
"width": { "type": "number" }, "steep_height": { "type": "number" },
"steep_width": { "type": "number" }, "steep_count": { "type": "integer" },
"bottom_elevation": { "type": "number" }, "steep_thickness": { "type": "number" },
"side_width": { "type": "number" }, "side_thickness": { "type": "number" },
"with_rail": { "type": "boolean" }, "in_slab_3d": { "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));
AecDbLineSegStair* pStair = AecDbLineSegStair::cast(pEnt);
if (pStair == NULL)
{
const std::string sCls = classNameOf(pEnt);
pEnt->close();
return errJson(kErrWrongType, L"实体 " + utf8ToWstr(sCls) + L" 不是直线楼梯(AecDbLineSegStair)");
}
LineStair data;
::memset(&data, 0, sizeof(LineStair));
pStair->GetLineStairData(data);
LineStairFromArgs(data, args, true);
if (hasKey(args, "steep_count")) data.nSteepS = intOr(args, "steep_count", data.nSteepS);
AcGePoint3d pt1(0.0, 0.0, 0.0), pt2(0.0, 0.0, 0.0);
if (readPoint(args, "start", pt1) && readPoint(args, "direction_point", pt2) &&
pt1.distanceTo(pt2) > 1.0)
{
pt1.z = data.dBomEl;
pt2.z = data.dBomEl;
AcDbLine ln(pt1, pt2);
pStair->SetBaseCurve(&ln);
}
pStair->SetLineStairData(data);
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_seg_stair";
body["handle"] = sHandle;
body["params"] = nlohmann::json::object();
LineStairToJson(body["params"], data);
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolUpdateLineSegStair, UpdateAecLineSegStairTool)
// ---------------------------------------------------------------------------
// add_aec_arc_stair —— 圆弧楼梯(PL_YHLT,AecDbArcStair)
// ---------------------------------------------------------------------------
class ToolAddArcStair : public mcp_Tool
{
public:
std::string private_name() const override { return "add_aec_arc_stair"; }
std::string private_description() const override
{
return "建圆弧楼梯(菜单 PL_YHLT 圆弧楼梯,实体 AecDbArcStair):给一条圆弧(curve=ARC 的 handle)"
"当中心线,再给踏步/梯宽/梯梁/材质等(缺省照抄 CPTLTDlg);start_at_bottom 指定从圆弧"
"起点还是终点起步。图层 LAY_CSTAIR。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"curve": { "type": "string", "description": "圆弧中心线的 handle(可选,给了就用它)" },
"center": { "type": "array", "items": { "type": "number" }, "description": "没给 curve 时:圆心 [x,y,z]" },
"radius": { "type": "number", "description": "没给 curve 时:半径" },
"start_angle": { "type": "number", "description": "没给 curve 时:起始角(度,缺省 0)" },
"end_angle": { "type": "number", "description": "没给 curve 时:终止角(度,缺省 180)" },
"start_at_bottom": { "type": "boolean", "description": "从圆弧起点起步(缺省 true)" },
"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" },
"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" }
},
})");
}
nlohmann::json Execute(const nlohmann::json& args) override
{
if (workingDb() == NULL) return errJson(kErrNoDocument, L"没有活动图纸");
DocLock lock;
if (!lock.isLocked()) return errJson(kErrInternal, L"拿不到图纸写锁(有别的命令在跑?)");
// 圆弧中心线:给 curve 就用已有圆弧;否则用 center/radius/start_angle/end_angle 现造一个。
// (命令是拾取圆弧,工具这边多一条"直接给参数"的路,省得先画一条 ARC)
AcDbArc* pArcClone = NULL;
if (hasKey(args, "curve"))
{
const std::wstring sCurve = wstrOr(args, "curve", L"");
AcDbEntity* pArcEnt = openByHandle(acharToUtf8(sCurve.c_str()), AcDb::kForRead);
AcDbArc* pArc = (pArcEnt != NULL) ? AcDbArc::cast(pArcEnt) : NULL;
if (pArc == NULL)
{
if (pArcEnt != NULL) pArcEnt->close();
return errJson(kErrWrongType, L"curve 必须是圆弧(ARC)实体:" + sCurve);
}
pArcClone = (AcDbArc*)pArc->clone();
pArcEnt->close();
}
else
{
AcGePoint3d ptCen(0.0, 0.0, 0.0);
if (!readPoint(args, "center", ptCen))
return errJson(kErrBadParam, L"要么给 curve(圆弧 handle),要么给 center + radius");
const double dR = numOr(args, "radius", 0.0);
if (dR <= 0.0) return errJson(kErrBadParam, L"radius 必须 > 0");
const double dA1 = CoreTools::A2R(numOr(args, "start_angle", 0.0));
const double dA2 = CoreTools::A2R(numOr(args, "end_angle", 180.0));
pArcClone = new AcDbArc(ptCen, AcGeVector3d::kZAxis, dR, dA1, dA2);
}
LineStair data;
InitLineStair(data);
LineStairFromArgs(data, args, true);
data.bBomSta = hasKey(args, "start_at_bottom") ? (intOr(args, "start_at_bottom", 1) ? TRUE : FALSE) : TRUE;
AecDbArcStair* pStair = new AecDbArcStair();
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(pArcClone); // 先设曲线(它会清专业数据)
delete pArcClone;
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"] = "arc_stair";
body["handle"] = HandleOfId(idNew);
body["params"] = nlohmann::json::object();
LineStairToJson(body["params"], data);
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolAddArcStair, AddAecArcStairTool)
class ToolUpdateArcStair : public mcp_Tool
{
public:
std::string private_name() const override { return "update_aec_arc_stair"; }
std::string private_description() const override
{
return "改圆弧楼梯(AecDbArcStair):踏步/梯宽/梯梁/扶手/材质,只传要改的字段;"
"给 curve 可以换中心线圆弧。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"handle": { "type": "string", "description": "楼梯实体 handle" },
"curve": { "type": "string", "description": "新的圆弧中心线 handle(可选)" },
"start_at_bottom": { "type": "boolean" },
"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" },
"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));
AecDbArcStair* pStair = AecDbArcStair::cast(pEnt);
if (pStair == NULL)
{
const std::string sCls = classNameOf(pEnt);
pEnt->close();
return errJson(kErrWrongType, L"实体 " + utf8ToWstr(sCls) + L" 不是圆弧楼梯(AecDbArcStair)");
}
LineStair data;
::memset(&data, 0, sizeof(LineStair));
pStair->GetLineStairData(data);
LineStairFromArgs(data, args, true);
if (hasKey(args, "start_at_bottom"))
data.bBomSta = intOr(args, "start_at_bottom", 1) ? TRUE : FALSE;
if (hasKey(args, "curve"))
{
const std::wstring sCurve = wstrOr(args, "curve", L"");
AcDbEntity* pArcEnt = openByHandle(acharToUtf8(sCurve.c_str()), AcDb::kForRead);
AcDbArc* pArc = (pArcEnt != NULL) ? AcDbArc::cast(pArcEnt) : NULL;
if (pArc == NULL)
{
if (pArcEnt != NULL) pArcEnt->close();
pEnt->close();
return errJson(kErrWrongType, L"curve 必须是圆弧(ARC)实体:" + sCurve);
}
AcDbArc* pArcClone = (AcDbArc*)pArc->clone();
pArcEnt->close();
pStair->SetBaseCurve(pArcClone);
delete pArcClone;
}
pStair->SetLineStairData(data);
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"] = "arc_stair";
body["handle"] = sHandle;
body["params"] = nlohmann::json::object();
LineStairToJson(body["params"], data);
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolUpdateArcStair, UpdateAecArcStairTool)