如何在 OpenCASCADE 中融合 TopoDS_Shapes(布尔 OR)

OCCUtils 库提供了在 OpenCASCADE 中融合两个形状的简便方法。

首先,让我们使用 OCCUtil 的基元生成函数生成立方体和圆柱体:

make_box_and_cylinder.cpp
#include <occutils/Primitive.hxx>

using namespace OCCUtils;

TopoDS_Shape solid1 = Primitive::MakeBox(18 /* X */, 8 /* Y */, 14 /* Z */,
    Primitive::CenterX | Primitive::CenterY);

TopoDS_Shape solid2 = Primitive::MakeCylinder(5 /* d */, 35 /* L */,
    Primitivs::Orientation::Z,
    Primitive::CenterD);

现在我们可以使用此代码片段融合基元:

fuse_with_boolean.cpp
#include <occutils/Boolean.hxx>

using namespace OCCUtils;

TopoDS_Shape fused = Boolean::Fuse({solid1, solid2});

Boolean::Fuse() 可以接受多种不同的参数类型。在此示例中,我们使用 std::initializer_list(这是花括号语法)- 但它也可以接受 TopTools_ListOfShapestd::vector<TopoDS_Shape>(以及大多数其他 STL 或非 STL 容器)。

如果你想在不使用 OCCUtils 的情况下执行融合,你可能会想使用此语法:

deprecated_fuse_constructor.cpp
BRepAlgoAPI_Fuse fuse(solid1, solid2);
fuse.Build();
TopoDS_Shape result = fuse.Shape();

但是,BRepAlgoAPI_Fuse(TopoDS, TopoDS) 构造函数是已弃用的,因此不应使用。相反,应使用以下语法:

correct_fuse_configuration.cpp
// 配置融合
BRepAlgoAPI_Fuse fuse;
fuse.SetArguments(arguments);
fuse.SetTools(tools);
// 运行融合
fuse.Build();
TopoDS_Shape shape = fuse.Shape(); // Raises NotDone if not done.

其中 argumentstools 是两个 TopTools_ListOfShape 实例。对于其他布尔算法如 BRepAlgoAPI_Cut(布尔差),两者有不同含义,但对于 BRepAlgoAPI_Fuse,重要的是***argumentstools 都至少各有一个元素***!

你可以使用 OCCUtils 中的 ListUtils::SplitIntoHeadAndTail 生成 std::pair 列表(argumentstools),其中 arguments 有一个元素而 tools 有其余元素,如下所示:

listutils_split_example.cpp
auto toolsAndArgs = ListUtils::SplitIntoHeadAndTail(shapes, 1);
auto tools = toolsAndArgs.first;
auto arguments = toolsAndArgs.second;

但是,如果你使用 ListUtils::SplitIntoHeadAndTail(),你可以直接使用 Boolean::Fuse()


Check out similar posts by category: C/C++, OpenCASCADE