Can't execute Boolean Operations on Breps that were created with code

I’ve been wrestling with this for over a week now and I’m at my wits end. I am creating Breps manually using code, for example using snippets like this:

        //right bottom fillet
        arcPlane.Origin = plane.PointAt(halfTenonWidth - radius, radius);
        c = new Circle(arcPlane, radius);
        a = new Arc(c, new Interval(Math.PI * 1.5, 2 * Math.PI)); 
        outCurves.Add(a.ToNurbsCurve());

and then combining NurbsCurve array outCurves like this:

        Curve joined = Curve.JoinCurves(outCurves, Config.Tolerance, false)[0]; 
        joined.Domain = new Interval(0, 1);

Then later, I create a Brep like this:

        Surface tSurf = Surface.CreateExtrusion(tShape, ext); //tShape being the joined curves
        Brep tBrep = tSurf.ToBrep();
        tBrep = tBrep.CapPlanarHoles(Config.Tolerance);
        
        if (tBrep.SolidOrientation != BrepSolidOrientation.Outward)
        {
            tBrep.Flip();
        }

When I check the results here by sending the curve, the surface, the brep or the brep with the capped holes into my RhinoDoc, they all appear totally normal. In fact, I can send my final Breps in to the RhinoDoc and using boolean operations, and they always work fine!

But no matter what I try, SOME of my breps will fail any boolean operation in my C# code. Oddly, some of them work perfectly fine.

I have confirmed that the Breps are outward facing, manifold, solid and valid. I have also tried various tolerance settings in the boolean operations, including the active rhinodoc tolerance. I’m convinced this has something to do with how I’m creating the Breps in my code.

When I tried to recreate the problem in a simple code snippet to post online, of course the boolean worked perfectly. Please let me know if anything comes to mind. Even if I could somehow recreate the process Rhino uses when adding a Brep to the document - since somehow that is “cleaning” my breps and I can then use Boolean op’s in Rhino’s UI.

Thanks!

Just had a bit of a late night break through - the problem seems to be the fact that the Breps have a coincident planar face. When the Breps intersect OR do not intersect, there is a successful boolean operation. But when the faces are coincident, the intersection fails (but still succeeds in the Rhino UI).

I know it’s a lot to ask but I have whittled the problem down to it’s bare bones and if anyone would be willing to have a look, you should be able to replace the template files in a brand new Rhino Plugin in Visual Studio with these two files and I think it should work.

Any help or advice appreciated!

DebugBrepsCommand.cs (8.2 KB)
DebugBrepsPlugin.cs (1.1 KB)

Hi @K119,

My corrections are commented with // **NEW**.

DebugBrepsCommand.cs (7.4 KB)

– Dale

Thank you! This works! For anyone reading this later Dale added

brepInstance.Faces.SplitKinkyFaces()

after converting the extruded surface to a brep, before

brepInstance = brepInstance.CapPlanarHoles(tolerance);

He also changed the tolerance to the RhinoDoc ModelAbsoluteTolerance.