Files
envi-code/SourceCode/Code2026/cad_mcp_aec/aec_mcp_attachment_tools.cpp
T
company-desktop 6f83ab55f1 feat: 带形窗(PL_DXCH)+ 支吊架三个类(支架/吊架/支墩)
一、带形窗 AecDbZLineXOpening(新文件 aec_mcp_zline_window_tools.cpp)
命令:GetPathOnWall 取墙路径 -> CDXCHDlg 的 OpeningData(dXData[0..10] 存柱距/首排高/
框料厚宽/玻璃厚/外伸×2/挑出×2/玻璃内偏/挡板厚)-> SetOpeningData(data, arIds)(数组重载)
+ SetZLinePoints + KEY_StartDir -> 入库后逐面墙 AppendAssocPart + SubtractDraw2d。
按总原则:add 只给窗高/窗台高/柱距/凸窗,update 收全套框料/玻璃/外伸/挑出/挡板。

二、支吊架(新文件 aec_mcp_holder_tools.cpp,6 个工具)
- PL_FZZJ 支架 AecDbHolder、PL_FZDJ 吊架 AecDbHanger、PL_FZZD 支墩 AecDbBossHolder
套路一致:对话框填结构体 -> jig 画路径 -> 点一下给支臂方向 -> 沿路径按
"个数=路径长/间距" 摆一串,每个 SetPosition(ptPos, vtLL) + LAY_SUPPORT。
工具里 points/spacing/direction_point,一次建整串,返回 count + handles。

顺带把"沿墙取路径"从附属构件里提到 aec_mcp_common(buildWallPath),
带形窗与阳台/台阶共用;前向声明放全局,避免和 using namespace aec_mcp 打架。

两个坑:
1. PL_FZDJ 建的是 AecDbHanger,不是 AecDbPipeHanger(后者没被这条命令用到)
2. AcDbCurve::getFirstDeriv 收参数、getPointAtDist 收距离,混用会报"沿路径取点/切线失败";
   改成 getParamAtDist 再取切线

做不了的:PL_ZJCH 转角窗 —— PL_ZJCHCmdFnc 打开对话框后函数体是空的、注册也被注释掉了。

实测(R230 Debug,空图):
- 带形窗(墙 12000、沿墙 1000~9000、柱距 1200)-> 311,bbox x 967~9000;
  精修框料 80/玻璃 40/首排 900/加挡板 回读正确
- 支架(9000 路径 / 间距 1500)-> 6 个;吊架 -> 4 个;支墩 -> 4 个;三者 update 都回读正确

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

