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
84 If the algorithm is global, return 0
85 else return the submesh associated to this algorithm
89 def GetAlgorithm(self):
91 Return the wrapped mesher
95 def Create(self, mesh, geom, hypo, so="libStdMeshersEngine.so"):
103 name = GetName(piece)
108 name = geompy.SubShapeName(geom, piece)
109 geompy.addToStudyInFather(piece, geom, name)
110 self.subm = mesh.mesh.GetSubMesh(geom, hypo)
112 self.algo = smesh.CreateHypothesis(hypo, so)
113 SetName(self.algo, name + "/" + hypo)
114 mesh.mesh.AddHypothesis(self.geom, self.algo)
116 def Hypothesis(self, hyp, args=[], so="libStdMeshersEngine.so"):
120 hypo = smesh.CreateHypothesis(hyp, so)
126 a = a + s + str(args[i])
129 SetName(hypo, GetName(self.geom) + "/" + hyp + a)
130 self.mesh.mesh.AddHypothesis(self.geom, hypo)
133 # Public class: Mesh_Segment
134 # --------------------------
136 class Mesh_Segment(Mesh_Algorithm):
138 Class to define a segment 1D algorithm for discretization
141 def __init__(self, mesh, geom=0):
145 self.Create(mesh, geom, "Regular_1D")
147 def LocalLength(self, l):
149 Define "LocalLength" hypothesis to cut an edge in several segments with the same length
150 \param l for the length of segments that cut an edge
152 hyp = self.Hypothesis("LocalLength", [l])
156 def NumberOfSegments(self, n, s=[]):
158 Define "NumberOfSegments" hypothesis to cut an edge in several fixed number of segments
159 \param n for the number of segments that cut an edge
160 \param s for the scale factor (optional)
163 hyp = self.Hypothesis("NumberOfSegments", [n])
165 hyp = self.Hypothesis("NumberOfSegments", [n,s])
166 hyp.SetDistrType( 1 )
167 hyp.SetScaleFactor(s)
168 hyp.SetNumberOfSegments(n)
171 def Arithmetic1D(self, start, end):
173 Define "Arithmetic1D" hypothesis to cut an edge in several segments with arithmetic length increasing
174 \param start for the length of the first segment
175 \param end for the length of the last segment
177 hyp = self.Hypothesis("Arithmetic1D", [start, end])
178 hyp.SetLength(start, 1)
179 hyp.SetLength(end , 0)
182 def StartEndLength(self, start, end):
184 Define "StartEndLength" hypothesis to cut an edge in several segments with geometric length increasing
185 \param start for the length of the first segment
186 \param end for the length of the last segment
188 hyp = self.Hypothesis("StartEndLength", [start, end])
189 hyp.SetLength(start, 1)
190 hyp.SetLength(end , 0)
193 def Deflection1D(self, d):
195 Define "Deflection1D" hypothesis
196 \param d for the deflection
198 hyp = self.Hypothesis("Deflection1D", [d])
202 def Propagation(self):
204 Define "Propagation" hypothesis that propagate all other hypothesis on all others edges that are in
205 the opposite side in the case of quadrangular faces
207 return self.Hypothesis("Propagation")
209 def AutomaticLength(self, fineness):
211 Define "AutomaticLength" hypothesis
212 \param fineness for the fineness [0-1]
214 hyp = self.Hypothesis("AutomaticLength")
215 hyp.SetFineness( fineness )
218 # Public class: Mesh_Segment_Python
219 # ---------------------------------
221 class Mesh_Segment_Python(Mesh_Segment):
223 Class to define a segment 1D algorithm for discretization with python function
226 def __init__(self, mesh, geom=0):
230 import Python1dPlugin
231 self.Create(mesh, geom, "Python_1D", "libPython1dEngine.so")
233 def PythonSplit1D(self, n, func):
235 Define "PythonSplit1D" hypothesis based on the Erwan Adam patch, awaiting equivalent SALOME functionality
236 \param n for the number of segments that cut an edge
237 \param func for the python function that calculate the length of all segments
239 hyp = self.Hypothesis("PythonSplit1D", [n], "libPython1dEngine.so")
240 hyp.SetNumberOfSegments(n)
241 hyp.SetPythonLog10RatioFunction(func)
244 # Public class: Mesh_Triangle
245 # ---------------------------
247 class Mesh_Triangle(Mesh_Algorithm):
249 Class to define a triangle 2D algorithm
252 def __init__(self, mesh, geom=0):
256 self.Create(mesh, geom, "MEFISTO_2D")
258 def MaxElementArea(self, area):
260 Define "MaxElementArea" hypothesis to give the maximun area of each triangles
261 \param area for the maximum area of each triangles
263 hyp = self.Hypothesis("MaxElementArea", [area])
264 hyp.SetMaxElementArea(area)
267 def LengthFromEdges(self):
269 Define "LengthFromEdges" hypothesis to build triangles based on the length of the edges taken from the wire
271 return self.Hypothesis("LengthFromEdges")
273 # Public class: Mesh_Quadrangle
274 # -----------------------------
276 class Mesh_Quadrangle(Mesh_Algorithm):
278 Class to define a quadrangle 2D algorithm
281 def __init__(self, mesh, geom=0):
285 self.Create(mesh, geom, "Quadrangle_2D")
287 def QuadranglePreference(self):
289 Define "QuadranglePreference" hypothesis, forcing construction
290 of quadrangles if the number of nodes on opposite edges is not the same
291 in the case where the global number of nodes on edges is even
293 hyp = self.Hypothesis("QuadranglePreference")
296 # Public class: Mesh_Tetrahedron
297 # ------------------------------
299 class Mesh_Tetrahedron(Mesh_Algorithm):
301 Class to define a tetrahedron 3D algorithm
304 def __init__(self, mesh, algo, geom=0):
309 self.Create(mesh, geom, "NETGEN_3D", "libNETGENEngine.so")
312 self.Create(mesh, geom, "GHS3D_3D" , "libGHS3DEngine.so")
314 def MaxElementVolume(self, vol):
316 Define "MaxElementVolume" hypothesis to give the maximun volume of each tetrahedral
317 \param vol for the maximum volume of each tetrahedral
319 hyp = self.Hypothesis("MaxElementVolume", [vol])
320 hyp.SetMaxElementVolume(vol)
323 # Public class: Mesh_Hexahedron
324 # ------------------------------
326 class Mesh_Hexahedron(Mesh_Algorithm):
328 Class to define a hexahedron 3D algorithm
331 def __init__(self, mesh, geom=0):
335 self.Create(mesh, geom, "Hexa_3D")
342 Class to define a mesh
348 def __init__(self, geom, name=0):
352 Creates mesh on the shape \a geom,
353 sets GUI name of this mesh to \a name.
354 \param geom Shape to be meshed
355 \param name Study name of the mesh
358 self.mesh = smesh.CreateMesh(geom)
360 SetName(self.mesh, GetName(geom))
362 SetName(self.mesh, name)
366 Method that returns the mesh
372 Method that returns the shape associated to the mesh
376 def MeshDimension(self):
378 Returns mesh dimension depending on shape one
380 shells = geompy.SubShapeAllIDs( self.geom, geompy.ShapeType["SHELL"] )
381 if len( shells ) > 0 :
383 elif geompy.NumberOfFaces( self.geom ) > 0 :
385 elif geompy.NumberOfEdges( self.geom ) > 0 :
391 def Segment(self, algo=REGULAR, geom=0):
393 Creates a segment discretization 1D algorithm.
394 If the optional \a algo parameter is not sets, this algorithm is REGULAR.
395 If the optional \a geom parameter is not sets, this algorithm is global.
396 Otherwise, this algorithm define a submesh based on \a geom subshape.
397 \param algo values are smesh.REGULAR or smesh.PYTHON for discretization via python function
398 \param geom If defined, subshape to be meshed
400 ## if Segment(geom) is called by mistake
401 if ( isinstance( algo, geompy.GEOM._objref_GEOM_Object)):
402 algo, geom = geom, algo
405 return Mesh_Segment(self, geom)
407 return Mesh_Segment_Python(self, geom)
409 return Mesh_Segment(self, geom)
411 def Triangle(self, geom=0):
413 Creates a triangle 2D algorithm for faces.
414 If the optional \a geom parameter is not sets, this algorithm is global.
415 Otherwise, this algorithm define a submesh based on \a geom subshape.
416 \param geom If defined, subshape to be meshed
418 return Mesh_Triangle(self, geom)
420 def Quadrangle(self, geom=0):
422 Creates a quadrangle 2D algorithm for faces.
423 If the optional \a geom parameter is not sets, this algorithm is global.
424 Otherwise, this algorithm define a submesh based on \a geom subshape.
425 \param geom If defined, subshape to be meshed
427 return Mesh_Quadrangle(self, geom)
429 def Tetrahedron(self, algo, geom=0):
431 Creates a tetrahedron 3D algorithm for solids.
432 The parameter \a algo permits to choice the algorithm: NETGEN or GHS3D
433 If the optional \a geom parameter is not sets, this algorithm is global.
434 Otherwise, this algorithm define a submesh based on \a geom subshape.
435 \param algo values are: smesh.NETGEN, smesh.GHS3D
436 \param geom If defined, subshape to be meshed
438 ## if Tetrahedron(geom) is called by mistake
439 if ( isinstance( algo, geompy.GEOM._objref_GEOM_Object)):
440 algo, geom = geom, algo
442 return Mesh_Tetrahedron(self, algo, geom)
444 def Hexahedron(self, geom=0):
446 Creates a hexahedron 3D algorithm for solids.
447 If the optional \a geom parameter is not sets, this algorithm is global.
448 Otherwise, this algorithm define a submesh based on \a geom subshape.
449 \param geom If defined, subshape to be meshed
451 return Mesh_Hexahedron(self, geom)
455 Compute the mesh and return the status of the computation
457 b = smesh.Compute(self.mesh, self.geom)
458 if salome.sg.hasDesktop():
459 smeshgui = salome.ImportComponentGUI("SMESH")
460 smeshgui.Init(salome.myStudyId)
461 smeshgui.SetMeshIcon( salome.ObjectToID( self.mesh ), b )
462 salome.sg.updateObjBrowser(1)
465 def AutomaticTetrahedralization(self):
467 Compute tetrahedral mesh using AutomaticLength + MEFISTO + NETGEN
469 dim = self.MeshDimension()
471 self.RemoveGlobalHypotheses()
472 self.Segment().AutomaticLength()
474 self.Triangle().LengthFromEdges()
477 self.Tetrahedron(NETGEN)
479 return self.Compute()
481 def AutomaticHexahedralization(self):
483 Compute hexahedral mesh using AutomaticLength + Quadrangle + Hexahedron
485 dim = self.MeshDimension()
487 self.RemoveGlobalHypotheses()
488 self.Segment().AutomaticLength()
495 return self.Compute()
497 def RemoveGlobalHypotheses(self):
499 Removes all global hypotheses
501 current_hyps = self.mesh.GetHypothesisList( self.geom )
502 for hyp in current_hyps:
503 self.mesh.RemoveHypothesis( self.geom, hyp )
507 def Group(self, grp, name=""):
509 Create a mesh group based on geometric object \a grp
510 and give a \a name, if this parameter is not defined
511 the name is the same as the geometric group name
512 \param grp is a geometric group, a vertex, an edge, a face or a solid
513 \param name is the name of the mesh group
519 tgeo = str(grp.GetShapeType())
526 elif tgeo == "SOLID":
528 elif tgeo == "SHELL":
530 elif tgeo == "COMPOUND":
531 tgeo = geompy.GetType(grp)
532 if tgeo == geompy.ShapeType["VERTEX"]:
534 elif tgeo == geompy.ShapeType["EDGE"]:
536 elif tgeo == geompy.ShapeType["FACE"]:
538 elif tgeo == geompy.ShapeType["SOLID"]:
542 print "Mesh.Group: bad first argument: expected a group, a vertex, an edge, a face or a solid"
545 return self.mesh.CreateGroupFromGEOM(type, name, grp)
547 def ExportToMED(self, f, version, opt=0):
549 Export the mesh in a file with the MED format and choice the \a version of MED format
550 \param f is the file name
551 \param version values are smesh.MED_V2_1, smesh.MED_V2_2
553 self.mesh.ExportToMED(f, opt, version)
555 def ExportMED(self, f, opt=0):
557 Export the mesh in a file with the MED format
558 \param f is the file name
560 self.mesh.ExportMED(f, opt)
562 def ExportDAT(self, f):
564 Export the mesh in a file with the DAT format
565 \param f is the file name
567 self.mesh.ExportDAT(f)
569 def ExportUNV(self, f):
571 Export the mesh in a file with the UNV format
572 \param f is the file name
574 self.mesh.ExportUNV(f)
576 def ExportSTL(self, f, ascii=1):
578 Export the mesh in a file with the STL format
579 \param f is the file name
580 \param ascii defined the kind of file contents
582 self.mesh.ExportSTL(f, ascii)