Files
company-desktop 4dd943763c feat: 第6项第二批——双跑/直线/圆弧楼梯 + 4 种屋顶
楼梯(aec_mcp_stair_tools.cpp 追加 6 个工具):
- add/update_aec_z_stair:双跑楼梯 AecDbZLineStair(PL_SPLT),参数按拍板的"只暴露关键尺寸",
  ZLineStair 其余字段用 CDPLTDlg 缺省
- add/update_aec_line_seg_stair:直线楼梯 AecDbLineSegStair(PL_ZXLT),缺省抄 CZXLTDlg;
  基准曲线水平(与命令一致),高度全靠踏步数
- add/update_aec_arc_stair:圆弧楼梯 AecDbArcStair(PL_YHLT);中心线除了 curve(ARC handle)
  还可以直接给 center+radius+起止角现造一条(命令是拾取,工具多给一条参数化路径)

屋顶(新文件 aec_mcp_roof_tools.cpp,2 个工具):
- add/update_aec_roof + style=slope|dual|cusp|mount,对应 PL_RYPD/PL_RZPD/PL_ZJPD/PL_JXPD

三个坑:
1. AecDbXRoof 没有 GetRoofStyle(),创建时往专业数据写 MCP_ROOF_STYLE,update 不传 style 时读它
2. 屋顶创建后先 close() 再按 id 取 handle:对象还开着时 acdbOpenObject 返回 eWasOpenForWrite,
   handle 会是空串(第一次实测就是这样)
3. slope 的标高只在传 bottom_elevation 时才写(否则改角度会把标高清 0);dual 只给一个坡角时
   另一个用 GetAngleLeft/Right() 补上,避免被 0 覆盖

实测(R230 Debug,空图):
- 四种屋顶 handle 311~314,bbox 与轮廓/eaves 对得上;改 slope 的 angle/slab 后标高 3000 保留;
  改 dual 的 left_angle 后右坡角仍 30;改 cusp 的 radius 生效
- 双跑 bbox 0~4170 / -2565~0;直线 0~5400 / 5400~6600;圆弧(center 0,15000 r3000)
  -1755~3500 / 14985~18474;改踏步数/梯宽都能重建

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

