Modelling in OCC/Meshing with Gmsh

I'm working on a project where I'm building 3D models of circuit boards in OCC, exporting them as STEP/BREP files, and using Gmsh to mesh them. However, Gmsh seems to crash a lot when trying to mesh my files, and I'm wondering whether there are some known issues or some known tricks on how to build OCC topologies for use with Gmsh. I've noticed that a bunch of people in the forum are using this.

I'm doing only some small experiments now. Basically, if I just build a box (with BRepPrimAPI_MakeBox) and export it, Gmsh works fine. If I build 2 boxes (one on top of the other), and then build a compound (with BRep_Builder::MakeCompound) and add each box, then Gmsh crashes. Should I fuse the two boxes before exporting?

One more question: I've seen that OCC has some meshing capabilities. Is it just surface meshing, or does it perform also 3D volume meshing?

Thank you.

Tiberiu Chelcea's picture

Sorry, one more clarification. For me it's essential to keep the two stacked boxes as two separate volumes (in the final model one could represent the copper substrate and the other the silicon), so they need to be meshed independently. I've tried to Fuse the two boxes, but that creates a single shape and not two stacked shapes.

Francois Lauzon's picture

I don't use Gmsh, instead I use Netgen directly from OCC for surface meshing and it's working really well (you might save the exporting step), it's not as fast as commercial mesher but the result is of the same quality. I know you can do volumetric meshing also, but never use it. You can use netgen::OCCGenerateMesh to generate the mesh from a TopoDS_Shape, I think there is an example that come in the source code.

Good Luck,

Tiberiu Chelcea's picture

Thanks Francois for the pointers. The funny thing is that Gmsh uses Netgen for mesh generation too. I'll try to look into it.

The issue here is a bit different though, maybe I should post it in a separate thread. I'm looking at a very simple example, imagine a cube A of size (1,1,1) sitting on top of cube B of size (3,3,3), aligned vertically. Now, the bottom side of cube A touches the top side of cube B; the size of the overlap being a 1x1 square. Now, the issue in trying to mesh this structure is that the top side of cube B and bottom side of cube A are meshed separately, which causes problems with non-aligned points.

What I want would be an operation such that, given the two cubes laying on top of each other, extracts this common face and rebuilds the two shapes (cubes) in such a way that the common face is part of both cube A and cube B. This way, in the mesher, the common face is meshed exactly once, and the 3d mesh could be build from there.

I've tried to use BRepAlgoAPI_Fuse and BRepBuilderAPI_Sewing, but the first one creates a single shape out of cube A and B (I still want to mesh each one separately, since in my model they could be of different materials) and the second one seems to output the empty shell of the two cubes, so 3d meshing does not work.


Bearloga's picture

You still need to use sewing. But make sure to turn on non-manifold mode. Then, you should create two shells by yourself from the faces got after sewing.

Tiberiu Chelcea's picture

Thanks Bearloga,

I've tried something along these lines, but I get something strange. If I do sewing for those two boxes, I get a compound of shells. These shells are closed, and can build the solids out of them. However, if I try a more complicated operation, I get this strange thing.

Let me describe the setup:
TopoDS_Solid box1 = BRepPrimAPI_MakeBox(gp_Pnt(0,0,0), 6, 6, 6);
TopoDS_Solid box2 = BRepPrimAPI_MakeBox(gp_Pnt(1,1,0), 3, 3, 1);
TopoDS_Solid box3 = BRepPrimAPI_MakeBox(gp_Pnt(2,2,1), 1, 1, 1);

Pretty much, two small boxes on top of each other (box2 and box3), and they should be covered by box1. I cut the two small boxes out of box1 (using BRepAlgoAPI_Cut), and then I use:

BRepBuilderAPI_Sewing mySewingTool;

add the three shapes (box2, box3, and the result of cutting). When I get the shapes back, the compound has 3 shells, but only 2 of them are closed; the third one (which should correspond to box3) has only 5 faces. Therefore, a solid cannot be created and meshing fails. I get the same behavior whether I set nonManifoldMode to true or false. Am I understanding the sewing operation wrong, or am I supposed to do something different after sewing (maybe more complicated)?


Bearloga's picture

The work of sewing algorithm is based on coincidence of edges. Your boxes box2 and box3 have no common edges. Try making a cut of box3 from box2. The result will contain two faces on top. Then use this result instead of box2 in the sewing. Hope this helps.

Bearloga's picture

I think you should make more complex treatment of the result of sewing than simply getting shells.
For each original solid, get each its face and replace it by the corresponding face from sewing. Use the methods Modified, ModifiedSubShape. To replace faces in solid, use the class BRepTools_ReShape.
Now your solids will have yet different touching faces but they will share the same edges.
Now you have to find all pairs of coinciding faces (faces which have all edges the same), and replace in your solids by one of each pair.
That's all! Good luck.

Tiberiu Chelcea's picture

Thanks again Bearloga, I will try your suggestion. In your description, it seems that you're suggesting that I should replace a face from the original solid with a face from the sewing. However, it seems that there are cases where a face from the original is to be replaced by several faces. Think about the top face of the (2,2,1) cube (with a size 2x2), on which the 1x1x1 cube lays: the end result should be two faces, one with a hole in it around the 1x1 face, and the 1x1 face. Would your suggestion account for these situations? (I'm asking because OCC is still a bit mysterious to me, and implementing things take a bit longer than I would like).


Steve Lockley's picture

I have just posted a report on the BREP Cut algorithm, I think they may be related. I found that if you have two solids which share all or part of a face the BREP alogoritms can crash. Have a look it may be related