Problem with Boolean Operations on Solids


once again, I have a problem with boolean operations:

I want to cut one cylinder from another so that I
obtain a ring. After that, the ring is to be fused with a torus.
My code is as follows:

double rr = 50.0;
double rs = 4.00;
double h = 15.0;

TopoDS_Shape torus = BRepPrimAPI_MakeTorus (rr-rs, rs).Shape ();
TopoDS_Shape cylinder1 = BRepPrimAPI_MakeCylinder (rr, h).Shape ();
TopoDS_Shape cylinder2 = BRepPrimAPI_MakeCylinder (rr - 2 * rs, h).Shape ();

TopoDS_Shape cut = BRepAlgoAPI_Cut(cylinder1, cylinder2);
TopoDS_Shape fuse = BRepAlgoAPI_Fuse(torus, cut);

Unfortunately, an exception is raised during the fuse operation:

Strange enough, everything works fine, if I fuse cylinder1 and torus
first and cut cylinder2 afterwards.

I also tried to use class BRepAlgo_Fuse instead of
BRepAlgoAPI_Fuse. With this algo I didn't have the exception, but the
result was not what I expected (Some faces are missing).

Does anybody know, what's wrong with my code?
What is the meaning of the exception?

Many thanks in advance


Stephane Routelous's picture

Hallo Angelika,

for the boolean operations, to make them work better, try to avoid cutting 2 solids having a common face (ot tangential faces)
in your case, you have the both cylinder "sharing" the 2 planar faces (in the plane z=0 and z=h.
try to make the cutting cylinder a little bigger (ex : from z=-0.1 to z=h+0.1 )
This trick usually solves problems during a cut. I don't really know if this problem is solved in the new versions of the Boolean operation tools.

Another thing you can try is to fix the shape after a boolean operation use ShapeFix package :
(taken from a previous post:)
ShapeFix_Shape FixShape;
ShapeToFix = FixShape.Shape();

I don't have OCC installed right now, but you can try that

Viel Spass.


Angelika Leibscher's picture

Hallo Stephane,

thanks a lot for your help.

Unfortunately, it didn't work for me. I tried the following test harness script, but all
I got is the StdFail_UndefinedDerivative exception:

pload ALL
pcylinder c1 50 15
pcylinder c2 42 15.2
ptorus t1 46 4
ttranslate c2 0 0 -0.1
bcut cut c1 c2
bfuse fuse cut t1

Fixing the shape didn't solve the problem, too.

Do you have a working example of code solving this problem? This would be very helpful.

The whole problem wouldn't bother me so much, if there were any general rules, when to use boolean operations and when to avoid them.
You already mentioned problems with common faces. Are there more known problems?

Any tips on this topic would be appreciated.