diff --git a/assembly_mesh_plugin/plugin.py b/assembly_mesh_plugin/plugin.py index df9d95d..02fabb0 100644 --- a/assembly_mesh_plugin/plugin.py +++ b/assembly_mesh_plugin/plugin.py @@ -45,10 +45,9 @@ def get_tagged_gmsh(self): # All the solids in the current part should be added to the mesh for s in obj.moved(loc).Solids(): # Add the current solid to the mesh - with tempfile.NamedTemporaryFile(suffix=".brep") as temp_file: s.exportBrep(temp_file.name) - gmsh.model.occ.importShapes(temp_file.name) + ps = gmsh.model.occ.importShapes(temp_file.name) # TODO find a way to check if the OCC in gmsh is compatible with the # OCC in CadQuery. When pip installed they tend to be incompatible @@ -60,6 +59,16 @@ def get_tagged_gmsh(self): gmsh.model.occ.synchronize() + # Technically, importShapes could import multiple entities/dimensions, so filter those + vol_ents = [] + for p in ps: + if p[0] == 3: + vol_ents.append(p[1]) + + # Set the physical name to be the part name in the assembly for all the solids + ps2 = gmsh.model.addPhysicalGroup(3, vol_ents) + gmsh.model.setPhysicalName(3, ps2, f"{name.split('/')[-1]}") + # All the faces in the current part should be added to the mesh for face in s.Faces(): # Face name can be based on a tag, or just be a generic name diff --git a/tests/test_meshes.py b/tests/test_meshes.py new file mode 100644 index 0000000..162fe74 --- /dev/null +++ b/tests/test_meshes.py @@ -0,0 +1,44 @@ +import gmsh +import assembly_mesh_plugin.plugin +from tests.sample_assemblies import ( + generate_nested_boxes, + generate_simple_nested_boxes, + generate_test_cross_section, + generate_assembly, +) + + +def test_simple_assembly(): + """ + Tests to make sure that the most basic assembly works correctly with tagging. + """ + + # Create the basic assembly + assy = generate_simple_nested_boxes() + + # Create a mesh that has all the faces tagged as physical groups + assy.saveToGmsh(mesh_path="tagged_mesh.msh") + + gmsh.initialize() + + gmsh.open("tagged_mesh.msh") + + # Check the solids for the correct tags + physical_groups = gmsh.model.getPhysicalGroups(3) + for group in physical_groups: + # Get the name for the current volume + cur_name = gmsh.model.getPhysicalName(3, group[1]) + + assert cur_name in ["shell", "insert"] + + # Check the surfaces for the correct tags + physical_groups = gmsh.model.getPhysicalGroups(2) + for group in physical_groups: + # Get the name for this group + cur_name = gmsh.model.getPhysicalName(2, group[1]) + + # Skip any groups that are not tagged explicitly + if "_surface_" in cur_name: + continue + + assert cur_name in ["shell_inner-right", "insert_outer-right", "in_contact"]