OpenCASCADE 7.7.0 完全教程(二十):布尔运算实战
OpenCASCADE 7.7.0 完全教程(二十):布尔运算实战
> 版本: OCCT 7.7.0
> 难度: 进阶
> 字数: 约 2500 字
> 案例: 组合零件设计
—
布尔运算概述
布尔运算是 CAD 建模中最核心的操作之一,用于将多个几何体组合成复杂形状。
三种基本运算
| 说明 | API 类 | 应用场景 |
| 交集(共同部分) | BRepAlgoAPI_Common | 提取重叠区域 |
—
基础示例:立方体与球体
创建基础几何体
#include
#include
#include
#include
#include
#include
int main() {
// 创建立方体 50x50x50
TopoDS_Solid box = BRepPrimAPI_MakeBox(50, 50, 50);
// 创建球体,半径 30,中心在 (25, 25, 25)
gp_Pnt center(25, 25, 25);
TopoDS_Solid sphere = BRepPrimAPI_MakeSphere(center, 30);
std::cout << "基础几何体创建完成" << std::endl;
return 0;
}
并集运算(Fuse)
// 合并立方体和球体
BRepAlgoAPI_Fuse fuseOp(box, sphere);
if (!fuseOp.IsDone()) {
std::cerr << "布尔并集运算失败!" << std::endl;
return 1;
}
TopoDS_Shape fusedShape = fuseOp.Shape();
// 验证结果
if (fusedShape.IsNull()) {
std::cerr << "结果为空!" << std::endl;
}
差集运算(Cut)
// 从立方体中切除球体
BRepAlgoAPI_Cut cutOp(box, sphere);
if (!cutOp.IsDone()) {
std::cerr << "布尔差集运算失败!" << std::endl;
return 1;
}
TopoDS_Shape cutShape = cutOp.Shape();
交集运算(Common)
// 取立方体和球体的共同部分
BRepAlgoAPI_Common commonOp(box, sphere);
if (!commonOp.IsDone()) {
std::cerr << "布尔交集运算失败!" << std::endl;
return 1;
}
TopoDS_Shape commonShape = commonOp.Shape();
---
实战案例:带孔法兰盘
案例描述
创建一个法兰盘零件:
- 主体:圆柱体(直径 100mm,厚度 10mm)
- 中心孔:直径 30mm
- 螺栓孔:4 个直径 8mm 的孔,均匀分布
完整代码
#include
#include
#include
#include
#include
int main() {
// 1. 创建法兰盘主体
gp_Pnt origin(0, 0, 0);
gp_Ax2 axis(origin, gp_Dir(0, 0, 1));
TopoDS_Solid disk = BRepPrimAPI_MakeCylinder(axis, 50, 10);
// 2. 创建中心孔刀具
TopoDS_Solid centerHole = BRepPrimAPI_MakeCylinder(axis, 15, 10);
// 3. 切除中心孔
BRepAlgoAPI_Cut cut1(disk, centerHole);
TopoDS_Shape withCenterHole = cut1.Shape();
// 4. 创建螺栓孔
double boltCircleRadius = 35; // 螺栓孔分布圆半径
TopTools_ListOfShape boltHoles;
for (int i = 0; i < 4; i++) {
double angle = i * M_PI / 2.0; // 90 度间隔
double x = boltCircleRadius * cos(angle);
double y = boltCircleRadius * sin(angle);
gp_Ax2 boltAxis(gp_Pnt(x, y, 0), gp_Dir(0, 0, 1));
TopoDS_Solid boltHole = BRepPrimAPI_MakeCylinder(boltAxis, 4, 10);
boltHoles.Append(boltHole);
}
// 5. 合并所有螺栓孔刀具
TopoDS_Shape allBoltHoles = boltHoles.First();
for (TopTools_ListIteratorOfListOfShape it(boltHoles); it.More(); it.Next()) {
if (it.Value() != boltHoles.First()) {
BRepAlgoAPI_Fuse fuse(allBoltHoles, it.Value());
allBoltHoles = fuse.Shape();
}
}
// 6. 切除所有螺栓孔
BRepAlgoAPI_Cut cut2(withCenterHole, allBoltHoles);
TopoDS_Shape finalPart = cut2.Shape();
std::cout << "法兰盘建模完成!" << std::endl;
return 0;
}
---
高级技巧
多体布尔运算
// 依次对多个物体进行布尔运算
TopoDS_Shape result = shape1;
for (const auto& tool : tools) {
BRepAlgoAPI_Cut cut(result, tool);
result = cut.Shape();
}
检查布尔运算状态
BRepAlgoAPI_Fuse fuseOp(shape1, shape2);
// 检查是否完成
if (!fuseOp.IsDone()) {
std::cerr << "运算未完成" << std::endl;
}
// 检查是否有错误
if (fuseOp.HasErrors()) {
std::cerr << "运算存在错误" << std::endl;
}
// 获取错误类型
Standard_Integer err = fuseOp.ErrorStatus();
优化布尔运算性能
// 设置模糊公差(影响运算精度和速度)
BRepAlgoAPI_Fuse fuseOp(shape1, shape2);
fuseOp.SetFuzzyValue(1e-5); // 默认值,增大可提高效率但降低精度
TopoDS_Shape result = fuseOp.Shape();
---
常见问题与解决方案
问题 1:布尔运算失败
原因: 几何体相切或重合导致计算困难
解决方案:
// 稍微偏移刀具几何体
gp_Trsf trsf;
trsf.SetTranslation(gp_Vec(0.001, 0.001, 0.001));
BRepBuilderAPI_Transform mover(tool, trsf);
TopoDS_Shape movedTool = mover.Shape();
BRepAlgoAPI_Cut cut(base, movedTool);
问题 2:结果几何体质量差
原因: 公差设置不当
解决方案:
// 使用更严格的公差
BRepAlgoAPI_Fuse fuseOp(shape1, shape2);
fuseOp.SetRunParallel(Standard_True); // 启用并行计算
TopoDS_Shape result = fuseOp.Shape();
---
总结
布尔运算是 OCCT 建模的核心技能:
| 要点 |
| Fuse 合并、Cut 切除、Common 取交集 |
---
字数:约 2500 字
分类:OpenCASCADE 教程
难度:进阶
版本:7.7.0