Salome HOME
DCQ:prepare 2.0.0
[modules/smesh.git] / src / SMESH_SWIG / SMESH_demo_tetra2.py
1 #
2 # Tetrahedrization of a geometry (box minus a inner cylinder).
3 # Hypothesis and algorithms for the mesh generation are not
4 # global: the mesh of some edges is thinner.
5 #
6
7 import salome
8 from salome import sg
9
10 import geompy
11
12 import SMESH
13 import smeshpy
14
15 # -----------------------------------------------------------------------------
16
17 geom = geompy.geom
18 myBuilder = geompy.myBuilder
19
20 ShapeTypeShell     = 3
21 ShapeTypeFace      = 4
22 ShapeTypeEdge      = 6
23
24 # ---- define a boxe and a cylinder
25 x0 = -1.
26 y0 = -1.
27 z0 = -1.
28
29 x1 = 1.
30 y1 = 1.
31 z1 = 1.
32
33 P0 = geom.MakePointStruct(0.,-1.,0.)
34 P1 = geom.MakePointStruct(0.,1.,0.)
35 Vect = geom.MakeDirection(P1)
36 radius = 0.5
37 height = 2.
38
39 boxe = geompy.MakeBox(x0,y0,z0,x1,y1,z1)
40
41 cylinder = geompy.MakeCylinder(P0,Vect,radius,height)
42
43 shape = geompy.MakeBoolean(boxe,cylinder,2)
44 idshape = geompy.addToStudy(shape,"shape")
45
46 print "Analysis of the shape :"
47 subShellList=geompy.SubShapeAllSorted(shape,ShapeTypeShell)
48 subFaceList=geompy.SubShapeAllSorted(shape,ShapeTypeFace)
49 subEdgeList=geompy.SubShapeAllSorted(shape,ShapeTypeEdge)
50
51 print "number of Shells in the shape : ",len(subShellList)
52 print "number of Faces in the shape : ",len(subFaceList)
53 print "number of Edges in the shape : ",len(subEdgeList)
54
55 idSubEdge = []
56 for k in range(len(subEdgeList)):
57     idSubEdge.append(geompy.addToStudyInFather(shape,subEdgeList[k],"SubEdge"+str(k)))
58
59 circle1 = subEdgeList[5]
60 idCircle1 = geompy.addToStudyInFather(shape,circle1,"circle1")
61
62 circle2 = subEdgeList[9]
63 idCircle2 = geompy.addToStudyInFather(shape,circle2,"circle2")
64
65 height = subEdgeList[7]
66 idHeight = geompy.addToStudyInFather(shape,height,"height")
67
68 # ---- launch SMESH
69
70 smeshgui = salome.ImportComponentGUI("SMESH")
71 smeshgui.Init(salome.myStudyId)
72
73 gen=smeshpy.smeshpy()
74
75 # ---- create Hypothesis
76
77 print "-------------------------- create Hypothesis"
78
79 print "-------------------------- NumberOfSegments Edge of the boxe"
80
81 numberOfSegmentsBoxe = 10
82
83 hypothesis=gen.CreateHypothesis("NumberOfSegments")
84 hypNbSegBoxe=hypothesis._narrow(SMESH.SMESH_NumberOfSegments)
85 hypNbSegBoxe.SetNumberOfSegments(numberOfSegmentsBoxe)
86 hypNbSegID = hypNbSegBoxe.GetId()
87 print hypNbSegBoxe.GetName()
88 print hypNbSegID
89 print hypNbSegBoxe.GetNumberOfSegments()
90
91 idSegBoxe = smeshgui.AddNewHypothesis(salome.orb.object_to_string(hypNbSegBoxe))
92 smeshgui.SetName(idSegBoxe, "NumberOfSegmentsBoxe")
93
94 print "-------------------------- NumberOfSegments Edge of the cylinder"
95
96 numberOfSegmentsCylinder = 40
97
98 hypothesis=gen.CreateHypothesis("NumberOfSegments")
99 hypNbSegCylinder=hypothesis._narrow(SMESH.SMESH_NumberOfSegments)
100 hypNbSegCylinder.SetNumberOfSegments(numberOfSegmentsCylinder)
101 hypNbSegID = hypNbSegCylinder.GetId()
102 print hypNbSegCylinder.GetName()
103 print hypNbSegID
104 print hypNbSegCylinder.GetNumberOfSegments()
105
106 idSegCylinder = smeshgui.AddNewHypothesis(salome.orb.object_to_string(hypNbSegCylinder))
107 smeshgui.SetName(idSegCylinder, "NumberOfSegmentsCylinder")
108
109 print "-------------------------- LengthFromEdges"
110
111 hypothesis=gen.CreateHypothesis("LengthFromEdges")
112 hypLengthFromEdge=hypothesis._narrow(SMESH.SMESH_LengthFromEdges)
113 hypLengthFromEdgeID = hypLengthFromEdge.GetId()
114 print hypLengthFromEdge.GetName()
115 print hypLengthFromEdgeID
116
117 idlenfromedge = smeshgui.AddNewHypothesis( salome.orb.object_to_string(hypLengthFromEdge) )
118 smeshgui.SetName(idlenfromedge, "LengthFromEdge")
119
120 print "-------------------------- MaxElementVolume"
121
122 maxElementVolume = 0.5
123
124 hypothesis=gen.CreateHypothesis("MaxElementVolume")
125 hypVolume=hypothesis._narrow(SMESH.SMESH_MaxElementVolume)
126 hypVolume.SetMaxElementVolume(maxElementVolume)
127 print hypVolume.GetName()
128 print hypVolume.GetId()
129 print hypVolume.GetMaxElementVolume()
130
131 idvolume = smeshgui.AddNewHypothesis( salome.orb.object_to_string(hypVolume) )
132 smeshgui.SetName(idvolume, "MaxElementVolume")
133
134 # ---- create Algorithms
135
136 print "-------------------------- create Algorithms"
137
138 print "-------------------------- Regular_1D"
139
140 hypothesis=gen.CreateHypothesis("Regular_1D")
141 regular1D = hypothesis._narrow(SMESH.SMESH_Regular_1D)
142 regularID = smeshgui.AddNewAlgorithms( salome.orb.object_to_string(regular1D) )
143 smeshgui.SetName(regularID, "Wire Discretisation")
144
145 print "-------------------------- MEFISTO_2D"
146
147 hypothesis=gen.CreateHypothesis("MEFISTO_2D")
148 mefisto2D = hypothesis._narrow(SMESH.SMESH_MEFISTO_2D)
149 mefistoID = smeshgui.AddNewAlgorithms( salome.orb.object_to_string(mefisto2D) )
150 smeshgui.SetName(mefistoID, "MEFISTO_2D")
151
152 print "-------------------------- NETGEN_3D"
153
154 hypothesis=gen.CreateHypothesis("NETGEN_3D")
155 netgen3D = hypothesis._narrow(SMESH.SMESH_NETGEN_3D)
156 netgenID = smeshgui.AddNewAlgorithms( salome.orb.object_to_string(netgen3D) )
157 smeshgui.SetName(netgenID, "NETGEN_3D")
158
159 # ---- init a Mesh with the shell
160
161 mesh=gen.Init(idshape)
162 idmesh = smeshgui.AddNewMesh( salome.orb.object_to_string(mesh) )
163 smeshgui.SetName(idmesh, "MeshShape")
164 smeshgui.SetShape(idshape, idmesh)
165
166 # ---- add hypothesis to flight
167
168 print "-------------------------- add hypothesis to the shape"
169
170 ret=mesh.AddHypothesis(shape,regular1D)
171 print ret
172 ret=mesh.AddHypothesis(shape,hypNbSegBoxe)
173 print ret
174 ret=mesh.AddHypothesis(shape,mefisto2D)
175 print ret
176 ret=mesh.AddHypothesis(shape,hypLengthFromEdge)
177 print ret
178 ret=mesh.AddHypothesis(shape,netgen3D)
179 print ret
180 ret=mesh.AddHypothesis(shape,hypVolume)
181 print ret
182
183 print "-------------------------- set algoritms"
184
185 smeshgui.SetAlgorithms( idmesh, regularID)
186 smeshgui.SetHypothesis( idmesh, idSegBoxe )
187 smeshgui.SetAlgorithms( idmesh, mefistoID )
188 smeshgui.SetHypothesis( idmesh, idlenfromedge)
189 smeshgui.SetAlgorithms( idmesh, netgenID )
190 smeshgui.SetHypothesis( idmesh, idvolume )
191
192 print "-------------------------- add hypothesis to the first circle"
193
194 subMeshEdgeCircle1 = mesh.GetElementsOnShape(circle1)
195 retCircle1 = mesh.AddHypothesis(circle1,hypNbSegCylinder)
196 print " add hyp to C1 ", retCircle1
197
198 idsmCircle1 = smeshgui.AddSubMeshOnShape(
199     idmesh,idCircle1,salome.orb.object_to_string(subMeshEdgeCircle1),
200     ShapeTypeEdge)
201
202 smeshgui.SetName(idsmCircle1, "SubMeshEdgeCircle1")
203 smeshgui.SetHypothesis(idsmCircle1, idSegCylinder)
204
205 print "-------------------------- add hypothesis to the second circle"
206
207 subMeshEdgeCircle2 = mesh.GetElementsOnShape(circle2)
208 retCircle2 = mesh.AddHypothesis(circle2,hypNbSegCylinder)
209 print " add hyp to C2 ", retCircle2
210
211 idsmCircle2 = smeshgui.AddSubMeshOnShape(
212     idmesh,idCircle2,salome.orb.object_to_string(subMeshEdgeCircle2),
213     ShapeTypeEdge)
214
215 smeshgui.SetName(idsmCircle2, "SubMeshEdgeCircle2")
216 smeshgui.SetHypothesis(idsmCircle2, idSegCylinder)
217
218 print "-------------------------- add hypothesis to the height of the cylinder"
219
220 subMeshEdgeHeight = mesh.GetElementsOnShape(height)
221 retHeight = mesh.AddHypothesis(height,hypNbSegCylinder)
222 print " add hyp to H ", retHeight
223
224 idsmHeight = smeshgui.AddSubMeshOnShape(
225     idmesh,idHeight,salome.orb.object_to_string(subMeshEdgeHeight),
226     ShapeTypeEdge)
227
228 smeshgui.SetName(idsmHeight, "SubMeshEdgeHeight")
229 smeshgui.SetHypothesis(idsmHeight, idSegCylinder)
230
231 sg.updateObjBrowser(1)
232
233 print "-------------------------- compute the mesh of the shape"
234 ret=gen.Compute(mesh,idshape)
235 print ret
236 if ret != 0:
237     log=mesh.GetLog(0) # no erase trace
238     for linelog in log:
239         print linelog
240 else:
241     print "probleme when computing the mesh"
242
243 sg.updateObjBrowser(1)