487 lines
18 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// ============================================================================
// aec_mcp_roof_tools.cpp
// cad_mcp_aec 的屋顶工具(工作计划第 6 项)
// PL_RYPD 任意屋顶 / PL_RZPD 人字屋顶 / PL_ZJPD 攒尖屋顶 / PL_JXPD 矩形屋顶
// 四种命令产出同一个类 AecDbXRoof,只是 RoofStyle 不同,所以做成"一个工具 + style 参数":
// add_aec_roof / update_aec_roof { style: slope | dual | cusp | mount }
//
// 四个命令的建模序列(Pool3dCmd/pool3dcmdCmd.cpp):
// slope : SetRoofStyle(kSlopeRoof) + SetSlabThick + SetEavesLen
// + InitEqualSlope(pPl, dBomEl - 出檐*tan(ang), ang)
// dual : SetRoofStyle(kDualRoof) + SetPolyPath + SetElevation + SetRidge
// + SetEqualAngle(false) + SetAngle(左,右) + SetSlabThick
// cusp : SetRoofStyle(kCuspRoof) + SetCenter + SetRadius + SetSlabThick
// + SetRoofHeight + SetEavesLen + SetSideNum + SetElevation
// mount : SetRoofStyle(kMountRoof) + SetLocation + SetElevation + SetRoofHeight
// + SetEavesLen + SetRoofType + SetMountHeight/SetSlopeAngle2/SetMountLen + SetSlabThick
// 都挂 KEY_MATERIAL / KEY_FACTORY,图层 LAY_ROOF。
// ============================================================================
#include "StdAfx.h"
#include "KTCad.h"
#include "aec_mcp_common.h"
#include "mcp_plugin_api.h"
#include "AecDbRoof.h"
#include "AecDbObjXDataMap.h"
#include "CoreTools.h"
#include "KTSetting.h"
using namespace aec_mcp;
namespace
{
// 自己记一份 style:CoreLibrary 没有 GetRoofStyle,update 时靠它认
const TCHAR* kRoofStyleKey = _T("MCP_ROOF_STYLE");
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;
}
struct RoofStyleDef
{
const wchar_t* pszKey;
AecDbXRoof::RoofStyle eStyle;
};
const RoofStyleDef g_roofStyles[] =
{
{ L"slope", AecDbXRoof::kSlopeRoof }, // PL_RYPD 任意屋顶
{ L"dual", AecDbXRoof::kDualRoof }, // PL_RZPD 人字屋顶
{ L"cusp", AecDbXRoof::kCuspRoof }, // PL_ZJPD 攒尖屋顶
{ L"mount", AecDbXRoof::kMountRoof }, // PL_JXPD 矩形屋顶
};
bool StyleOf(const std::wstring& sKey, AecDbXRoof::RoofStyle& eStyle)
{
for (int i = 0; i < _countof(g_roofStyles); ++i)
{
if (sKey == g_roofStyles[i].pszKey)
{
eStyle = g_roofStyles[i].eStyle;
return true;
}
}
return false;
}
std::wstring StyleKeyOf(AecDbXRoof::RoofStyle eStyle)
{
for (int i = 0; i < _countof(g_roofStyles); ++i)
{
if (eStyle == g_roofStyles[i].eStyle)
return g_roofStyles[i].pszKey;
}
return L"";
}
// 闭合多段线(屋顶轮廓),z 用 elevation
AcDbPolyline* PolylineOfPoints(const nlohmann::json& a, double dElev, std::wstring& err)
{
if (!hasKey(a, "points") || !a["points"].is_array() || a["points"].size() < 3)
{
err = L"需要 points:[[x,y],...](至少 3 点、能闭合)";
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 < 3)
{
delete pPl;
err = L"points 有效点少于 3 个";
return NULL;
}
pPl->setClosed(true);
pPl->setElevation(dElev);
return pPl;
}
// 按 style 设置参数(add / update 共用)
bool ApplyRoof(AecDbXRoof* pRoof, AecDbXRoof::RoofStyle eStyle, const nlohmann::json& a,
bool bCreate, std::wstring& err)
{
if (bCreate)
pRoof->SetRoofStyle(eStyle);
if (hasKey(a, "material")) pRoof->GetSpecialtyData()->SetAt(KEY_MATERIAL, wstrOr(a, "material", L"").c_str());
if (hasKey(a, "factory")) pRoof->GetSpecialtyData()->SetAt(KEY_FACTORY, wstrOr(a, "factory", L"").c_str());
const double dSlabT = numOr(a, "slab_thickness", 0.0);
const double dEaves = numOr(a, "eaves_length", 0.0);
if (dSlabT > 0.0) pRoof->SetSlabThick(dSlabT);
if (hasKey(a, "eaves_length")) pRoof->SetEavesLen(dEaves);
switch (eStyle)
{
case AecDbXRoof::kSlopeRoof:
{
const double dAng = CoreTools::A2R(numOr(a, "angle", 0.0));
const double dBomEl = numOr(a, "bottom_elevation", 0.0);
if (hasKey(a, "angle") || hasKey(a, "bottom_elevation") || !hasKey(a, "_no_init"))
{
std::wstring sErr;
AcDbPolyline* pPl = hasKey(a, "points") ? PolylineOfPoints(a, dBomEl, sErr) : NULL;
if (pPl == NULL && bCreate)
{
err = sErr.empty() ? std::wstring(L"任意屋顶需要 points(闭合轮廓)") : sErr;
return false;
}
if (pPl != NULL)
{
const double dAngUse = (fabs(dAng) < 1e-9) ? (PI / 6.0) : dAng;
const double dElev = dBomEl - dEaves * tan(dAngUse);
const bool bOk = pRoof->InitEqualSlope(pPl, dElev, dAngUse);
delete pPl;
if (!bOk && bCreate)
{
err = L"InitEqualSlope 失败(轮廓要闭合、角度非 0)";
return false;
}
if (hasKey(a, "bottom_elevation"))
pRoof->SetElevation(dBomEl);
}
}
}
break;
case AecDbXRoof::kDualRoof:
{
if (hasKey(a, "points"))
{
std::wstring sErr;
AcDbPolyline* pPl = PolylineOfPoints(a, numOr(a, "elevation", 0.0), sErr);
if (pPl == NULL)
{
if (bCreate) { err = sErr; return false; }
}
else
{
pRoof->SetPolyPath(pPl);
delete pPl;
}
}
else if (bCreate)
{
err = L"人字屋顶需要 points(闭合轮廓)";
return false;
}
AcGePoint3d ptR1(0.0, 0.0, 0.0), ptR2(0.0, 0.0, 0.0);
const bool bHasRidge = readPoint(a, "ridge_start", ptR1) && readPoint(a, "ridge_end", ptR2);
if (bHasRidge) pRoof->SetRidge(ptR1, ptR2);
else if (bCreate)
{
err = L"人字屋顶需要 ridge_start / ridge_end(屋脊线两点)";
return false;
}
if (hasKey(a, "elevation")) pRoof->SetElevation(numOr(a, "elevation", 0.0));
if (hasKey(a, "left_angle") || hasKey(a, "right_angle"))
{
// 只给一个时另一个沿用原来的,别被 0 覆盖
const double dL = hasKey(a, "left_angle") ? numOr(a, "left_angle", 0.0)
: CoreTools::R2A(pRoof->GetAngleLeft());
const double dR = hasKey(a, "right_angle") ? numOr(a, "right_angle", 0.0)
: CoreTools::R2A(pRoof->GetAngleRight());
pRoof->SetEqualAngle(Adesk::kFalse);
pRoof->SetAngle(CoreTools::A2R(dL), CoreTools::A2R(dR));
}
}
break;
case AecDbXRoof::kCuspRoof:
{
AcGePoint3d ptCen(0.0, 0.0, 0.0);
if (readPoint(a, "center", ptCen)) pRoof->SetCenter(ptCen);
else if (bCreate) { err = L"攒尖屋顶需要 center"; return false; }
if (hasKey(a, "radius")) pRoof->SetRadius(numOr(a, "radius", 0.0));
if (hasKey(a, "roof_height")) pRoof->SetRoofHeight(numOr(a, "roof_height", 0.0));
if (hasKey(a, "side_count")) pRoof->SetSideNum((Adesk::UInt8)intOr(a, "side_count", 4));
if (hasKey(a, "elevation")) pRoof->SetElevation(numOr(a, "elevation", 0.0));
}
break;
case AecDbXRoof::kMountRoof:
{
AcGePoint3d ptLoc(0.0, 0.0, 0.0);
if (readPoint(a, "location", ptLoc)) pRoof->SetLocation(ptLoc);
else if (bCreate) { err = L"矩形屋顶需要 location(主坡墙外皮左下角点)"; return false; }
if (hasKey(a, "elevation")) pRoof->SetElevation(numOr(a, "elevation", 0.0));
if (hasKey(a, "roof_height")) pRoof->SetRoofHeight(numOr(a, "roof_height", 0.0));
if (hasKey(a, "roof_type")) pRoof->SetRoofType((Adesk::UInt16)intOr(a, "roof_type", 0));
if (hasKey(a, "mount_height")) pRoof->SetMountHeight(numOr(a, "mount_height", 0.0));
if (hasKey(a, "slope_angle")) pRoof->SetSlopeAngle2(CoreTools::A2R(numOr(a, "slope_angle", 0.0)));
if (hasKey(a, "mount_length")) pRoof->SetMountLen(numOr(a, "mount_length", 0.0));
if (hasKey(a, "length")) pRoof->SetLength(numOr(a, "length", 0.0));
if (hasKey(a, "width")) pRoof->SetWidth(numOr(a, "width", 0.0));
}
break;
default:
err = L"不认识的屋顶样式";
return false;
}
return true;
}
nlohmann::json RoofToJson(AecDbXRoof* pRoof, AecDbXRoof::RoofStyle eStyle)
{
nlohmann::json o;
o["style"] = acharToUtf8(StyleKeyOf(eStyle).c_str());
o["slab_thickness"] = pRoof->GetSlabThick();
o["eaves_length"] = pRoof->GetEavesLen();
o["elevation"] = pRoof->GetElevation();
switch (eStyle)
{
case AecDbXRoof::kCuspRoof:
case AecDbXRoof::kMountRoof:
o["roof_height"] = pRoof->GetRoofHeight(); // 只有这两种样式用屋脊高
break;
default:
break;
}
switch (eStyle)
{
case AecDbXRoof::kDualRoof:
o["left_angle"] = CoreTools::R2A(pRoof->GetAngleLeft());
o["right_angle"] = CoreTools::R2A(pRoof->GetAngleRight());
break;
case AecDbXRoof::kCuspRoof:
o["radius"] = pRoof->GetRadius();
break;
case AecDbXRoof::kMountRoof:
o["mount_height"] = pRoof->GetMountHeight();
o["slope_angle"] = CoreTools::R2A(pRoof->GetSlopeAngle2());
o["mount_length"] = pRoof->GetMountLen();
o["length"] = pRoof->GetLength();
o["width"] = pRoof->GetWidth();
break;
default:
break;
}
return o;
}
}
// ---------------------------------------------------------------------------
// add_aec_roof —— 四种屋顶(PL_RYPD / PL_RZPD / PL_ZJPD / PL_JXPD)
// ---------------------------------------------------------------------------
class ToolAddRoof : public mcp_Tool
{
public:
std::string private_name() const override { return "add_aec_roof"; }
std::string private_description() const override
{
return "建屋顶(实体 AecDbXRoof),四种命令共用一个工具,用 style 选:\n"
" slope = PL_RYPD 任意屋顶:points(闭合轮廓) + angle(度) + eaves_length + bottom_elevation\n"
" dual = PL_RZPD 人字屋顶:points + ridge_start/ridge_end(屋脊线) + left_angle/right_angle(度) + elevation\n"
" cusp = PL_ZJPD 攒尖屋顶:center + radius + roof_height + eaves_length + side_count + elevation\n"
" mount = PL_JXPD 矩形屋顶:location + roof_height + eaves_length + slope_angle(度) + mount_height + elevation\n"
"公共参数:slab_thickness、material、factory。图层 LAY_ROOF。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"style": { "type": "string", "enum": [ "slope", "dual", "cusp", "mount" ],
"description": "slope=任意屋顶 PL_RYPD / dual=人字屋顶 PL_RZPD / cusp=攒尖屋顶 PL_ZJPD / mount=矩形屋顶 PL_JXPD" },
"points": { "type": "array", "items": { "type": "array", "items": { "type": "number" } },
"description": "闭合轮廓 [[x,y],...](slope / dual 必需,自动闭合)" },
"bottom_elevation": { "type": "number", "description": "slope:檐口标高" },
"angle": { "type": "number", "description": "slope:坡度角(度),缺省 30" },
"ridge_start": { "type": "array", "items": { "type": "number" }, "description": "dual:屋脊线起点" },
"ridge_end": { "type": "array", "items": { "type": "number" }, "description": "dual:屋脊线终点" },
"left_angle": { "type": "number", "description": "dual:左坡角(度)" },
"right_angle": { "type": "number", "description": "dual:右坡角(度)" },
"center": { "type": "array", "items": { "type": "number" }, "description": "cusp:屋顶中心" },
"radius": { "type": "number", "description": "cusp:外接圆半径" },
"side_count": { "type": "integer", "description": "cusp:边数,缺省 4" },
"location": { "type": "array", "items": { "type": "number" }, "description": "mount:主坡墙外皮左下角点" },
"roof_height": { "type": "number", "description": "屋顶高(cusp / mount)" },
"mount_height": { "type": "number", "description": "mount:主坡高" },
"mount_length": { "type": "number", "description": "mount:主坡长" },
"slope_angle": { "type": "number", "description": "mount:主坡角(度)" },
"roof_type": { "type": "integer", "description": "mount:屋顶类型编号(对话框里的选项)" },
"length": { "type": "number", "description": "mount:长" },
"width": { "type": "number", "description": "mount:宽" },
"elevation": { "type": "number", "description": "屋顶标高(dual / cusp / mount)" },
"eaves_length": { "type": "number", "description": "出檐长度" },
"slab_thickness": { "type": "number", "description": "屋面板厚" },
"material": { "type": "string", "description": "材质" },
"factory": { "type": "string", "description": "厂家" },
"layer": { "type": "string", "description": "图层,缺省 LAY_ROOF" },
"color": { "type": "integer", "description": "ACI 颜色号,256=ByLayer" }
},
"required": [ "style" ]
})");
}
nlohmann::json Execute(const nlohmann::json& args) override
{
if (workingDb() == NULL) return errJson(kErrNoDocument, L"没有活动图纸");
DocLock lock;
if (!lock.isLocked()) return errJson(kErrInternal, L"拿不到图纸写锁(有别的命令在跑?)");
AecDbXRoof::RoofStyle eStyle;
if (!StyleOf(wstrOr(args, "style", L""), eStyle))
return errJson(kErrBadParam, L"style 只支持 slope / dual / cusp / mount");
AecDbXRoof* pRoof = new AecDbXRoof();
std::wstring sLay = hasKey(args, "layer") ? wstrOr(args, "layer", L"") : L"";
if (sLay.empty()) sLay = L"LAY_ROOF";
AcDbObjectId idLay = KTSetting::InitLayer(sLay.c_str());
if (!idLay.isNull()) pRoof->setLayer(idLay);
if (hasKey(args, "color")) pRoof->setColorIndex((Adesk::UInt16)intOr(args, "color", 256));
std::wstring err;
if (!ApplyRoof(pRoof, eStyle, args, true, err))
{
delete pRoof;
return errJson(kErrBadParam, err);
}
pRoof->GetSpecialtyData()->SetAt(kRoofStyleKey, StyleKeyOf(eStyle).c_str());
AcDbObjectId idNew;
Acad::ErrorStatus es = addToSpace(pRoof, &idNew);
if (es != Acad::eOk)
{
delete pRoof;
return errJson(kErrInternal, std::wstring(L"加入模型空间失败,err=") + std::to_wstring((int)es));
}
touchEntity(pRoof);
nlohmann::json body;
body["ok"] = true;
body["type"] = "roof";
body["params"] = RoofToJson(pRoof, eStyle);
pRoof->close(); // 先 close,否则下面按 id 打开会拿到 eWasOpenForWrite
body["handle"] = HandleOfId(idNew);
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolAddRoof, AddAecRoofTool)
// ---------------------------------------------------------------------------
// update_aec_roof —— 改屋顶(同一工具,按 style 分派)
// ---------------------------------------------------------------------------
class ToolUpdateRoof : public mcp_Tool
{
public:
std::string private_name() const override { return "update_aec_roof"; }
std::string private_description() const override
{
return "改屋顶(AecDbXRoof):只传要改的字段;style 不给就用创建时记下的那个。"
"各 style 可改的字段见 add_aec_roof;给 points / ridge_* / center / location 可以换轮廓与定位。";
}
nlohmann::json private_input_schema() const override
{
return nlohmann::json::parse(R"({
"type": "object",
"properties": {
"handle": { "type": "string", "description": "屋顶实体 handle" },
"style": { "type": "string", "enum": [ "slope", "dual", "cusp", "mount" ] },
"points": { "type": "array", "items": { "type": "array", "items": { "type": "number" } } },
"bottom_elevation": { "type": "number" }, "angle": { "type": "number" },
"ridge_start": { "type": "array", "items": { "type": "number" } },
"ridge_end": { "type": "array", "items": { "type": "number" } },
"left_angle": { "type": "number" }, "right_angle": { "type": "number" },
"center": { "type": "array", "items": { "type": "number" } },
"radius": { "type": "number" }, "side_count": { "type": "integer" },
"location": { "type": "array", "items": { "type": "number" } },
"roof_height": { "type": "number" }, "mount_height": { "type": "number" },
"mount_length": { "type": "number" }, "slope_angle": { "type": "number" },
"roof_type": { "type": "integer" }, "length": { "type": "number" }, "width": { "type": "number" },
"elevation": { "type": "number" }, "eaves_length": { "type": "number" },
"slab_thickness": { "type": "number" },
"material": { "type": "string" }, "factory": { "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));
AecDbXRoof* pRoof = AecDbXRoof::cast(pEnt);
if (pRoof == NULL)
{
const std::string sCls = classNameOf(pEnt);
pEnt->close();
return errJson(kErrWrongType, L"实体 " + utf8ToWstr(sCls) + L" 不是屋顶(AecDbXRoof)");
}
// style:给了就用给的,否则用创建时记下的
AecDbXRoof::RoofStyle eStyle = AecDbXRoof::kSlopeRoof;
CString sStyle;
if (hasKey(args, "style"))
{
if (!StyleOf(wstrOr(args, "style", L""), eStyle))
{
pEnt->close();
return errJson(kErrBadParam, L"style 只支持 slope / dual / cusp / mount");
}
}
else if (pRoof->GetSpecialtyData()->GetAt(kRoofStyleKey, sStyle))
{
if (!StyleOf(std::wstring((LPCTSTR)sStyle), eStyle))
eStyle = AecDbXRoof::kSlopeRoof;
}
std::wstring err;
if (!ApplyRoof(pRoof, eStyle, args, false, err))
{
pEnt->close();
return errJson(kErrBadParam, err);
}
pRoof->GetSpecialtyData()->SetAt(kRoofStyleKey, StyleKeyOf(eStyle).c_str());
if (hasKey(args, "color")) pRoof->setColorIndex((Adesk::UInt16)intOr(args, "color", 256));
nlohmann::json body;
body["ok"] = true;
body["type"] = "roof";
body["handle"] = sHandle;
body["params"] = RoofToJson(pRoof, eStyle);
touchEntity(pRoof);
pRoof->close();
return okJson(body);
}
};
EXPORT_MCP_TOOL(ToolUpdateRoof, UpdateAecRoofTool)