Skip to content

Commit eb94aa2

Browse files
committed
Handle subshape names and layers in addition to tags now
1 parent 9177f3b commit eb94aa2

File tree

4 files changed

+90
-1
lines changed

4 files changed

+90
-1
lines changed

assembly_mesh_plugin/plugin.py

+28
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,34 @@ def get_tagged_gmsh(self):
4242
else:
4343
tagged_faces[short_name][tag] = [face.val()]
4444

45+
# Extract the tagged faces that have been added by the addSubshape method of cq.Assembly
46+
if self._subshape_names:
47+
for subshape, subshape_tag in self._subshape_names.items():
48+
if subshape_tag in tagged_faces[short_name]:
49+
# Check to see if this is a duplicate
50+
if subshape in tagged_faces[short_name][subshape_tag]:
51+
print(
52+
f"WARNING: Duplicate subshape found for tag {subshape_tag}."
53+
)
54+
55+
tagged_faces[short_name][subshape_tag].append(subshape)
56+
else:
57+
tagged_faces[short_name][subshape_tag] = [subshape]
58+
59+
# Extract the tagged faces that have been added by the addSubshape method of cq.Assembly
60+
if self._subshape_layers:
61+
for subshape, subshape_tag in self._subshape_layers.items():
62+
if subshape_tag in tagged_faces[short_name]:
63+
# Check to see if this is a duplicate
64+
if subshape in tagged_faces[short_name][subshape_tag]:
65+
print(
66+
f"WARNING: Duplicate subshape found for tag {subshape_tag}."
67+
)
68+
69+
tagged_faces[short_name][subshape_tag].append(subshape)
70+
else:
71+
tagged_faces[short_name][subshape_tag] = [subshape]
72+
4573
# All the solids in the current part should be added to the mesh
4674
for s in obj.moved(loc).Solids():
4775
# Add the current solid to the mesh

pyproject.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "hatchling.build"
66
name = "assembly_mesh_plugin"
77
version = "0.1.1"
88
dependencies = [
9-
"cadquery",
9+
"cadquery@git+https://github.com/CadQuery/cadquery.git",
1010
"gmsh",
1111
]
1212
requires-python = ">=3.9"
@@ -31,6 +31,9 @@ dev = [
3131
"black",
3232
]
3333

34+
[tool.hatch.metadata]
35+
allow-direct-references = true
36+
3437
[project.urls]
3538
Repository = "https://github.com/cadquery/assembly-mesh-plugin.git"
3639
"Bug Tracker" = "https://github.com/cadquery/assembly-mesh-plugin/issues"

tests/sample_assemblies.py

+21
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,24 @@ def generate_assembly():
262262
assembly.add(mat4_body, name="mat4", color=cq.Color("gray"))
263263

264264
return assembly
265+
266+
267+
def generate_subshape_assembly():
268+
"""
269+
Generates a simple assembly with subshapes for testing.
270+
"""
271+
272+
# Create a simple assembly
273+
assy = cq.Assembly(name="top-level")
274+
cube_1 = cq.Workplane().box(10.0, 10.0, 10.0)
275+
assy.add(cube_1, name="cube_1", color=cq.Color("green"))
276+
277+
# Add subshape name, color and layer
278+
assy.addSubshape(
279+
cube_1.faces(">Z").val(),
280+
name="cube_1_top_face",
281+
color=cq.Color("red"),
282+
layer="cube_1_top_face",
283+
)
284+
285+
return assy

tests/test_meshes.py

+37
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
generate_simple_nested_boxes,
66
generate_test_cross_section,
77
generate_assembly,
8+
generate_subshape_assembly,
89
)
910

1011

@@ -42,3 +43,39 @@ def test_simple_assembly():
4243
continue
4344

4445
assert cur_name in ["shell_inner-right", "insert_outer-right", "in_contact"]
46+
47+
48+
def test_subshape_assembly():
49+
"""
50+
Tests whether subshapes in assemblies get exported to physical groups in the resulting mesh.
51+
"""
52+
53+
# Generate a simple assembly with a subshape
54+
assy = generate_subshape_assembly()
55+
56+
# Create a mesh that has all the faces tagged as physical groups
57+
assy.saveToGmsh(mesh_path="tagged_subshape_mesh.msh")
58+
59+
gmsh.initialize()
60+
61+
gmsh.open("tagged_subshape_mesh.msh")
62+
63+
# Check the solids for the correct tags
64+
physical_groups = gmsh.model.getPhysicalGroups(3)
65+
for group in physical_groups:
66+
# Get the name for the current volume
67+
cur_name = gmsh.model.getPhysicalName(3, group[1])
68+
69+
assert cur_name in ["cube_1"]
70+
71+
# Check the surfaces for the correct tags
72+
physical_groups = gmsh.model.getPhysicalGroups(2)
73+
for group in physical_groups:
74+
# Get the name for this group
75+
cur_name = gmsh.model.getPhysicalName(2, group[1])
76+
77+
# Skip any groups that are not tagged explicitly
78+
if "_surface_" in cur_name:
79+
continue
80+
81+
assert cur_name in ["cube_1_cube_1_top_face"]

0 commit comments

Comments
 (0)