Salome HOME
adding python scripts to illustrate the way to mesh a shape giving different hypothes...
[modules/smesh.git] / src / SMESH_SWIG / SMESH_demo_tetra.py
1 #
2 # Tetrahedrization of a geometry (box minus a inner cylinder).
3 # Hypothesis and algorithms for the mesh generation are global
4 #
5
6 import salome
7 from salome import sg
8
9 import geompy
10
11 import SMESH
12 import smeshpy
13
14 # -----------------------------------------------------------------------------
15
16 geom = geompy.geom
17 myBuilder = geompy.myBuilder
18
19 ShapeTypeShell     = 3
20 ShapeTypeFace      = 4
21 ShapeTypeEdge      = 6
22
23 # ---- define a boxe and a cylinder
24 x0 = -1.
25 y0 = -1.
26 z0 = -1.
27
28 x1 = 1.
29 y1 = 1.
30 z1 = 1.
31
32 P0 = geom.MakePointStruct(0.,-1.,0.)
33 P1 = geom.MakePointStruct(0.,1.,0.)
34 Vect = geom.MakeDirection(P1)
35 radius = 0.5
36 height = 2.
37
38 boxe = geompy.MakeBox(x0,y0,z0,x1,y1,z1)
39
40 cylinder = geompy.MakeCylinder(P0,Vect,radius,height)
41
42 shape = geompy.MakeBoolean(boxe,cylinder,2)
43 idshape = geompy.addToStudy(shape,"shape")
44
45 print ""
46
47 print "Analysis of the shape :"
48 subShellList=geompy.SubShapeAll(shape,ShapeTypeShell)
49 subFaceList=geompy.SubShapeAll(shape,ShapeTypeFace)
50 subEdgeList=geompy.SubShapeAll(shape,ShapeTypeEdge)
51
52 print "number of Shells in the shape : ",len(subShellList)
53 print "number of Faces in the shape : ",len(subFaceList)
54 print "number of Edges in the shape : ",len(subEdgeList)
55
56 # ---- launch SMESH
57
58 smeshgui = salome.ImportComponentGUI("SMESH")
59 smeshgui.Init(salome.myStudyId)
60
61 gen=smeshpy.smeshpy()
62
63 # ---- create Hypothesis
64
65 print "-------------------------- create Hypothesis"
66
67 print "-------------------------- NumberOfSegments"
68
69 numberOfSegments = 10
70
71 hypothesis=gen.CreateHypothesis("NumberOfSegments")
72 hypNbSeg=hypothesis._narrow(SMESH.SMESH_NumberOfSegments)
73 hypNbSeg.SetNumberOfSegments(numberOfSegments)
74 hypNbSegID = hypNbSeg.GetId()
75 print hypNbSeg.GetName()
76 print hypNbSegID
77 print hypNbSeg.GetNumberOfSegments()
78
79 idseg = smeshgui.AddNewHypothesis( salome.orb.object_to_string(hypNbSeg) )
80 smeshgui.SetName(idseg, "NumberOfSegments")
81
82 print "-------------------------- LengthFromEdges"
83
84 hypothesis=gen.CreateHypothesis("LengthFromEdges")
85 hypLengthFromEdge=hypothesis._narrow(SMESH.SMESH_LengthFromEdges)
86 hypLengthFromEdgeID = hypLengthFromEdge.GetId()
87 print hypLengthFromEdge.GetName()
88 print hypLengthFromEdgeID
89
90 idlenfromedge = smeshgui.AddNewHypothesis( salome.orb.object_to_string(hypLengthFromEdge) )
91 smeshgui.SetName(idlenfromedge, "LengthFromEdge")
92
93 print "-------------------------- MaxElementVolume"
94
95 maxElementVolume = 0.5
96
97 hypothesis=gen.CreateHypothesis("MaxElementVolume")
98 hypVolume=hypothesis._narrow(SMESH.SMESH_MaxElementVolume)
99 hypVolume.SetMaxElementVolume(maxElementVolume)
100 print hypVolume.GetName()
101 print hypVolume.GetId()
102 print hypVolume.GetMaxElementVolume()
103
104 idvolume = smeshgui.AddNewHypothesis( salome.orb.object_to_string(hypVolume) )
105 smeshgui.SetName(idvolume, "MaxElementVolume")
106
107 # ---- create Algorithms
108
109 print "-------------------------- create Algorithms"
110
111 print "-------------------------- Regular_1D"
112
113 hypothesis=gen.CreateHypothesis("Regular_1D")
114 regular1D = hypothesis._narrow(SMESH.SMESH_Regular_1D)
115 regularID = smeshgui.AddNewAlgorithms( salome.orb.object_to_string(regular1D) )
116 smeshgui.SetName(regularID, "Wire Discretisation")
117
118 print "-------------------------- MEFISTO_2D"
119
120 hypothesis=gen.CreateHypothesis("MEFISTO_2D")
121 mefisto2D = hypothesis._narrow(SMESH.SMESH_MEFISTO_2D)
122 mefistoID = smeshgui.AddNewAlgorithms( salome.orb.object_to_string(mefisto2D) )
123 smeshgui.SetName(mefistoID, "MEFISTO_2D")
124
125 print "-------------------------- NETGEN_3D"
126
127 hypothesis=gen.CreateHypothesis("NETGEN_3D")
128 netgen3D = hypothesis._narrow(SMESH.SMESH_NETGEN_3D)
129 netgenID = smeshgui.AddNewAlgorithms( salome.orb.object_to_string(netgen3D) )
130 smeshgui.SetName(netgenID, "NETGEN_3D")
131
132 # ---- init a Mesh with the shell
133
134 mesh=gen.Init(idshape)
135 idmesh = smeshgui.AddNewMesh( salome.orb.object_to_string(mesh) )
136 smeshgui.SetName(idmesh, "MeshShape")
137 smeshgui.SetShape(idshape, idmesh)
138
139 # ---- add hypothesis to flight
140
141 print "-------------------------- add hypothesis to the shape"
142
143 ret=mesh.AddHypothesis(shape,regular1D)
144 print ret
145 ret=mesh.AddHypothesis(shape,hypNbSeg)
146 print ret
147 ret=mesh.AddHypothesis(shape,mefisto2D)
148 print ret
149 ret=mesh.AddHypothesis(shape,hypLengthFromEdge)
150 print ret
151 ret=mesh.AddHypothesis(shape,netgen3D)
152 print ret
153 ret=mesh.AddHypothesis(shape,hypVolume)
154 print ret
155
156 smeshgui.SetAlgorithms( idmesh, regularID)
157 smeshgui.SetHypothesis( idmesh, idseg )
158 smeshgui.SetAlgorithms( idmesh, mefistoID )
159 smeshgui.SetHypothesis( idmesh, idlenfromedge)
160 smeshgui.SetAlgorithms( idmesh, netgenID )
161 smeshgui.SetHypothesis( idmesh, idvolume )
162
163 sg.updateObjBrowser(1)
164
165
166 print "-------------------------- compute the mesh of the shape"
167 ret=gen.Compute(mesh,idshape)
168 print ret
169 if ret != 0:
170     log=mesh.GetLog(0) # no erase trace
171     for linelog in log:
172         print linelog
173 else:
174     print "probleme when computing the mesh"
175
176 sg.updateObjBrowser(1)