Skip to content

Commit 95bf9d7

Browse files
committed
Added materials support by postfixing the material name onto the volume name
1 parent cf112d1 commit 95bf9d7

File tree

3 files changed

+83
-6
lines changed

3 files changed

+83
-6
lines changed

assembly_mesh_plugin/plugin.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ def get_gmsh(self, imprint=True):
161161
tagged_faces = {}
162162
multi_material_groups = {}
163163
surface_groups = {}
164+
solid_materials = []
164165

165166
gmsh.initialize()
166167
gmsh.option.setNumber(
@@ -171,13 +172,23 @@ def get_gmsh(self, imprint=True):
171172
# Get all of the subshapes and their corresponding names/positions
172173
extract_subshape_names(self, self.name)
173174

174-
# Imprint the assembly
175-
imprinted_assembly, imprinted_solids_with_orginal_ids = (
176-
cq.occ_impl.assembly.imprint(self)
177-
)
178-
179175
# Handle the imprinted assembly
180176
if imprint:
177+
# Imprint the assembly
178+
imprinted_assembly, imprinted_solids_with_orginal_ids = (
179+
cq.occ_impl.assembly.imprint(self)
180+
)
181+
182+
# Collect the materials
183+
for imp_solid, solid_id in imprinted_solids_with_orginal_ids.items():
184+
# Track down the original assembly object so that we can retrieve materials, if present
185+
short_id = solid_id[0].split("/")[-1] if "/" in solid_id[0] else solid_id[0]
186+
subassy = self.objects[short_id]
187+
188+
# Save the assembly material associated with this solid
189+
if subassy.material:
190+
solid_materials.append(subassy.material.name)
191+
181192
for solid, name in imprinted_solids_with_orginal_ids.items():
182193
# Get just the name of the current assembly
183194
short_name = name[0].split("/")[-1]
@@ -202,11 +213,22 @@ def get_gmsh(self, imprint=True):
202213
# Add faces to the mesh and handle tagged faces
203214
add_faces_to_mesh(gmsh, solid, short_name, loc)
204215

216+
# Keep track of the materials
217+
if self.objects[name.split("/")[-1]].material:
218+
solid_materials.append(self.objects[name.split("/")[-1]].material.name)
219+
205220
# Step through each of the volumes and add physical groups for each
206221
for volume_id in volumes.keys():
207222
gmsh.model.occ.synchronize()
223+
224+
# Include the material name, if present
208225
ps = gmsh.model.addPhysicalGroup(3, volumes[volume_id][0])
209-
gmsh.model.setPhysicalName(3, ps, f"{volume_map[volume_id]}")
226+
227+
# See if we need to include the material name as part of the physical volume name
228+
volume_name = f"{volume_map[volume_id]}"
229+
if len(solid_materials) >= volume_id:
230+
volume_name = f"{volume_map[volume_id]}~{solid_materials[volume_id - 1]}"
231+
gmsh.model.setPhysicalName(3, ps, volume_name)
210232

211233
# Handle tagged surface groups
212234
for t_name, surf_group in surface_groups.items():

tests/sample_assemblies.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,20 @@ def generate_subshape_assembly():
315315
)
316316

317317
return assy
318+
319+
320+
def generate_materials_assembly():
321+
"""
322+
Generates a simple assembly with materials.
323+
"""
324+
325+
# Create the assembly children
326+
cube_1 = cq.Workplane().box(10, 10, 10)
327+
cube_2 = cq.Workplane().box(5, 5, 5)
328+
329+
# Put the assembly together
330+
assy = cq.Assembly(name="top-level")
331+
assy.add(cube_1, name="cube_1", material="copper")
332+
assy.add(cube_2, name="cube_2", loc=cq.Location(0, 0, 5), material="steel")
333+
334+
return assy

tests/test_meshes.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
generate_test_cross_section,
99
generate_assembly,
1010
generate_subshape_assembly,
11+
generate_materials_assembly,
1112
)
1213

1314

@@ -175,3 +176,40 @@ def _check_physical_groups():
175176

176177
# Ensure that there are physical groups
177178
_check_physical_groups()
179+
180+
181+
def test_mesh_materials():
182+
"""
183+
Tests to make sure that assembly materials are preserved in the mesh data.
184+
"""
185+
186+
# Create the basic assembly with materials
187+
assy = generate_materials_assembly()
188+
189+
#
190+
# Imprinted assembly
191+
#
192+
gmsh = assy.getGmsh(imprint=True)
193+
gmsh.model.mesh.generate(3)
194+
195+
phys_groups = gmsh.model.getPhysicalGroups(3)
196+
197+
# Make sure we got the correct names
198+
name = gmsh.model.getPhysicalName(3, 1)
199+
assert name == "cube_1~copper"
200+
name = gmsh.model.getPhysicalName(3, 2)
201+
assert name == "cube_2~steel"
202+
203+
#
204+
# Non-imprinted assembly
205+
#
206+
gmsh = assy.getGmsh(imprint=False)
207+
gmsh.model.mesh.generate(3)
208+
209+
phys_groups = gmsh.model.getPhysicalGroups(3)
210+
211+
# Make sure we got the correct names
212+
name = gmsh.model.getPhysicalName(3, 1)
213+
assert name == "cube_1~copper"
214+
name = gmsh.model.getPhysicalName(3, 2)
215+
assert name == "cube_2~steel"

0 commit comments

Comments
 (0)