1. PIPE_DN/PIPE_OD/PIPE_THICK 存的是图纸单位(米),CoreTools::QueryFlangData 要毫米: AddFlangeAt 里先换算(<100 就 ×1000),DN<=0 直接报错不建(DN=0 查不到法兰数据)。 2. 阀门 DN 必须和管线一致:add_aec_valve 从管线专业数据读 DN(×1000 换 mm), 和 dn_mm 差超过 0.5 就报错,提示"阀门和管道 DN 必须相同"。 3. 系统没初始化别建管道:PL_DHGD -> PL_wpipedrawEx 之前会 CoreTools::hasCSSZ(...), 要求跑过 pl_cssz(KTModelLayoutXData::ReadModelXData(KT_PIPEINIT) + 检查 DOC_PIDPROJECT)。 之前在空图里直接 add_aec_pipe,建出来的管 DN/等级/系统都是空的(特性面板 DN=0)。 现在 add_aec_pipe / add_aec_valve 都先 hasCSSZ(false),没初始化明确报错"先运行 pl_cssz"。 测试也要在已初始化的图纸(如 pcf_1.dwg)里做,或先跑一次 pl_cssz。 R230 编译通过。CAD 实测(已初始化图纸)待补。
656 lines
26 KiB
C++
656 lines
26 KiB
C++
// ============================================================================
|
||
// 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<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 "AecDbEwFitting.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;
|
||
}
|
||
|
||
|
||
// 断管:把管线在阀门两端各切一刀,原来的管留一段、克隆出来一段。
|
||
// 老代码里是 TDbPipeLine::SplitWPipe(PipelineCmd 模块,我们链不到),
|
||
// 但它收尾时用的就是 AecDbEwPipe::SetStartPoint/SetEndPoint 挪管端,
|
||
// 这里照那个思路做:靠起点的那端给原管、另一端给克隆出来的新管。
|
||
bool SplitPipeAtValve(AecDbEwPipe* pPipe, AecDbEwEquipment* pEqu, AcGePoint3d& ptA, AcGePoint3d& ptB,
|
||
std::wstring& err)
|
||
{
|
||
if (pPipe == NULL || pEqu == NULL) { err = L"断管缺管线或阀门"; return false; }
|
||
|
||
AecInterface inf1, inf2;
|
||
::memset(&inf1, 0, sizeof(AecInterface));
|
||
::memset(&inf2, 0, sizeof(AecInterface));
|
||
AecMInterfaces* pInf = dynamic_cast<AecMInterfaces*>(pEqu);
|
||
if (pInf == NULL || pInf->GetInterfaceCount() < 2) { err = L"阀门取不到两个接口"; return false; }
|
||
if (!pInf->GetInterface(0, inf1) || !pInf->GetInterface(1, inf2))
|
||
{
|
||
err = L"阀门接口取点失败";
|
||
return false;
|
||
}
|
||
|
||
AcGePoint3d ptSta(0.0, 0.0, 0.0), ptEnd(0.0, 0.0, 0.0);
|
||
pPipe->getStartPoint(ptSta);
|
||
pPipe->getEndPoint(ptEnd);
|
||
|
||
// 哪一端离管起点近,就把那一端留给原管
|
||
if (inf1.ptPos.distanceTo(ptSta) <= inf2.ptPos.distanceTo(ptSta))
|
||
{
|
||
ptA = inf1.ptPos; // 原管 end
|
||
ptB = inf2.ptPos; // 新管 start
|
||
}
|
||
else
|
||
{
|
||
ptA = inf2.ptPos;
|
||
ptB = inf1.ptPos;
|
||
}
|
||
|
||
// 新的一段:从阀门另一端到原来的终点
|
||
AcDbEntity* pClone = (AcDbEntity*)pPipe->clone();
|
||
AecDbEwPipe* pNewPipe = (pClone != NULL) ? AecDbEwPipe::cast(pClone) : NULL;
|
||
if (pNewPipe == NULL)
|
||
{
|
||
delete pClone;
|
||
err = L"复制管线失败";
|
||
return false;
|
||
}
|
||
|
||
pNewPipe->SetStartPoint(ptB);
|
||
pNewPipe->SetEndPoint(ptEnd);
|
||
|
||
AcDbObjectId idNew;
|
||
if (addToSpace(pNewPipe, &idNew) != Acad::eOk)
|
||
{
|
||
delete pNewPipe;
|
||
err = L"新管段入库失败";
|
||
return false;
|
||
}
|
||
touchEntity(pNewPipe);
|
||
pNewPipe->close();
|
||
|
||
// 原管:终点缩到阀门这一端
|
||
pPipe->SetEndPoint(ptA);
|
||
touchEntity(pPipe);
|
||
return true;
|
||
}
|
||
|
||
|
||
// 断口上的法兰:照 TDbPipeLine::Add1Flange 建一个 eFlange 的 AecDbEwFitting。
|
||
// 尺寸来自 CoreTools::QueryFlangData(查 sys\Plumbing.db 的 AecFlangeData 表):
|
||
// dWD 法兰盘外径 / dWh 大盘面厚度 / dThk 总厚 / dN 螺栓数
|
||
// 老代码 Add2Flange 里有一句 “dThk = dWh; // 默认一律不带颈”,
|
||
// 所以法兰一律 KEY_FLANGNECK = FALSE(用户也确认:阀门的法兰没有法兰颈)。
|
||
bool AddFlangeAt(const AcGePoint3d& ptPos, const AcGeVector3d& vtDir, double dDN, double dOD,
|
||
double dPressure, LPCTSTR pszSys, const nlohmann::json& a, std::wstring& err)
|
||
{
|
||
// PIPE_DN / PIPE_OD 存的是图纸单位(米),查法兰库要毫米
|
||
if (dDN < 100.0) dDN *= 1000.0;
|
||
if (dOD < 100.0) dOD *= 1000.0;
|
||
if (dDN <= 0.0)
|
||
{
|
||
err = L"管线 DN 是 0,查不了法兰库;先用 pl_cssz 初始化系统并给 add_aec_pipe 传 dn_mm";
|
||
return false;
|
||
}
|
||
// 查库:法兰尺寸
|
||
double dWD = 0.0, dWh = 0.0, dThk = 0.0, dN = 0.0;
|
||
{
|
||
CUIISQLiteServer db;
|
||
CString sDb = FilePath::GetSysDir() + _T("\Plumbing.db");
|
||
if (!db.InitADOServer((LPCTSTR)sDb))
|
||
{
|
||
err = L"打不开法兰库 sys\Plumbing.db";
|
||
return false;
|
||
}
|
||
if (!CoreTools::QueryFlangData(&db, dDN, dPressure, dWD, dWh, dThk, dN))
|
||
{
|
||
err = L"法兰库里没有该 DN/压力 的法兰数据";
|
||
return false;
|
||
}
|
||
}
|
||
if (dWD <= 0.0) { err = L"法兰外径查出来是 0"; return false; }
|
||
|
||
dThk = dWh; // 无颈:总厚 = 大盘面厚度(与老代码一致)
|
||
|
||
AecDbEwFitting* pFlange = new AecDbEwFitting();
|
||
AecDbObjXDataMap* pMap = pFlange->GetSpecialtyData();
|
||
|
||
pMap->SetAt(KEY_FITSRV, KEY_FITWPIPE);
|
||
pMap->SetAt(KEY_RUNTIME, eFlange);
|
||
pMap->SetAt(KEY_MAJBR, vtDir); // 朝向:背向阀门
|
||
pMap->SetAt(KEY_NORMAL, AcGeVector3d::kZAxis);
|
||
pMap->SetAt(KEY_2DSTYLE, (dDN > 50.0) ? 2 : 1); // 大管双线画(对应 isDoubleLine)
|
||
pMap->SetAt(PIPE_DN, dDN);
|
||
pMap->SetAt(PIPE_N, dN); // 螺栓数
|
||
pMap->SetAt(PIPE_OD, dOD);
|
||
pMap->SetAt(PIPE_WD, dWD); // 法兰盘外径
|
||
pMap->SetAt(PIPE_WTHICK, dWh); // 大盘面厚度
|
||
pMap->SetAt(PIPE_THICK, dThk); // 总厚
|
||
pMap->SetAt(KEY_START, ptPos);
|
||
pMap->SetAt(KEY_FLANGNECK, FALSE); // 无颈(阀门的法兰就是这样)
|
||
pMap->SetAt(KEY_FLANGSLAB, FALSE); // 不是盲板
|
||
if (pszSys != NULL && pszSys[0] != 0)
|
||
pMap->SetAt(PIPE_SYS, pszSys);
|
||
if (hasKey(a, "flange_spec"))
|
||
pMap->SetAt(PIPE_FLANGF, wstrOr(a, "flange_spec", L"").c_str());
|
||
|
||
// 垫片:老代码查 TDbPipeSupport::QueryGasketData(PipelineCmd 模块,我们链不到),
|
||
// 先用参数给厚度,缺省 0
|
||
const double dGasket = numOr(a, "gasket_thickness", 0.0);
|
||
pMap->SetAt(PIPE_GASKETM, _T("石棉橡胶板"));
|
||
pMap->SetAt(PIPE_PRESSURE, dPressure);
|
||
pMap->SetAt(PIPE_GASKET, dGasket);
|
||
|
||
if (hasKey(a, "flange_layer"))
|
||
pFlange->setLayer(KTSetting::InitLayer(wstrOr(a, "flange_layer", L"").c_str()));
|
||
else if (hasKey(a, "layer"))
|
||
pFlange->setLayer(KTSetting::InitLayer(wstrOr(a, "layer", L"").c_str()));
|
||
else
|
||
pFlange->setLayer(KTSetting::InitLayer(LAY_PIPE_OUTSIDE));
|
||
|
||
AcDbObjectId idNew;
|
||
if (addToSpace(pFlange, &idNew) != Acad::eOk)
|
||
{
|
||
delete pFlange;
|
||
err = L"法兰入库失败";
|
||
return false;
|
||
}
|
||
touchEntity(pFlange);
|
||
pFlange->close();
|
||
return true;
|
||
}
|
||
|
||
// 收尾:图层 + 管网系统 + 母体管线 + 入库
|
||
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": "是否带法兰,缺省否" },
|
||
"break_pipe": { "type": "boolean", "description": "是否在插入处把管线断开(缺省是)" },
|
||
"add_flanges": { "type": "boolean", "description": "【实验性,缺省会崩 CAD,别开】断口两侧各生成一个法兰(尺寸查 sys\Plumbing.db)" },
|
||
"gasket_thickness": { "type": "number", "description": "垫片厚度,缺省 0" },
|
||
"flange_spec": { "type": "string", "description": "法兰标准/等级文字(PIPE_FLANGF)" },
|
||
"flange_layer": { "type": "string", "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::kForWrite); // 断管要改端点
|
||
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 (!CoreTools::hasCSSZ(false))
|
||
return errJson(kErrBadParam, L"管道系统没有初始化:先运行 pl_cssz 再插阀门");
|
||
|
||
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);
|
||
|
||
// 阀门 DN 必须和管线一致:管线 DN 从专业数据读(存米),不一致直接报错
|
||
if (pPipe != NULL)
|
||
{
|
||
double dDnPipe = 0.0;
|
||
AecDbEwPipe* pPWd = AecDbEwPipe::cast(pPipe);
|
||
const AecDbObjXDataMap* pXP = (pPWd != NULL) ? pPWd->GetSpecialtyData(AcDb::kForRead) : NULL;
|
||
if (pXP != NULL) pXP->GetAt(PIPE_DN, dDnPipe);
|
||
if (dDnPipe > 0.0 && dDnPipe < 100.0) dDnPipe *= 1000.0;
|
||
const double dDnArg = numOr(args, "dn_mm", 0.0);
|
||
if (dDnPipe > 0.0 && dDnArg > 0.0 && fabs(dDnPipe - dDnArg) > 0.5)
|
||
{
|
||
pEqu->erase();
|
||
pEqu->close();
|
||
pPipe->close();
|
||
wchar_t buf[160];
|
||
swprintf(buf, 160, L"阀门 DN%g 和管线 DN%g 不一致(阀门和管道 DN 必须相同)", dDnArg, dDnPipe);
|
||
return errJson(kErrBadParam, buf);
|
||
}
|
||
}
|
||
|
||
// 断管(老代码里的 SplitWPipe):缺省开,管线要在插入点断开
|
||
AcGePoint3d ptA(0.0, 0.0, 0.0), ptB(0.0, 0.0, 0.0);
|
||
bool bSplit = hasKey(args, "break_pipe") ? (intOr(args, "break_pipe", 1) != 0) : true;
|
||
bool bSplitOk = false;
|
||
if (bSplit && pPipe != NULL)
|
||
{
|
||
AecDbEwPipe* pPW = AecDbEwPipe::cast(pPipe);
|
||
if (pPW != NULL)
|
||
{
|
||
bSplitOk = SplitPipeAtValve(pPW, pEqu, ptA, ptB, err);
|
||
|
||
// 断口两侧各来一个法兰(老代码 Add2Flange:无颈)
|
||
const bool bFlange = hasKey(args, "add_flanges") ? (intOr(args, "add_flanges", 0) != 0) : false; // 实验性:会崩,见文档
|
||
if (bSplitOk && bFlange)
|
||
{
|
||
// 管径/外径/系统 从管线专业数据取
|
||
double dDN = 0.0, dOD = 0.0;
|
||
CString sSys;
|
||
const AecDbObjXDataMap* pXP = pPW->GetSpecialtyData(AcDb::kForRead);
|
||
if (pXP != NULL)
|
||
{
|
||
pXP->GetAt(PIPE_DN, dDN);
|
||
pXP->GetAt(PIPE_OD, dOD);
|
||
pXP->GetAt(PIPE_SYS, sSys);
|
||
}
|
||
const double dPre = numOr(args, "pn", 1.0);
|
||
std::wstring sErrF;
|
||
if (!AddFlangeAt(ptA, -vtDir, dDN, dOD, dPre, (LPCTSTR)sSys, args, sErrF))
|
||
err = sErrF;
|
||
else if (!AddFlangeAt(ptB, vtDir, dDN, dOD, dPre, (LPCTSTR)sSys, args, sErrF))
|
||
err = sErrF;
|
||
}
|
||
}
|
||
else
|
||
err = L"pipe 不是管道(AecDbEwPipe),不打断管";
|
||
}
|
||
|
||
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);
|
||
body["pipe_split"] = bSplitOk;
|
||
if (!bSplitOk && bSplit) body["split_error"] = acharToUtf8(err.c_str());
|
||
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<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<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<CString, CString> 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)
|