1297 lines
50 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_attachment_tools.cpp
// cad_mcp_aec 的附属构件工具(工作计划第 6 项)
// * add/update_aec_balcony —— 放置阳台 AecDbBalcony(PL_FZYT)
//
// 命令的做法(ArchCmd/command2.cpp PL_FZYTCmdFnc):
// GetPathOnWall() 拿"墙上的路径段 + 墙数据 + 墙侧方向" -> CFZYTDlg 填 BalconyData
// -> data.dBomEl += 墙底标高;data.dRailWidth += 墙厚*0.5
// -> new AecDbBalcony + SetBalconyData + SetBaseCurve(path)
// + KEY_StartDir = 墙侧方向 + KEY_2DSTYLE = 0 -> 图层 LAY_BALCONY
//
// 命令里的 GetPathOnWall 是交互的(两个点 + 选墙 + 点一下确认墙侧方向)。
// 工具这版:给 wall handle + start/end(两个点,会投影到墙中心线)+ side_point(确认墙
// 往外的那一侧),路径用"沿墙中线采样出来的多段线",直墙就是那一段直线。
// ============================================================================
#include "StdAfx.h"
#include "KTCad.h"
#include "aec_mcp_common.h"
#include "mcp_plugin_api.h"
#include "AecDbBalcony.h"
#include "AecDbXStep.h"
#include "AecDbAscent.h"
#include "AecDbXApron.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;
}
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;
}
void InitBalcony(BalconyData& d)
{
::memset(&d, 0, sizeof(BalconyData));
d.nRailEdge = 0;
d.dRailHeight = 1200.0;
d.dSlabThick = 100.0;
d.dRailWidth = 1500.0; // 阳台宽(往外挑多少)
d.dGirderThick = 150.0;
d.dBomEl = 160.0; // 相对墙底
d.dBeamH = 100.0;
d.dBeamW = 100.0;
d.bAddOpening = FALSE;
d.duFrame = 1200.0;
d.dvFrame = 30.0;
d.dWinHeight = 1000.0;
CopyStr(d.m_sMaterial, 256, L"铝合金玻璃");
CopyStr(d.m_sFactory, 256, L"现场定制");
}
// 阳台参数不多,add/update 都给全
void BalconyFromArgs(BalconyData& d, const nlohmann::json& a, bool bMerge)
{
if (!bMerge) InitBalcony(d);
if (hasKey(a, "rail_height")) d.dRailHeight = numOr(a, "rail_height", d.dRailHeight);
if (hasKey(a, "width")) d.dRailWidth = numOr(a, "width", d.dRailWidth);
if (hasKey(a, "slab_thickness")) d.dSlabThick = numOr(a, "slab_thickness", d.dSlabThick);
if (hasKey(a, "girder_thickness")) d.dGirderThick = numOr(a, "girder_thickness", d.dGirderThick);
if (hasKey(a, "bottom_elevation")) d.dBomEl = numOr(a, "bottom_elevation", d.dBomEl); // 相对墙底
if (hasKey(a, "beam_height")) d.dBeamH = numOr(a, "beam_height", d.dBeamH);
if (hasKey(a, "beam_width")) d.dBeamW = numOr(a, "beam_width", d.dBeamW);
if (hasKey(a, "rail_edge")) d.nRailEdge = intOr(a, "rail_edge", d.nRailEdge);
if (hasKey(a, "add_opening")) d.bAddOpening = intOr(a, "add_opening", 0) ? TRUE : FALSE;
if (hasKey(a, "frame_vertical")) d.duFrame = numOr(a, "frame_vertical", d.duFrame);
if (hasKey(a, "frame_horizontal")) d.dvFrame = numOr(a, "frame_horizontal", d.dvFrame);
if (hasKey(a, "window_height")) d.dWinHeight = numOr(a, "win_height", d.dWinHeight);
if (hasKey(a, "material")) CopyStr(d.m_sMaterial, 256, wstrOr(a, "material", L""));
if (hasKey(a, "factory")) CopyStr(d.m_sFactory, 256, wstrOr(a, "factory", L""));
if (hasKey(a, "no")) CopyStr(d.m_sNo, 256, wstrOr(a, "no", L""));
}
nlohmann::json BalconyToJson(const BalconyData& d)
{
nlohmann::json o;
o["rail_height"] = d.dRailHeight;
o["width"] = d.dRailWidth;
o["slab_thickness"] = d.dSlabThick;
o["girder_thickness"] = d.dGirderThick;
o["bottom_elevation"] = d.dBomEl;
o["beam_height"] = d.dBeamH;
o["beam_width"] = d.dBeamW;
o["rail_edge"] = d.nRailEdge;
o["add_opening"] = (d.bAddOpening != FALSE);
o["frame_vertical"] = d.duFrame;
o["frame_horizontal"] = d.dvFrame;
o["window_height"] = d.dWinHeight;
o["material"] = acharToUtf8(d.m_sMaterial);
o["factory"] = acharToUtf8(d.m_sFactory);
o["no"] = acharToUtf8(d.m_sNo);
return o;
}
}
// ---------------------------------------------------------------------------
// add_aec_balcony —— 放置阳台(PL_FZYT)
// ---------------------------------------------------------------------------
class ToolAddBalcony : public mcp_Tool
{
public:
std::string private_name() const override { return "add_aec_balcony"; }
std::string private_description() const override
{
return "沿墙放置阳台(菜单 PL_FZYT 放置阳台,实体 AecDbBalcony):给墙 handle + 起止点"
"(投影到墙中心线)+ 一个确认墙外侧方向的点,再给挑出宽度、栏杆高、板厚、梁尺寸等"
"(缺省照抄 CFZYTDlg)。阳台底标高按\"相对墙底\"给。图层 LAY_BALCONY。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"wall": { "type": "string", "description": "墙(AecDbXWall)的 handle" },
"start": { "type": "array", "items": { "type": "number" }, "description": "阳台沿墙的起点 [x,y]" },
"end": { "type": "array", "items": { "type": "number" }, "description": "阳台沿墙的终点 [x,y]" },
"side_point": { "type": "array", "items": { "type": "number" }, "description": "墙外侧任意一点 [x,y],用来定挑出方向" },
"width": { "type": "number", "description": "阳台挑出宽度,缺省 1500" },
"rail_height": { "type": "number", "description": "栏杆高,缺省 1200" },
"slab_thickness": { "type": "number", "description": "底板厚,缺省 100" },
"girder_thickness": { "type": "number", "description": "阳台梁厚,缺省 150" },
"beam_height": { "type": "number", "description": "阳台梁高,缺省 100" },
"beam_width": { "type": "number", "description": "阳台梁宽,缺省 100" },
"bottom_elevation": { "type": "number", "description": "底板底标高(相对墙底),缺省 160" },
"rail_edge": { "type": "integer", "description": "无扶手的边(按对话框选项)" },
"add_opening": { "type": "boolean", "description": "阳台栏板上加门窗" },
"frame_vertical": { "type": "number", "description": "竖框高,缺省 1200" },
"frame_horizontal": { "type": "number", "description": "横框宽,缺省 30" },
"window_height": { "type": "number", "description": "窗高,缺省 1000" },
"material": { "type": "string", "description": "材质,缺省 铝合金玻璃" },
"factory": { "type": "string", "description": "厂家,缺省 现场定制" },
"no": { "type": "string", "description": "编号" },
"layer": { "type": "string", "description": "图层,缺省 LAY_BALCONY" },
"color": { "type": "integer", "description": "ACI 颜色号,256=ByLayer" }
},
"required": [ "wall", "start", "end", "side_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), ptSide(0.0, 0.0, 0.0);
if (!readPoint(args, "start", ptS) || !readPoint(args, "end", ptE))
return errJson(kErrBadParam, L"需要 start / end([x,y])");
readPoint(args, "side_point", ptSide);
if (!hasKey(args, "wall")) return errJson(kErrBadParam, L"缺少 wall(墙 handle)");
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"handle " + sWall + L" 是 " + utf8ToWstr(sCls) + L",不是墙");
}
WallData wd;
::memset(&wd, 0, sizeof(WallData));
pWall->GetWallData(wd);
std::wstring err;
AcGeVector3d vtDir(0.0, 1.0, 0.0);
AcDbPolyline* pPath = buildWallPath(pWall, ptS, ptE, ptSide, vtDir, err);
pWallEnt->close();
if (pPath == NULL) return errJson(kErrBadParam, err);
BalconyData data;
BalconyFromArgs(data, args, false);
data.dBomEl += wd.dBomEl; // 命令里:底标高 += 墙底
data.dRailWidth += wd.dThick * 0.5; // 命令里:挑出宽度 += 半墙厚
AecDbBalcony* pBalcony = new AecDbBalcony();
std::wstring sLay = hasKey(args, "layer") ? wstrOr(args, "layer", L"") : L"";
if (sLay.empty()) sLay = L"LAY_BALCONY";
AcDbObjectId idLay = KTSetting::InitLayer(sLay.c_str());
if (!idLay.isNull()) pBalcony->setLayer(idLay);
if (hasKey(args, "color")) pBalcony->setColorIndex((Adesk::UInt16)intOr(args, "color", 256));
pBalcony->SetBalconyData(data);
pBalcony->SetBaseCurve(pPath);
pBalcony->GetSpecialtyData()->SetAt(KEY_StartDir, vtDir);
pBalcony->GetSpecialtyData()->SetAt(KEY_2DSTYLE, 0);
delete pPath; // SetBaseCurve 会自己 clone
AcDbObjectId idNew;
Acad::ErrorStatus es = addToSpace(pBalcony, &idNew);
if (es != Acad::eOk)
{
delete pBalcony;
return errJson(kErrInternal, std::wstring(L"加入模型空间失败,err=") + std::to_wstring((int)es));
}
touchEntity(pBalcony);
nlohmann::json body;
body["ok"] = true;
body["type"] = "balcony";
body["direction"] = { vtDir.x, vtDir.y, vtDir.z };
body["params"] = BalconyToJson(data);
pBalcony->close();
body["handle"] = HandleOfId(idNew);
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolAddBalcony, AddAecBalconyTool)
// ---------------------------------------------------------------------------
// update_aec_balcony —— 改阳台(字段全收)
// ---------------------------------------------------------------------------
class ToolUpdateBalcony : public mcp_Tool
{
public:
std::string private_name() const override { return "update_aec_balcony"; }
std::string private_description() const override
{
return "修改阳台(AecDbBalcony):只传要改的字段(挑出宽度/栏杆高/板厚/梁尺寸/栏板门窗/材质编号);"
"给 wall+start+end+side_point 可以换位置和走向。注意 bottom_elevation 这里按**绝对标高**给。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"handle": { "type": "string", "description": "阳台实体 handle" },
"wall": { "type": "string", "description": "新的墙 handle(可选,改位置时一起给)" },
"start": { "type": "array", "items": { "type": "number" } },
"end": { "type": "array", "items": { "type": "number" } },
"side_point": { "type": "array", "items": { "type": "number" } },
"width": { "type": "number" }, "rail_height": { "type": "number" },
"slab_thickness": { "type": "number" }, "girder_thickness": { "type": "number" },
"beam_height": { "type": "number" }, "beam_width": { "type": "number" },
"bottom_elevation": { "type": "number", "description": "底板底标高(绝对)" },
"rail_edge": { "type": "integer" }, "add_opening": { "type": "boolean" },
"frame_vertical": { "type": "number" }, "frame_horizontal": { "type": "number" },
"window_height": { "type": "number" },
"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));
AecDbBalcony* pBalcony = AecDbBalcony::cast(pEnt);
if (pBalcony == NULL)
{
const std::string sCls = classNameOf(pEnt);
pEnt->close();
return errJson(kErrWrongType, L"实体 " + utf8ToWstr(sCls) + L" 不是阳台(AecDbBalcony)");
}
BalconyData data;
::memset(&data, 0, sizeof(BalconyData));
pBalcony->GetBalconyData(data);
BalconyFromArgs(data, args, true);
// 换位置:需要 wall + start + end(side_point 可选,缺省沿用原方向)
if (hasKey(args, "wall") && hasKey(args, "start") && hasKey(args, "end"))
{
AcGePoint3d ptS(0.0, 0.0, 0.0), ptE(0.0, 0.0, 0.0), ptSide(0.0, 0.0, 0.0);
readPoint(args, "start", ptS);
readPoint(args, "end", ptE);
readPoint(args, "side_point", ptSide);
const std::wstring sWall = wstrOr(args, "wall", L"");
AcDbEntity* pWallEnt = openByHandle(acharToUtf8(sWall.c_str()), AcDb::kForRead);
AecDbXWall* pWall = (pWallEnt != NULL) ? AecDbXWall::cast(pWallEnt) : NULL;
if (pWall == NULL)
{
if (pWallEnt != NULL) pWallEnt->close();
pEnt->close();
return errJson(kErrWrongType, L"wall 不是墙或打不开:" + sWall);
}
if (!hasKey(args, "side_point"))
{
AcGeVector3d vtOld(0.0, 0.0, 0.0);
if (pBalcony->GetSpecialtyData()->GetAt(KEY_StartDir, vtOld))
ptSide = ptS + vtOld * 1000.0;
else
ptSide = ptS + AcGeVector3d(0.0, 1000.0, 0.0);
}
std::wstring err;
AcGeVector3d vtDir(0.0, 1.0, 0.0);
AcDbPolyline* pPath = buildWallPath(pWall, ptS, ptE, ptSide, vtDir, err);
pWallEnt->close();
if (pPath == NULL)
{
pEnt->close();
return errJson(kErrBadParam, err);
}
pBalcony->SetBaseCurve(pPath);
delete pPath;
pBalcony->GetSpecialtyData()->SetAt(KEY_StartDir, vtDir);
}
pBalcony->SetBalconyData(data);
if (hasKey(args, "color")) pBalcony->setColorIndex((Adesk::UInt16)intOr(args, "color", 256));
pBalcony->ReCreateDraw3d();
touchEntity(pBalcony);
nlohmann::json body;
body["ok"] = true;
body["type"] = "balcony";
body["handle"] = sHandle;
body["params"] = BalconyToJson(data);
pEnt->close();
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolUpdateBalcony, UpdateAecBalconyTool)
// ===========================================================================
// 台阶(PL_FZTJ,AecDbXStep)—— 沿墙路径
// ===========================================================================
namespace
{
void InitXStep(XStepData& d)
{
::memset(&d, 0, sizeof(XStepData));
d.dW = 450.0; // 台阶总宽
d.dSteepW = 300.0;
d.dSteepH = 150.0;
d.nSteepS = 3;
d.dBaseEl = 0.0; // 相对墙底
d.dBaseW = 2000.0; // 平台进深
d.nBaseA = 0;
d.nStpStyle = 0;
d.nStrStyle = 0;
}
void XStepFromArgs(XStepData& d, const nlohmann::json& a, bool bMerge)
{
if (!bMerge) InitXStep(d);
if (hasKey(a, "width")) d.dW = numOr(a, "width", d.dW);
if (hasKey(a, "steep_width")) d.dSteepW = numOr(a, "steep_width", d.dSteepW);
if (hasKey(a, "steep_height")) d.dSteepH = numOr(a, "steep_height", d.dSteepH);
if (hasKey(a, "steep_count")) d.nSteepS = intOr(a, "steep_count", d.nSteepS);
if (hasKey(a, "bottom_elevation")) d.dBaseEl = numOr(a, "bottom_elevation", d.dBaseEl);
if (hasKey(a, "base_width")) d.dBaseW = numOr(a, "base_width", d.dBaseW);
if (hasKey(a, "base_align")) d.nBaseA = intOr(a, "base_align", d.nBaseA);
if (hasKey(a, "step_style")) d.nStpStyle = intOr(a, "step_style", d.nStpStyle);
if (hasKey(a, "struct_style")) d.nStrStyle = intOr(a, "struct_style", d.nStrStyle);
}
nlohmann::json XStepToJson(const XStepData& d)
{
nlohmann::json o;
o["width"] = d.dW;
o["steep_width"] = d.dSteepW;
o["steep_height"] = d.dSteepH;
o["steep_count"] = d.nSteepS;
o["bottom_elevation"] = d.dBaseEl;
o["base_width"] = d.dBaseW;
o["base_align"] = d.nBaseA;
o["step_style"] = d.nStpStyle;
o["struct_style"] = d.nStrStyle;
return o;
}
}
class ToolAddXStep : public mcp_Tool
{
public:
std::string private_name() const override { return "add_aec_xstep"; }
std::string private_description() const override
{
return "沿墙放置台阶(菜单 PL_FZTJ 放置台阶,实体 AecDbXStep):给墙 handle + 起止点"
"(投影到墙中心线)+ 墙外侧点,再给台阶宽/踏步/平台进深等(缺省照抄 CFZTJDlg)。"
"bottom_elevation 按相对墙底给。图层 LAY_BALCONY。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"wall": { "type": "string", "description": "墙(AecDbXWall)的 handle" },
"start": { "type": "array", "items": { "type": "number" } },
"end": { "type": "array", "items": { "type": "number" } },
"side_point": { "type": "array", "items": { "type": "number" }, "description": "墙外侧任意一点,定台阶朝外方向" },
"width": { "type": "number", "description": "台阶总宽,缺省 450" },
"steep_width": { "type": "number", "description": "踏步宽,缺省 300" },
"steep_height": { "type": "number", "description": "踏步高,缺省 150" },
"steep_count": { "type": "integer", "description": "踏步数,缺省 3" },
"bottom_elevation": { "type": "number", "description": "底标高(相对墙底),缺省 0" },
"base_width": { "type": "number", "description": "平台进深,缺省 2000" },
"base_align": { "type": "integer", "description": "0 平台左 / 1 平台右" },
"step_style": { "type": "integer", "description": "台阶方式" },
"struct_style": { "type": "integer", "description": "结构形式" },
"layer": { "type": "string", "description": "图层,缺省 LAY_BALCONY" },
"color": { "type": "integer" }
},
"required": [ "wall", "start", "end", "side_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), ptSide(0.0, 0.0, 0.0);
if (!readPoint(args, "start", ptS) || !readPoint(args, "end", ptE))
return errJson(kErrBadParam, L"需要 start / end([x,y])");
readPoint(args, "side_point", ptSide);
if (!hasKey(args, "wall")) return errJson(kErrBadParam, L"缺少 wall(墙 handle)");
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"handle " + sWall + L" 是 " + utf8ToWstr(sCls) + L",不是墙");
}
WallData wd;
::memset(&wd, 0, sizeof(WallData));
pWall->GetWallData(wd);
std::wstring err;
AcGeVector3d vtDir(0.0, 1.0, 0.0);
AcDbPolyline* pPath = buildWallPath(pWall, ptS, ptE, ptSide, vtDir, err);
pWallEnt->close();
if (pPath == NULL) return errJson(kErrBadParam, err);
XStepData data;
XStepFromArgs(data, args, false);
data.dBaseEl += wd.dBomEl; // 命令里:底标高 += 墙底
AecDbXStep* pStep = new AecDbXStep();
std::wstring sLay = hasKey(args, "layer") ? wstrOr(args, "layer", L"") : L"";
if (sLay.empty()) sLay = L"LAY_BALCONY";
AcDbObjectId idLay = KTSetting::InitLayer(sLay.c_str());
if (!idLay.isNull()) pStep->setLayer(idLay);
if (hasKey(args, "color")) pStep->setColorIndex((Adesk::UInt16)intOr(args, "color", 256));
pStep->SetXStepData(data);
pStep->SetBaseCurve(pPath);
pStep->GetSpecialtyData()->SetAt(KEY_StartDir, vtDir);
pStep->GetSpecialtyData()->SetAt(KEY_2DSTYLE, 0);
delete pPath;
AcDbObjectId idNew;
Acad::ErrorStatus es = addToSpace(pStep, &idNew);
if (es != Acad::eOk)
{
delete pStep;
return errJson(kErrInternal, std::wstring(L"加入模型空间失败,err=") + std::to_wstring((int)es));
}
touchEntity(pStep);
nlohmann::json body;
body["ok"] = true;
body["type"] = "xstep";
body["direction"] = { vtDir.x, vtDir.y, vtDir.z };
body["params"] = XStepToJson(data);
pStep->close();
body["handle"] = HandleOfId(idNew);
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolAddXStep, AddAecXStepTool)
class ToolUpdateXStep : public mcp_Tool
{
public:
std::string private_name() const override { return "update_aec_xstep"; }
std::string private_description() const override
{
return "改台阶(AecDbXStep):台阶宽/踏步/平台进深/样式,只传要改的字段;"
"给 wall+start+end+side_point 可以换位置。bottom_elevation 这里按绝对标高给。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"handle": { "type": "string", "description": "台阶实体 handle" },
"wall": { "type": "string" }, "start": { "type": "array", "items": { "type": "number" } },
"end": { "type": "array", "items": { "type": "number" } },
"side_point": { "type": "array", "items": { "type": "number" } },
"width": { "type": "number" }, "steep_width": { "type": "number" },
"steep_height": { "type": "number" }, "steep_count": { "type": "integer" },
"bottom_elevation": { "type": "number", "description": "底标高(绝对)" },
"base_width": { "type": "number" }, "base_align": { "type": "integer" },
"step_style": { "type": "integer" }, "struct_style": { "type": "integer" },
"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));
AecDbXStep* pStep = AecDbXStep::cast(pEnt);
if (pStep == NULL)
{
const std::string sCls = classNameOf(pEnt);
pEnt->close();
return errJson(kErrWrongType, L"实体 " + utf8ToWstr(sCls) + L" 不是台阶(AecDbXStep)");
}
XStepData data;
::memset(&data, 0, sizeof(XStepData));
pStep->GetXStepData(data);
XStepFromArgs(data, args, true);
if (hasKey(args, "wall") && hasKey(args, "start") && hasKey(args, "end"))
{
AcGePoint3d ptS(0.0, 0.0, 0.0), ptE(0.0, 0.0, 0.0), ptSide(0.0, 0.0, 0.0);
readPoint(args, "start", ptS);
readPoint(args, "end", ptE);
if (!readPoint(args, "side_point", ptSide))
{
AcGeVector3d vtOld(0.0, 0.0, 0.0);
if (pStep->GetSpecialtyData()->GetAt(KEY_StartDir, vtOld)) ptSide = ptS + vtOld * 1000.0;
else ptSide = ptS + AcGeVector3d(0.0, 1000.0, 0.0);
}
const std::wstring sWall = wstrOr(args, "wall", L"");
AcDbEntity* pWallEnt = openByHandle(acharToUtf8(sWall.c_str()), AcDb::kForRead);
AecDbXWall* pWall = (pWallEnt != NULL) ? AecDbXWall::cast(pWallEnt) : NULL;
if (pWall == NULL)
{
if (pWallEnt != NULL) pWallEnt->close();
pEnt->close();
return errJson(kErrWrongType, L"wall 不是墙或打不开:" + sWall);
}
std::wstring err;
AcGeVector3d vtDir(0.0, 1.0, 0.0);
AcDbPolyline* pPath = buildWallPath(pWall, ptS, ptE, ptSide, vtDir, err);
pWallEnt->close();
if (pPath == NULL) { pEnt->close(); return errJson(kErrBadParam, err); }
pStep->SetBaseCurve(pPath);
delete pPath;
pStep->GetSpecialtyData()->SetAt(KEY_StartDir, vtDir);
}
pStep->SetXStepData(data);
if (hasKey(args, "color")) pStep->setColorIndex((Adesk::UInt16)intOr(args, "color", 256));
pStep->ReCreateDraw3d();
touchEntity(pStep);
nlohmann::json body;
body["ok"] = true;
body["type"] = "xstep";
body["handle"] = sHandle;
body["params"] = XStepToJson(data);
pEnt->close();
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolUpdateXStep, UpdateAecXStepTool)
// ===========================================================================
// 坡道(PL_FZPD,AecDbAscent)—— 两点
// ===========================================================================
namespace
{
void InitAscent(AscentData& d)
{
::memset(&d, 0, sizeof(AscentData));
d.dLx = 2100.0;
d.dLy = 450.0;
d.dW = 3000.0;
d.dSideW = 300.0;
d.dTopEl = 450.0;
d.bAlignL = FALSE;
d.bAlignR = FALSE;
d.bNonslip = FALSE;
}
void AscentFromArgs(AscentData& d, const nlohmann::json& a, bool bMerge)
{
if (!bMerge) InitAscent(d);
if (hasKey(a, "run_length")) d.dLx = numOr(a, "run_length", d.dLx);
if (hasKey(a, "rise")) d.dLy = numOr(a, "rise", d.dLy);
if (hasKey(a, "width")) d.dW = numOr(a, "width", d.dW);
if (hasKey(a, "side_width")) d.dSideW = numOr(a, "side_width", d.dSideW);
if (hasKey(a, "top_elevation")) d.dTopEl = numOr(a, "top_elevation", d.dTopEl);
if (hasKey(a, "align_left")) d.bAlignL = intOr(a, "align_left", 0) ? TRUE : FALSE;
if (hasKey(a, "align_right")) d.bAlignR = intOr(a, "align_right", 0) ? TRUE : FALSE;
if (hasKey(a, "nonslip")) d.bNonslip = intOr(a, "nonslip", 0) ? TRUE : FALSE;
}
nlohmann::json AscentToJson(const AscentData& d)
{
nlohmann::json o;
o["run_length"] = d.dLx;
o["rise"] = d.dLy;
o["width"] = d.dW;
o["side_width"] = d.dSideW;
o["top_elevation"] = d.dTopEl;
o["align_left"] = (d.bAlignL != FALSE);
o["align_right"] = (d.bAlignR != FALSE);
o["nonslip"] = (d.bNonslip != FALSE);
return o;
}
}
class ToolAddAscent : public mcp_Tool
{
public:
std::string private_name() const override { return "add_aec_ascent"; }
std::string private_description() const override
{
return "放置坡道(菜单 PL_FZPD 放置坡道,实体 AecDbAscent):给起点和终点(坡道走向),"
"再给水平投影长/坡高/坡宽/翻边宽/顶标高等(缺省照抄 CFZPDDlg)。图层 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]" },
"end": { "type": "array", "items": { "type": "number" }, "description": "坡道终点 [x,y]" },
"run_length": { "type": "number", "description": "水平投影长,缺省 2100" },
"rise": { "type": "number", "description": "坡高,缺省 450" },
"width": { "type": "number", "description": "坡宽,缺省 3000" },
"side_width": { "type": "number", "description": "翻边宽,缺省 300" },
"top_elevation": { "type": "number", "description": "顶标高,缺省 450" },
"align_left": { "type": "boolean" }, "align_right": { "type": "boolean" },
"nonslip": { "type": "boolean", "description": "是否有防滑条" },
"layer": { "type": "string", "description": "图层,缺省 LAY_CSTAIR" },
"color": { "type": "integer" }
},
"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])");
if (ptS.distanceTo(ptE) < 1.0)
return errJson(kErrBadParam, L"坡道起点和终点太近");
AscentData data;
AscentFromArgs(data, args, false);
AecDbAscent* pAscent = new AecDbAscent();
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()) pAscent->setLayer(idLay);
if (hasKey(args, "color")) pAscent->setColorIndex((Adesk::UInt16)intOr(args, "color", 256));
pAscent->SetAscentData(data);
pAscent->SetBaseCurve(ptS, ptE);
AcDbObjectId idNew;
Acad::ErrorStatus es = addToSpace(pAscent, &idNew);
if (es != Acad::eOk)
{
delete pAscent;
return errJson(kErrInternal, std::wstring(L"加入模型空间失败,err=") + std::to_wstring((int)es));
}
touchEntity(pAscent);
nlohmann::json body;
body["ok"] = true;
body["type"] = "ascent";
body["params"] = AscentToJson(data);
pAscent->close();
body["handle"] = HandleOfId(idNew);
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolAddAscent, AddAecAscentTool)
class ToolUpdateAscent : public mcp_Tool
{
public:
std::string private_name() const override { return "update_aec_ascent"; }
std::string private_description() const override
{
return "改坡道(AecDbAscent):水平投影长/坡高/坡宽/翻边/顶标高/对齐/防滑条,"
"只传要改的字段;给 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" } },
"end": { "type": "array", "items": { "type": "number" } },
"run_length": { "type": "number" }, "rise": { "type": "number" },
"width": { "type": "number" }, "side_width": { "type": "number" },
"top_elevation": { "type": "number" },
"align_left": { "type": "boolean" }, "align_right": { "type": "boolean" },
"nonslip": { "type": "boolean" }, "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));
AecDbAscent* pAscent = AecDbAscent::cast(pEnt);
if (pAscent == NULL)
{
const std::string sCls = classNameOf(pEnt);
pEnt->close();
return errJson(kErrWrongType, L"实体 " + utf8ToWstr(sCls) + L" 不是坡道(AecDbAscent)");
}
AscentData data;
::memset(&data, 0, sizeof(AscentData));
pAscent->GetAscentData(data);
AscentFromArgs(data, args, true);
AcGePoint3d ptS(0.0, 0.0, 0.0), ptE(0.0, 0.0, 0.0);
if (readPoint(args, "start", ptS) && readPoint(args, "end", ptE) && ptS.distanceTo(ptE) > 1.0)
pAscent->SetBaseCurve(ptS, ptE);
pAscent->SetAscentData(data);
if (hasKey(args, "color")) pAscent->setColorIndex((Adesk::UInt16)intOr(args, "color", 256));
pAscent->ReCreateDraw3d();
touchEntity(pAscent);
nlohmann::json body;
body["ok"] = true;
body["type"] = "ascent";
body["handle"] = sHandle;
body["params"] = AscentToJson(data);
pEnt->close();
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolUpdateAscent, UpdateAecAscentTool)
// ===========================================================================
// 散水(PL_FZSS,AecDbXApron)—— 闭合路径
// ===========================================================================
class ToolAddApron : public mcp_Tool
{
public:
std::string private_name() const override { return "add_aec_apron"; }
std::string private_description() const override
{
return "放置散水(菜单 PL_FZSS 放置散水,实体 AecDbXApron):给一条走向 points(自动闭合),"
"再给散水宽/内边厚/外边厚/离墙偏移/顶标高等(缺省照抄 CFZSSDlg)。图层 LAY_BALCONY。";
}
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 点,自动闭合)" },
"width": { "type": "number", "description": "散水宽" },
"inside_thickness": { "type": "number", "description": "内侧厚" },
"outside_thickness": { "type": "number", "description": "外侧厚" },
"offset_from_wall": { "type": "number", "description": "离墙偏移" },
"elevation": { "type": "number", "description": "顶标高" },
"layer": { "type": "string", "description": "图层,缺省 LAY_BALCONY" },
"color": { "type": "integer" }
},
"required": [ "points", "width" ]
})");
}
nlohmann::json Execute(const nlohmann::json& args) override
{
if (workingDb() == NULL) return errJson(kErrNoDocument, L"没有活动图纸");
DocLock lock;
if (!lock.isLocked()) return errJson(kErrInternal, L"拿不到图纸写锁(有别的命令在跑?)");
if (!hasKey(args, "points") || !args["points"].is_array() || args["points"].size() < 2)
return errJson(kErrBadParam, L"需要 points:[[x,y],...](至少 2 点)");
const double dW = numOr(args, "width", 0.0);
if (dW <= 0.0) return errJson(kErrBadParam, L"width 必须 > 0");
AcDbPolyline* pPl = new AcDbPolyline((int)args["points"].size());
int nIdx = 0;
for (size_t i = 0; i < args["points"].size(); ++i)
{
const nlohmann::json& pt = args["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; return errJson(kErrBadParam, L"points 有效点少于 2 个"); }
pPl->setClosed(Adesk::kTrue);
AecDbXApron* pApron = new AecDbXApron();
std::wstring sLay = hasKey(args, "layer") ? wstrOr(args, "layer", L"") : L"";
if (sLay.empty()) sLay = L"LAY_BALCONY";
AcDbObjectId idLay = KTSetting::InitLayer(sLay.c_str());
if (!idLay.isNull()) pApron->setLayer(idLay);
if (hasKey(args, "color")) pApron->setColorIndex((Adesk::UInt16)intOr(args, "color", 256));
pApron->SetBaseCurve(pPl);
pApron->SetSection(10.0, 10.0, 0.0); // 与命令一致的占位截面
pApron->SetConstantWidth(dW);
if (hasKey(args, "inside_thickness")) pApron->SetInsideThick(numOr(args, "inside_thickness", 0.0));
if (hasKey(args, "outside_thickness")) pApron->SetOutSideThick(numOr(args, "outside_thickness", 0.0));
if (hasKey(args, "offset_from_wall")) pApron->SetOffsetFromWall(numOr(args, "offset_from_wall", 0.0));
pApron->GetSpecialtyData()->SetAt(KEY_BOMEL, numOr(args, "elevation", 0.0));
AcGeVector3d vtZero(0.0, 0.0, 0.0);
pApron->GetSpecialtyData()->SetAt(KEY_StartDir, vtZero);
delete pPl;
AcDbObjectId idNew;
Acad::ErrorStatus es = addToSpace(pApron, &idNew);
if (es != Acad::eOk)
{
delete pApron;
return errJson(kErrInternal, std::wstring(L"加入模型空间失败,err=") + std::to_wstring((int)es));
}
touchEntity(pApron);
nlohmann::json o;
o["width"] = dW;
o["inside_thickness"] = pApron->GetInsideThick();
o["outside_thickness"] = pApron->GetOutSideThick();
o["offset_from_wall"] = pApron->GetOffsetFromWall();
nlohmann::json body;
body["ok"] = true;
body["type"] = "apron";
body["params"] = o;
pApron->close();
body["handle"] = HandleOfId(idNew);
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolAddApron, AddAecApronTool)
class ToolUpdateApron : public mcp_Tool
{
public:
std::string private_name() const override { return "update_aec_apron"; }
std::string private_description() const override
{
return "改散水(AecDbXApron):散水宽/内边厚/外边厚/离墙偏移/顶标高,只传要改的字段;"
"给 points 可以换走向。";
}
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" } } },
"width": { "type": "number" },
"inside_thickness": { "type": "number" }, "outside_thickness": { "type": "number" },
"offset_from_wall": { "type": "number" }, "elevation": { "type": "number" },
"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));
AecDbXApron* pApron = AecDbXApron::cast(pEnt);
if (pApron == NULL)
{
const std::string sCls = classNameOf(pEnt);
pEnt->close();
return errJson(kErrWrongType, L"实体 " + utf8ToWstr(sCls) + L" 不是散水(AecDbXApron)");
}
if (hasKey(args, "points") && args["points"].is_array() && args["points"].size() >= 2)
{
AcDbPolyline* pPl = new AcDbPolyline((int)args["points"].size());
int nIdx = 0;
for (size_t i = 0; i < args["points"].size(); ++i)
{
const nlohmann::json& pt = args["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)
{
pPl->setClosed(Adesk::kTrue);
pApron->SetBaseCurve(pPl);
}
delete pPl;
}
if (hasKey(args, "width")) pApron->SetConstantWidth(numOr(args, "width", 0.0));
if (hasKey(args, "inside_thickness")) pApron->SetInsideThick(numOr(args, "inside_thickness", 0.0));
if (hasKey(args, "outside_thickness")) pApron->SetOutSideThick(numOr(args, "outside_thickness", 0.0));
if (hasKey(args, "offset_from_wall")) pApron->SetOffsetFromWall(numOr(args, "offset_from_wall", 0.0));
if (hasKey(args, "elevation")) pApron->GetSpecialtyData()->SetAt(KEY_BOMEL, numOr(args, "elevation", 0.0));
if (hasKey(args, "color")) pApron->setColorIndex((Adesk::UInt16)intOr(args, "color", 256));
pApron->ReCreateDraw3d();
touchEntity(pApron);
nlohmann::json o;
o["width"] = pApron->GetWidth(0);
o["inside_thickness"] = pApron->GetInsideThick();
o["outside_thickness"] = pApron->GetOutSideThick();
o["offset_from_wall"] = pApron->GetOffsetFromWall();
nlohmann::json body;
body["ok"] = true;
body["type"] = "apron";
body["handle"] = sHandle;
body["params"] = o;
pEnt->close();
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolUpdateApron, UpdateAecApronTool)
// ===========================================================================
// 沟渠(PL_LSJM,AecDbEwPipeBase)—— U 形断面 + 路径
// ===========================================================================
namespace
{
// 断面几何照搬 CHZGQDlg::GetSection():U 形槽,9 点闭合
// W 总宽 / H 总高 / V 内底宽 / angle 侧壁坡度角(度) / T 壁厚;断面中心在原点
AcDbRegion* BuildDitchRegion(double dW, double dH, double dV, double dAngDeg, double dT,
AcGePoint3d& ptCen, std::wstring& err)
{
if (dW <= 0.0 || dH <= 0.0 || dV <= 0.0 || dT <= 0.0)
{
err = L"沟渠需要 width / height / inner_width / wall_thickness,且都 > 0";
return NULL;
}
if (dV >= dW || dT >= dH)
{
err = L"内底宽要小于总宽、壁厚要小于总高";
return NULL;
}
AcGePoint3d ptGe[9];
ptGe[0].set(-0.5 * dW, 0.0, 0.0);
ptGe[1] = ptGe[0];
ptGe[1].y += dH;
ptGe[3].set(-0.5 * dV, dT, 0.0);
const double dA = dAngDeg * PI / 180.0;
const double dX = (dH - dT) * tan(dA);
ptGe[2] = ptGe[3];
ptGe[2].y += (dH - dT);
ptGe[2].x -= dX;
ptGe[4] = ptGe[3];
ptGe[4].x += dV;
ptGe[5] = ptGe[4];
ptGe[5].x += dX;
ptGe[5].y += (dH - dT);
ptGe[6].set(0.5 * dW, dH, 0.0);
ptGe[7].set(0.5 * dW, 0.0, 0.0);
ptGe[8] = ptGe[0];
AcDbVoidPtrArray lnPtrs;
for (int i = 0; i < 8; ++i)
lnPtrs.append(new AcDbLine(ptGe[i], ptGe[i + 1]));
AcDbRegion* pReg = CoreTools::createRegion(lnPtrs);
for (int i = 0; i < lnPtrs.length(); ++i)
delete (AcDbLine*)lnPtrs[i];
ptCen.set(0.0, 0.0, 0.0);
if (pReg == NULL && err.empty()) err = L"沟渠断面(U 形区域)生成失败";
return pReg;
}
AcDbPolyline* DitchPathOf(const nlohmann::json& a, double dElev, 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(dElev);
return pPl;
}
// 断面参数除了进区域,还要写进专业数据(命令里是分开写的两份)
void SetDitchXData(AecDbEwPipeBase* pPipe, const nlohmann::json& a)
{
if (hasKey(a, "width")) pPipe->GetSpecialtyData()->SetAt(KEY_W, numOr(a, "width", 0.0));
if (hasKey(a, "height")) pPipe->GetSpecialtyData()->SetAt(KEY_H, numOr(a, "height", 0.0));
if (hasKey(a, "inner_width")) pPipe->GetSpecialtyData()->SetAt(KEY_V, numOr(a, "inner_width", 0.0));
if (hasKey(a, "angle")) pPipe->GetSpecialtyData()->SetAt(KEY_Angle, numOr(a, "angle", 0.0));
if (hasKey(a, "wall_thickness")) pPipe->GetSpecialtyData()->SetAt(KEY_T, numOr(a, "wall_thickness", 0.0));
if (hasKey(a, "material")) pPipe->GetSpecialtyData()->SetAt(KEY_MATERIAL, wstrOr(a, "material", L"").c_str());
if (hasKey(a, "purpose")) pPipe->GetSpecialtyData()->SetAt(KEY_PURPOSE, wstrOr(a, "purpose", L"").c_str());
}
}
class ToolAddDitch : public mcp_Tool
{
public:
std::string private_name() const override { return "add_aec_ditch"; }
std::string private_description() const override
{
return "绘制沟渠(菜单 PL_LSJM 绘制沟渠,实体 AecDbEwPipeBase):给走向 points + U 形断面尺寸"
"(总宽/总高/内底宽/侧壁坡度角/壁厚,缺省照抄 CHZGQDlg:800/800/700/0°/10)+ 底标高、材质、用途。"
"断面按命令的 GetSection 现算成面域再沿路径扫。";
}
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 点)" },
"width": { "type": "number", "description": "总宽,缺省 800" },
"height": { "type": "number", "description": "总高,缺省 800" },
"inner_width": { "type": "number", "description": "内底宽,缺省 700" },
"angle": { "type": "number", "description": "侧壁坡度角(度),缺省 0" },
"wall_thickness": { "type": "number", "description": "壁厚,缺省 10" },
"bottom_elevation": { "type": "number", "description": "底标高,缺省 0" },
"material": { "type": "string", "description": "材质,缺省 素砼" },
"purpose": { "type": "string", "description": "用途,缺省 进水渠" },
"color": { "type": "integer" }
},
"required": [ "points" ]
})");
}
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 double dW = numOr(args, "width", 800.0);
const double dH = numOr(args, "height", 800.0);
const double dV = numOr(args, "inner_width", 700.0);
const double dAng = numOr(args, "angle", 0.0);
const double dT = numOr(args, "wall_thickness", 10.0);
const double dEl = numOr(args, "bottom_elevation", 0.0);
std::wstring err;
AcGePoint3d ptCen(0.0, 0.0, 0.0);
AcDbRegion* pReg = BuildDitchRegion(dW, dH, dV, dAng, dT, ptCen, err);
if (pReg == NULL) return errJson(kErrBadParam, err);
AcDbPolyline* pPath = DitchPathOf(args, dEl, err);
if (pPath == NULL) { delete pReg; return errJson(kErrBadParam, err); }
AecDbEwPipeBase* pPipe = new AecDbEwPipeBase();
pPipe->SetSection(pReg, ptCen);
pPipe->SetBaseCurve(pPath);
delete pPath;
delete pReg;
pPipe->GetSpecialtyData()->SetAt(KEY_NAME, _T("沟渠"));
pPipe->GetSpecialtyData()->SetAt(KEY_2DSTYLE, 2);
pPipe->GetSpecialtyData()->SetAt(KEY_CENNODRAW, TRUE);
pPipe->GetSpecialtyData()->SetAt(KEY_BOMEL, dEl); // 底标高也记下来,update 要读
SetDitchXData(pPipe, args); // 没传的用缺省(与命令一致)
if (!hasKey(args, "material")) pPipe->GetSpecialtyData()->SetAt(KEY_MATERIAL, _T("素砼"));
if (!hasKey(args, "purpose")) pPipe->GetSpecialtyData()->SetAt(KEY_PURPOSE, _T("进水渠"));
AcDbObjectId idNew;
Acad::ErrorStatus es = addToSpace(pPipe, &idNew);
if (es != Acad::eOk)
{
delete pPipe;
return errJson(kErrInternal, std::wstring(L"加入模型空间失败,err=") + std::to_wstring((int)es));
}
touchEntity(pPipe);
nlohmann::json o;
o["points"] = args.contains("points") ? args["points"] : nlohmann::json::array();
o["width"] = dW;
o["height"] = dH;
o["inner_width"] = dV;
o["angle"] = dAng;
o["wall_thickness"] = dT;
o["bottom_elevation"] = dEl;
nlohmann::json body;
body["ok"] = true;
body["type"] = "ditch";
body["params"] = o;
pPipe->close();
body["handle"] = HandleOfId(idNew);
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolAddDitch, AddAecDitchTool)
class ToolUpdateDitch : public mcp_Tool
{
public:
std::string private_name() const override { return "update_aec_ditch"; }
std::string private_description() const override
{
return "改沟渠(AecDbEwPipeBase):总宽/总高/内底宽/坡度角/壁厚/底标高/材质/用途,只传要改的字段;"
"给 points 可以换走向。改断面会重建面域。";
}
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" } } },
"width": { "type": "number" }, "height": { "type": "number" },
"inner_width": { "type": "number" }, "angle": { "type": "number" },
"wall_thickness": { "type": "number" }, "bottom_elevation": { "type": "number" },
"material": { "type": "string" }, "purpose": { "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));
AecDbEwPipeBase* pPipe = AecDbEwPipeBase::cast(pEnt);
if (pPipe == NULL)
{
const std::string sCls = classNameOf(pEnt);
pEnt->close();
return errJson(kErrWrongType, L"实体 " + utf8ToWstr(sCls) + L" 不是沟渠(AecDbEwPipeBase)");
}
// 读现有断面尺寸(专业数据里存着)
const AecDbObjXDataMap* pX = pPipe->GetSpecialtyData(AcDb::kForRead);
double dW = 0.0, dH = 0.0, dV = 0.0, dAng = 0.0, dT = 0.0, dEl = 0.0;
if (pX != NULL)
{
pX->GetAt(KEY_W, dW);
pX->GetAt(KEY_H, dH);
pX->GetAt(KEY_V, dV);
pX->GetAt(KEY_Angle, dAng);
pX->GetAt(KEY_T, dT);
pX->GetAt(KEY_BOMEL, dEl);
}
dW = numOr(args, "width", dW);
dH = numOr(args, "height", dH);
dV = numOr(args, "inner_width", dV);
dAng = numOr(args, "angle", dAng);
dT = numOr(args, "wall_thickness", dT);
dEl = numOr(args, "bottom_elevation", dEl);
std::wstring err;
AcGePoint3d ptCen(0.0, 0.0, 0.0);
AcDbRegion* pReg = BuildDitchRegion(dW, dH, dV, dAng, dT, ptCen, err);
if (pReg == NULL)
{
pEnt->close();
return errJson(kErrBadParam, err);
}
pPipe->SetSection(pReg, ptCen);
delete pReg;
if (hasKey(args, "points"))
{
AcDbPolyline* pPath = DitchPathOf(args, dEl, err);
if (pPath == NULL)
{
pEnt->close();
return errJson(kErrBadParam, err);
}
pPipe->SetBaseCurve(pPath);
delete pPath;
}
else if (hasKey(args, "bottom_elevation"))
{
// 只改底标高:把路径整体抬到新标高
AcDbCurve* pCrv = AcDbCurve::cast(pEnt);
AcDbPolyline* pOld = (pCrv != NULL) ? AcDbPolyline::cast(pCrv->clone()) : NULL;
if (pOld != NULL)
{
pOld->setElevation(dEl);
pPipe->SetBaseCurve(pOld);
delete pOld;
}
}
SetDitchXData(pPipe, args);
pPipe->GetSpecialtyData()->SetAt(KEY_BOMEL, dEl);
if (hasKey(args, "color")) pPipe->setColorIndex((Adesk::UInt16)intOr(args, "color", 256));
pPipe->ReCreateDraw3d();
touchEntity(pPipe);
nlohmann::json o;
o["width"] = dW;
o["height"] = dH;
o["inner_width"] = dV;
o["angle"] = dAng;
o["wall_thickness"] = dT;
o["bottom_elevation"] = dEl;
nlohmann::json body;
body["ok"] = true;
body["type"] = "ditch";
body["handle"] = sHandle;
body["params"] = o;
pEnt->close();
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolUpdateDitch, UpdateAecDitchTool)