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)
50 ior = salome.orb.object_to_string(obj)
51 sobj = salome.myStudy.FindObjectIOR(ior)
52 attr = sobj.FindAttribute("AttributeName")[1]
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]
61 # Algorithms and hypothesis
62 # =========================
64 # Private class: Mesh_Algorithm
65 # -----------------------------
69 Mother class to define algorithm, recommended to don't use directly
78 If the algorithm is global, return 0
79 else return the submesh associated to this algorithm
83 def Create(self, mesh, geom, hypo, so="libStdMeshersEngine.so"):
94 name = geompy.SubShapeName(geom, piece)
95 geompy.addToStudyInFather(piece, geom, name)
96 self.subm = mesh.mesh.GetSubMesh(geom, hypo)
98 algo = smesh.CreateHypothesis(hypo, so)
99 SetName(algo, name + "/" + hypo)
100 mesh.mesh.AddHypothesis(self.geom, algo)
102 def Hypothesis(self, hyp, args=[], so="libStdMeshersEngine.so"):
106 hypo = smesh.CreateHypothesis(hyp, so)
112 a = a + s + str(args[i])
115 SetName(hypo, GetName(self.geom) + "/" + hyp + a)
116 self.mesh.mesh.AddHypothesis(self.geom, hypo)
119 # Public class: Mesh_Segment
120 # --------------------------
122 class Mesh_Segment(Mesh_Algorithm):
124 Class to define a segment 1D algorithm for discretization
127 def __init__(self, mesh, geom=0):
131 self.Create(mesh, geom, "Regular_1D")
133 def LocalLength(self, l):
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
138 hyp = self.Hypothesis("LocalLength", [l])
142 def NumberOfSegments(self, n, s=[]):
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)
149 hyp = self.Hypothesis("NumberOfSegments", [n])
151 hyp = self.Hypothesis("NumberOfSegments", [n,s])
152 hyp.SetScaleFactor(s)
153 hyp.SetNumberOfSegments(n)
156 def Arithmetic1D(self, start, end):
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
162 hyp = self.Hypothesis("Arithmetic1D", [start, end])
163 hyp.SetLength(start, 1)
164 hyp.SetLength(end , 0)
167 def StartEndLength(self, start, end):
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
173 hyp = self.Hypothesis("StartEndLength", [start, end])
174 hyp.SetLength(start, 1)
175 hyp.SetLength(end , 0)
178 def Deflection1D(self, d):
180 Define "Deflection1D" hypothesis
181 \param d for the deflection
183 hyp = self.Hypothesis("Deflection1D", [d])
187 def Propagation(self):
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
192 return self.Hypothesis("Propagation")
194 # Public class: Mesh_Segment_Python
195 # ---------------------------------
197 class Mesh_Segment_Python(Mesh_Segment):
199 Class to define a segment 1D algorithm for discretization with python function
202 def __init__(self, mesh, geom=0):
206 import Python1dPlugin
207 self.Create(mesh, geom, "Python_1D", "libPython1dEngine.so")
209 def PythonSplit1D(self, n, func):
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
215 hyp = self.Hypothesis("PythonSplit1D", [n], "libPython1dEngine.so")
216 hyp.SetNumberOfSegments(n)
217 hyp.SetPythonLog10RatioFunction(func)
220 # Public class: Mesh_Triangle
221 # ---------------------------
223 class Mesh_Triangle(Mesh_Algorithm):
225 Class to define a triangle 2D algorithm
228 def __init__(self, mesh, geom=0):
232 self.Create(mesh, geom, "MEFISTO_2D")
234 def MaxElementArea(self, area):
236 Define "MaxElementArea" hypothesis to give the maximun area of each triangles
237 \param area for the maximum area of each triangles
239 hyp = self.Hypothesis("MaxElementArea", [area])
240 hyp.SetMaxElementArea(area)
243 def LengthFromEdges(self):
245 Define "LengthFromEdges" hypothesis to build triangles based on the length of the edges taken from the wire
247 return self.Hypothesis("LengthFromEdges")
249 # Public class: Mesh_Quadrangle
250 # -----------------------------
252 class Mesh_Quadrangle(Mesh_Algorithm):
254 Class to define a quadrangle 2D algorithm
257 def __init__(self, mesh, geom=0):
261 self.Create(mesh, geom, "Quadrangle_2D")
263 # Public class: Mesh_Tetrahedron
264 # ------------------------------
266 class Mesh_Tetrahedron(Mesh_Algorithm):
268 Class to define a tetrahedron 3D algorithm
271 def __init__(self, mesh, algo, geom=0):
276 self.Create(mesh, geom, "NETGEN_3D", "libNETGENEngine.so")
279 self.Create(mesh, geom, "GHS3D_3D" , "libGHS3DEngine.so")
281 def MaxElementVolume(self, vol):
283 Define "MaxElementVolume" hypothesis to give the maximun volume of each tetrahedral
284 \param vol for the maximum volume of each tetrahedral
286 hyp = self.Hypothesis("MaxElementVolume", [vol])
287 hyp.SetMaxElementVolume(vol)
290 # Public class: Mesh_Hexahedron
291 # ------------------------------
293 class Mesh_Hexahedron(Mesh_Algorithm):
295 Class to define a hexahedron 3D algorithm
298 def __init__(self, mesh, geom=0):
302 self.Create(mesh, geom, "Hexa_3D")
309 Class to define a mesh
315 def __init__(self, geom, name=0):
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
325 self.mesh = smesh.CreateMesh(geom)
327 SetName(self.mesh, GetName(geom))
329 SetName(self.mesh, name)
333 Method that returns the mesh
339 Method that returns the shape associated to the mesh
343 def Segment(self, algo=REGULAR, geom=0):
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
353 return Mesh_Segment(self, geom)
355 return Mesh_Segment_Python(self, geom)
357 return Mesh_Segment(self, algo)
359 def Triangle(self, geom=0):
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
366 return Mesh_Triangle(self, geom)
368 def Quadrangle(self, geom=0):
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
375 return Mesh_Quadrangle(self, geom)
377 def Tetrahedron(self, algo, geom=0):
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
386 return Mesh_Tetrahedron(self, algo, geom)
388 def Hexahedron(self, geom=0):
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
395 return Mesh_Hexahedron(self, geom)
399 Compute the mesh and return the status of the computation
401 b = smesh.Compute(self.mesh, self.geom)
402 if salome.sg.hasDesktop():
403 salome.sg.updateObjBrowser(1)
406 def Group(self, grp, name=""):
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
416 tgeo = geompy.GetType(grp)
417 if tgeo == geompy.ShapeType["VERTEX"]:
419 elif tgeo == geompy.ShapeType["EDGE"]:
421 elif tgeo == geompy.ShapeType["FACE"]:
423 elif tgeo == geompy.ShapeType["SOLID"]:
425 return self.mesh.CreateGroupFromGEOM(type, name, grp)
427 def ExportToMED(self, f, version, opt=0):
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
433 self.mesh.ExportToMED(f, opt, version)
435 def ExportMED(self, f, opt=0):
437 Export the mesh in a file with the MED format
438 \param f is the file name
440 self.mesh.ExportMED(f, opt)
442 def ExportDAT(self, f):
444 Export the mesh in a file with the DAT format
445 \param f is the file name
447 self.mesh.ExportDAT(f)
449 def ExportUNV(self, f):
451 Export the mesh in a file with the UNV format
452 \param f is the file name
454 self.mesh.ExportUNV(f)
456 def ExportSTL(self, f, ascii=1):
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
462 self.mesh.ExportSTL(f, ascii)