Same face saved twice in the BRep file

I'm working on this problem that looks at detecting touching faces between two solids, and replacing them so they are shared between the two solids. In particular, I'm having problems with the following case (a small cylinder inside a larger one, their bottom faces being aligned):

TopoDS_Solid cyl1 = BRepPrimAPI_MakeCylinder(3, 1).Solid();
TopoDS_Solid cyl2 = BRepPrimAPI_MakeCylinder(1, 1).Solid();
cyl1 = cut cyl2 from cyl1

After this step, faces touching each other are identified (F1 the cylindrical face of the small cylinder, and F2 the round face on top of the small cylinder), and BRepTools_ReShape is used to replace F1 and F2 in cyl1 with the corresponding faces in cyl2.

After applying ReShape, the corresponding faces in each solid are checked against each other. For both F1 and F2, the result of IsPartner is TRUE, result of IsSame is TRUE, and result of IsEqual is FALSE. Before applying ReShape, all three were FALSE. However, when saving the compound of the two solids, F1 is saved twice (as if there are two distinct but equal faces that occupy the same location, and thus not shared between the two solids), whereas F2 is saved once (and thus shared between the two solids). Since F1 is not shared, the model cannot be meshed correctly.

Changing the orientation of the faces to correspond to each other and/or applying ShapeFix_Solid does not change the outcome -- there are still too many faces in the BREP file.

How can this happen? Is there anything else I should do before saving the two solids in a BREP file?

Any help is appreciated (I've had this problem for a long time).

Tiberiu Chelcea's picture

I don't know if it helps, but I'm attaching the resulting BREP file.

Attachments: 
Evgeny Lodyzhehsky's picture

Dear Tiberiu Chelcea.

1.The shapes in "demo.brp" are not correspond to those in the
beggining of your messasge :
"...
TopoDS_Solid cyl1 = BRepPrimAPI_MakeCylinder(3, 1).Solid();
TopoDS_Solid cyl2 = BRepPrimAPI_MakeCylinder(1, 1).Solid();
cyl1 = cut cyl2 from cyl1
..."

2. It is really hard to understand the 3D problem without pictures.
So, I've prepared some template.
Could you clarify using the pictures from the template
what is the problem here.

Tiberiu Chelcea's picture

Hi Evgeny,

I'm sorry for providing the wrong example. While trying to reproduce the example, I've discovered the issue. The problem was with how I was isolating this case -- in effect, I was not looking at the simple case that I thought I isolated. These examples are taken from a very large system that creates 3D models on the fly from XML descriptions; unfortunately, somewhere in the bowels of the system the two simple solids were further modified, and thus I was getting that error. Once I've eliminated those extra modifications, the result was what I expected. Sorry again for the false alarm.

m-sazonov's picture

You have two problems.
1. You are wrong when you attempt to replace the face in the result of cut with the face from one of input shapes. After such replacing, there will be error of connectivity between faces, i.e. two neighboring faces won't share the same edge. Such shape cannot be considered as a valid solid. After substituting faces, you should ensure connectivity. For that you have to place both solids in one compound and perform sewing in non-manifold mode. This process will identify coinciding edges and replace duplicates.
2. You are doing something wrong when saving. When I join two such solids (with shared faces) into one compound, save it to the brep, and then restore again I am getting the shape with the same number of faces. No problem is here.

Tiberiu Chelcea's picture

Michael, thanks for the reply. When doing face replacement, I was replacing everything all the way down to vertices, so #1 is not the issue. #2 is also not the issue. The problem was with how I was isolating this case -- in effect, I was not looking at the simple case that I thought I isolated. These examples are taken from a very large system that creates 3D models on the fly from XML descriptions; unfortunately, somewhere in the bowels of the system the two simple solids were further modified, and thus I was getting that error. Once I've eliminated those extra modifications, the result was what I expected. I'm really sorry for the false alarm.