Salome HOME
36f34541af5506808c9b2f6e4673f0574bfd31e5
[modules/smesh.git] / src / SMESH_SWIG / smesh.py
1 #  Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 #
4 #  This library is free software; you can redistribute it and/or
5 #  modify it under the terms of the GNU Lesser General Public
6 #  License as published by the Free Software Foundation; either
7 #  version 2.1 of the License.
8 #
9 #  This library is distributed in the hope that it will be useful,
10 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 #  Lesser General Public License for more details.
13 #
14 #  You should have received a copy of the GNU Lesser General Public
15 #  License along with this library; if not, write to the Free Software
16 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 #  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
19 #
20 #  File   : smesh.py
21 #  Author : Francis KLOSS, OCC
22 #  Module : SMESH
23
24 """
25  \namespace smesh
26  \brief Module smesh
27 """
28
29 import salome
30 import geompy
31 import StdMeshers
32 import SMESH
33
34 # Public variables
35 # ----------------
36
37 REGULAR = 1
38 PYTHON  = 2
39
40 NETGEN  = 3
41 GHS3D   = 4
42
43 smesh = salome.lcc.FindOrLoadComponent("FactoryServer", "SMESH")
44 smesh.SetCurrentStudy(salome.myStudy)
45
46 # Private functions
47 # -----------------
48
49 def GetName(obj):
50     ior  = salome.orb.object_to_string(obj)
51     sobj = salome.myStudy.FindObjectIOR(ior)
52     attr = sobj.FindAttribute("AttributeName")[1]
53     return attr.Value()
54
55 def SetName(obj, name):
56     ior  = salome.orb.object_to_string(obj)
57     sobj = salome.myStudy.FindObjectIOR(ior)
58     attr = sobj.FindAttribute("AttributeName")[1]
59     attr.SetValue(name)
60
61 # Algorithms and hypothesis
62 # =========================
63
64 # Private class: Mesh_Algorithm
65 # -----------------------------
66
67 class Mesh_Algorithm:
68     """
69     Mother class to define algorithm, recommended to don't use directly
70     """
71
72     mesh = 0
73     geom = 0
74     subm = 0
75
76     def GetSubMesh(self):
77         """
78          If the algorithm is global, return 0
79          else return the submesh associated to this algorithm
80         """
81         return self.subm
82
83     def Create(self, mesh, geom, hypo, so="libStdMeshersEngine.so"):
84         """
85          Private method
86         """
87         self.mesh = mesh
88         piece = mesh.geom
89         if geom==0:
90             self.geom = piece
91             name = GetName(piece)
92         else:
93             self.geom = geom
94             name = geompy.SubShapeName(geom, piece)
95             geompy.addToStudyInFather(piece, geom, name)
96             self.subm = mesh.mesh.GetSubMesh(geom, hypo)
97
98         algo = smesh.CreateHypothesis(hypo, so)
99         SetName(algo, name + "/" + hypo)
100         mesh.mesh.AddHypothesis(self.geom, algo)
101
102     def Hypothesis(self, hyp, args=[], so="libStdMeshersEngine.so"):
103         """
104          Private method
105         """
106         hypo = smesh.CreateHypothesis(hyp, so)
107         a = ""
108         s = "="
109         i = 0
110         n = len(args)
111         while i<n:
112             a = a + s + str(args[i])
113             s = ","
114             i = i + 1
115         SetName(hypo, GetName(self.geom) + "/" + hyp + a)
116         self.mesh.mesh.AddHypothesis(self.geom, hypo)
117         return hypo
118
119 # Public class: Mesh_Segment
120 # --------------------------
121
122 class Mesh_Segment(Mesh_Algorithm):
123     """
124     Class to define a segment 1D algorithm for discretization
125     """
126
127     def __init__(self, mesh, geom=0):
128         """
129          Private constructor
130         """
131         self.Create(mesh, geom, "Regular_1D")
132
133     def LocalLength(self, l):
134         """
135          Define "LocalLength" hypothesis to cut an edge in several segments with the same length
136          \param l for the length of segments that cut an edge
137         """
138         hyp = self.Hypothesis("LocalLength", [l])
139         hyp.SetLength(l)
140         return hyp
141
142     def NumberOfSegments(self, n, s=[]):
143         """
144          Define "NumberOfSegments" hypothesis to cut an edge in several fixed number of segments
145          \param n for the number of segments that cut an edge
146          \param s for the scale factor (optional)
147         """
148         if s == []:
149             hyp = self.Hypothesis("NumberOfSegments", [n])
150         else:
151             hyp = self.Hypothesis("NumberOfSegments", [n,s])
152             hyp.SetScaleFactor(s)
153         hyp.SetNumberOfSegments(n)
154         return hyp
155
156     def Arithmetic1D(self, start, end):
157         """
158          Define "Arithmetic1D" hypothesis to cut an edge in several segments with arithmetic length increasing
159          \param start for the length of the first segment
160          \param end   for the length of the last  segment
161         """
162         hyp = self.Hypothesis("Arithmetic1D", [start, end])
163         hyp.SetLength(start, 1)
164         hyp.SetLength(end  , 0)
165         return hyp
166
167     def StartEndLength(self, start, end):
168         """
169          Define "StartEndLength" hypothesis to cut an edge in several segments with geometric length increasing
170          \param start for the length of the first segment
171          \param end   for the length of the last  segment
172         """
173         hyp = self.Hypothesis("StartEndLength", [start, end])
174         hyp.SetLength(start, 1)
175         hyp.SetLength(end  , 0)
176         return hyp
177
178     def Deflection1D(self, d):
179         """
180          Define "Deflection1D" hypothesis
181          \param d for the deflection
182         """
183         hyp = self.Hypothesis("Deflection1D", [d])
184         hyp.SetDeflection(d)
185         return hyp
186
187     def Propagation(self):
188         """
189          Define "Propagation" hypothesis that propagate all other hypothesis on all others edges that are in
190          the opposite side in the case of quadrangular faces
191         """
192         return self.Hypothesis("Propagation")
193
194 # Public class: Mesh_Segment_Python
195 # ---------------------------------
196
197 class Mesh_Segment_Python(Mesh_Segment):
198     """
199     Class to define a segment 1D algorithm for discretization with python function
200     """
201
202     def __init__(self, mesh, geom=0):
203         """
204          Private constructor
205         """
206         import Python1dPlugin
207         self.Create(mesh, geom, "Python_1D", "libPython1dEngine.so")
208
209     def PythonSplit1D(self, n, func):
210         """
211          Define "PythonSplit1D" hypothesis based on the Erwan Adam patch, awaiting equivalent SALOME functionality
212          \param n for the number of segments that cut an edge
213          \param func for the python function that calculate the length of all segments
214         """
215         hyp = self.Hypothesis("PythonSplit1D", [n], "libPython1dEngine.so")
216         hyp.SetNumberOfSegments(n)
217         hyp.SetPythonLog10RatioFunction(func)
218         return hyp
219
220 # Public class: Mesh_Triangle
221 # ---------------------------
222
223 class Mesh_Triangle(Mesh_Algorithm):
224     """
225     Class to define a triangle 2D algorithm
226     """
227
228     def __init__(self, mesh, geom=0):
229         """
230          Private constructor
231         """
232         self.Create(mesh, geom, "MEFISTO_2D")
233
234     def MaxElementArea(self, area):
235         """
236          Define "MaxElementArea" hypothesis to give the maximun area of each triangles
237          \param area for the maximum area of each triangles
238         """
239         hyp = self.Hypothesis("MaxElementArea", [area])
240         hyp.SetMaxElementArea(area)
241         return hyp
242
243     def LengthFromEdges(self):
244         """
245          Define "LengthFromEdges" hypothesis to build triangles based on the length of the edges taken from the wire
246         """
247         return self.Hypothesis("LengthFromEdges")
248
249 # Public class: Mesh_Quadrangle
250 # -----------------------------
251
252 class Mesh_Quadrangle(Mesh_Algorithm):
253     """
254     Class to define a quadrangle 2D algorithm
255     """
256
257     def __init__(self, mesh, geom=0):
258         """
259          Private constructor
260         """
261         self.Create(mesh, geom, "Quadrangle_2D")
262
263 # Public class: Mesh_Tetrahedron
264 # ------------------------------
265
266 class Mesh_Tetrahedron(Mesh_Algorithm):
267     """
268     Class to define a tetrahedron 3D algorithm
269     """
270
271     def __init__(self, mesh, algo, geom=0):
272         """
273          Private constructor
274         """
275         if algo == NETGEN:
276             self.Create(mesh, geom, "NETGEN_3D", "libNETGENEngine.so")
277         elif algo == GHS3D:
278             import GHS3DPlugin
279             self.Create(mesh, geom, "GHS3D_3D" , "libGHS3DEngine.so")
280
281     def MaxElementVolume(self, vol):
282         """
283          Define "MaxElementVolume" hypothesis to give the maximun volume of each tetrahedral
284          \param vol for the maximum volume of each tetrahedral
285         """
286         hyp = self.Hypothesis("MaxElementVolume", [vol])
287         hyp.SetMaxElementVolume(vol)
288         return hyp
289
290 # Public class: Mesh_Hexahedron
291 # ------------------------------
292
293 class Mesh_Hexahedron(Mesh_Algorithm):
294     """
295     Class to define a hexahedron 3D algorithm
296     """
297
298     def __init__(self, mesh, geom=0):
299         """
300          Private constructor
301         """
302         self.Create(mesh, geom, "Hexa_3D")
303
304 # Public class: Mesh
305 # ==================
306
307 class Mesh:
308     """
309     Class to define a mesh
310     """
311
312     geom = 0
313     mesh = 0
314
315     def __init__(self, geom, name=0):
316         """
317          Constructor
318
319          Creates mesh on the shape \a geom,
320          sets GUI name of this mesh to \a name.
321          \param geom Shape to be meshed
322          \param name Study name of the mesh
323         """
324         self.geom = geom
325         self.mesh = smesh.CreateMesh(geom)
326         if name == 0:
327             SetName(self.mesh, GetName(geom))
328         else:
329             SetName(self.mesh, name)
330
331     def GetMesh(self):
332         """
333          Method that returns the mesh
334         """
335         return self.mesh
336
337     def GetShape(self):
338         """
339          Method that returns the shape associated to the mesh
340         """
341         return self.geom
342
343     def Segment(self, algo=REGULAR, geom=0):
344         """
345          Creates a segment discretization 1D algorithm.
346          If the optional \a algo parameter is not sets, this algorithm is REGULAR.
347          If the optional \a geom parameter is not sets, this algorithm is global.
348          Otherwise, this algorithm define a submesh based on \a geom subshape.
349          \param algo values are smesh.REGULAR or smesh.PYTHON for discretization via python function
350          \param geom If defined, subshape to be meshed
351         """
352         if algo == REGULAR:
353             return Mesh_Segment(self, geom)
354         elif algo == PYTHON:
355             return Mesh_Segment_Python(self, geom)
356         else:
357             return Mesh_Segment(self, algo)
358
359     def Triangle(self, geom=0):
360         """
361          Creates a triangle 2D algorithm for faces.
362          If the optional \a geom parameter is not sets, this algorithm is global.
363          Otherwise, this algorithm define a submesh based on \a geom subshape.
364          \param geom If defined, subshape to be meshed
365         """
366         return Mesh_Triangle(self, geom)
367
368     def Quadrangle(self, geom=0):
369         """
370          Creates a quadrangle 2D algorithm for faces.
371          If the optional \a geom parameter is not sets, this algorithm is global.
372          Otherwise, this algorithm define a submesh based on \a geom subshape.
373          \param geom If defined, subshape to be meshed
374         """
375         return Mesh_Quadrangle(self, geom)
376
377     def Tetrahedron(self, algo, geom=0):
378         """
379          Creates a tetrahedron 3D algorithm for solids.
380          The parameter \a algo permits to choice the algorithm: NETGEN or GHS3D
381          If the optional \a geom parameter is not sets, this algorithm is global.
382          Otherwise, this algorithm define a submesh based on \a geom subshape.
383          \param algo values are: smesh.NETGEN, smesh.GHS3D
384          \param geom If defined, subshape to be meshed
385         """
386         return Mesh_Tetrahedron(self, algo, geom)
387
388     def Hexahedron(self, geom=0):
389         """
390          Creates a hexahedron 3D algorithm for solids.
391          If the optional \a geom parameter is not sets, this algorithm is global.
392          Otherwise, this algorithm define a submesh based on \a geom subshape.
393          \param geom If defined, subshape to be meshed
394         """
395         return Mesh_Hexahedron(self, geom)
396
397     def Compute(self):
398         """
399          Compute the mesh and return the status of the computation
400         """
401         b = smesh.Compute(self.mesh, self.geom)
402         if salome.sg.hasDesktop():
403             salome.sg.updateObjBrowser(1)
404         return b
405
406     def Group(self, grp, name=""):
407         """
408          Create a mesh group based on geometric object \a grp
409          and give a \a name, if this parameter is not defined
410          the name is the same as the geometric group name
411          \param grp  is a geometric group
412          \param name is the name of the mesh group
413         """
414         if name == "":
415             name = grp.GetName()
416         tgeo = geompy.GetType(grp)
417         if tgeo == geompy.ShapeType["VERTEX"]:
418             type = SMESH.NODE
419         elif tgeo == geompy.ShapeType["EDGE"]:
420             type = SMESH.EDGE
421         elif tgeo == geompy.ShapeType["FACE"]:
422             type = SMESH.FACE
423         elif tgeo == geompy.ShapeType["SOLID"]:
424             type = SMESH.VOLUME
425         return self.mesh.CreateGroupFromGEOM(type, name, grp)
426
427     def ExportToMED(self, f, version, opt=0):
428         """
429          Export the mesh in a file with the MED format and choice the \a version of MED format
430          \param f is the file name
431          \param version values are smesh.MED_V2_1, smesh.MED_V2_2
432         """
433         self.mesh.ExportToMED(f, opt, version)
434
435     def ExportMED(self, f, opt=0):
436         """
437          Export the mesh in a file with the MED format
438          \param f is the file name
439         """
440         self.mesh.ExportMED(f, opt)
441
442     def ExportDAT(self, f):
443         """
444          Export the mesh in a file with the DAT format
445          \param f is the file name
446         """
447         self.mesh.ExportDAT(f)
448
449     def ExportUNV(self, f):
450         """
451          Export the mesh in a file with the UNV format
452          \param f is the file name
453         """
454         self.mesh.ExportUNV(f)
455
456     def ExportSTL(self, f, ascii=1):
457         """
458          Export the mesh in a file with the STL format
459          \param f is the file name
460          \param ascii defined the kind of file contents
461         """
462         self.mesh.ExportSTL(f, ascii)