-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathcore_mesh_gmsh.py
More file actions
63 lines (50 loc) · 2.03 KB
/
core_mesh_gmsh.py
File metadata and controls
63 lines (50 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#! /usr/bin/python
##Copyright 2018 Thomas Paviot (tpaviot@gmail.com)
##
##This file is part of pythonOCC.
##
##pythonOCC is free software: you can redistribute it and/or modify
##it under the terms of the GNU Lesser General Public License as published by
##the Free Software Foundation, either version 3 of the License, or
##(at your option) any later version.
##
##pythonOCC is distributed in the hope that it will be useful,
##but WITHOUT ANY WARRANTY; without even the implied warranty of
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
##GNU Lesser General Public License for more details.
##
##You should have received a copy of the GNU Lesser General Public License
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import os
import sys
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeTorus
from OCC.Core.BRepTools import breptools_Write
from OCC.Display.SimpleGui import init_display
from OCC.Extend.DataExchange import read_stl_file
def mesh_shape(a_topods_shape):
"""Takes a BRep filename (extension .brep) and returns
a topods_shp ready to be displayed
"""
# dump the geometry to a brep file
breptools_Write(a_topods_shape, "shape.brep")
# create the gmesh file
gmsh_geo_file_content = """SetFactory("OpenCASCADE");
Mesh.CharacteristicLengthMin = 1;
Mesh.CharacteristicLengthMax = 5;
a() = ShapeFromFile("shape.brep");
"""
with open("shape.geo", "w") as gmsh_geo_file:
gmsh_geo_file.write(gmsh_geo_file_content)
# call gmsh
gmsh_success = os.system("gmsh shape.geo -2 -o shape.stl -format stl")
if gmsh_success != 0 and os.path.isfile("shape.stl"):
return read_stl_file("shape.stl")
print("Be sure gmsh is in your PATH")
sys.exit()
# First example, a simple torus
torus_shp = BRepPrimAPI_MakeTorus(40.0, 10.0).Shape()
torus_mesh_shp = mesh_shape(torus_shp)
display, start_display, add_menu, add_function_to_menu = init_display()
display.DisplayShape(torus_mesh_shp, update=True)
start_display()