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:

#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);

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

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

Complete main.cpp example with STEP export:

#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");
}

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