Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F77597527
meshing.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, Aug 15, 09:39
Size
2 KB
Mime Type
text/x-python
Expires
Sat, Aug 17, 09:39 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
19890364
Attached To
R11910 Additive Manufacturing Work
meshing.py
View Options
import
gmsh
import
os
import
sys
"""
function to mesh given .step file
geom_dir: location of the step file
name: name of the file
ext: extension of the file
mesh_dir: location to save .msh files
"""
def
meshing
(
geom_dir
,
name
,
ext
,
mesh_dir
):
#initialize gmesh
gmsh
.
initialize
()
gmsh
.
model
.
add
(
name
)
#read the .step file
gmsh
.
model
.
occ
.
importShapes
(
geom_dir
+
name
+
ext
)
#get solid part of the model, determine the edges of the top surfaces
solids
=
gmsh
.
model
.
occ
.
getEntities
(
dim
=
3
)
lines_upper
=
[]
for
solid
in
solids
:
x_min
,
y_min
,
z_min
,
x_max
,
y_max
,
z_max
=
gmsh
.
model
.
occ
.
getBoundingBox
(
solid
[
0
],
solid
[
1
])
lines
=
gmsh
.
model
.
occ
.
getEntitiesInBoundingBox
(
x_min
,
y_min
,
z_min
,
x_max
,
y_max
,
z_max
,
dim
=
1
)
#print(x_min,y_min, z_min, x_max, y_max, z_max)
for
line
in
lines
:
_
,
_
,
z_l
,
_
,
_
,
z_u
=
gmsh
.
model
.
occ
.
getBoundingBox
(
line
[
0
],
line
[
1
])
if
z_l
>
0.9
*
z_max
and
z_u
>
0.9
*
z_max
:
lines_upper
.
append
(
line
)
#activate
gmsh
.
model
.
occ
.
synchronize
()
#create field based on distance from top edges
gmsh
.
model
.
mesh
.
field
.
add
(
'Distance'
,
1
)
gmsh
.
model
.
mesh
.
field
.
setNumbers
(
1
,
'CurvesList'
,
[
i
[
1
]
for
i
in
lines_upper
])
gmsh
.
model
.
mesh
.
field
.
setNumber
(
1
,
'NumPointsPerCurve'
,
800
)
#create threshold field based on the distance field
lc
=
0.5
gmsh
.
model
.
mesh
.
field
.
add
(
'Threshold'
,
2
)
gmsh
.
model
.
mesh
.
field
.
setNumber
(
2
,
'InField'
,
1
)
gmsh
.
model
.
mesh
.
field
.
setNumber
(
2
,
"SizeMin"
,
lc
/
10
)
gmsh
.
model
.
mesh
.
field
.
setNumber
(
2
,
"SizeMax"
,
lc
)
gmsh
.
model
.
mesh
.
field
.
setNumber
(
2
,
"DistMin"
,
0.15
)
gmsh
.
model
.
mesh
.
field
.
setNumber
(
2
,
"DistMax"
,
0.5
)
#set threshold field as the background mesh
gmsh
.
model
.
mesh
.
field
.
setAsBackgroundMesh
(
2
)
#prevent over-refinement due to small meshes on the boundary
gmsh
.
option
.
setNumber
(
"Mesh.MeshSizeExtendFromBoundary"
,
0
)
gmsh
.
option
.
setNumber
(
"Mesh.MeshSizeFromPoints"
,
0
)
gmsh
.
option
.
setNumber
(
"Mesh.MeshSizeFromCurvature"
,
0
)
#generate 3D mesh
gmsh
.
model
.
mesh
.
generate
(
3
)
#create directory to save
try
:
os
.
mkdir
(
mesh_dir
)
except
FileExistsError
:
pass
#write the mesh file
extension
=
'.msh'
gmsh
.
write
(
mesh_dir
+
name
+
extension
)
#launch the GUI to see the results:
#gmsh.fltk.run()
#close gmesh
gmsh
.
finalize
()
Event Timeline
Log In to Comment