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"):
101 name = geompy.SubShapeName(geom, piece)
102 geompy.addToStudyInFather(piece, geom, name)
103 self.subm = mesh.mesh.GetSubMesh(geom, hypo)
105 algo = smesh.CreateHypothesis(hypo, so)
106 SetName(algo, name + "/" + hypo)
107 mesh.mesh.AddHypothesis(self.geom, algo)
109 def Hypothesis(self, hyp, args=[], so="libStdMeshersEngine.so"):
113 hypo = smesh.CreateHypothesis(hyp, so)
119 a = a + s + str(args[i])
122 SetName(hypo, GetName(self.geom) + "/" + hyp + a)
123 self.mesh.mesh.AddHypothesis(self.geom, hypo)
126 # Public class: Mesh_Segment
127 # --------------------------
129 class Mesh_Segment(Mesh_Algorithm):
131 Class to define a segment 1D algorithm for discretization
134 def __init__(self, mesh, geom=0):
138 self.Create(mesh, geom, "Regular_1D")
140 def LocalLength(self, l):
142 Define "LocalLength" hypothesis to cut an edge in several segments with the same length
143 \param l for the length of segments that cut an edge
145 hyp = self.Hypothesis("LocalLength", [l])
149 def NumberOfSegments(self, n, s=[]):
151 Define "NumberOfSegments" hypothesis to cut an edge in several fixed number of segments
152 \param n for the number of segments that cut an edge
153 \param s for the scale factor (optional)
156 hyp = self.Hypothesis("NumberOfSegments", [n])
158 hyp = self.Hypothesis("NumberOfSegments", [n,s])
159 hyp.SetDistrType( 1 )
160 hyp.SetScaleFactor(s)
161 hyp.SetNumberOfSegments(n)
164 def Arithmetic1D(self, start, end):
166 Define "Arithmetic1D" hypothesis to cut an edge in several segments with arithmetic length increasing
167 \param start for the length of the first segment
168 \param end for the length of the last segment
170 hyp = self.Hypothesis("Arithmetic1D", [start, end])
171 hyp.SetLength(start, 1)
172 hyp.SetLength(end , 0)
175 def StartEndLength(self, start, end):
177 Define "StartEndLength" hypothesis to cut an edge in several segments with geometric length increasing
178 \param start for the length of the first segment
179 \param end for the length of the last segment
181 hyp = self.Hypothesis("StartEndLength", [start, end])
182 hyp.SetLength(start, 1)
183 hyp.SetLength(end , 0)
186 def Deflection1D(self, d):
188 Define "Deflection1D" hypothesis
189 \param d for the deflection
191 hyp = self.Hypothesis("Deflection1D", [d])
195 def Propagation(self):
197 Define "Propagation" hypothesis that propagate all other hypothesis on all others edges that are in
198 the opposite side in the case of quadrangular faces
200 return self.Hypothesis("Propagation")
202 # Public class: Mesh_Segment_Python
203 # ---------------------------------
205 class Mesh_Segment_Python(Mesh_Segment):
207 Class to define a segment 1D algorithm for discretization with python function
210 def __init__(self, mesh, geom=0):
214 import Python1dPlugin
215 self.Create(mesh, geom, "Python_1D", "libPython1dEngine.so")
217 def PythonSplit1D(self, n, func):
219 Define "PythonSplit1D" hypothesis based on the Erwan Adam patch, awaiting equivalent SALOME functionality
220 \param n for the number of segments that cut an edge
221 \param func for the python function that calculate the length of all segments
223 hyp = self.Hypothesis("PythonSplit1D", [n], "libPython1dEngine.so")
224 hyp.SetNumberOfSegments(n)
225 hyp.SetPythonLog10RatioFunction(func)
228 # Public class: Mesh_Triangle
229 # ---------------------------
231 class Mesh_Triangle(Mesh_Algorithm):
233 Class to define a triangle 2D algorithm
236 def __init__(self, mesh, geom=0):
240 self.Create(mesh, geom, "MEFISTO_2D")
242 def MaxElementArea(self, area):
244 Define "MaxElementArea" hypothesis to give the maximun area of each triangles
245 \param area for the maximum area of each triangles
247 hyp = self.Hypothesis("MaxElementArea", [area])
248 hyp.SetMaxElementArea(area)
251 def LengthFromEdges(self):
253 Define "LengthFromEdges" hypothesis to build triangles based on the length of the edges taken from the wire
255 return self.Hypothesis("LengthFromEdges")
257 # Public class: Mesh_Quadrangle
258 # -----------------------------
260 class Mesh_Quadrangle(Mesh_Algorithm):
262 Class to define a quadrangle 2D algorithm
265 def __init__(self, mesh, geom=0):
269 self.Create(mesh, geom, "Quadrangle_2D")
271 # Public class: Mesh_Tetrahedron
272 # ------------------------------
274 class Mesh_Tetrahedron(Mesh_Algorithm):
276 Class to define a tetrahedron 3D algorithm
279 def __init__(self, mesh, algo, geom=0):
284 self.Create(mesh, geom, "NETGEN_3D", "libNETGENEngine.so")
287 self.Create(mesh, geom, "GHS3D_3D" , "libGHS3DEngine.so")
289 def MaxElementVolume(self, vol):
291 Define "MaxElementVolume" hypothesis to give the maximun volume of each tetrahedral
292 \param vol for the maximum volume of each tetrahedral
294 hyp = self.Hypothesis("MaxElementVolume", [vol])
295 hyp.SetMaxElementVolume(vol)
298 # Public class: Mesh_Hexahedron
299 # ------------------------------
301 class Mesh_Hexahedron(Mesh_Algorithm):
303 Class to define a hexahedron 3D algorithm
306 def __init__(self, mesh, geom=0):
310 self.Create(mesh, geom, "Hexa_3D")
317 Class to define a mesh
323 def __init__(self, geom, name=0):
327 Creates mesh on the shape \a geom,
328 sets GUI name of this mesh to \a name.
329 \param geom Shape to be meshed
330 \param name Study name of the mesh
333 self.mesh = smesh.CreateMesh(geom)
335 SetName(self.mesh, GetName(geom))
337 SetName(self.mesh, name)
341 Method that returns the mesh
347 Method that returns the shape associated to the mesh
351 def Segment(self, algo=REGULAR, geom=0):
353 Creates a segment discretization 1D algorithm.
354 If the optional \a algo parameter is not sets, this algorithm is REGULAR.
355 If the optional \a geom parameter is not sets, this algorithm is global.
356 Otherwise, this algorithm define a submesh based on \a geom subshape.
357 \param algo values are smesh.REGULAR or smesh.PYTHON for discretization via python function
358 \param geom If defined, subshape to be meshed
361 return Mesh_Segment(self, geom)
363 return Mesh_Segment_Python(self, geom)
365 return Mesh_Segment(self, algo)
367 def Triangle(self, geom=0):
369 Creates a triangle 2D algorithm for faces.
370 If the optional \a geom parameter is not sets, this algorithm is global.
371 Otherwise, this algorithm define a submesh based on \a geom subshape.
372 \param geom If defined, subshape to be meshed
374 return Mesh_Triangle(self, geom)
376 def Quadrangle(self, geom=0):
378 Creates a quadrangle 2D algorithm for faces.
379 If the optional \a geom parameter is not sets, this algorithm is global.
380 Otherwise, this algorithm define a submesh based on \a geom subshape.
381 \param geom If defined, subshape to be meshed
383 return Mesh_Quadrangle(self, geom)
385 def Tetrahedron(self, algo, geom=0):
387 Creates a tetrahedron 3D algorithm for solids.
388 The parameter \a algo permits to choice the algorithm: NETGEN or GHS3D
389 If the optional \a geom parameter is not sets, this algorithm is global.
390 Otherwise, this algorithm define a submesh based on \a geom subshape.
391 \param algo values are: smesh.NETGEN, smesh.GHS3D
392 \param geom If defined, subshape to be meshed
394 return Mesh_Tetrahedron(self, algo, geom)
396 def Hexahedron(self, geom=0):
398 Creates a hexahedron 3D algorithm for solids.
399 If the optional \a geom parameter is not sets, this algorithm is global.
400 Otherwise, this algorithm define a submesh based on \a geom subshape.
401 \param geom If defined, subshape to be meshed
403 return Mesh_Hexahedron(self, geom)
407 Compute the mesh and return the status of the computation
409 b = smesh.Compute(self.mesh, self.geom)
410 if salome.sg.hasDesktop():
411 smeshgui = salome.ImportComponentGUI("SMESH")
412 smeshgui.Init(salome.myStudyId)
413 smeshgui.SetMeshIcon( salome.ObjectToID( self.mesh ), b )
414 salome.sg.updateObjBrowser(1)
417 def Group(self, grp, name=""):
419 Create a mesh group based on geometric object \a grp
420 and give a \a name, if this parameter is not defined
421 the name is the same as the geometric group name
422 \param grp is a geometric group, a vertex, an edge, a face or a solid
423 \param name is the name of the mesh group
429 tgeo = str(grp.GetShapeType())
436 elif tgeo == "SOLID":
438 elif tgeo == "SHELL":
440 elif tgeo == "COMPOUND":
441 tgeo = geompy.GetType(grp)
442 if tgeo == geompy.ShapeType["VERTEX"]:
444 elif tgeo == geompy.ShapeType["EDGE"]:
446 elif tgeo == geompy.ShapeType["FACE"]:
448 elif tgeo == geompy.ShapeType["SOLID"]:
452 print "Mesh.Group: bad first argument: expected a group, a vertex, an edge, a face or a solid"
455 return self.mesh.CreateGroupFromGEOM(type, name, grp)
457 def ExportToMED(self, f, version, opt=0):
459 Export the mesh in a file with the MED format and choice the \a version of MED format
460 \param f is the file name
461 \param version values are smesh.MED_V2_1, smesh.MED_V2_2
463 self.mesh.ExportToMED(f, opt, version)
465 def ExportMED(self, f, opt=0):
467 Export the mesh in a file with the MED format
468 \param f is the file name
470 self.mesh.ExportMED(f, opt)
472 def ExportDAT(self, f):
474 Export the mesh in a file with the DAT format
475 \param f is the file name
477 self.mesh.ExportDAT(f)
479 def ExportUNV(self, f):
481 Export the mesh in a file with the UNV format
482 \param f is the file name
484 self.mesh.ExportUNV(f)
486 def ExportSTL(self, f, ascii=1):
488 Export the mesh in a file with the STL format
489 \param f is the file name
490 \param ascii defined the kind of file contents
492 self.mesh.ExportSTL(f, ascii)