Skip to content

Commit a47ce50

Browse files
authored
Merge pull request #6 from CadQuery/composition
Broke the methods apart to make it cleaner if the user wants more meshing control
2 parents 747e507 + 3595878 commit a47ce50

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import cadquery_assembly_mesh_plugin.plugin
2626
You can then tag faces in each of the assembly parts and create your asembly. To export the assembly to a mesh file, you do the following.
2727

2828
```python
29-
your_assembly.assemblyToGmsh(mesh_path="tagged_mesh.msh")
29+
your_assembly.saveToGmsh(mesh_path="tagged_mesh.msh")
3030
```
3131

3232
Normal tag names lead to a physical group with the assembly part name prefixed. So a tag name of `inner-bottom` on an assembly part with the name `steel_plate` will be `steel_plate_inner-bottom`
@@ -54,11 +54,42 @@ assy = cq.Assembly()
5454
assy.add(shell, name="shell", loc=cq.Location(cq.Vector(0, 0, 0)), color=cq.Color("red"))
5555
assy.add(insert, name="insert", loc=cq.Location(cq.Vector(0, 0, 0)), color=cq.Color("blue"))
5656

57-
assy.assemblyToGmsh(mesh_path="tagged_mesh.msh")
57+
assy.saveToGmsh(mesh_path="tagged_mesh.msh")
5858
```
5959

6060
The resulting `.msh` file should have three physical groups named for tags in it. The `in_contact` group should include the faces from both the shell and the insert.
6161

62+
If you want more control over the mesh generation and export, you can use the `getTaggedGmsh` method and then finalize the mesh yourself.
63+
64+
```python
65+
import cadquery as cq
66+
import cadquery_assembly_mesh_plugin.plugin
67+
import gmsh
68+
69+
shell = cq.Workplane("XY").box(50, 50, 50)
70+
shell = shell.faces(">Z").workplane().rect(21, 21).cutThruAll()
71+
shell.faces(">X[-2]").tag("inner-right")
72+
shell.faces("<X[-2]").tag("~in_contact")
73+
74+
# Create the insert
75+
insert = cq.Workplane("XY").box(20, 20, 50)
76+
insert.faces("<X").tag("~in_contact")
77+
insert.faces(">X").tag("outer-right")
78+
79+
assy = cq.Assembly()
80+
assy.add(shell, name="shell", loc=cq.Location(cq.Vector(0, 0, 0)), color=cq.Color("red"))
81+
assy.add(insert, name="insert", loc=cq.Location(cq.Vector(0, 0, 0)), color=cq.Color("blue"))
82+
83+
# Get a Gmsh object back with all the tagged faces as physical groups
84+
gmsh = assy.getTaggedGmsh()
85+
86+
# Generate the mesh and write it to the file
87+
gmsh.model.mesh.field.setAsBackgroundMesh(2)
88+
gmsh.model.mesh.generate(3)
89+
gmsh.write(mesh_path)
90+
gmsh.finalize()
91+
```
92+
6293
## Tests
6394

6495
These tests are also run in Github Actions, and the meshes which are generated can be viewed as artifacts on the successful `tests` Actions there.

assembly_mesh_plugin/plugin.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
import gmsh
66

77

8-
def assembly_to_gmsh(self, mesh_path="tagged_mesh.msh"):
8+
def get_tagged_gmsh(self):
99
"""
10-
Pack the assembly into a gmsh object, respecting assembly part names and face tags when creating
11-
the physical groups.
10+
Allows the user to get a gmsh object from the assembly, respecting assembly part names and face
11+
tags, but have more control over how it is meshed.
1212
"""
13-
1413
gmsh.initialize()
1514
gmsh.option.setNumber("General.Terminal", 0)
1615
gmsh.model.add("coil_assembly")
@@ -126,6 +125,18 @@ def assembly_to_gmsh(self, mesh_path="tagged_mesh.msh"):
126125

127126
gmsh.model.occ.synchronize()
128127

128+
return gmsh
129+
130+
131+
def assembly_to_gmsh(self, mesh_path="tagged_mesh.msh"):
132+
"""
133+
Pack the assembly into a gmsh object, respecting assembly part names and face tags when creating
134+
the physical groups.
135+
"""
136+
137+
# Turn this assembly with potentially tagged faces into a gmsh object
138+
gmsh = get_tagged_gmsh(self)
139+
129140
gmsh.model.mesh.field.setAsBackgroundMesh(2)
130141

131142
gmsh.model.mesh.generate(3)
@@ -136,3 +147,5 @@ def assembly_to_gmsh(self, mesh_path="tagged_mesh.msh"):
136147

137148
# Patch the new assembly functions into CadQuery's importers package
138149
cq.Assembly.assemblyToGmsh = assembly_to_gmsh
150+
cq.Assembly.saveToGmsh = assembly_to_gmsh # Alias name that works better on cq.Assembly
151+
cq.Assembly.getTaggedGmsh = get_tagged_gmsh

tests/smoke_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_basic_assembly():
1515
assy = generate_simple_nested_boxes()
1616

1717
# Create a mesh that has all the faces tagged as physical groups
18-
assy.assemblyToGmsh(mesh_path="tagged_mesh.msh")
18+
assy.saveToGmsh(mesh_path="tagged_mesh.msh")
1919

2020

2121
def test_basic_cross_section():
@@ -27,7 +27,7 @@ def test_basic_cross_section():
2727
assy = generate_test_cross_section()
2828

2929
# Create a mesh that has all the faces in the correct physical groups
30-
assy.assemblyToGmsh(mesh_path="tagged_cross_section.msh")
30+
assy.saveToGmsh(mesh_path="tagged_cross_section.msh")
3131

3232

3333
def test_planar_coil():
@@ -39,4 +39,4 @@ def test_planar_coil():
3939
assy = generate_assembly()
4040

4141
# Create a mesh that has all the faces in the correct physical groups
42-
assy.assemblyToGmsh(mesh_path="tagged_planar_coil.msh")
42+
assy.saveToGmsh(mesh_path="tagged_planar_coil.msh")

0 commit comments

Comments
 (0)