1 # Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
18 # See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 # Author : Francis KLOSS, OCC
43 smesh = salome.lcc.FindOrLoadComponent("FactoryServer", "SMESH")
44 smesh.SetCurrentStudy(salome.myStudy)
52 ior = salome.orb.object_to_string(obj)
53 sobj = salome.myStudy.FindObjectIOR(ior)
57 attr = sobj.FindAttribute("AttributeName")[1]
60 def SetName(obj, name):
61 ior = salome.orb.object_to_string(obj)
62 sobj = salome.myStudy.FindObjectIOR(ior)
63 attr = sobj.FindAttribute("AttributeName")[1]
66 # Algorithms and hypothesis
67 # =========================
69 # Private class: Mesh_Algorithm
70 # -----------------------------
74 Mother class to define algorithm, recommended to don't use directly
83 If the algorithm is global, return 0
84 else return the submesh associated to this algorithm
88 def Create(self, mesh, geom, hypo, so="libStdMeshersEngine.so"):
93 raise RuntimeError, "Attemp to create " + hypo + " algoritm on None shape"
103 name = geompy.SubShapeName(geom, piece)
104 geompy.addToStudyInFather(piece, geom, name)
105 self.subm = mesh.mesh.GetSubMesh(geom, hypo)
107 algo = smesh.CreateHypothesis(hypo, so)
108 SetName(algo, name + "/" + hypo)
109 mesh.mesh.AddHypothesis(self.geom, algo)
111 def Hypothesis(self, hyp, args=[], so="libStdMeshersEngine.so"):
115 hypo = smesh.CreateHypothesis(hyp, so)
121 a = a + s + str(args[i])
124 SetName(hypo, GetName(self.geom) + "/" + hyp + a)
125 self.mesh.mesh.AddHypothesis(self.geom, hypo)
128 # Public class: Mesh_Segment
129 # --------------------------
131 class Mesh_Segment(Mesh_Algorithm):
133 Class to define a segment 1D algorithm for discretization
136 def __init__(self, mesh, geom=0):
140 self.Create(mesh, geom, "Regular_1D")
142 def LocalLength(self, l):
144 Define "LocalLength" hypothesis to cut an edge in several segments with the same length
145 \param l for the length of segments that cut an edge
147 hyp = self.Hypothesis("LocalLength", [l])
151 def NumberOfSegments(self, n, s=[]):
153 Define "NumberOfSegments" hypothesis to cut an edge in several fixed number of segments
154 \param n for the number of segments that cut an edge
155 \param s for the scale factor (optional)
158 hyp = self.Hypothesis("NumberOfSegments", [n])
160 hyp = self.Hypothesis("NumberOfSegments", [n,s])
161 hyp.SetScaleFactor(s)
162 hyp.SetNumberOfSegments(n)
165 def Arithmetic1D(self, start, end):
167 Define "Arithmetic1D" hypothesis to cut an edge in several segments with arithmetic length increasing
168 \param start for the length of the first segment
169 \param end for the length of the last segment
171 hyp = self.Hypothesis("Arithmetic1D", [start, end])
172 hyp.SetLength(start, 1)
173 hyp.SetLength(end , 0)
176 def StartEndLength(self, start, end):
178 Define "StartEndLength" hypothesis to cut an edge in several segments with geometric length increasing
179 \param start for the length of the first segment
180 \param end for the length of the last segment
182 hyp = self.Hypothesis("StartEndLength", [start, end])
183 hyp.SetLength(start, 1)
184 hyp.SetLength(end , 0)
187 def Deflection1D(self, d):
189 Define "Deflection1D" hypothesis
190 \param d for the deflection
192 hyp = self.Hypothesis("Deflection1D", [d])
196 def Propagation(self):
198 Define "Propagation" hypothesis that propagate all other hypothesis on all others edges that are in
199 the opposite side in the case of quadrangular faces
201 return self.Hypothesis("Propagation")
203 # Public class: Mesh_Segment_Python
204 # ---------------------------------
206 class Mesh_Segment_Python(Mesh_Segment):
208 Class to define a segment 1D algorithm for discretization with python function
211 def __init__(self, mesh, geom=0):
215 import Python1dPlugin
216 self.Create(mesh, geom, "Python_1D", "libPython1dEngine.so")
218 def PythonSplit1D(self, n, func):
220 Define "PythonSplit1D" hypothesis based on the Erwan Adam patch, awaiting equivalent SALOME functionality
221 \param n for the number of segments that cut an edge
222 \param func for the python function that calculate the length of all segments
224 hyp = self.Hypothesis("PythonSplit1D", [n], "libPython1dEngine.so")
225 hyp.SetNumberOfSegments(n)
226 hyp.SetPythonLog10RatioFunction(func)
229 # Public class: Mesh_Triangle
230 # ---------------------------
232 class Mesh_Triangle(Mesh_Algorithm):
234 Class to define a triangle 2D algorithm
237 def __init__(self, mesh, geom=0):
241 self.Create(mesh, geom, "MEFISTO_2D")
243 def MaxElementArea(self, area):
245 Define "MaxElementArea" hypothesis to give the maximun area of each triangles
246 \param area for the maximum area of each triangles
248 hyp = self.Hypothesis("MaxElementArea", [area])
249 hyp.SetMaxElementArea(area)
252 def LengthFromEdges(self):
254 Define "LengthFromEdges" hypothesis to build triangles based on the length of the edges taken from the wire
256 return self.Hypothesis("LengthFromEdges")
258 # Public class: Mesh_Quadrangle
259 # -----------------------------
261 class Mesh_Quadrangle(Mesh_Algorithm):
263 Class to define a quadrangle 2D algorithm
266 def __init__(self, mesh, geom=0):
270 self.Create(mesh, geom, "Quadrangle_2D")
272 # Public class: Mesh_Tetrahedron
273 # ------------------------------
275 class Mesh_Tetrahedron(Mesh_Algorithm):
277 Class to define a tetrahedron 3D algorithm
280 def __init__(self, mesh, algo, geom=0):
285 self.Create(mesh, geom, "NETGEN_3D", "libNETGENEngine.so")
288 self.Create(mesh, geom, "GHS3D_3D" , "libGHS3DEngine.so")
290 def MaxElementVolume(self, vol):
292 Define "MaxElementVolume" hypothesis to give the maximun volume of each tetrahedral
293 \param vol for the maximum volume of each tetrahedral
295 hyp = self.Hypothesis("MaxElementVolume", [vol])
296 hyp.SetMaxElementVolume(vol)
299 # Public class: Mesh_Hexahedron
300 # ------------------------------
302 class Mesh_Hexahedron(Mesh_Algorithm):
304 Class to define a hexahedron 3D algorithm
307 def __init__(self, mesh, geom=0):
311 self.Create(mesh, geom, "Hexa_3D")
318 Class to define a mesh
324 def __init__(self, geom, name=0):
328 Creates mesh on the shape \a geom,
329 sets GUI name of this mesh to \a name.
330 \param geom Shape to be meshed
331 \param name Study name of the mesh
334 self.mesh = smesh.CreateMesh(geom)
336 SetName(self.mesh, GetName(geom))
338 SetName(self.mesh, name)
342 Method that returns the mesh
348 Method that returns the shape associated to the mesh
352 def Segment(self, algo=REGULAR, geom=0):
354 Creates a segment discretization 1D algorithm.
355 If the optional \a algo parameter is not sets, this algorithm is REGULAR.
356 If the optional \a geom parameter is not sets, this algorithm is global.
357 Otherwise, this algorithm define a submesh based on \a geom subshape.
358 \param algo values are smesh.REGULAR or smesh.PYTHON for discretization via python function
359 \param geom If defined, subshape to be meshed
362 return Mesh_Segment(self, geom)
364 return Mesh_Segment_Python(self, geom)
366 return Mesh_Segment(self, algo)
368 def Triangle(self, geom=0):
370 Creates a triangle 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
375 return Mesh_Triangle(self, geom)
377 def Quadrangle(self, geom=0):
379 Creates a quadrangle 2D algorithm for faces.
380 If the optional \a geom parameter is not sets, this algorithm is global.
381 Otherwise, this algorithm define a submesh based on \a geom subshape.
382 \param geom If defined, subshape to be meshed
384 return Mesh_Quadrangle(self, geom)
386 def Tetrahedron(self, algo, geom=0):
388 Creates a tetrahedron 3D algorithm for solids.
389 The parameter \a algo permits to choice the algorithm: NETGEN or GHS3D
390 If the optional \a geom parameter is not sets, this algorithm is global.
391 Otherwise, this algorithm define a submesh based on \a geom subshape.
392 \param algo values are: smesh.NETGEN, smesh.GHS3D
393 \param geom If defined, subshape to be meshed
395 return Mesh_Tetrahedron(self, algo, geom)
397 def Hexahedron(self, geom=0):
399 Creates a hexahedron 3D algorithm for solids.
400 If the optional \a geom parameter is not sets, this algorithm is global.
401 Otherwise, this algorithm define a submesh based on \a geom subshape.
402 \param geom If defined, subshape to be meshed
404 return Mesh_Hexahedron(self, geom)
408 Compute the mesh and return the status of the computation
410 b = smesh.Compute(self.mesh, self.geom)
411 if salome.sg.hasDesktop():
412 smeshgui = salome.ImportComponentGUI("SMESH")
413 smeshgui.Init(salome.myStudyId)
414 smeshgui.SetMeshIcon( salome.ObjectToID( self.mesh ), b )
415 salome.sg.updateObjBrowser(1)
418 def Group(self, grp, name=""):
420 Create a mesh group based on geometric object \a grp
421 and give a \a name, if this parameter is not defined
422 the name is the same as the geometric group name
423 \param grp is a geometric group, a vertex, an edge, a face or a solid
424 \param name is the name of the mesh group
430 tgeo = str(grp.GetShapeType())
437 elif tgeo == "SOLID":
439 elif tgeo == "SHELL":
441 elif tgeo == "COMPOUND":
442 if len( geompy.GetObjectIDs( grp )) == 0:
443 print "Mesh.Group: empty geometric group", GetName( grp )
445 tgeo = geompy.GetType(grp)
446 if tgeo == geompy.ShapeType["VERTEX"]:
448 elif tgeo == geompy.ShapeType["EDGE"]:
450 elif tgeo == geompy.ShapeType["FACE"]:
452 elif tgeo == geompy.ShapeType["SOLID"]:
456 print "Mesh.Group: bad first argument: expected a group, a vertex, an edge, a face or a solid"
459 return self.mesh.CreateGroupFromGEOM(type, name, grp)
461 def ExportToMED(self, f, version, opt=0):
463 Export the mesh in a file with the MED format and choice the \a version of MED format
464 \param f is the file name
465 \param version values are SMESH.MED_V2_1, SMESH.MED_V2_2
467 self.mesh.ExportToMED(f, opt, version)
469 def ExportMED(self, f, opt=0):
471 Export the mesh in a file with the MED format
472 \param f is the file name
474 self.mesh.ExportMED(f, opt)
476 def ExportDAT(self, f):
478 Export the mesh in a file with the DAT format
479 \param f is the file name
481 self.mesh.ExportDAT(f)
483 def ExportUNV(self, f):
485 Export the mesh in a file with the UNV format
486 \param f is the file name
488 self.mesh.ExportUNV(f)
490 def ExportSTL(self, f, ascii=1):
492 Export the mesh in a file with the STL format
493 \param f is the file name
494 \param ascii defined the kind of file contents
496 self.mesh.ExportSTL(f, ascii)