diff --git a/SourceCode/Code2026/Pool3dCmd/paraDesignDlg.cpp b/SourceCode/Code2026/Pool3dCmd/paraDesignDlg.cpp index 1618e35..095eefc 100644 --- a/SourceCode/Code2026/Pool3dCmd/paraDesignDlg.cpp +++ b/SourceCode/Code2026/Pool3dCmd/paraDesignDlg.cpp @@ -496,35 +496,42 @@ void CParaDesignDlg::OnPaint() bitmap.Detach(); } +// 把当前对象的参数摊成 m_datas(绘制时按 key 取值)与 m_Bims(BIM 数据)。 +// 原来这段只写在"确定"按钮里,无界面建模(startForPipeValveNoUI)也要用,所以抽出来。 +void CParaDesignDlg::BuildDatas() +{ + m_datas.clear(); + m_Bims.clear(); + if (!m_pCurObj) return; + + int idx(0); + for (size_t i = 0; i < m_pCurObj->_params.size(); ++i) + { + Param ¶m(m_pCurObj->_params[i]); + if (!param._key.empty()) + { + if (!param._data.empty()) + { + idx = TypeConvert::s2i(param._value.c_str()); + if (idx >= param._data.size() || idx < 0) idx = 0; + m_datas.insert(std::make_pair(param._key.c_str(), param._data[idx].c_str())); + } + else + m_datas.insert(std::make_pair(param._key.c_str(), param._value.c_str())); + } + + if (param._data.empty()) + m_Bims.push_back(std::make_pair(param._name.c_str(), param._value.c_str())); + else + m_Bims.push_back(std::make_pair(param._name.c_str(), param.getCombo2String().c_str())); + } +} + void CParaDesignDlg::OnBnClickedOk() { if (!UpdateData(TRUE)) return; - - if (m_pCurObj) - { - int idx(0); - m_datas.clear(); - for (size_t i = 0; i < m_pCurObj->_params.size(); ++i) - { - Param ¶m(m_pCurObj->_params[i]); - if (!param._key.empty()) - { - if (!param._data.empty()) - { - idx = TypeConvert::s2i(param._value.c_str()); - if (idx >= param._data.size() || idx < 0) idx = 0; - m_datas.insert(std::make_pair(param._key.c_str(), param._data[idx].c_str())); - } - else - m_datas.insert(std::make_pair(param._key.c_str(), param._value.c_str())); - } - if (param._data.empty()) - m_Bims.push_back(std::make_pair(param._name.c_str(), param._value.c_str())); - else - m_Bims.push_back(std::make_pair(param._name.c_str(), param.getCombo2String().c_str())); - } - } + BuildDatas(); //int row = m_wndListCtrl.GetItemCount(); //int col = m_wndListCtrl.GetColumns(); @@ -866,6 +873,70 @@ void CParaDesignDlg::start(LPCTSTR objType, LPCTSTR objName, const int &idx) #define _SET_MAP_(key1, key2)if (data.find(##key1) != data.end()) kv.insert(std::make_pair(L#key2, data[##key1]));; + +// --------------------------------------------------------------------------- +// 把 startForPipeValve 里"DoModal 之前"的准备逻辑抽出来, +// 供 startForPipeValve(有界面)与 startForPipeValveNoUI(无界面,给 MCP 用)共用。 +// bSilent = true 时失败不弹 AfxMessageBox(MCP 里弹框会把调用方卡住)。 +// --------------------------------------------------------------------------- +static bool PreparePipeValveDlg(CParaDesignDlg& dlg + , LPCTSTR objType + , const double &dDN + , const double &dPN + , const AecDbObjXDataMap *pSrcData + , bool &bHasFlange + , bool bSilent) +{ + // 首先检查一下有没有 objType 数据 + CString sSizePath = FilePath::GetSysDir() + VALVE_JSON_PATH; + WDocument doc; + if (!file2Json(sSizePath, doc)) + { + if (!bSilent) AfxMessageBox(_T("打开valvesize.josn文件失败。")); + return false; + } + + MIt it = doc.FindMember(_T("Pipe")); + if (it == doc.MemberEnd()) + { + if (!bSilent) AfxMessageBox(_T("未找到当前阀门类型。")); + return false; + } + it = it->value.FindMember(objType); + if (it == doc.MemberEnd()) + { + if (!bSilent) AfxMessageBox(_T("未找到当前阀门类型。")); + return false; + } + + if (it->value.MemberCount() == 0 && !bSilent) + { + AfxMessageBox(_T("当前阀门类型没有数据。请自行输入尺寸数据。")); + } + + // 向 json 参数里注入 DN、PN 等 + std::map kv, data; + auto &objs = dlg.getObjects(); + for (size_t i(0); i < objs.size(); ++i) + { + GetValveInsSizeByLevel(objs[i].first.c_str(), dDN, pSrcData, data); + + kv.clear(); + kv.insert(std::make_pair(_T("DN"), TypeConvert::convert(dDN).c_str())); + _SET_MAP_(PID_PIPE_DPRESS, PN); + _SET_MAP_(PIPE_LEVEL, LEVEL); + _SET_MAP_(DOC_PROJECT, PROJ); + _SET_MAP_(DOC_TRADE, TRADE); + _SET_MAP_(_T("阀长"), L); + _SET_MAP_(_T("手轮高度"), H); + _SET_MAP_(_T("手轮直径"), W); + _SET_MAP_(_T("端面"), R); + dlg.modifyParams(i, kv); + dlg.modifyParams(i, _T("F"), bHasFlange ? _T("0") : _T("1")); + } + return !dlg.getObjects().empty(); +} + AcDbEntity * CParaDesignDlg::startForPipeValve(LPCTSTR objType , LPCTSTR objName , const double &dDN @@ -877,55 +948,9 @@ AcDbEntity * CParaDesignDlg::startForPipeValve(LPCTSTR objType CAcModuleResourceOverride myResources; if (objType && objName) { - // 首先检查一下有没有 objType 数据 - CString sSizePath = FilePath::GetSysDir() + VALVE_JSON_PATH; - WDocument doc; - if (!file2Json(sSizePath, doc)) - { - AfxMessageBox(_T("打开valvesize.josn文件失败。")); - return NULL; - } - - MIt it = doc.FindMember(_T("Pipe")); - if (it == doc.MemberEnd()) - { - AfxMessageBox(_T("未找到当前阀门类型。")); - return NULL; - } - it = it->value.FindMember(objType); - if (it == doc.MemberEnd()) - { - AfxMessageBox(_T("未找到当前阀门类型。")); - return NULL; - } - - if (it->value.MemberCount() == 0) - { - AfxMessageBox(_T("当前阀门类型没有数据。请自行输入尺寸数据。")); - } - - // 在这之前,向 json 文件中注入DN,PN两个参数 CParaDesignDlg dlg(objType, objName, idx); - - std::map kv, data; - for (size_t i(0); i < dlg.m_objs.size(); ++i) - { - GetValveInsSizeByLevel(dlg.m_objs[i].first.c_str(), dDN, pSrcData, data); - - kv.clear(); - kv.insert(std::make_pair(_T("DN"), TypeConvert::convert(dDN).c_str())); - _SET_MAP_(PID_PIPE_DPRESS, PN); - _SET_MAP_(PIPE_LEVEL, LEVEL); - _SET_MAP_(DOC_PROJECT, PROJ); - _SET_MAP_(DOC_TRADE, TRADE); - _SET_MAP_(_T("阀长"), L); - _SET_MAP_(_T("手轮高度"), H); - _SET_MAP_(_T("手轮直径"), W); - _SET_MAP_(_T("端面"), R); - dlg.modifyParams(i, kv); - dlg.modifyParams(i, _T("F"), bHasFlange ? _T("0") : _T("1")); - //dlg.hideParam(i, _T("F"), bHasFlange); - } + if (!PreparePipeValveDlg(dlg, objType, dDN, dPN, pSrcData, bHasFlange, false)) + return NULL; if (dlg.DoModal() != IDOK) return NULL; if (!dlg.m_pCurObj) return NULL; @@ -1113,6 +1138,36 @@ extern "C" __declspec(dllexport) AcDbEntity* startForPipeValve(LPCTSTR objType return CParaDesignDlg::startForPipeValve(objType, objName, dDN, dPN, bHasFlange, idx, pSrcData); } +// 无界面版:不弹"参数化设计"对话框,按 DN/PN 与 idx 直接取第 idx 个规格建模。 +// 给 MCP(cad_mcp_aec)用 —— 有界面的那条路在 MCP 里会卡在模态框上。 +// 失败返回 NULL(不弹 AfxMessageBox)。 +extern "C" __declspec(dllexport) AcDbEntity* startForPipeValveNoUI(LPCTSTR objType + , LPCTSTR objName + , const double &dDN + , const double &dPN + , bool &bHasFlange + , const int & idx + , const AecDbObjXDataMap *pSrcData = NULL) +{ + CAcModuleResourceOverride myResources; + if (!objType) return NULL; + + CParaDesignDlg dlg(objType, objName ? objName : objType, idx); + if (!PreparePipeValveDlg(dlg, objType, dDN, dPN, pSrcData, bHasFlange, true)) + return NULL; + + auto &objs = dlg.getObjects(); + int n = (idx >= 0 && idx < (int)objs.size()) ? idx : 0; + dlg.m_pCurObj = objs[n].second; + if (!dlg.m_pCurObj) return NULL; + dlg.BuildDatas(); // 原来只有点"确定"时才填 m_datas / m_Bims,start2Ptr 依赖它 + + CString ss = dlg.getCurrentObjValve(_T("F")); + bHasFlange = ss == _T("是"); + + return dlg.m_pCurObj->start2Ptr(dlg); +} + extern "C" __declspec(dllexport) void inplaceForValve(AcDbObjectId &id) { CParaDesignDlg::inplace(id); diff --git a/SourceCode/Code2026/Pool3dCmd/paraDesignDlg.h b/SourceCode/Code2026/Pool3dCmd/paraDesignDlg.h index caefaca..d5a1aaf 100644 --- a/SourceCode/Code2026/Pool3dCmd/paraDesignDlg.h +++ b/SourceCode/Code2026/Pool3dCmd/paraDesignDlg.h @@ -77,6 +77,9 @@ public: void modifyParams(const std::map &kv); void hideParam(const int &idxObj, LPCTSTR key, const bool &bHide); CString getCurrentObjValve(LPCTSTR key); + // 把 m_pCurObj 的参数摊成 m_datas / m_Bims(原来只在"确定"按钮里做, + // 无界面建模(startForPipeValveNoUI)也要用) + void BuildDatas(); CString getObjType() { return m_objType; } CString getEquType() { return m_equType; } Objects &getObjects() { return m_objs; } diff --git a/SourceCode/Code2026/cad_mcp_aec/aec_mcp_valve_tools.cpp b/SourceCode/Code2026/cad_mcp_aec/aec_mcp_valve_tools.cpp new file mode 100644 index 0000000..1fa89b8 --- /dev/null +++ b/SourceCode/Code2026/cad_mcp_aec/aec_mcp_valve_tools.cpp @@ -0,0 +1,435 @@ +// ============================================================================ +// aec_mcp_valve_tools.cpp +// cad_mcp_aec 的阀门插入工具(工作计划第 6 项) +// * add_aec_valve 参数化阀门(P3_CR* 那 19 个命令:闸阀/球阀/截止阀…) +// * add_aec_custom_valve 定制阀门(PL_CR* 那 20+ 个命令:从设备库取预制件) +// +// 一、参数化阀门 +// 命令链:P3_CRZF 等 → initParamPipeValveJsonData("闸阀") → PL_wValveEx("闸阀", …, false) +// → getEquiment(bByDB=false) → startForPipeValve(...) → 里面 dlg.DoModal()(弹"参数化设计"对话框) +// → dlg.m_pCurObj->start2Ptr(dlg) 生成 AecDbEwEquipment。 +// 产品自己的 startForPipeValve 是 pool3dcmd.arx 用 __declspec(dllexport) 导出的, +// PipelineCmd 就是 LoadLibrary + GetProcAddress 调它。我们照做,但用的是本次新增的 +// **无界面版 startForPipeValveNoUI**(不 DoModal,按 DN/PN + idx 直接建模), +// 否则 MCP 一调就卡在模态框上。 +// +// 二、定制阀门 +// 命令链:PL_CRZF → PL_wValveEx(VALVE_CRZF) → getEquiment(bByDB=true) +// → CEquipmentLibraryDlg::selectCustomValve(类别, DN) → 在 sys\Aec3DMep.db 里 +// 按 aec_class.class=<类别> + aec_package.name like '%DN%' 找包, +// select package 的 blob 落成临时 .equ 文件 → new AecDbEwEquipment; ReadFromFile(tmp)。 +// 这些 SQL 很浅,这里直接照做(不弹选型对话框)。 +// +// 两者最后都是:SetInsert(pt, vtDir) + 图层 LAY_PIPE_OUTSIDE + 入库, +// 并在专业数据里记 KEY_ASSOIC(所在管线)与 PIPE_SYS(管网系统)。 +// shortcut: 还没做插入时"断管 + 法兰对接"那套(命令里是 InsertValAtInfPos,150 行), +// 阀门现在只是摆在管线上的位置与方向,管段不会自动断开。 +// ============================================================================ + +#include "StdAfx.h" +#include "KTCad.h" +#include "aec_mcp_common.h" +#include "mcp_plugin_api.h" + +#include "AecDbEwEquipment.h" +#include "AecDbEwPipe.h" +#include "AecDbObjXDataMap.h" +#include "CoreTools.h" +#include "FilePath.h" +#include "KTSetting.h" +#include "uiiado.h" // 先定义 TGADOSRV_API(sqlite server 的头要用) +#include "UIISQLiteServer.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; + } + + // 插入点与方向:给了 pipe 就投影到管轴、方向取管轴;否则用 position/direction + bool ResolveInsert(AcDbEntity* pPipe, const nlohmann::json& a, AcGePoint3d& ptIns, AcGeVector3d& vtDir, + std::wstring& err) + { + if (!readPoint(a, "position", ptIns)) + return false; + + if (pPipe != NULL) + { + AcDbCurve* pCrv = AcDbCurve::cast(pPipe); + if (pCrv == NULL) { err = L"pipe 不是曲线"; return false; } + + AcGePoint3d ptSta(0.0, 0.0, 0.0), ptEnd(0.0, 0.0, 0.0); + pCrv->getStartPoint(ptSta); + pCrv->getEndPoint(ptEnd); + AcGeVector3d vtAxis = ptEnd - ptSta; + const double dLen2 = vtAxis.dotProduct(vtAxis); + if (dLen2 > 1e-18) + { + double t = (ptIns - ptSta).dotProduct(vtAxis) / dLen2; + if (t < 0.0) t = 0.0; + if (t > 1.0) t = 1.0; + ptIns = ptSta + vtAxis * t; // 落到管轴上 + vtDir = vtAxis; + vtDir.normalize(); + return true; + } + } + + AcGePoint3d ptDir(0.0, 0.0, 0.0); + if (readPoint(a, "direction", ptDir) && ptDir.asVector().length() > 1e-9) + { + vtDir = ptDir.asVector(); + vtDir.normalize(); + return true; + } + err = L"没给 pipe 时必须给 direction(阀门朝向)"; + return false; + } + + // 收尾:图层 + 管网系统 + 母体管线 + 入库 + bool PlaceEquipment(AecDbEwEquipment* pEqu, AcDbEntity* pPipe, const AcGePoint3d& ptIns, + const AcGeVector3d& vtDir, const nlohmann::json& a, std::wstring& err) + { + if (pEqu == NULL) { err = L"生成阀门实体失败"; return false; } + + pEqu->SetInsert(ptIns, vtDir); + + if (!hasKey(a, "layer")) + pEqu->setLayer(KTSetting::InitLayer(LAY_PIPE_OUTSIDE)); + + if (pPipe != NULL) + { + pEqu->GetSpecialtyData()->SetAt(KEY_ASSOIC, pPipe->objectId()); + + // 管网系统跟着管线走 + AecDbEwPipe* pP = AecDbEwPipe::cast(pPipe); + if (pP != NULL) + { + const AecDbObjXDataMap* pXPipe = pP->GetSpecialtyData(AcDb::kForRead); + CString sSys; + if (pXPipe != NULL && pXPipe->GetAt(PIPE_SYS, sSys) && !sSys.IsEmpty()) + pEqu->GetSpecialtyData()->SetAt(PIPE_SYS, sSys); + } + } + if (hasKey(a, "system")) + pEqu->GetSpecialtyData()->SetAt(PIPE_SYS, wstrOr(a, "system", L"").c_str()); + + return true; + } +} + +// --------------------------------------------------------------------------- +// add_aec_valve —— 参数化阀门(调 pool3dcmd.arx 的无界面导出) +// --------------------------------------------------------------------------- +class ToolAddValve : public mcp_Tool +{ +public: + std::string private_name() const override { return "add_aec_valve"; } + + std::string private_description() const override + { + return "在管线上插入**参数化阀门**(对应菜单 参数阀门 P3_CRZF 闸阀 / P3_CRQF 球阀 / P3_CRJZF 截止阀 / " + "P3_CRDF 蝶阀 / P3_CRZHF 止回阀 / P3_CRPQF 排气阀 / P3_CRDXZF 刀型闸阀 / P3_CRZSF 柱塞阀 / " + "P3_CRFQF 浮球阀 / P3_CRTJF 调节阀 / P3_CRSLKZF 水力控制阀 / P3_CRJYF 浆液阀 / P3_CRPNF 排泥阀 / " + "P3_CRDCF 电磁阀 / P3_GDBC 补偿器 / P3_CRSSJT 伸缩接头 / P3_CRCLJT 传力接头 / P3_CRXJJT 橡胶接头)。\n" + "实现:调用 pool3dcmd.arx 本次新增的无界面导出 startForPipeValveNoUI" + "(参数取自 <安装目录>/sys 里的 valvesize.json,与命令同一套),不弹对话框。\n" + "给 pipe(管线 handle)+ position(沿管的位置,会投影到管轴),阀门朝向自动取管轴;" + "dn_mm 公称直径、pn 压力等级、has_flange 是否带法兰、idx 同类型多个规格时选第几个。" + "图层缺省 LAY_PIPE_OUTSIDE。"; + } + + nlohmann::json private_input_schema() const override + { + return nlohmann::json::parse(R"({ + "type": "object", + "properties": { + "kind": { "type": "string", + "description": "阀门类型名,与 valvesize.json 里的一致:闸阀/刀型闸阀/截止阀/球阀/止回阀/蝶阀/排气阀/柱塞阀/浮球阀/调节阀/水力控制阀/浆液阀/排泥阀/电磁阀/补偿器/伸缩接头/传力接头/橡胶接头" }, + "pipe": { "type": "string", "description": "所在管线的 handle(给了就把 position 投影到管轴、朝向取管轴)" }, + "position": { "type": "array", "items": { "type": "number" }, "description": "插入位置 [x,y,z](在管线附近即可)" }, + "direction": { "type": "array", "items": { "type": "number" }, "description": "没给 pipe 时用它当阀门朝向" }, + "dn_mm": { "type": "number", "description": "公称直径 DN(毫米)" }, + "pn": { "type": "number", "description": "压力等级(MPa),缺省 1.0" }, + "has_flange": { "type": "boolean", "description": "是否带法兰,缺省否" }, + "idx": { "type": "integer", "description": "同类型有多个规格时选第几个(0 起),缺省 0" }, + "system": { "type": "string", "description": "管网系统名(缺省跟管线)" }, + "layer": { "type": "string", "description": "图层,缺省 LAY_PIPE_OUTSIDE" }, + "color": { "type": "integer" } + }, + "required": [ "kind", "position", "dn_mm" ] + })"); + } + + nlohmann::json Execute(const nlohmann::json& args) override + { + if (workingDb() == NULL) return errJson(kErrNoDocument, L"没有活动图纸"); + DocLock lock; + if (!lock.isLocked()) return errJson(kErrInternal, L"拿不到图纸写锁(有别的命令在跑?)"); + + const std::wstring sKind = wstrOr(args, "kind", L""); + if (sKind.empty()) return errJson(kErrBadParam, L"需要 kind(阀门类型名)"); + const double dDn = numOr(args, "dn_mm", 0.0); + if (dDn <= 0.0) return errJson(kErrBadParam, L"需要 dn_mm > 0"); + + // 所在管线 + AcDbEntity* pPipe = NULL; + if (hasKey(args, "pipe")) + { + const std::wstring sPipe = wstrOr(args, "pipe", L""); + pPipe = openByHandle(acharToUtf8(sPipe.c_str()), AcDb::kForRead); + if (pPipe == NULL) + return errJson(kErrNotFound, L"按 handle 打不开管线:" + sPipe); + } + + AcGePoint3d ptIns(0.0, 0.0, 0.0); + AcGeVector3d vtDir(0.0, 0.0, 1.0); + std::wstring err; + if (!ResolveInsert(pPipe, args, ptIns, vtDir, err)) + { + if (pPipe != NULL) pPipe->close(); + return errJson(kErrBadParam, err); + } + + // 调 pool3dcmd.arx 导出的无界面版 + typedef AcDbEntity*(_cdecl *FnStartValve)(LPCTSTR, LPCTSTR, const double&, const double&, + bool&, const int&, const AecDbObjXDataMap*); + AecDbEwEquipment* pEqu = NULL; + { + HINSTANCE hMod = ::LoadLibrary(FilePath::GetArxDir() + _T("\\pool3dcmd.arx")); + if (hMod == NULL) + { + if (pPipe != NULL) pPipe->close(); + return errJson(kErrInternal, L"加载 pool3dcmd.arx 失败"); + } + FnStartValve pfn = (FnStartValve)::GetProcAddress(hMod, "startForPipeValveNoUI"); + if (pfn == NULL) + { + ::FreeLibrary(hMod); + if (pPipe != NULL) pPipe->close(); + return errJson(kErrInternal, L"pool3dcmd.arx 里没有 startForPipeValveNoUI(版本太旧?)"); + } + + bool bHasFlange = hasKey(args, "has_flange") ? (intOr(args, "has_flange", 0) != 0) : false; + CString sTitle; + sTitle.Format(_T("插入%s"), sKind.c_str()); + AcDbEntity* pEnt = pfn(sKind.c_str(), (LPCTSTR)sTitle, dDn, numOr(args, "pn", 1.0), + bHasFlange, intOr(args, "idx", 0), NULL); + ::FreeLibrary(hMod); + pEqu = AecDbEwEquipment::cast(pEnt); + if (pEqu == NULL) + { + if (pEnt != NULL) { pEnt->close(); delete pEnt; } + if (pPipe != NULL) pPipe->close(); + return errJson(kErrBadParam, std::wstring(L"生成参数化阀门失败:类型名要说 valvesize.json 里的名字(") + + sKind + L");也可能是该类型没有对应 DN 的尺寸"); + } + } + + if (!PlaceEquipment(pEqu, pPipe, ptIns, vtDir, args, err)) + { + delete pEqu; + if (pPipe != NULL) pPipe->close(); + return errJson(kErrInternal, err); + } + if (hasKey(args, "color")) pEqu->setColorIndex((Adesk::UInt16)intOr(args, "color", 256)); + if (hasKey(args, "layer")) + { + AcDbObjectId idLay = KTSetting::InitLayer(wstrOr(args, "layer", L"").c_str()); + if (!idLay.isNull()) pEqu->setLayer(idLay); + } + + AcDbObjectId idNew; + Acad::ErrorStatus es = addToSpace(pEqu, &idNew); + if (es != Acad::eOk) + { + delete pEqu; + if (pPipe != NULL) pPipe->close(); + return errJson(kErrInternal, std::wstring(L"加入模型空间失败,err=") + std::to_wstring((int)es)); + } + touchEntity(pEqu); + pEqu->close(); + if (pPipe != NULL) pPipe->close(); + + nlohmann::json body; + body["ok"] = true; + body["type"] = "valve"; + body["mode"] = "parametric"; + body["kind"] = acharToUtf8(sKind.c_str()); + body["position"] = { ptIns.x, ptIns.y, ptIns.z }; + body["direction"] = { vtDir.x, vtDir.y, vtDir.z }; + body["handle"] = HandleOfId(idNew); + return okJson(body); + } +}; +EXPORT_MCP_TOOL(ToolAddValve, AddAecValveTool) + +// --------------------------------------------------------------------------- +// add_aec_custom_valve —— 定制阀门(从 sys\Aec3DMep.db 取预制件) +// --------------------------------------------------------------------------- +class ToolAddCustomValve : public mcp_Tool +{ +public: + std::string private_name() const override { return "add_aec_custom_valve"; } + + std::string private_description() const override + { + return "在管线上插入**定制阀门**(对应菜单 定制阀门 PL_CRZF 闸阀 / PL_CJZF 截止阀 / PL_AQFM 球阀 / " + "PL_CRDF 碟阀 / PL_CZHF 止回阀 / PL_CRJF 角阀 / PL_CAQF 安全阀 等)。\n" + "实现:查 <安装目录>/sys/Aec3DMep.db 里的设备库(aec_class.class = 类别," + "aec_package.name like '%DN%'),把 package blob 落成临时 .equ 文件," + "再 AecDbEwEquipment::ReadFromFile 生成实体 —— 与命令 selectCustomValve 取的是同一份数据," + "但不弹选型对话框。\n" + "给 category(类别名)+ dn_mm + pipe/position,朝向自动取管轴。图层缺省 LAY_PIPE_OUTSIDE。"; + } + + nlohmann::json private_input_schema() const override + { + return nlohmann::json::parse(R"({ + "type": "object", + "properties": { + "category": { "type": "string", "description": "设备库里的类别名(aec_class.class),如 闸阀/截止阀/球阀/碟阀/止回阀" }, + "dn_mm": { "type": "number", "description": "公称直径 DN(毫米),用来匹配库里 name 含 DN 的包" }, + "pipe": { "type": "string", "description": "所在管线的 handle(给了就把 position 投影到管轴、朝向取管轴)" }, + "position": { "type": "array", "items": { "type": "number" }, "description": "插入位置 [x,y,z]" }, + "direction": { "type": "array", "items": { "type": "number" }, "description": "没给 pipe 时用它当朝向" }, + "system": { "type": "string", "description": "管网系统名(缺省跟管线)" }, + "layer": { "type": "string", "description": "图层,缺省 LAY_PIPE_OUTSIDE" }, + "color": { "type": "integer" } + }, + "required": [ "category", "position", "dn_mm" ] + })"); + } + + nlohmann::json Execute(const nlohmann::json& args) override + { + if (workingDb() == NULL) return errJson(kErrNoDocument, L"没有活动图纸"); + DocLock lock; + if (!lock.isLocked()) return errJson(kErrInternal, L"拿不到图纸写锁(有别的命令在跑?)"); + + const std::wstring sCat = wstrOr(args, "category", L""); + if (sCat.empty()) return errJson(kErrBadParam, L"需要 category(设备库类别名,如 闸阀)"); + const double dDn = numOr(args, "dn_mm", 0.0); + if (dDn <= 0.0) return errJson(kErrBadParam, L"需要 dn_mm > 0"); + + AcDbEntity* pPipe = NULL; + if (hasKey(args, "pipe")) + { + const std::wstring sPipe = wstrOr(args, "pipe", L""); + pPipe = openByHandle(acharToUtf8(sPipe.c_str()), AcDb::kForRead); + if (pPipe == NULL) return errJson(kErrNotFound, L"按 handle 打不开管线:" + sPipe); + } + + AcGePoint3d ptIns(0.0, 0.0, 0.0); + AcGeVector3d vtDir(0.0, 0.0, 1.0); + std::wstring err; + if (!ResolveInsert(pPipe, args, ptIns, vtDir, err)) + { + if (pPipe != NULL) pPipe->close(); + return errJson(kErrBadParam, err); + } + + // 查库:类别 -> aec_class.id -> aec_package(blob) + CUIISQLiteServer db; + CString sDb = FilePath::GetSysDir() + _T("\\Aec3DMep.db"); + if (!db.InitADOServer((LPCTSTR)sDb)) + { + if (pPipe != NULL) pPipe->close(); + return errJson(kErrNotFound, L"打不开设备库:" + utf8ToWstr(acharToUtf8((LPCTSTR)sDb))); + } + + CString sDnKey; + sDnKey.Format(_T("DN%d"), (int)(dDn + 0.5)); + + CString sSql; + // 库里 aec_class.class 存的是**多级路径**(如 "管路附件\闸阀"), + // 所以用 like '%<类别>' 匹配末级,而不是等号 + sSql.Format(_T("select id,name from aec_package where idclass in") + _T(" (select id from aec_class where class='%s' or class like '%%\%s')") + _T(" and name like '%%%s%%'"), + (LPCTSTR)sCat.c_str(), (LPCTSTR)sCat.c_str(), (LPCTSTR)sDnKey); + + std::map hits; + if (db.GetSelData((LPCTSTR)sSql, hits) <= 0 || hits.empty()) + { + if (pPipe != NULL) pPipe->close(); + return errJson(kErrNotFound, std::wstring(L"设备库里没找到:类别=") + sCat + L",DN=" + (LPCTSTR)sDnKey); + } + + const CString sId = hits.begin()->first; + const CString sName = hits.begin()->second; + + // blob -> 临时 .equ 文件 + CString sTmp; + sTmp.Format(_T("%s\\mcp_valve_%d.equ"), (LPCTSTR)FilePath::getSystemTemporaryFolder(), (int)GetTickCount()); + CString sBlobSql; + sBlobSql.Format(_T("select package from aec_package where ID='%s'"), (LPCTSTR)sId); + if (db.GetSelBlob((LPCTSTR)sBlobSql, (LPCTSTR)sTmp) <= 0) + { + if (pPipe != NULL) pPipe->close(); + return errJson(kErrInternal, L"从设备库导出 package 失败(id=" + utf8ToWstr(acharToUtf8((LPCTSTR)sId)) + L")"); + } + + AecDbEwEquipment* pEqu = new AecDbEwEquipment(); + const BOOL bRead = pEqu->ReadFromFile((LPCTSTR)sTmp); + ::DeleteFile((LPCTSTR)sTmp); + if (!bRead) + { + delete pEqu; + if (pPipe != NULL) pPipe->close(); + return errJson(kErrInternal, L"ReadFromFile 失败(设备库里的 .equ 数据读不出来)"); + } + + pEqu->GetSpecialtyData()->SetAt(KEY_ID, (LPCTSTR)sId); + pEqu->GetSpecialtyData()->SetAt(KEY_NAME, (LPCTSTR)sName); + pEqu->SetDashLayer(KTSetting::InitLayer(LAY_FITN_DASH)); + pEqu->SetCenterLayer(KTSetting::InitLayer(LAY_FITN_CENTER)); + + if (!PlaceEquipment(pEqu, pPipe, ptIns, vtDir, args, err)) + { + delete pEqu; + if (pPipe != NULL) pPipe->close(); + return errJson(kErrInternal, err); + } + if (hasKey(args, "color")) pEqu->setColorIndex((Adesk::UInt16)intOr(args, "color", 256)); + if (hasKey(args, "layer")) + { + AcDbObjectId idLay = KTSetting::InitLayer(wstrOr(args, "layer", L"").c_str()); + if (!idLay.isNull()) pEqu->setLayer(idLay); + } + + AcDbObjectId idNew; + Acad::ErrorStatus es = addToSpace(pEqu, &idNew); + if (es != Acad::eOk) + { + delete pEqu; + if (pPipe != NULL) pPipe->close(); + return errJson(kErrInternal, std::wstring(L"加入模型空间失败,err=") + std::to_wstring((int)es)); + } + touchEntity(pEqu); + pEqu->close(); + if (pPipe != NULL) pPipe->close(); + + nlohmann::json body; + body["ok"] = true; + body["type"] = "valve"; + body["mode"] = "custom"; + body["category"] = acharToUtf8((LPCTSTR)sCat.c_str()); + body["dn"] = (int)(dDn + 0.5); + body["library_id"] = acharToUtf8((LPCTSTR)sId); + body["library_name"] = acharToUtf8((LPCTSTR)sName); + body["position"] = { ptIns.x, ptIns.y, ptIns.z }; + body["direction"] = { vtDir.x, vtDir.y, vtDir.z }; + body["handle"] = HandleOfId(idNew); + return okJson(body); + } +}; +EXPORT_MCP_TOOL(ToolAddCustomValve, AddAecCustomValveTool) diff --git a/SourceCode/Code2026/cad_mcp_aec/cmakelists.txt b/SourceCode/Code2026/cad_mcp_aec/cmakelists.txt index 71d5d64..bf148f9 100644 --- a/SourceCode/Code2026/cad_mcp_aec/cmakelists.txt +++ b/SourceCode/Code2026/cad_mcp_aec/cmakelists.txt @@ -14,6 +14,7 @@ set(SOURCES "aec_mcp_holder_tools.cpp" "aec_mcp_pipehanger_tools.cpp" "aec_mcp_extra_tools.cpp" + "aec_mcp_valve_tools.cpp" "aec_mcp_attachment_tools.cpp" "aec_mcp_dim_tools.cpp" "aec_mcp_roof_tools.cpp" @@ -40,6 +41,9 @@ target_link_objectarx(CadMcpAec CADMCPAEC_EXPORT) target_link_libraries(CadMcpAec PRIVATE AecRxBase CoreCommand_BAL_REMOTE + # custom valve needs CUIISQLiteServer (reads sys/Aec3DMep.db). + # CoreCommand links sqliteserver but does not propagate it. + sqliteserver ) diff --git a/SourceCode/Code2026/cad_mcp_aec/mcp_config.json b/SourceCode/Code2026/cad_mcp_aec/mcp_config.json index 4334c93..8f1daa6 100644 --- a/SourceCode/Code2026/cad_mcp_aec/mcp_config.json +++ b/SourceCode/Code2026/cad_mcp_aec/mcp_config.json @@ -6880,6 +6880,130 @@ "dll_name": "cad_mcp_aec.dll", "factory_function": "AddAecVerticalPipeHolderTool" } + }, + { + "name": "add_aec_valve", + "description": "在管线上插入**参数化阀门**(菜单 参数阀门 P3_CRZF 闸阀/P3_CRQF 球阀/P3_CRJZF 截止阀/P3_CRDF 蝶阀/P3_CRZHF 止回阀/P3_CRPQF 排气阀/P3_CRDXZF 刀型闸阀/P3_CRZSF 柱塞阀/P3_CRFQF 浮球阀/P3_CRTJF 调节阀/P3_CRSLKZF 水力控制阀/P3_CRJYF 浆液阀/P3_CRPNF 排泥阀/P3_CRDCF 电磁阀/P3_GDBC 补偿器/P3_CRSSJT 伸缩接头/P3_CRCLJT 传力接头/P3_CRXJJT 橡胶接头)。实现:调 pool3dcmd.arx 新增的无界面导出 startForPipeValveNoUI(尺寸取自 sys/valvesize.json),不弹对话框。给 pipe + position(会投影到管轴,朝向自动取管轴)+ dn_mm,可选 pn/has_flange/idx。图层缺省 LAY_PIPE_OUTSIDE。", + "inputSchema": { + "type": "object", + "properties": { + "kind": { + "type": "string", + "description": "阀门类型名,与 valvesize.json 一致:闸阀/刀型闸阀/截止阀/球阀/止回阀/蝶阀/排气阀/柱塞阀/浮球阀/调节阀/水力控制阀/浆液阀/排泥阀/电磁阀/补偿器/伸缩接头/传力接头/橡胶接头" + }, + "pipe": { + "type": "string", + "description": "所在管线的 handle" + }, + "position": { + "type": "array", + "items": { + "type": "number" + }, + "description": "插入位置 [x,y,z]" + }, + "direction": { + "type": "array", + "items": { + "type": "number" + }, + "description": "没给 pipe 时的阀门朝向" + }, + "dn_mm": { + "type": "number", + "description": "公称直径 DN(毫米)" + }, + "pn": { + "type": "number", + "description": "压力等级(MPa),缺省 1.0" + }, + "has_flange": { + "type": "boolean", + "description": "是否带法兰,缺省否" + }, + "idx": { + "type": "integer", + "description": "同类型多个规格时选第几个(0 起),缺省 0" + }, + "system": { + "type": "string", + "description": "管网系统名(缺省跟管线)" + }, + "layer": { + "type": "string", + "description": "图层,缺省 LAY_PIPE_OUTSIDE" + }, + "color": { + "type": "integer" + } + }, + "required": [ + "kind", + "position", + "dn_mm" + ] + }, + "backend": { + "type": "c++_dll", + "dll_name": "cad_mcp_aec.dll", + "factory_function": "AddAecValveTool" + } + }, + { + "name": "add_aec_custom_valve", + "description": "在管线上插入**定制阀门**(菜单 定制阀门 PL_CRZF 闸阀/PL_CJZF 截止阀/PL_AQFM 球阀/PL_CRDF 碟阀/PL_CZHF 止回阀/PL_CRJF 角阀/PL_CAQF 安全阀 等)。实现:查 sys/Aec3DMep.db 设备库(aec_class.class=类别,aec_package.name like '%DN%'),把 package blob 落成临时 .equ 再 AecDbEwEquipment::ReadFromFile,与命令 selectCustomValve 同一份数据,但不弹选型对话框。给 category + dn_mm + pipe/position,朝向自动取管轴。", + "inputSchema": { + "type": "object", + "properties": { + "category": { + "type": "string", + "description": "设备库类别名(aec_class.class),如 闸阀/截止阀/球阀/碟阀/止回阀" + }, + "dn_mm": { + "type": "number", + "description": "公称直径 DN(毫米),匹配库里 name 含 DN 的包" + }, + "pipe": { + "type": "string", + "description": "所在管线的 handle" + }, + "position": { + "type": "array", + "items": { + "type": "number" + }, + "description": "插入位置 [x,y,z]" + }, + "direction": { + "type": "array", + "items": { + "type": "number" + }, + "description": "没给 pipe 时的朝向" + }, + "system": { + "type": "string", + "description": "管网系统名(缺省跟管线)" + }, + "layer": { + "type": "string", + "description": "图层,缺省 LAY_PIPE_OUTSIDE" + }, + "color": { + "type": "integer" + } + }, + "required": [ + "category", + "position", + "dn_mm" + ] + }, + "backend": { + "type": "c++_dll", + "dll_name": "cad_mcp_aec.dll", + "factory_function": "AddAecCustomValveTool" + } } ] } diff --git a/cad-mcp-aec.md b/cad-mcp-aec.md index 32853bf..37e4ddf 100644 --- a/cad-mcp-aec.md +++ b/cad-mcp-aec.md @@ -104,6 +104,8 @@ if (IS_ZWCAD OR CAD_SDK_VERSION GREATER_EQUAL 20) | `add_aec_pipe_dim` / `update_aec_pipe_dim` | 建/改管线标注(`AecDbEwPipeDim`,纯 2D):覆盖 序号/序号批标/洞口/套管/翼环/阀门/标注阀门/标注管件 8 个命令;`kind=system` 另走「系统标注」的字段脚本。**文字默认从被标注实体的数据自动拼**(与命令格式一致),`text`/`text_down` 可选覆盖 | `target`、`kind`、`points`(引出点+文字点)、`unit`(m/mm)、`elevation_base`、`text`/`text_down`、`dim_style`/`dim_dir`/`text_up_down`/`text_height`/`text_width_scale`/`p_scale` | | `add_aec_apron` / `update_aec_apron` | 建/改散水(`AecDbXApron`,`PL_FZSS`):走向 `points` | `points`、`width`/`inside_thickness`/`outside_thickness`/`offset_from_wall`/`elevation` | | `add_aec_window` / `update_aec_window` | 建/改普通门窗(`AecDbXOpening`,`PL_QTCC` 等"插入门窗"):给墙 handle + 定位点(自动投影到墙中心线)+ 门窗块 + 宽高窗台高,内部扣墙的 2D 洞口并挂到墙的关联件 | `wall`、`position`、`block`/`block_3d`、`type`(window/door)、`width`/`height`/`sill_height`/`thickness`、`no`/`name`/`material`/`factory` | +| `add_aec_valve` | 参数化阀门(`AecDbEwEquipment`,`P3_CR*` 19 个命令):调 `pool3dcmd.arx` 新增的无界面导出 `startForPipeValveNoUI` | `kind`(闸阀/球阀/…)、`pipe`/`position`/`direction`、`dn_mm`、`pn`、`has_flange`、`idx` | +| `add_aec_custom_valve` | 定制阀门(`AecDbEwEquipment`,`PL_CR*` 20+ 命令):查 `sys\Aec3DMep.db` 取 package blob → `ReadFromFile` | `category`、`dn_mm`、`pipe`/`position`/`direction`、`layer` | | `add_aec_sfp_stair` / `update_aec_sfp_stair` | 建/改双分·双平行家族楼梯(`SFPXStair` 一套数据,7 种实体 + `kind`):双平行/双分转角/双分三跑/交叉/剪刀/三角/矩形转角 | `kind`、`start`+`direction_point`、`height`/`steep_*`/`total_width`/`run_width`/`mid_run_width`/`well_width`/`rest_platform_width`/`platform_thickness`、扶手栏杆开关与高度、`material`/`rail_material`;update 另收边梯/中梯/剖切步数、内外边梁、上梯位置、踏步取齐、层显示、平台形状、加强筋、扶手/栏杆截面与伸出、双分系列的对称与 1~4 跑跑数跑宽 | | `add_aec_roof` / `update_aec_roof` | 建/改屋顶(`AecDbXRoof`,`PL_RYPD`/`PL_RZPD`/`PL_ZJPD`/`PL_JXPD`),一个工具 + `style` | `style`(slope/dual/cusp/mount)、`points`、`ridge_start`/`ridge_end`、`left_angle`/`right_angle`、`center`/`radius`/`side_count`、`location`、`roof_height`、`slope_angle`/`mount_height`/`mount_length`、`elevation`/`bottom_elevation`、`eaves_length`、`slab_thickness`、`material`/`factory` | @@ -1151,3 +1153,52 @@ SetDimStyle(1),SetDimupdownSpace(DN) **实测**(R230 Debug,空图):DN100 管沿 +X、`placement=on_line`、`pick=(3000,0,1000)` → 返回两点 `(3000, 50, 1000)` 与 `(3500, 50, 1000)`(法向偏 DN/2=50、沿线 +500,符合规则); `above` 只剩上排 `100`、`below` 只剩下排 `114x4.0-<标高符号>1.000m`。 + +**第十批:阀门插入(参数化 + 定制)** + +`env-pipe.mnu` 里「参数阀门」19 个、「定制阀门」20+ 个命令,底层是两种完全不同的做法, +工具做成两个:`add_aec_valve`(参数化)、`add_aec_custom_valve`(定制)。 + +**① 参数化阀门(`P3_CRZF` 闸阀 / `P3_CRQF` 球阀 / …)** + +命令链:`P3_CRZF` → `initParamPipeValveJsonData("闸阀")` → `PL_wValveEx("闸阀", …, false)` +→ `getEquiment(bByDB=false)` → `startForPipeValve(...)` → 里面 `dlg.DoModal()`(弹"参数化设计"对话框) +→ `dlg.m_pCurObj->start2Ptr(dlg)` → `AecDbEwEquipment`。 +几何参数来自 `<安装目录>/sys/valveSize.json`(`Pipe.闸阀.通径闸阀(一).size[{DN,PN,L,W,H}]`), +建模代码在 `Pool3dCmd/makeValve.cpp`(6000+ 行)。 + +**MCP 侧不搬 makeValve.cpp**,而是给 `Pool3dCmd/paraDesignDlg.cpp` 加了一个**无界面导出** +`startForPipeValveNoUI(objType, objName, DN, PN, bHasFlange, idx, pSrcData)`, +`LoadLibrary(pool3dcmd.arx) + GetProcAddress` 调它(产品自己调 `startForPipeValve` 也是这个套路)。 +为此在 Pool3dCmd 里做了两处小改造(都不改变原命令行为): + +1. 把 `startForPipeValve` 里"DoModal 之前"的准备逻辑(校验 valveSize.json、构造 dlg、注入 + DN/PN/阀长/手轮…)抽成 `PreparePipeValveDlg(..., bSilent)`,两条路共用;`bSilent=true` 时不弹 + `AfxMessageBox`(MCP 里弹框会把调用方卡死)。 +2. 把"点确定"里填 `m_datas`/`m_Bims` 的那段抽成 `CParaDesignDlg::BuildDatas()`, + 无界面版在 `start2Ptr` 之前也调一次 —— **这是必须的**,`VALVE::start2Ptr` 里 + `getDatasByDlg(dlg)` 读的就是 `m_datas`,不填就返回 NULL。 + +**② 定制阀门(`PL_CRZF` 闸阀 / `PL_CJZF` 截止阀 / …)** + +命令链:`PL_CRZF` → `PL_wValveEx(VALVE_CRZF)` → `getEquiment(bByDB=true)` → +`CEquipmentLibraryDlg::selectCustomValve(类别, DN)`(**弹设备库选型对话框**)→ +在 `sys\Aec3DMep.db` 里按 `aec_class.class` + `aec_package.name like '%DN%'` 找包 → +`select package` 的 blob 落成临时 `.equ` → `new AecDbEwEquipment; ReadFromFile(tmp)`。 + +工具直接照做这段 SQL(不弹选型框)。**注意**:`Aec_Class.Class` 存的是**多级路径** +(如 `管路附件\闸阀`),所以要用 `class = '<类别>' or class like '%\<类别>'` 匹配末级, +用等号是查不到的(第一版就栽在这)。`Aec_Package.Name` 里带 `DN100` 这种字样,可以 like 过滤。 + +**共同部分**:`SetInsert(pt, vtDir)`(给 `pipe` 时位置投影到管轴、朝向取管轴)+ 图层 +`LAY_PIPE_OUTSIDE` + 专业数据记 `KEY_ASSOIC`(所在管线)与 `PIPE_SYS`(跟管线)。 + +**工程改动**:`cad_mcp_aec/cmakelists.txt` 里补了 `sqliteserver`(`CUIISQLiteServer` 在 +`UIISQLiteServer.h`,但 `TGADOSRV_API` 宏要先 `#include "uiiado.h"`;CoreCommand 链了 sqliteserver +却**不传递给**我们,得自己再链一次)。 + +**实测**(R230 Debug,空图,DN100 给水管):参数化闸阀 → handle `31F`;定制闸阀 → +`320`,库里取到 `Z45X(RVHX)弹性座封楔式闸阀DN100`。 + +**限制**:`shortcut:` 还没做插入时的"断管 + 法兰对接"(命令里是 `InsertValAtInfPos`,约 150 行), +阀门只是摆在管线上的位置与方向,管段不会自动断开。