diff --git a/SourceCode/Code2026/cad_mcp_aec/aec_mcp_shape_tools.cpp b/SourceCode/Code2026/cad_mcp_aec/aec_mcp_shape_tools.cpp index 48d301a..ec2a98c 100644 --- a/SourceCode/Code2026/cad_mcp_aec/aec_mcp_shape_tools.cpp +++ b/SourceCode/Code2026/cad_mcp_aec/aec_mcp_shape_tools.cpp @@ -46,12 +46,50 @@ namespace return s; } + // 方向: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(); + } + // ---------------------------------------------------------------- 造型表 + // 造型的建体方式:基本体直接 Create,其余各自一套(见 BuildShapeSolid) + enum ShapeKind + { + kPrim = 0, // AcDbX3dSolid::Create(prim, ...) + kLoft, + kExtrude, + kRevolve, + kSweep, + kCut, + kWeir, + }; + struct ShapeSpec { const wchar_t* pszKey; // 工具参数 shape 的取值 const wchar_t* pszName; // DTYG_TYPE / 部件名(与命令 getName() 一致) AcDbX3dSolid::Primitive ePrim; + int nKind; // ShapeKind int nParams; const char* aszParam[6]; // 参数名,顺序与 Create 一致 }; @@ -59,15 +97,20 @@ namespace // 参数顺序全部照抄命令里各 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, { } }, + { L"box", L"立方体", AcDbX3dSolid::kBox, kPrim, 3, { "length", "width", "height" } }, + { L"cylinder", L"圆柱", AcDbX3dSolid::kCylinder, kPrim, 2, { "radius", "height" } }, + { L"cone", L"圆台", AcDbX3dSolid::kCone, kPrim, 3, { "radius_bottom", "radius_top", "height" } }, + { L"pyramid", L"四方台", AcDbX3dSolid::kPyramid, kPrim, 6, { "top_length", "top_width", "bottom_length", "bottom_width", "height", "x_offset" } }, + { L"sphere", L"球体", AcDbX3dSolid::kSpere, kPrim, 1, { "radius" } }, + { L"drum", L"球台", AcDbX3dSolid::kDrum, kPrim, 5, { "radius", "len1", "len2", "start_angle", "end_angle" } }, + { L"wedge", L"楔形体", AcDbX3dSolid::kWedge, kPrim, 3, { "a", "b", "thickness" } }, + // 下面这几种没有固定尺寸参数,靠 profile / sections / axis / path 描述 + { L"loft", L"放样体", AcDbX3dSolid::kLoft, kLoft, 0, { } }, + { L"extrude", L"拉伸体", AcDbX3dSolid::kExtrude, kExtrude, 0, { } }, + { L"revolve", L"旋转体", AcDbX3dSolid::kRevolve, kRevolve, 0, { } }, + { L"sweep", L"扫掠体", AcDbX3dSolid::kSweep, kSweep, 0, { } }, + { L"cut", L"切割", AcDbX3dSolid::kSliceSolid, kCut, 0, { } }, + { L"weir", L"异形堰", AcDbX3dSolid::kRevolve2, kWeir, 0, { } }, }; const ShapeSpec* FindShape(const std::wstring& sKey) @@ -136,6 +179,399 @@ namespace return pSolid; } + // 把在 XY 平面上造好的 2D 曲线搬到"原点 ptOrg + 三轴 x/y/z"的坐标系上。 + // 之前直接给 AcDbPolyline 设 normal 是错的:normal 不是 Z 时 OCS 轴会转, + // 二维坐标就不再是你给的那两个数了(扫掠体就是这么失败的)。 + void PlaceOnFrame(AcDbEntity* pEnt, const AcGePoint3d& ptOrg, + const AcGeVector3d& vtX, const AcGeVector3d& vtY, const AcGeVector3d& vtZ) + { + if (pEnt == NULL) return; + AcGeMatrix3d mat; + mat.setToAlignCoordSys(AcGePoint3d::kOrigin, AcGeVector3d::kXAxis, AcGeVector3d::kYAxis, AcGeVector3d::kZAxis, + ptOrg, vtX, vtY, vtZ); + pEnt->transformBy(mat); + } + + // 三点定弧(命令用 KTCADUtilityEx::FormatArc,这里直接用 AcGeCircArc3d 推) + AcDbArc* ArcBy3Point(const AcGePoint3d& pt1, const AcGePoint3d& pt2, const AcGePoint3d& pt3) + { + AcGeCircArc3d geArc(pt1, pt2, pt3); + if (geArc.radius() < 1e-9) return NULL; + return new AcDbArc(geArc.center(), geArc.normal(), geArc.radius(), geArc.startAng(), geArc.endAng()); + } + + // ---------------------------------------------------------------- 轮廓 + // 2D 轮廓 -> 面域:{points:[[x,y],...]} 或 {diameter: d}, + // 落在"过 ptIns、法向 vtZ"的平面上,dElev 是沿法向的额外抬高。 + // shortcut: points 按 OCS 的 x/y 解释,法向不是 Z 时平面原点与预期可能差一点, + // 需要精确摆放就用 revolve/extrude 的 3D 写法或先画好轮廓再引用。 + AcDbRegion* BuildProfileRegion(const nlohmann::json& sec, const AcGePoint3d& ptIns, + const AcGeVector3d& vtZ, double dElev, std::wstring& err) + { + const AcGePoint3d ptSec = ptIns + vtZ * dElev; + AcGeVector3d vtX, vtY; + AxesOf(vtZ, vtX, vtY); + + AcDbRegion* pReg = NULL; + + if (sec.contains("diameter") && sec["diameter"].is_number()) + { + const double dD = sec["diameter"].get(); + if (dD <= 0.0) { err = L"轮廓 diameter 必须 > 0"; return NULL; } + AcDbCircle cir(AcGePoint3d::kOrigin, AcGeVector3d::kZAxis, 0.5 * dD); + PlaceOnFrame(&cir, ptSec, vtX, vtY, vtZ); + pReg = CoreTools::createRegion(&cir); + } + else if (sec.contains("points") && sec["points"].is_array() && sec["points"].size() >= 3) + { + AcDbPolyline pl((int)sec["points"].size()); + int nIdx = 0; + 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(nIdx++, AcGePoint2d(pt[0].get(), pt[1].get()), 0.0, 0.0, 0.0); + } + if (nIdx >= 3) + { + pl.setClosed(true); + PlaceOnFrame(&pl, ptSec, vtX, vtY, vtZ); + pReg = CoreTools::createRegion(&pl); + } + } + + if (pReg == NULL && err.empty()) + err = L"轮廓生成面域失败:需要 points(至少 3 点、能闭合)或 diameter"; + return pReg; + } + + // 一串 3D 点连成的闭合折线 -> 面域(任意平面都行,revolve 用这个) + AcDbRegion* BuildRegionFrom3dLoop(const AcGePoint3dArray& pts, std::wstring& err) + { + if (pts.length() < 3) { err = L"轮廓点少于 3 个"; return NULL; } + + AcDbVoidPtrArray lnPtrs; + for (int i = 0; i < pts.length(); ++i) + lnPtrs.append(new AcDbLine(pts[i], pts[(i + 1) % pts.length()])); + + AcDbRegion* pReg = CoreTools::createRegion(lnPtrs); + for (int i = 0; i < lnPtrs.length(); ++i) + delete (AcDbLine*)lnPtrs[i]; + + if (pReg == NULL && err.empty()) err = L"3D 轮廓闭合失败(点不共面?)"; + return pReg; + } + + // 面域质心(拉伸/切割的路径线从这里出发,与命令 getCenterOfReg 一致) + AcGePoint3d RegionCenter(AcDbRegion* pReg) + { + if (pReg == NULL) return AcGePoint3d(0.0, 0.0, 0.0); + AcDbExtents ext; + if (pReg->getGeomExtents(ext) != Acad::eOk) return AcGePoint3d(0.0, 0.0, 0.0); + return AcGePoint3d(0.5 * (ext.minPoint().x + ext.maxPoint().x), + 0.5 * (ext.minPoint().y + ext.maxPoint().y), + 0.5 * (ext.minPoint().z + ext.maxPoint().z)); + } + + // ---------------------------------------------------------------- 拉伸体 + // 命令 CreateExtrude:轮廓沿自身法向拉 dH,路径线从轮廓质心出发。 + AcDbX3dSolid* BuildExtrudeSolid(const nlohmann::json& a, const AcGePoint3d& ptIns, + const AcGeVector3d& vtZ, std::wstring& err) + { + if (!hasKey(a, "profile")) { err = L"拉伸体需要 profile{points|diameter}"; return NULL; } + const double dH = numOr(a, "height", 0.0); + if (fabs(dH) < 1e-9) { err = L"拉伸体需要 height(拉伸高度,可以为负)"; return NULL; } + + AcDbRegion* pReg = BuildProfileRegion(a["profile"], ptIns, vtZ, 0.0, err); + if (pReg == NULL) return NULL; + + const AcGePoint3d ptCen = RegionCenter(pReg); + AcDbX3dSolid* pSolid = new AcDbX3dSolid(); + const bool bOk = pSolid->CreateExtrude(pReg, AcDbLine(ptCen, ptCen + vtZ * dH)); + delete pReg; + if (!bOk) + { + delete pSolid; + err = L"CreateExtrude 失败"; + return NULL; + } + return pSolid; + } + + // ---------------------------------------------------------------- 旋转体 + // 命令 CreateRevolve:轮廓绕一条直线转 2π(CreateRevolve2)。 + // profile 用 [r,h] 两点式:r = 到轴的距离、h = 沿轴的坐标,轴由 axis_start/axis_end 给。 + AcDbX3dSolid* BuildRevolveSolid(const nlohmann::json& a, std::wstring& err) + { + AcGePoint3d ptAxisS(0.0, 0.0, 0.0), ptAxisE(0.0, 0.0, 1.0); + readPoint(a, "axis_start", ptAxisS); + readPoint(a, "axis_end", ptAxisE); + AcGeVector3d vtAxis = ptAxisE - ptAxisS; + if (vtAxis.length() < 1e-9) { err = L"旋转体需要 axis_start / axis_end(两点不能重合)"; return NULL; } + vtAxis.normalize(); + + // 径向:优先取 direction 里与轴垂直的分量,否则随便找一个垂直方向 + AcGeVector3d vtRad = DirOf(a); + vtRad -= vtAxis * vtRad.dotProduct(vtAxis); + if (vtRad.length() < 1e-9) + { + vtRad = AcGeVector3d::kXAxis - vtAxis * vtAxis.x; + if (vtRad.length() < 1e-9) + vtRad = AcGeVector3d::kYAxis - vtAxis * vtAxis.y; + } + vtRad.normalize(); + + if (!hasKey(a, "profile") || !a["profile"].is_array() || a["profile"].size() < 3) + { + err = L"旋转体需要 profile:[[r,h],...](r=到轴距离、h=沿轴坐标),至少 3 点"; + return NULL; + } + + AcGePoint3dArray pts; + for (size_t i = 0; i < a["profile"].size(); ++i) + { + const nlohmann::json& pr = a["profile"][i]; + if (!pr.is_array() || pr.size() < 2) continue; + pts.append(ptAxisS + vtRad * pr[0].get() + vtAxis * pr[1].get()); + } + + AcDbRegion* pReg = BuildRegionFrom3dLoop(pts, err); + if (pReg == NULL) return NULL; + + const double dAngle = numOr(a, "angle", 360.0); + AcDbX3dSolid* pSolid = new AcDbX3dSolid(); + const bool bOk = pSolid->CreateRevolve2(pReg, ptAxisS, vtAxis, + CoreTools::A2R(dAngle), 0.0); + delete pReg; + if (!bOk) + { + delete pSolid; + err = L"CreateRevolve2 失败(轮廓不能与轴相交/穿过)"; + return NULL; + } + return pSolid; + } + + // ---------------------------------------------------------------- 扫掠体 + // 命令 CreateSweep:轮廓 + 一条路径多段线。 + // path 用 [[x,y],...],与该轮廓同一个 OCS 平面。 + AcDbX3dSolid* BuildSweepSolid(const nlohmann::json& a, const AcGePoint3d& ptIns, + const AcGeVector3d& vtZ, std::wstring& err) + { + if (!hasKey(a, "profile")) { err = L"扫掠体需要 profile{points|diameter}"; return NULL; } + if (!hasKey(a, "path") || !a["path"].is_array() || a["path"].size() < 2) + { + err = L"扫掠体需要 path:[[x,y],...](至少 2 点,与轮廓同一 OCS 平面)"; + return NULL; + } + + AcDbRegion* pReg = BuildProfileRegion(a["profile"], ptIns, vtZ, 0.0, err); + if (pReg == NULL) return NULL; + + // 路径所在的平面必须"立"在轮廓平面上:坐标 (u,v),u 沿 vtX、v 沿 vtZ(v 就是扫掠前进方向) + AcGeVector3d vtX, vtY; + AxesOf(vtZ, vtX, vtY); + const AcGeVector3d vtThird = vtX.crossProduct(vtZ).normalize(); + + AcDbPolyline plPath; + int nIdx = 0; + for (size_t i = 0; i < a["path"].size(); ++i) + { + const nlohmann::json& pt = a["path"][i]; + if (!pt.is_array() || pt.size() < 2) continue; + plPath.addVertexAt(nIdx++, AcGePoint2d(pt[0].get(), pt[1].get()), 0.0, 0.0, 0.0); + } + PlaceOnFrame(&plPath, ptIns, vtX, vtZ, vtThird); + if (nIdx < 2) + { + delete pReg; + err = L"扫掠体 path 至少要 2 个点"; + return NULL; + } + + AcDbX3dSolid* pSolid = new AcDbX3dSolid(); + const bool bOk = pSolid->CreateSweep(pReg, &plPath); + delete pReg; + if (!bOk) + { + delete pSolid; + err = L"CreateSweep 失败(路径要穿过轮廓平面、且是平面曲线)"; + return NULL; + } + return pSolid; + } + + // ---------------------------------------------------------------- 切割 + // 命令 CreateCut:把轮廓沿 -Z 拉出 maxLen 的柱体当"刀"(永远是减料)。 + AcDbX3dSolid* BuildCutSolid(const nlohmann::json& a, const AcGePoint3d& ptIns, + const AcGeVector3d& vtZ, double dHostMax, std::wstring& err) + { + if (!hasKey(a, "profile")) { err = L"切割需要 profile{points|diameter}(切割轮廓)"; return NULL; } + double dDepth = numOr(a, "depth", 0.0); + if (dDepth <= 0.0) dDepth = dHostMax; // 缺省跟命令一样取母体最大尺寸 + if (dDepth <= 0.0) { err = L"切割需要 depth(母体尺寸也取不到)"; return NULL; } + + AcDbRegion* pReg = BuildProfileRegion(a["profile"], ptIns, vtZ, 0.0, err); + if (pReg == NULL) return NULL; + + const AcGePoint3d ptCen = RegionCenter(pReg); + AcDbX3dSolid* pSolid = new AcDbX3dSolid(); + const bool bOk = pSolid->CreateExtrude(pReg, AcDbLine(ptCen, ptCen - vtZ * dDepth)); + delete pReg; + if (!bOk) + { + delete pSolid; + err = L"切割体的 CreateExtrude 失败"; + return NULL; + } + return pSolid; + } + + // ------------------------------------------------ 异形堰(沿圆/弧排布的堰体) + // 移植自 CYiXingYanDlg::makeRegion4Arc / makeXSolid4Arc: + // 基准线上取 [dStart, dStart+弧长] 的一段,向圆心方向偏 dT 生成上下两个截面再放样。 + AcDbRegion* WeirRegionAt(double dStart, AcDbCurve* pCurve, double dL, double dT, + const AcGePoint3d& ptCen, double dR, std::wstring& err) + { + if (dR <= 1e-6 || dL <= 1e-6) { err = L"基准线半径/堰宽太小"; return NULL; } + + const double alpha = asin((std::min)(0.999, (dL * 0.5) / dR)); + const double l = dT / cos(alpha); + const double dEnd = dStart + dR * alpha * 2.0; + + AcGePoint3d ptStart1, ptMid1, ptEnd1, ptStart2, ptMid2, ptEnd2; + if (pCurve->getPointAtDist(dStart, ptStart1) != Acad::eOk || + pCurve->getPointAtDist((dEnd + dStart) * 0.5, ptMid1) != Acad::eOk || + pCurve->getPointAtDist(dEnd, ptEnd1) != Acad::eOk) + { + err = L"基准线取点失败(start_distance 超出基线长度?)"; + return NULL; + } + + AcGeVector3d vt = ptCen - ptMid1; + vt.normalize(); + ptMid2 = ptMid1 + vt * dT; + ptStart2 = ptStart1 + vt * l; + ptEnd2 = ptEnd1 + vt * l; + + // 命令里留 0.1 的缝,避免上下两条弧完全重合时面域失败 + const double dScale = 0.1; + ptStart1 -= vt * dScale; + ptMid1 -= vt * dScale; + ptEnd1 -= vt * dScale; + AcDbArc* pArc1 = ArcBy3Point(ptStart1, ptMid1, ptEnd1); + ptStart2 += vt * dScale; + ptMid2 += vt * dScale; + ptEnd2 += vt * dScale; + AcDbArc* pArc2 = ArcBy3Point(ptStart2, ptMid2, ptEnd2); + if (pArc1 == NULL || pArc2 == NULL) + { + delete pArc1; delete pArc2; + err = L"基准线三点定弧失败"; + return NULL; + } + + AcDbLine ln1(ptStart1, ptStart2); + AcDbLine ln2(ptEnd1, ptEnd2); + AcDbVoidPtrArray ptrs; + ptrs.append(pArc1); + ptrs.append(pArc2); + ptrs.append(&ln1); + ptrs.append(&ln2); + + AcDbRegion* pReg = CoreTools::createRegion(ptrs); + delete pArc1; + delete pArc2; + if (pReg == NULL) err = L"堰体截面(弧形面域)生成失败"; + return pReg; + } + + // 异形堰:基准线必须是圆或弧(命令也只能选 CIRCLE/ARC) + AcDbX3dSolid* BuildWeirSolid(const nlohmann::json& a, AcDbEntity* pBaseCurve, + double dStartDist, std::wstring& err) + { + AcDbCurve* pCurve = (pBaseCurve != NULL) ? AcDbCurve::cast(pBaseCurve) : NULL; + if (pCurve == NULL) { err = L"异形堰需要 base_curve:圆或圆弧的 handle"; return NULL; } + + AcGePoint3d ptCen(0.0, 0.0, 0.0); + double dR = 0.0; + if (AcDbCircle* pCir = AcDbCircle::cast(pBaseCurve)) + { + ptCen = pCir->center(); + dR = pCir->radius(); + } + else if (AcDbArc* pArc = AcDbArc::cast(pBaseCurve)) + { + ptCen = pArc->center(); + dR = pArc->radius(); + } + else + { + err = L"异形堰的 base_curve 只支持圆或圆弧(命令里也只能选 CIRCLE/ARC)"; + return NULL; + } + + const int nType = intOr(a, "weir_type", 0); // 0=三角(上宽下窄) 1=方形(上下同宽) + const double dL = numOr(a, "weir_width", 0.0); // m_dL 堰宽(沿基线) + const double dT = numOr(a, "weir_thickness", 0.0); // m_dT 堰厚(径向) + const double dH1 = numOr(a, "weir_height1", 0.0); // m_dH1 上段高 + const double dH2 = numOr(a, "weir_height2", 0.0); // m_dH2 下段高 + if (dL <= 0.0 || dT <= 0.0 || dH1 <= 0.0) + { + err = L"异形堰需要 weir_width / weir_thickness / weir_height1,且都 > 0"; + return NULL; + } + + AcDbRegion* pReg1 = WeirRegionAt(dStartDist, pCurve, dL, dT, ptCen, dR, err); + if (pReg1 == NULL) return NULL; + AcGeMatrix3d mat; + if (nType == 0 || nType == 1) + { + mat.setToTranslation(-AcGeVector3d::kZAxis * dH1); + pReg1->transformBy(mat); + } + + AcDbRegion* pReg2 = NULL; + if (nType == 1) + { + pReg2 = (AcDbRegion*)pReg1->clone(); + mat.setToTranslation(-AcGeVector3d::kZAxis * (dH1 + dH2)); + pReg2->transformBy(mat); + } + else + { + // 三角堰:下截面在更远处,且只有 10 宽(与命令一致) + const double a1 = asin((std::min)(0.999, (dL * 0.5) / dR)); + const double dArcLen1 = a1 * dR; + const double dL2 = 10.0; + const double a2 = asin((std::min)(0.999, dL2 * 0.5 / dR)); + const double dArcLen2 = a2 * dR; + pReg2 = WeirRegionAt(dStartDist + (dArcLen1 - dArcLen2), pCurve, dL2, dT, ptCen, dR, err); + if (pReg2 == NULL) + { + delete pReg1; + return NULL; + } + mat.setToTranslation(-AcGeVector3d::kZAxis * (dH1 + dH2)); + pReg2->transformBy(mat); + } + + AcArray regs; + regs.append(pReg1); + regs.append(pReg2); + + AcDbX3dSolid* pSolid = new AcDbX3dSolid(); + if (!pSolid->CreateLoft(regs)) + { + delete pSolid; + pSolid = NULL; + err = L"堰体 CreateLoft 失败(两截面法向不一致?)"; + } + delete pReg1; + delete pReg2; + return pSolid; + } + // 放样体(loft):命令 CreateLoft 是"一级一级选闭合轮廓 + 给层高", // 工具里换成一次给 sections:[ {diameter, elevation} 或 {points:[[x,y],...], elevation} ] // 每级沿 direction 抬 elevation,轮廓用 OCS(x/y 就是给定坐标)。 @@ -156,28 +592,9 @@ namespace 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); - } + AcGePoint3d ptSec2 = ptSec; + AcDbRegion* pReg = BuildProfileRegion(sec, ptIns, vtZ, dEl, err); + (void)ptSec2; if (pReg == NULL) { @@ -210,6 +627,41 @@ namespace return pSolid; } + // ---------------------------------------------------------------- 总分派 + AcDbX3dSolid* BuildShapeSolid(const ShapeSpec& sp, const nlohmann::json& a, AecDbBDPipeBase* pHost, + double dHostMax, const AcGePoint3d& ptIns, const AcGeVector3d& vtZ, std::wstring& err) + { + switch (sp.nKind) + { + case kLoft: return BuildLoftSolid(a, ptIns, vtZ, err); + case kExtrude: return BuildExtrudeSolid(a, ptIns, vtZ, err); + case kRevolve: return BuildRevolveSolid(a, err); + case kSweep: return BuildSweepSolid(a, ptIns, vtZ, err); + case kCut: return BuildCutSolid(a, ptIns, vtZ, dHostMax, err); + case kWeir: + { + // 基准线:给 handle(圆/圆弧),命令里是拾取 + if (!hasKey(a, "base_curve")) + { + err = L"异形堰需要 base_curve:圆或圆弧实体的 handle"; + return NULL; + } + const std::string sId = a["base_curve"].get(); + AcDbEntity* pBase = openByHandle(sId, AcDb::kForRead); + if (pBase == NULL) + { + err = L"按 handle 打不开基准线:" + utf8ToWstr(sId); + return NULL; + } + AcDbX3dSolid* pSolid = BuildWeirSolid(a, pBase, numOr(a, "start_distance", 0.0), err); + pBase->close(); + return pSolid; + } + default: break; + } + return NULL; + } + // 部件:DTYG_TYPE + BIM 数据(名称/颜色/统计类型)+ 单个 solid,与命令一致 AecDbX3dPart* MakePart(const ShapeSpec& sp, AcDbX3dSolid* pSolid, const std::wstring& sName, int nColor, const AcGePoint3d& ptIns, @@ -268,31 +720,6 @@ namespace 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) @@ -483,7 +910,8 @@ public: "properties": { "host": { "type": "string", "description": "母体 handle(墙/板/梁/柱/堰/异形构筑物)" }, "shape": { "type": "string", - "enum": [ "box", "cylinder", "cone", "pyramid", "sphere", "drum", "wedge", "loft" ], + "enum": [ "box", "cylinder", "cone", "pyramid", "sphere", "drum", "wedge", + "loft", "extrude", "revolve", "sweep", "cut", "weir" ], "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]" }, @@ -498,6 +926,23 @@ public: "len1": { "type": "number" }, "len2": { "type": "number" }, "start_angle": { "type": "number" }, "end_angle": { "type": "number" }, "a": { "type": "number" }, "b": { "type": "number" }, "thickness": { "type": "number" }, + "profile": { "description": "extrude/sweep/cut 的轮廓:{points:[[x,y],...]} 或 {diameter},落在过 position、法向 direction 的平面上;revolve 用 [[r,h],...](r=到轴距离、h=沿轴坐标)", + "type": [ "object", "array" ], + "properties": { + "diameter": { "type": "number" }, + "points": { "type": "array", "items": { "type": "array", "items": { "type": "number" } } } } }, + "path": { "type": "array", "items": { "type": "array", "items": { "type": "number" } }, + "description": "sweep 的路径 [[x,y],...](与轮廓同一 OCS 平面,至少 2 点)" }, + "axis_start": { "type": "array", "items": { "type": "number" }, "description": "revolve 的轴起点" }, + "axis_end": { "type": "array", "items": { "type": "number" }, "description": "revolve 的轴终点" }, + "angle": { "type": "number", "description": "revolve 的旋转角(度),缺省 360" }, + "base_curve": { "type": "string", "description": "weir 的基准线 handle(圆或圆弧实体)" }, + "start_distance": { "type": "number", "description": "weir 起始位置(沿基准线的弧长),缺省 0" }, + "weir_type": { "type": "integer", "description": "weir 类型:0=三角堰(上宽下窄)、1=方形堰" }, + "weir_width": { "type": "number", "description": "weir 堰宽(沿基线)" }, + "weir_thickness": { "type": "number", "description": "weir 堰厚(径向)" }, + "weir_height1": { "type": "number", "description": "weir 上段高" }, + "weir_height2": { "type": "number", "description": "weir 下段高" }, "sections": { "type": "array", "description": "loft 的各级截面(至少 2 级,自下而上)", "items": { "type": "object", "properties": { @@ -527,16 +972,16 @@ public: if (sp == NULL) { pHost->close(); - return errJson(kErrBadParam, L"shape 只支持 box / cylinder / cone / pyramid / sphere / drum / wedge"); + return errJson(kErrBadParam, L"shape 只支持 box / cylinder / cone / pyramid / sphere / drum / wedge / " + L"loft / extrude / revolve / sweep / cut / weir"); } - const bool bLoft = (sp->ePrim == AcDbX3dSolid::kLoft); + const bool bSpecial = (sp->nKind != kPrim); 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) + for (int i = 0; !bSpecial && i < sp->nParams; ++i) { p[i] = numOr(args, sp->aszParam[i], 0.0); if (sp->aszParam[i] == std::string("x_offset") || @@ -571,7 +1016,16 @@ public: AcGeVector3d vtX, vtY; AxesOf(vtZ, vtX, vtY); - AcDbX3dSolid* pSolid = bLoft ? BuildLoftSolid(args, ptIns, vtZ, err) + // 切割缺省刀长取母体最大尺寸(命令 getMaxLength) + double dHostMax = 0.0; + { + AcGePoint3d ptMin, ptMax; + if (entityBBox(pHost, ptMin, ptMax)) + dHostMax = (std::max)((std::max)(ptMax.x - ptMin.x, ptMax.y - ptMin.y), ptMax.z - ptMin.z); + } + + AcDbX3dSolid* pSolid = bSpecial + ? BuildShapeSolid(*sp, args, pHost, dHostMax, ptIns, vtZ, err) : BuildSolid(*sp, p, ptIns, vtZ, err); if (pSolid == NULL) { @@ -579,7 +1033,8 @@ public: return errJson(kErrBadParam, err); } - const bool bSub = (wstrOr(args, "mode", L"unite") == L"subtract"); + // 切割在命令里永远是减料 + const bool bSub = (sp->nKind == kCut) || (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; diff --git a/SourceCode/Code2026/cad_mcp_aec/mcp_config.json b/SourceCode/Code2026/cad_mcp_aec/mcp_config.json index 2e01545..eda9492 100644 --- a/SourceCode/Code2026/cad_mcp_aec/mcp_config.json +++ b/SourceCode/Code2026/cad_mcp_aec/mcp_config.json @@ -2588,7 +2588,7 @@ }, { "name": "add_aec_shape", - "description": "向墙/板/梁/柱/堰/异形构筑物(AecDbBDPipeBase 派生)加一个几何体(菜单 PL_DTYG 造型设计):创建 AcDbX3dSolid + AecDbX3dPart,按 mode 与母体做并/差运算。尺寸用图纸单位,position 是插入点(立方体/圆柱/圆台/球台为底面中心,球体为球底,四方台/楔形体以该点为原点)。支持 7 种基本体 + 放样体(shape=loft,sections 给多级截面)。返回 part_guid,配套 list_aec_shapes / update_aec_shape / delete_aec_shape(放样体只能新增不能改)。", + "description": "向墙/板/梁/柱/堰/异形构筑物(AecDbBDPipeBase 派生)加一个几何体(菜单 PL_DTYG 造型设计):创建 AcDbX3dSolid + AecDbX3dPart,按 mode 与母体做并/差运算。尺寸用图纸单位,position 是插入点(基本体:立方体/圆柱/圆台/球台为底面中心,球体为球底,四方台/楔形体以该点为原点)。覆盖 PL_DTYG 对话框上全部几何体:7 种基本体 + 放样体(loft) + 拉伸体(extrude) + 旋转体(revolve) + 扫掠体(sweep) + 切割(cut) + 异形堰(weir)。返回 part_guid;配套 list_aec_shapes / update_aec_shape / delete_aec_shape(只有基本体能 update,曲线类要删了重建)。", "inputSchema": { "type": "object", "properties": { @@ -2606,9 +2606,14 @@ "sphere", "drum", "wedge", - "loft" + "loft", + "extrude", + "revolve", + "sweep", + "cut", + "weir" ], - "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" + "description": "基本体: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。曲线类:loft 放样体 sections;extrude 拉伸体 profile+height;revolve 旋转体 profile([r,h])+axis_start/axis_end+angle;sweep 扫掠体 profile+path;cut 切割 profile(+depth,永远减料);weir 异形堰 base_curve+weir_* 参数" }, "position": { "type": "array", @@ -2738,6 +2743,83 @@ } } } + }, + "profile": { + "type": [ + "object", + "array" + ], + "description": "extrude/sweep/cut 的轮廓:{points:[[x,y],...]} 或 {diameter},落在过 position、法向 direction 的平面上;revolve 用 [[r,h],...](r=到轴距离、h=沿轴坐标)", + "properties": { + "diameter": { + "type": "number" + }, + "points": { + "type": "array", + "items": { + "type": "array", + "items": { + "type": "number" + } + } + } + } + }, + "path": { + "type": "array", + "items": { + "type": "array", + "items": { + "type": "number" + } + }, + "description": "sweep 的路径 [[x,y],...](与轮廓同一 OCS 平面)" + }, + "axis_start": { + "type": "array", + "items": { + "type": "number" + }, + "description": "revolve 的轴起点" + }, + "axis_end": { + "type": "array", + "items": { + "type": "number" + }, + "description": "revolve 的轴终点" + }, + "angle": { + "type": "number", + "description": "revolve 的旋转角(度),缺省 360" + }, + "base_curve": { + "type": "string", + "description": "weir 的基准线 handle(圆或圆弧实体)" + }, + "start_distance": { + "type": "number", + "description": "weir 起始弧长,缺省 0" + }, + "weir_type": { + "type": "integer", + "description": "weir 类型:0=三角堰、1=方形堰" + }, + "weir_width": { + "type": "number", + "description": "weir 堰宽(沿基线)" + }, + "weir_thickness": { + "type": "number", + "description": "weir 堰厚(径向)" + }, + "weir_height1": { + "type": "number", + "description": "weir 上段高" + }, + "weir_height2": { + "type": "number", + "description": "weir 下段高" } }, "required": [