// ============================================================================ // aec_mcp_shape_tools.cpp // cad_mcp_aec 的"造型"工具:对应菜单命令 // * PL_DTYG —— 向墙/板/梁/柱/堰/异形构筑物(AecDbBDPipeBase 派生)里加几何体 // * PL_YXGZ —— 新建一个空的异形构筑物(AecDbMultiPartEntity)再加几何体 // // 命令的做法(Pool3dCmd/DTYGDlg.cpp): // 每一种造型都是 AcDbX3dSolid,用 Create(prim, 中心, vtZ, n, 尺寸...) 造出来, // 然后包一层 AecDbX3dPart(DTYG_TYPE 记中文造型名、BIM 数据记名称/颜色/统计类型), // 再 host->AddX3dPart(pPart, kBoolUnite / kBoolSubtract) —— 并/差就是这么来的。 // 修改时命令是"重建 solid":pPart->RemoveAt(0) + AddX3dSolid(新的)。 // 本工具改成整块部件重建(host->RemoveX3dPart(guid) + AddX3dPart(新 part, mode)), // 这样连"并/差"模式、名称、位置都能一起改。 // // 尺寸都用**图纸单位**,与命令的对话框一致。 // shortcut: 只支持能直接用 AcDbX3dSolid::Create 造出来的基本体 // (立方体/圆柱/圆台/四方台/球体/球台/楔形体);拉伸体、旋转体、放样体、扫掠体、 // 环体、切割、异形堰要选曲线/面域,需要的话再单独加。 // ============================================================================ #include "StdAfx.h" #include "KTCad.h" #include "aec_mcp_common.h" #include "mcp_plugin_api.h" #include "AecDbBDPipeBase.h" #include "AecDbMultiPartEntity.h" #include "AecDbX3dPart.h" #include "AecDbObjXDataMap.h" #include "AcDbX3dSolid.h" #include "CoreTools.h" #include "KTSetting.h" using namespace aec_mcp; namespace { // id -> 句柄字符串(刚入库的对象用) 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; } // ---------------------------------------------------------------- 造型表 struct ShapeSpec { const wchar_t* pszKey; // 工具参数 shape 的取值 const wchar_t* pszName; // DTYG_TYPE / 部件名(与命令 getName() 一致) AcDbX3dSolid::Primitive ePrim; int nParams; const char* aszParam[6]; // 参数名,顺序与 Create 一致 }; // 参数顺序全部照抄命令里各 makeSolid() 的 Create(...),顺序错了尺寸就串位。 const ShapeSpec g_shapes[] = { { L"box", L"立方体", AcDbX3dSolid::kBox, 3, { "length", "width", "height" } }, { L"cylinder", L"圆柱", AcDbX3dSolid::kCylinder, 2, { "radius", "height" } }, { L"cone", L"圆台", AcDbX3dSolid::kCone, 3, { "radius_bottom", "radius_top", "height" } }, { L"pyramid", L"四方台", AcDbX3dSolid::kPyramid, 6, { "top_length", "top_width", "bottom_length", "bottom_width", "height", "x_offset" } }, { L"sphere", L"球体", AcDbX3dSolid::kSpere, 1, { "radius" } }, { L"drum", L"球台", AcDbX3dSolid::kDrum, 5, { "radius", "len1", "len2", "start_angle", "end_angle" } }, { L"wedge", L"楔形体", AcDbX3dSolid::kWedge, 3, { "a", "b", "thickness" } }, // 放样体没有固定尺寸参数,截面由 sections 描述(见 BuildLoftSolid) { L"loft", L"放样体", AcDbX3dSolid::kLoft, 0, { } }, }; const ShapeSpec* FindShape(const std::wstring& sKey) { for (int i = 0; i < _countof(g_shapes); ++i) { if (sKey == g_shapes[i].pszKey) return &g_shapes[i]; } return NULL; } const ShapeSpec* FindShapeByName(const CString& sName) { for (int i = 0; i < _countof(g_shapes); ++i) { if (sName == g_shapes[i].pszName) return &g_shapes[i]; } return NULL; } // 造型名字 -> 参数数组(缺省全 0) void InitParams(double* p) { for (int i = 0; i < 6; ++i) p[i] = 0.0; } // Create() 用的中心点偏移:命令里 ptOrg 是"插入点",各造型把自己挪到中心 double CenterOffset(const ShapeSpec& sp, const double* p) { switch (sp.ePrim) { case AcDbX3dSolid::kBox: case AcDbX3dSolid::kCylinder: case AcDbX3dSolid::kCone: return 0.5 * p[sp.nParams - 1]; // 最后一个都是高 case AcDbX3dSolid::kSpere: return p[0]; case AcDbX3dSolid::kDrum: return p[2]; // len2 default: return 0.0; // pyramid / wedge 以插入点为原点 } } AcDbX3dSolid* BuildSolid(const ShapeSpec& sp, const double* p, const AcGePoint3d& ptIns, const AcGeVector3d& vtZ, std::wstring& err) { const AcGePoint3d ptCen = ptIns + vtZ * CenterOffset(sp, p); AcDbX3dSolid* pSolid = new AcDbX3dSolid(); bool bOk = false; switch (sp.nParams) { case 1: bOk = pSolid->Create(sp.ePrim, ptCen, vtZ, 1, p[0]); break; case 2: bOk = pSolid->Create(sp.ePrim, ptCen, vtZ, 2, p[0], p[1]); break; case 3: bOk = pSolid->Create(sp.ePrim, ptCen, vtZ, 3, p[0], p[1], p[2]); break; case 5: bOk = pSolid->Create(sp.ePrim, ptCen, vtZ, 5, p[0], p[1], p[2], p[3], p[4]); break; case 6: bOk = pSolid->Create(sp.ePrim, ptCen, vtZ, 6, p[0], p[1], p[2], p[3], p[4], p[5]); break; default: break; } if (!bOk) { delete pSolid; pSolid = NULL; err = std::wstring(L"造型几何创建失败:") + sp.pszName + L"(检查尺寸是否都 > 0)"; return NULL; } return pSolid; } // 放样体(loft):命令 CreateLoft 是"一级一级选闭合轮廓 + 给层高", // 工具里换成一次给 sections:[ {diameter, elevation} 或 {points:[[x,y],...], elevation} ] // 每级沿 direction 抬 elevation,轮廓用 OCS(x/y 就是给定坐标)。 // shortcut: 放样体不支持 update(AcDbX3dSolid 只存尺寸不存截面),要改就删了重建。 AcDbX3dSolid* BuildLoftSolid(const nlohmann::json& a, const AcGePoint3d& ptIns, const AcGeVector3d& vtZ, std::wstring& err) { if (!hasKey(a, "sections") || !a["sections"].is_array() || a["sections"].size() < 2) { err = L"放样体需要 sections:至少两级截面,每级 {diameter} 或 {points:[[x,y],...]},可选 elevation"; return NULL; } AcArray regs; for (size_t i = 0; i < a["sections"].size(); ++i) { const nlohmann::json& sec = a["sections"][i]; const double dEl = sec.contains("elevation") ? sec["elevation"].get() : 0.0; AcGePoint3d ptSec = ptIns + vtZ * dEl; AcDbRegion* pReg = NULL; if (sec.contains("diameter")) { const double dD = sec["diameter"].get(); if (dD <= 0.0) { err = L"sections[i].diameter 必须 > 0"; break; } AcDbCircle cir(ptSec, vtZ, 0.5 * dD); pReg = CoreTools::createRegion(&cir); } else if (sec.contains("points") && sec["points"].is_array() && sec["points"].size() >= 3) { AcDbPolyline pl((int)sec["points"].size()); for (size_t j = 0; j < sec["points"].size(); ++j) { const nlohmann::json& pt = sec["points"][j]; if (!pt.is_array() || pt.size() < 2) continue; pl.addVertexAt((int)j, AcGePoint2d(pt[0].get(), pt[1].get()), 0.0, 0.0, 0.0); } pl.setClosed(true); pl.setNormal(vtZ); pl.setElevation((ptSec - AcGePoint3d::kOrigin).dotProduct(vtZ)); // OCS 标高 = 沿单位法向的投影 pReg = CoreTools::createRegion(&pl); } if (pReg == NULL) { err = L"第 " + std::to_wstring(i + 1) + L" 级截面生成失败(要不闭合,要不 points 太少)"; break; } regs.append(pReg); } AcDbX3dSolid* pSolid = NULL; if (err.empty() && regs.length() >= 2) { pSolid = new AcDbX3dSolid(); if (!pSolid->CreateLoft(regs)) { delete pSolid; pSolid = NULL; err = L"CreateLoft 失败(各级截面得共面于同一法向、并且层高不为 0)"; } } else if (err.empty()) { err = L"放样体至少需要 2 级有效截面"; } for (int i = 0; i < regs.length(); ++i) { delete regs[i]; } return pSolid; } // 部件:DTYG_TYPE + BIM 数据(名称/颜色/统计类型)+ 单个 solid,与命令一致 AecDbX3dPart* MakePart(const ShapeSpec& sp, AcDbX3dSolid* pSolid, const std::wstring& sName, int nColor, const AcGePoint3d& ptIns, const AcGeVector3d& vtX, const AcGeVector3d& vtY, const AcGeVector3d& vtZ) { AecDbX3dPart* pPart = new AecDbX3dPart(); pPart->GetSpecialtyData()->SetAt(_T("DTYG_TYPE"), sp.pszName); // solid 的 org 是**中心**,工具的 position 是**插入点**,两个都留着免得回调时错位 pPart->GetSpecialtyData()->SetAt(_T("DTYG_INS"), ptIns); pPart->SetName(sName.c_str()); CString sClr; if (nColor == 0) sClr = _T("ByBlock"); else if (nColor == 256) sClr = _T("ByLayer"); else sClr.Format(_T("%d"), nColor); SH_BIM_BLOCK bims; bims.push_back(std::make_pair(CString(_T("名称")), CString(sName.c_str()))); bims.push_back(std::make_pair(CString(_T("颜色")), sClr)); bims.push_back(std::make_pair(CString(_T("统计类型")), CString(_T("其它")))); pPart->setBimDatas(bims); pPart->AddX3dSolid(pSolid); pPart->SetOrgPoint(pSolid->GetCentoridPoint()); pPart->SetAxis(0, vtX); pPart->SetAxis(1, vtY); pPart->SetAxis(2, vtZ); return pPart; } // 母体:墙/板/梁/柱/堰/异形构筑物 都是 AecDbBDPipeBase 派生 AecDbBDPipeBase* OpenHost(const nlohmann::json& args, AcDbEntity*& pOut, std::wstring& err) { pOut = NULL; if (!hasKey(args, "host")) { err = L"缺少 host(母体实体的 handle:墙/板/梁/柱/堰/异形构筑物)"; return NULL; } const std::string sHost = args["host"].get(); pOut = openByHandle(sHost, AcDb::kForWrite); if (pOut == NULL) { err = L"按 handle 打不开母体:" + utf8ToWstr(sHost); return NULL; } AecDbBDPipeBase* pHost = AecDbBDPipeBase::cast(pOut); if (pHost == NULL) { const std::string sCls = classNameOf(pOut); err = L"handle " + utf8ToWstr(sHost) + L" 是 " + utf8ToWstr(sCls) + L",不是可加造型的实体(需要 AecDbBDPipeBase 派生:墙/板/梁/柱/堰/异形构筑物)"; pOut->close(); pOut = NULL; } return pHost; } // 方向:give direction 就用,否则 Z AcGeVector3d DirOf(const nlohmann::json& a) { AcGePoint3d ptDir; AcGeVector3d vt = AcGeVector3d::kZAxis; if (readPoint(a, "direction", ptDir)) { AcGeVector3d vtGiven = ptDir.asVector(); if (vtGiven.length() > 1e-9) vt = vtGiven; } vt.normalize(); return vt; } // 与 vtZ 垂直的一组轴(命令用当前 UCS 的 X/Y;工具里按 vtZ 推,避免依赖 UCS) void AxesOf(const AcGeVector3d& vtZ, AcGeVector3d& vtX, AcGeVector3d& vtY) { vtX = AcGeVector3d::kZAxis.crossProduct(vtZ); if (vtX.length() < 1e-9) vtX = AcGeVector3d::kXAxis; vtX.normalize(); vtY = vtZ.crossProduct(vtX).normalize(); } // 造型的插入点:优先用记下的 DTYG_INS,老数据(命令建的)按中心反推 AcGePoint3d InsertPointOf(AecDbX3dPart* pPart, const ShapeSpec& sp, const double* p, const AcGePoint3d& ptCenter, const AcGeVector3d& vtZ) { AcGePoint3d ptIns(0.0, 0.0, 0.0); if (!pPart->GetSpecialtyData()->GetAt(_T("DTYG_INS"), ptIns)) ptIns = ptCenter - vtZ * CenterOffset(sp, p); return ptIns; } std::string ModeToStr(int nOper) { return (nOper == AcDb::kBoolSubtract) ? "subtract" : "unite"; } nlohmann::json ParamsToJson(const ShapeSpec& sp, const double* p) { nlohmann::json o = nlohmann::json::object(); for (int i = 0; i < sp.nParams; ++i) o[sp.aszParam[i]] = p[i]; return o; } nlohmann::json PartToJson(AecDbBDPipeBase* pHost, int nIndex) { AecDbX3dPart* pPart = pHost->GetX3dPart(nIndex); if (pPart == NULL) return nlohmann::json(); nlohmann::json o; CString sType; pPart->GetSpecialtyData()->GetAt(_T("DTYG_TYPE"), sType); CString sName; pPart->GetName(sName); o["guid"] = acharToUtf8(pPart->GetGUID()); o["name"] = acharToUtf8((LPCTSTR)sName); o["type"] = acharToUtf8((LPCTSTR)sType); o["mode"] = ModeToStr(pPart->GetBoolOperFor()); const ShapeSpec* sp = FindShapeByName(sType); if (sp != NULL) o["shape"] = acharToUtf8(sp->pszKey); if (pPart->GetX3dSolidCount() > 0) { AcDbX3dSolid* pSolid = pPart->GetX3dSolid(0); if (pSolid != NULL) { const AcGePoint3d ptCenter = pSolid->GetOrgPoint(); AcGeVector3d vtZ = pSolid->GetvtZ(); if (vtZ.length() < 1e-9) vtZ = AcGeVector3d::kZAxis; // 放样体没存 vtZ o["direction"] = { vtZ.x, vtZ.y, vtZ.z }; if (sp != NULL) { AcArray ar; double p[6] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; if (pSolid->GetSolidData(sp->ePrim, ar)) { for (int i = 0; i < sp->nParams && i < ar.length(); ++i) p[i] = ar[i]; o["params"] = ParamsToJson(*sp, p); } const AcGePoint3d ptIns = InsertPointOf(pPart, *sp, p, ptCenter, vtZ); o["position"] = { ptIns.x, ptIns.y, ptIns.z }; } else { o["position"] = { ptCenter.x, ptCenter.y, ptCenter.z }; } } } return o; } } // --------------------------------------------------------------------------- // add_aec_multipart —— 新建异形构筑物(PL_YXGZ) // --------------------------------------------------------------------------- class ToolAddMultiPart : public mcp_Tool { public: std::string private_name() const override { return "add_aec_multipart"; } std::string private_description() const override { return "新建一个空的异形构筑物(AecDbMultiPartEntity,菜单 PL_YXGZ)," "之后用 add_aec_shape 往里加几何体。命令里截面是个半径 1mm 的占位圆、基准线是 1mm 的竖线," "真正的形体全靠后加的造型,这里保持一致。position 是安放点(图纸单位),图层缺省 LAY_YGDT。" "返回新实体的 handle。"; } nlohmann::json private_input_schema() const override { return nlohmann::json::parse(R"({ "type": "object", "properties": { "position": { "type": "array", "items": { "type": "number" }, "description": "安放点 [x,y] 或 [x,y,z],缺省原点" }, "direction": { "type": "array", "items": { "type": "number" }, "description": "竖向 [x,y,z],缺省 [0,0,1]" }, "layer": { "type": "string", "description": "图层,缺省 LAY_YGDT" } } })"); } 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 pt(0.0, 0.0, 0.0); readPoint(args, "position", pt); const AcGeVector3d vtZ = DirOf(args); AecDbMultiPartEntity* pEnt = new AecDbMultiPartEntity(); // 截面与基准线:与命令一致,1mm 占位 const double l = 0.001, h = 0.001; AcDbCircle cir(pt, AcGeVector3d::kZAxis, l); AcDbLine line(pt, pt + vtZ * h); pEnt->SetBaseCurve(&line); AcDbRegion* pReg = CoreTools::createRegion(&cir); if (pReg == NULL) { delete pEnt; return errJson(kErrInternal, L"占位截面(1mm 圆)生成失败"); } pEnt->SetSection(pReg, pt); delete pReg; pReg = NULL; AcGeVector3d vtX, vtY; AxesOf(vtZ, vtX, vtY); pEnt->SetOrg(pt); pEnt->SetXDir(vtX); pEnt->SetYDir(vtY); pEnt->SetZDir(vtZ); std::wstring sLay = hasKey(args, "layer") ? wstrOr(args, "layer", L"") : L""; if (sLay.empty()) sLay = L"LAY_YGDT"; AcDbObjectId idLay = KTSetting::InitLayer(sLay.c_str()); if (!idLay.isNull()) pEnt->setLayer(idLay); AcDbObjectId idNew; Acad::ErrorStatus es = addToSpace(pEnt, &idNew); if (es != Acad::eOk) { delete pEnt; return errJson(kErrInternal, std::wstring(L"加入模型空间失败,err=") + std::to_wstring((int)es)); } touchEntity(pEnt); pEnt->close(); nlohmann::json body; body["ok"] = true; body["type"] = "multipart"; body["handle"] = HandleOfId(idNew); body["position"] = { pt.x, pt.y, pt.z }; return okJson(body); } }; EXPORT_MCP_TOOL(ToolAddMultiPart, AddAecMultiPartTool) // --------------------------------------------------------------------------- // add_aec_shape —— 向已有实体加造型(PL_DTYG) // --------------------------------------------------------------------------- class ToolAddShape : public mcp_Tool { public: std::string private_name() const override { return "add_aec_shape"; } std::string private_description() const override { return "向墙/板/梁/柱/堰/异形构筑物(AecDbBDPipeBase 派生)里加一个几何体(菜单 PL_DTYG 造型设计)。" "造型种类见 shape,尺寸参数见各 shape 说明,全部图纸单位;mode=unite 加料 / subtract 挖料。" "position 是造型的插入点(立方体/圆柱/圆台/球台是底面中心,球体是球底,四方台/楔形体以该点为原点)。" "命令里同一个实体上可以叠很多造型,这里一次加一个,返回 part_guid;改/删/查分别用 " "update_aec_shape / delete_aec_shape / list_aec_shapes。" "支持 7 种基本体 + 放样体”(shape=loft,用 sections 给多级截面,命令里是一级一级选轮廓 + 层高)。" "拉伸/旋转/扫掠/环/切割/异形堰没做。放样体只能新增,不能 update(截面没存在实体里)。"; } nlohmann::json private_input_schema() const override { return nlohmann::json::parse(R"({ "type": "object", "properties": { "host": { "type": "string", "description": "母体 handle(墙/板/梁/柱/堰/异形构筑物)" }, "shape": { "type": "string", "enum": [ "box", "cylinder", "cone", "pyramid", "sphere", "drum", "wedge", "loft" ], "description": "loft 放样体(用 sections);box 立方体 length,width,height;cylinder 圆柱 radius,height;cone 圆台 radius_bottom,radius_top,height;pyramid 四方台 top_length,top_width,bottom_length,bottom_width,height,x_offset;sphere 球体 radius;drum 球台 radius,len1,len2,start_angle,end_angle(度);wedge 楔形体 a,b,thickness" }, "position": { "type": "array", "items": { "type": "number" }, "description": "插入点 [x,y,z],缺省母体中心" }, "direction": { "type": "array", "items": { "type": "number" }, "description": "造型竖向 [x,y,z],缺省 [0,0,1]" }, "mode": { "type": "string", "enum": [ "unite", "subtract" ], "description": "并/差,缺省 unite" }, "name": { "type": "string", "description": "部件名,缺省用造型中文名" }, "color": { "type": "integer", "description": "ACI 颜色号,256=ByLayer(缺省)" }, "length": { "type": "number" }, "width": { "type": "number" }, "height": { "type": "number" }, "radius": { "type": "number" }, "radius_bottom": { "type": "number" }, "radius_top": { "type": "number" }, "top_length": { "type": "number" }, "top_width": { "type": "number" }, "bottom_length": { "type": "number" }, "bottom_width": { "type": "number" }, "x_offset": { "type": "number" }, "len1": { "type": "number" }, "len2": { "type": "number" }, "start_angle": { "type": "number" }, "end_angle": { "type": "number" }, "a": { "type": "number" }, "b": { "type": "number" }, "thickness": { "type": "number" }, "sections": { "type": "array", "description": "loft 的各级截面(至少 2 级,自下而上)", "items": { "type": "object", "properties": { "diameter": { "type": "number", "description": "圆形截面直径" }, "points": { "type": "array", "items": { "type": "array", "items": { "type": "number" } }, "description": "多边形截面点串 [[x,y],...](自动闭合,坐标相对 position)" }, "elevation": { "type": "number", "description": "该级沿 direction 的标高,缺省 0" } } } } }, "required": [ "host", "shape" ] })"); } nlohmann::json Execute(const nlohmann::json& args) override { if (workingDb() == NULL) return errJson(kErrNoDocument, L"没有活动图纸"); DocLock lock; if (!lock.isLocked()) return errJson(kErrInternal, L"拿不到图纸写锁(有别的命令在跑?)"); std::wstring err; AcDbEntity* pOut = NULL; AecDbBDPipeBase* pHost = OpenHost(args, pOut, err); if (pHost == NULL) return errJson(kErrWrongType, err); const ShapeSpec* sp = FindShape(wstrOr(args, "shape", L"")); if (sp == NULL) { pHost->close(); return errJson(kErrBadParam, L"shape 只支持 box / cylinder / cone / pyramid / sphere / drum / wedge"); } const bool bLoft = (sp->ePrim == AcDbX3dSolid::kLoft); double p[6]; InitParams(p); bool bMissing = false; for (int i = 0; !bLoft && i < sp->nParams; ++i) for (int i = 0; !bLoft && i < sp->nParams; ++i) { p[i] = numOr(args, sp->aszParam[i], 0.0); if (sp->aszParam[i] == std::string("x_offset") || sp->aszParam[i] == std::string("len1") || sp->aszParam[i] == std::string("start_angle")) continue; // 这几个允许 0 if (sp->ePrim == AcDbX3dSolid::kDrum && (sp->aszParam[i] == std::string("start_angle") || sp->aszParam[i] == std::string("end_angle"))) { p[i] = CoreTools::A2R(p[i]); // 度 -> 弧度,与命令一致 continue; } if (p[i] <= 0.0) bMissing = true; } if (bMissing) { pHost->close(); return errJson(kErrBadParam, std::wstring(L"造型 ") + sp->pszName + L" 的尺寸参数没给全或 <= 0"); } // 插入点:没给就用母体位置/包围盒中心 AcGePoint3d ptIns(0.0, 0.0, 0.0); if (!readPoint(args, "position", ptIns)) { ptIns = pHost->GetPosition(); AcGePoint3d ptMin, ptMax; if (entityBBox(pHost, ptMin, ptMax)) ptIns = AcGePoint3d(0.5 * (ptMin.x + ptMax.x), 0.5 * (ptMin.y + ptMax.y), 0.5 * (ptMin.z + ptMax.z)); } const AcGeVector3d vtZ = DirOf(args); AcGeVector3d vtX, vtY; AxesOf(vtZ, vtX, vtY); AcDbX3dSolid* pSolid = bLoft ? BuildLoftSolid(args, ptIns, vtZ, err) : BuildSolid(*sp, p, ptIns, vtZ, err); if (pSolid == NULL) { pHost->close(); return errJson(kErrBadParam, err); } const bool bSub = (wstrOr(args, "mode", L"unite") == L"subtract"); std::wstring sName = hasKey(args, "name") ? wstrOr(args, "name", L"") : std::wstring(); if (sName.empty()) sName = sp->pszName; AecDbX3dPart* pPart = MakePart(*sp, pSolid, sName, intOr(args, "color", 256), ptIns, vtX, vtY, vtZ); const CString sGuid = pPart->GetGUID(); pHost->AddX3dPart(pPart, bSub ? AcDb::kBoolSubtract : AcDb::kBoolUnite); pHost->ReCreateDraw3d(); // 3D 缓存要重建,命令里是 regen touchEntity(pHost); pHost->close(); nlohmann::json body; body["ok"] = true; body["part_guid"] = acharToUtf8(sGuid); body["shape"] = acharToUtf8(sp->pszKey); body["type"] = acharToUtf8(sp->pszName); body["name"] = acharToUtf8((LPCTSTR)CString(sName.c_str())); body["mode"] = bSub ? "subtract" : "unite"; body["params"] = ParamsToJson(*sp, p); body["position"] = { ptIns.x, ptIns.y, ptIns.z }; return okJson(body); } }; EXPORT_MCP_TOOL(ToolAddShape, AddAecShapeTool) // --------------------------------------------------------------------------- // list_aec_shapes —— 列出实体上已有的造型 // --------------------------------------------------------------------------- class ToolListShapes : public mcp_Tool { public: std::string private_name() const override { return "list_aec_shapes"; } std::string private_description() const override { return "列出一个实体上的所有造型部件(PL_DTYG 加的几何体):guid、名称、造型种类、并/差、" "插入点、方向与尺寸参数,供 update_aec_shape / delete_aec_shape 用。"; } nlohmann::json private_input_schema() const override { return nlohmann::json::parse(R"({ "type": "object", "properties": { "host": { "type": "string", "description": "实体 handle" } }, "required": [ "host" ] })"); } nlohmann::json Execute(const nlohmann::json& args) override { if (workingDb() == NULL) return errJson(kErrNoDocument, L"没有活动图纸"); DocLock lock; if (!lock.isLocked()) return errJson(kErrInternal, L"拿不到图纸写锁(有别的命令在跑?)"); std::wstring err; AcDbEntity* pOut = NULL; AecDbBDPipeBase* pHost = OpenHost(args, pOut, err); if (pHost == NULL) return errJson(kErrWrongType, err); nlohmann::json arItems = nlohmann::json::array(); const int nCount = pHost->GetX3dPartCount(); for (int i = 0; i < nCount; ++i) { nlohmann::json o = PartToJson(pHost, i); if (!o.is_null()) arItems.push_back(o); } pHost->close(); nlohmann::json body; body["ok"] = true; body["count"] = (int)arItems.size(); body["shapes"] = arItems; return okJson(body); } }; EXPORT_MCP_TOOL(ToolListShapes, ListAecShapesTool) // --------------------------------------------------------------------------- // update_aec_shape —— 改造型(尺寸/位置/方向/并差/名字/颜色) // --------------------------------------------------------------------------- class ToolUpdateShape : public mcp_Tool { public: std::string private_name() const override { return "update_aec_shape"; } std::string private_description() const override { return "修改实体上的某个造型:给新的尺寸/插入点/方向/并差模式/名字/颜色,没给的沿用原来的。" "实现是整块部件重建(等价于命令里的\"重建 solid\"),位置和并差模式也能改。" "造型种类(shape)也能换,但必须还是基本体那 7 种。"; } nlohmann::json private_input_schema() const override { return nlohmann::json::parse(R"({ "type": "object", "properties": { "host": { "type": "string", "description": "实体 handle" }, "part_guid": { "type": "string", "description": "造型部件的 guid(list_aec_shapes 里拿)" }, "shape": { "type": "string", "enum": [ "box", "cylinder", "cone", "pyramid", "sphere", "drum", "wedge" ], "description": "换造型种类(可选)" }, "position": { "type": "array", "items": { "type": "number" }, "description": "新插入点" }, "direction": { "type": "array", "items": { "type": "number" }, "description": "新方向" }, "mode": { "type": "string", "enum": [ "unite", "subtract" ] }, "name": { "type": "string" }, "color": { "type": "integer" }, "length": { "type": "number" }, "width": { "type": "number" }, "height": { "type": "number" }, "radius": { "type": "number" }, "radius_bottom": { "type": "number" }, "radius_top": { "type": "number" }, "top_length": { "type": "number" }, "top_width": { "type": "number" }, "bottom_length": { "type": "number" }, "bottom_width": { "type": "number" }, "x_offset": { "type": "number" }, "len1": { "type": "number" }, "len2": { "type": "number" }, "start_angle": { "type": "number" }, "end_angle": { "type": "number" }, "a": { "type": "number" }, "b": { "type": "number" }, "thickness": { "type": "number" } }, "required": [ "host", "part_guid" ] })"); } nlohmann::json Execute(const nlohmann::json& args) override { if (workingDb() == NULL) return errJson(kErrNoDocument, L"没有活动图纸"); DocLock lock; if (!lock.isLocked()) return errJson(kErrInternal, L"拿不到图纸写锁(有别的命令在跑?)"); std::wstring err; AcDbEntity* pOut = NULL; AecDbBDPipeBase* pHost = OpenHost(args, pOut, err); if (pHost == NULL) return errJson(kErrWrongType, err); const std::string sGuid = args["part_guid"].get(); AecDbX3dPart* pOld = pHost->GetX3dPart(utf8ToWstr(sGuid).c_str()); if (pOld == NULL) { pHost->close(); return errJson(kErrNotFound, L"母体上找不到这个造型部件:" + utf8ToWstr(sGuid)); } if (pOld->GetX3dSolidCount() != 1) { pHost->close(); return errJson(kErrBadParam, L"这个部件不是单 solid 造型(solid 数 != 1),不支持修改"); } // 先把旧部件的信息全读出来(后面重建会把它删掉) CString sTypeOld; pOld->GetSpecialtyData()->GetAt(_T("DTYG_TYPE"), sTypeOld); const ShapeSpec* spOld = FindShapeByName(sTypeOld); CString sName; pOld->GetName(sName); const std::wstring sNameOld = acharToUtf8((LPCTSTR)sName).empty() ? std::wstring() : utf8ToWstr(acharToUtf8((LPCTSTR)sName)); int nOper = pOld->GetBoolOperFor(); AcGeVector3d vtXA(0.0, 0.0, 0.0), vtYA(0.0, 0.0, 0.0), vtZA(0.0, 0.0, 1.0); pOld->GetAxis(0, vtXA); pOld->GetAxis(1, vtYA); pOld->GetAxis(2, vtZA); AcDbX3dSolid* pOldSolid = pOld->GetX3dSolid(0); if (pOldSolid == NULL) { pHost->close(); return errJson(kErrInternal, L"旧造型取不到 solid"); } const AcGePoint3d ptCenter = pOldSolid->GetOrgPoint(); AcGeVector3d vtZ = pOldSolid->GetvtZ(); AcGePoint3d ptIns = ptCenter; if (spOld != NULL && spOld->ePrim == AcDbX3dSolid::kLoft) { pHost->close(); return errJson(kErrBadParam, L"放样体不支持改造(截面数据没存在实体里),请 delete_aec_shape 后重建"); } // 旧尺寸:按旧种类的参数顺序读出来 double p[6]; InitParams(p); if (spOld != NULL) { AcArray ar; if (pOldSolid->GetSolidData(spOld->ePrim, ar)) { for (int i = 0; i < spOld->nParams && i < ar.length(); ++i) p[i] = ar[i]; } } if (FindShapeByName(sTypeOld) != NULL) ptIns = InsertPointOf(pOld, *FindShapeByName(sTypeOld), p, ptCenter, vtZ); // 新种类:没给就沿用旧的 const ShapeSpec* spNew = spOld; if (hasKey(args, "shape") && wstrOr(args, "shape", L"") == L"loft") { pHost->close(); return errJson(kErrBadParam, L"造型种类不能改成放样体(改造型按尺寸重建,放样体没有尺寸),请新增一个"); } if (hasKey(args, "shape")) { spNew = FindShape(wstrOr(args, "shape", L"")); if (spNew == NULL) { pHost->close(); return errJson(kErrBadParam, L"shape 只支持 box / cylinder / cone / pyramid / sphere / drum / wedge"); } if (spNew != spOld) { // 换了种类,旧尺寸不能沿用,必须全给 double pNewIn[6]; InitParams(pNewIn); bool bMissing = false; for (int i = 0; i < spNew->nParams; ++i) { if (!hasKey(args, spNew->aszParam[i])) { bMissing = true; break; } } if (bMissing) { pHost->close(); return errJson(kErrBadParam, std::wstring(L"换成 ") + spNew->pszName + L" 需要把它的尺寸参数一起给全"); } InitParams(p); } } if (spNew == NULL) { pHost->close(); return errJson(kErrBadParam, L"这个部件的造型种类不认识(DTYG_TYPE 不是基本体),没法按参数重建"); } // 覆盖尺寸 for (int i = 0; i < spNew->nParams; ++i) { if (!hasKey(args, spNew->aszParam[i])) continue; double dVal = numOr(args, spNew->aszParam[i], p[i]); if (spNew->ePrim == AcDbX3dSolid::kDrum && (std::string(spNew->aszParam[i]) == "start_angle" || std::string(spNew->aszParam[i]) == "end_angle")) dVal = CoreTools::A2R(dVal); p[i] = dVal; } readPoint(args, "position", ptIns); if (hasKey(args, "direction")) vtZ = DirOf(args); if (wstrOr(args, "mode", L"").compare(0, 1, L"s") == 0) nOper = AcDb::kBoolSubtract; else if (hasKey(args, "mode")) nOper = AcDb::kBoolUnite; std::wstring sNameNew = hasKey(args, "name") ? wstrOr(args, "name", L"") : sNameOld; if (sNameNew.empty()) sNameNew = spNew->pszName; // 旧颜色:从 BIM 数据里读(没有就 ByLayer) int nColor = intOr(args, "color", 256); if (!hasKey(args, "color")) { SH_BIM_BLOCK bims; pOld->getBimDatas(bims); for (size_t i = 0; i < bims.size(); ++i) { if (bims[i].first != _T("颜色")) continue; const CString sClr = bims[i].second; if (sClr == _T("ByLayer")) nColor = 256; else if (sClr == _T("ByBlock")) nColor = 0; else nColor = _ttoi(sClr); } } AcDbX3dSolid* pSolid = BuildSolid(*spNew, p, ptIns, vtZ, err); if (pSolid == NULL) { pHost->close(); return errJson(kErrBadParam, err); } AcGeVector3d vtX, vtY; AxesOf(vtZ, vtX, vtY); // 重建:删旧部件 -> 加新部件(并/差模式随之改变) pHost->RemoveX3dPart(utf8ToWstr(sGuid).c_str()); AecDbX3dPart* pNewPart = MakePart(*spNew, pSolid, sNameNew, nColor, ptIns, vtX, vtY, vtZ); const CString sGuidNew = pNewPart->GetGUID(); pHost->AddX3dPart(pNewPart, nOper); pHost->ReCreateDraw3d(); // 3D 缓存要重建,命令里是 regen touchEntity(pHost); pHost->close(); nlohmann::json body; body["ok"] = true; body["part_guid"] = acharToUtf8(sGuidNew); body["old_guid"] = sGuid; body["shape"] = acharToUtf8(spNew->pszKey); body["type"] = acharToUtf8(spNew->pszName); body["name"] = acharToUtf8(CString(sNameNew.c_str())); body["mode"] = ModeToStr(nOper); body["params"] = ParamsToJson(*spNew, p); body["position"] = { ptIns.x, ptIns.y, ptIns.z }; return okJson(body); } }; EXPORT_MCP_TOOL(ToolUpdateShape, UpdateAecShapeTool) // --------------------------------------------------------------------------- // delete_aec_shape —— 删造型 // --------------------------------------------------------------------------- class ToolDeleteShape : public mcp_Tool { public: std::string private_name() const override { return "delete_aec_shape"; } std::string private_description() const override { return "删掉实体上的一个造型部件(按 guid)。不传 part_guid 就删掉该实体上所有造型。"; } nlohmann::json private_input_schema() const override { return nlohmann::json::parse(R"({ "type": "object", "properties": { "host": { "type": "string", "description": "实体 handle" }, "part_guid": { "type": "string", "description": "造型部件 guid;不给则删全部造型" } }, "required": [ "host" ] })"); } nlohmann::json Execute(const nlohmann::json& args) override { if (workingDb() == NULL) return errJson(kErrNoDocument, L"没有活动图纸"); DocLock lock; if (!lock.isLocked()) return errJson(kErrInternal, L"拿不到图纸写锁(有别的命令在跑?)"); std::wstring err; AcDbEntity* pOut = NULL; AecDbBDPipeBase* pHost = OpenHost(args, pOut, err); if (pHost == NULL) return errJson(kErrWrongType, err); nlohmann::json body; body["ok"] = true; if (hasKey(args, "part_guid")) { const std::wstring sGuid = utf8ToWstr(args["part_guid"].get()); if (!pHost->RemoveX3dPart(sGuid.c_str())) { pHost->close(); return errJson(kErrNotFound, L"母体上找不到这个造型部件:" + sGuid); } body["removed"] = 1; } else { const int nCount = pHost->GetX3dPartCount(); pHost->RemoveAllX3dPart(); body["removed"] = nCount; } pHost->ReCreateDraw3d(); // 3D 缓存要重建,命令里是 regen touchEntity(pHost); pHost->close(); return okJson(body); } }; EXPORT_MCP_TOOL(ToolDeleteShape, DeleteAecShapeTool)