How to cut shapes using OCCUtils for OpenCascade (boolean difference)

The OCCUtils library provides an easy way to compute the difference, or cut one shapefrom the other, in OpenCASCADE. Also see How to create a Box TopoDS_Solid in OpenCASCADE and How to create a Cylinder TopoDS_Solid in OpenCASCADE for more details on how we can generate those shapes using OCCUtils.

If we have two shapes:

#```cpp {filename=“occutils_boolean_difference.cpp”} #include <occutils/Primitive.hxx>

// … TopoDS_Solid box = Primitive::MakeBox(10, 10, 10 /* mm */);

TopoDS_Solid cylinder = Primitive::MakeCylinder(3 /* mm diameter /, 100 / mm length */, Primitive::Orientation::Y);

occutils_boolean_cut_example.cpp

we can use `Boolean::Cut` from OCCUtils to compute the boolean difference:

```cpp {filename="occutils_boolean_cut_example.cpp"}
TopoDS_Shape result = Boolean::Cut(box, cylinder);

```plaintext {filename="occutils_boolean_cut_example.cpp"}

### Complete `main.cpp` example with STEP export:



#```cpp {filename="occutils_main.cpp"}
#include <occutils/Primitive.hxx>
#include <occutils/STEPExport.hxx>
#include <occutils/Boolean.hxx>

using namespace OCCUtils;

int main() {
    // Make basic box
    TopoDS_Solid box = Primitive::MakeBox(10, 10, 10 /* mm */);

    TopoDS_Solid cylinder = Primitive::MakeCylinder(3 /* mm diameter */,
        100 /* mm length */, Primitive::Orientation::Y);

    TopoDS_Shape result = Boolean::Cut(box, cylinder);

    STEP::ExportSTEP(result, "out.step");
}
```plaintext {filename="out.step"}

The result (`out.step`) will look like this when viewed in FreeCAD

![](/images/2022/11/OCCUtils-Cut.png)

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