Remove Duplicate Faces Leftover From Cut

I'm trying to combine several adjacent solids into a compound with shared faces. The best way I've found to do this so far is to cut each object out of the combination of all the objects. This way, Cut will create multiple solids that share their faces. However, one of the cases where Cut does NOT create shared faces is when two boxes share a face. If one box is slightly larger or smaller than the other, then cutting works fine, but if the adjacent faces are the same size, then cut will not remove the duplicate face. Running BRepAlgoAPI_Check on the resulting compound contains BOPAlgo_SelfIntersect statuses, and if I open the brep file in Gmsh, I can see two surfaces between the boxes instead of one. How can I get BRepAlgoAPI_Cut to more consistently find the shared faces? If that's not possible, how can I remove the duplicate surface myself after running the Cut?

//Create the boxes 
double factor = 1; // 0.99999 or 1.000001 work
double boxSize = 0.5;
TopoDS_Shape box1 = BRepPrimAPI_MakeBox(gp_Pnt(-boxSize*factor,-boxSize*factor,0),gp_Pnt(boxSize*factor,boxSize*factor,boxSize));
TopoDS_Shape box2 = BRepPrimAPI_MakeBox(gp_Pnt(-boxSize,-boxSize,-boxSize),gp_Pnt(boxSize,boxSize,0));

//Cut the objects so they share their faces
TopoDS_Shape block = BRepAlgoAPI_Fuse(box1,box2);
box1 = BRepAlgoAPI_Cut(block,box2);
box2 = BRepAlgoAPI_Cut(block,box1); 

//Create the compound
TopoDS_Compound compound;
BRep_Builder solidBuilder;
solidBuilder.MakeCompound(compound);
solidBuilder.Add(compound,box1);
solidBuilder.Add(compound,box2);

BRepAlgoAPI_Check check(compound);
std::cout << "Valid: " << check.IsValid() << std::endl; //This returns false because of BOPAlgo_SelfIntersect
BRepTools::Write(compound,"example.brep");

 

Attachments: 
Addison Denchik's picture

For anyone with the same problem, the way to make BRepAlgoAPI_Cut more consistent is to use ShapeUpgrade_UnifySameDomain. In this case, the fusion of both boxes should be run through UnifySameDomain before each of the boxes is cut out of it:

TopoDS_Shape block = BRepAlgoAPI_Fuse(box1,box2);
ShapeUpgrade_UnifySameDomain unify(block);
unify.Build();
block = unify.Shape();
box1 = BRepAlgoAPI_Cut(block,box2);
box2 = BRepAlgoAPI_Cut(block,box1);
// box1 and box2 share a face now, and BRepAlgoAPI_Check will succeed on the compound

However, this still did not work in all cases. It turns out that a better way to combine several solids into a compound with shared faces is to use BRepAlgoAPI_BuilderAlgo:

TopTools_ListOfShape shapes;
shapes.Append(box1);
shapes.Append(box2);
BRepAlgoAPI_BuilderAlgo builder;
builder.SetArguments(shapes);
builder.SetNonDestructive(true);
builder.Build();
TopoDS_Compound compound = TopoDS::Compound(builder.Shape());