1 # Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 ## @package smesh_algorithm
21 # Python API for base Mesh_Algorithm class.
22 # This package is a part of SALOME %Mesh module Python API
25 from salome.geom import geomBuilder
26 import SMESH, StdMeshers
28 ## The base class to define meshing algorithms
30 # @note This class should not be used directly, it is supposed to be sub-classed
31 # for implementing Python API for specific meshing algorithms
33 # For each meshing algorithm, a python class inheriting from class %Mesh_Algorithm
34 # should be defined. This descendant class should have two attributes defining the way
35 # it is created by class Mesh (see e.g. class @ref StdMeshersBuilder.StdMeshersBuilder_Segment "StdMeshersBuilder_Segment"
36 # in StdMeshersBuilder package):
37 # - @c meshMethod attribute defines name of method of class smesh.Mesh by calling which the
38 # python class of algorithm is created; this method is dynamically added to the smesh.Mesh class
39 # in runtime. For example, if in @c class MyPlugin_Algorithm this attribute is defined as
41 # meshMethod = "MyAlgorithm"
43 # then an instance of @c MyPlugin_Algorithm can be created by the direct invocation of the function
44 # of smesh.Mesh class:
46 # my_algo = mesh.MyAlgorithm()
48 # - @c algoType defines type of algorithm and is used mostly to discriminate
49 # algorithms that are created by the same method of class smesh.Mesh. For example, if this attribute
50 # is specified in @c MyPlugin_Algorithm class as
52 # algoType = "MyPLUGIN"
54 # then it's creation code can be:
56 # my_algo = mesh.MyAlgorithm(algo="MyPLUGIN")
58 # @ingroup l2_algorithms
69 ## Finds a hypothesis in the study by its type name and parameters.
70 # Finds only the hypotheses created in smeshpyD engine.
71 # @return SMESH.SMESH_Hypothesis
72 def FindHypothesis (self, hypname, args, CompareMethod, smeshpyD):
73 study = smeshpyD.GetCurrentStudy()
74 if not study: return None
75 #to do: find component by smeshpyD object, not by its data type
76 scomp = study.FindComponent(smeshpyD.ComponentDataType())
78 res,hypRoot = scomp.FindSubObject(SMESH.Tag_HypothesisRoot)
79 # Check if the root label of the hypotheses exists
80 if res and hypRoot is not None:
81 iter = study.NewChildIterator(hypRoot)
82 # Check all published hypotheses
84 hypo_so_i = iter.Value()
85 attr = hypo_so_i.FindAttribute("AttributeIOR")[1]
88 hypo_o_i = salome.orb.string_to_object(anIOR)
89 if hypo_o_i is not None:
90 # Check if this is a hypothesis
91 hypo_i = hypo_o_i._narrow(SMESH.SMESH_Hypothesis)
92 if hypo_i is not None:
93 # Check if the hypothesis belongs to current engine
94 if smeshpyD.GetObjectId(hypo_i) > 0:
95 # Check if this is the required hypothesis
96 if hypo_i.GetName() == hypname:
98 if CompareMethod(hypo_i, args):
112 ## Finds the algorithm in the study by its type name.
113 # Finds only the algorithms, which have been created in smeshpyD engine.
114 # @return SMESH.SMESH_Algo
115 def FindAlgorithm (self, algoname, smeshpyD):
116 study = smeshpyD.GetCurrentStudy()
117 if not study: return None
118 #to do: find component by smeshpyD object, not by its data type
119 scomp = study.FindComponent(smeshpyD.ComponentDataType())
120 if scomp is not None:
121 res,hypRoot = scomp.FindSubObject(SMESH.Tag_AlgorithmsRoot)
122 # Check if the root label of the algorithms exists
123 if res and hypRoot is not None:
124 iter = study.NewChildIterator(hypRoot)
125 # Check all published algorithms
127 algo_so_i = iter.Value()
128 attr = algo_so_i.FindAttribute("AttributeIOR")[1]
131 algo_o_i = salome.orb.string_to_object(anIOR)
132 if algo_o_i is not None:
133 # Check if this is an algorithm
134 algo_i = algo_o_i._narrow(SMESH.SMESH_Algo)
135 if algo_i is not None:
136 # Checks if the algorithm belongs to the current engine
137 if smeshpyD.GetObjectId(algo_i) > 0:
138 # Check if this is the required algorithm
139 if algo_i.GetName() == algoname:
152 ## If the algorithm is global, returns 0; \n
153 # else returns the submesh associated to this algorithm.
154 def GetSubMesh(self):
157 ## Returns the wrapped mesher.
158 def GetAlgorithm(self):
161 ## Gets the list of hypothesis that can be used with this algorithm
162 def GetCompatibleHypothesis(self):
165 mylist = self.algo.GetCompatibleHypothesis()
168 ## Gets the name of the algorithm
170 from salome.smesh.smeshBuilder import GetName
171 return GetName(self.algo)
173 ## Sets the name to the algorithm
174 def SetName(self, name):
175 self.mesh.smeshpyD.SetName(self.algo, name)
177 ## Gets the id of the algorithm
179 return self.algo.GetId()
182 def Create(self, mesh, geom, hypo, so="libStdMeshersEngine.so"):
183 if geom is None and mesh.mesh.HasShapeToMesh():
184 raise RuntimeError, "Attemp to create " + hypo + " algoritm on None shape"
185 algo = self.FindAlgorithm(hypo, mesh.smeshpyD)
187 algo = mesh.smeshpyD.CreateHypothesis(hypo, so)
189 self.Assign(algo, mesh, geom)
193 def Assign(self, algo, mesh, geom):
194 from salome.smesh.smeshBuilder import AssureGeomPublished, TreatHypoStatus, GetName
195 if geom is None and mesh.mesh.HasShapeToMesh():
196 raise RuntimeError, "Attemp to create " + algo + " algoritm on None shape"
198 if not geom or geom.IsSame( mesh.geom ):
199 self.geom = mesh.geom
202 AssureGeomPublished( mesh, geom )
203 self.subm = mesh.mesh.GetSubMesh(geom, algo.GetName())
205 status = mesh.AddHypothesis(self.algo, self.geom)
208 def CompareHyp (self, hyp, args):
209 print "CompareHyp is not implemented for ", self.__class__.__name__, ":", hyp.GetName()
212 def CompareEqualHyp (self, hyp, args):
216 def Hypothesis (self, hyp, args=[], so="libStdMeshersEngine.so",
217 UseExisting=0, CompareMethod="", toAdd=True):
218 from salome.smesh.smeshBuilder import TreatHypoStatus, GetName
221 if CompareMethod == "": CompareMethod = self.CompareHyp
222 hypo = self.FindHypothesis(hyp, args, CompareMethod, self.mesh.smeshpyD)
225 hypo = self.mesh.smeshpyD.CreateHypothesis(hyp, so)
230 if isinstance( arg, geomBuilder.GEOM._objref_GEOM_Object ):
231 argStr = arg.GetStudyEntry()
232 if not argStr: argStr = "GEOM_Obj_%s", arg.GetEntry()
233 if len( argStr ) > 10:
234 argStr = argStr[:7]+"..."
235 if argStr[0] == '[': argStr += ']'
241 self.mesh.smeshpyD.SetName(hypo, hyp + a)
245 geomName = GetName(self.geom)
247 status = self.mesh.mesh.AddHypothesis(self.geom, hypo)
248 TreatHypoStatus( status, GetName(hypo), geomName, 0, self.mesh )
251 ## Returns entry of the shape to mesh in the study
252 def MainShapeEntry(self):
253 if not self.mesh or not self.mesh.GetMesh(): return ""
254 if not self.mesh.GetMesh().HasShapeToMesh(): return ""
255 shape = self.mesh.GetShape()
256 return shape.GetStudyEntry()
258 ## Defines "ViscousLayers" hypothesis to give parameters of layers of prisms to build
259 # near mesh boundary. This hypothesis can be used by several 3D algorithms:
260 # NETGEN 3D, MG-Tetra, Hexahedron(i,j,k)
261 # @param thickness total thickness of layers of prisms
262 # @param numberOfLayers number of layers of prisms
263 # @param stretchFactor factor (>1.0) of growth of layer thickness towards inside of mesh
264 # @param faces list of geometrical faces (or their ids).
265 # Viscous layers are either generated on these faces or not, depending on
266 # the value of \a isFacesToIgnore parameter.
267 # @param isFacesToIgnore if \c True, the Viscous layers are not generated on the
268 # faces specified by the previous parameter (\a faces).
269 # @param extrMethod extrusion method defines how position of new nodes are found during
270 # prism construction and how creation of distorted and intersecting prisms is
271 # prevented. Possible values are:
272 # - StdMeshers.SURF_OFFSET_SMOOTH (default) method extrudes nodes along normal
273 # to underlying geometrical surface. Smoothing of internal surface of
274 # element layers can be used to avoid creation of invalid prisms.
275 # - StdMeshers.FACE_OFFSET method extrudes nodes along average normal of
276 # surrounding mesh faces till intersection with a neighbor mesh face
277 # translated along its own normal by the layers thickness. Thickness
278 # of layers can be limited to avoid creation of invalid prisms.
279 # - StdMeshers.NODE_OFFSET method extrudes nodes along average normal of
280 # surrounding mesh faces by the layers thickness. Thickness of
281 # layers can be limited to avoid creation of invalid prisms.
282 # @ingroup l3_hypos_additi
283 def ViscousLayers(self, thickness, numberOfLayers, stretchFactor,
284 faces=[], isFacesToIgnore=True, extrMethod=StdMeshers.SURF_OFFSET_SMOOTH ):
285 if not isinstance(self.algo, SMESH._objref_SMESH_3D_Algo):
286 raise TypeError, "ViscousLayers are supported by 3D algorithms only"
287 if not "ViscousLayers" in self.GetCompatibleHypothesis():
288 raise TypeError, "ViscousLayers are not supported by %s"%self.algo.GetName()
289 if faces and isinstance( faces[0], geomBuilder.GEOM._objref_GEOM_Object ):
293 if self.mesh.geompyD.ShapeIdToType( f.GetType() ) == "GROUP":
294 faceIDs += f.GetSubShapeIndices()
296 faceIDs += [self.mesh.geompyD.GetSubShapeID(self.mesh.geom, f)]
298 hyp = self.Hypothesis("ViscousLayers",
299 [thickness, numberOfLayers, stretchFactor, faces, isFacesToIgnore],
301 hyp.SetTotalThickness( thickness )
302 hyp.SetNumberLayers( numberOfLayers )
303 hyp.SetStretchFactor( stretchFactor )
304 hyp.SetFaces( faces, isFacesToIgnore )
305 hyp.SetMethod( extrMethod )
306 self.mesh.AddHypothesis( hyp, self.geom )
309 ## Defines "ViscousLayers2D" hypothesis to give parameters of layers of quadrilateral
310 # elements to build near mesh boundary. This hypothesis can be used by several 2D algorithms:
311 # NETGEN 2D, NETGEN 1D-2D, Quadrangle (mapping), MEFISTO, MG-CADSurf
312 # @param thickness total thickness of layers of quadrilaterals
313 # @param numberOfLayers number of layers
314 # @param stretchFactor factor (>1.0) of growth of layer thickness towards inside of mesh
315 # @param edges list of geometrical edges (or their ids).
316 # Viscous layers are either generated on these edges or not, depending on
317 # the value of \a isEdgesToIgnore parameter.
318 # @param isEdgesToIgnore if \c True, the Viscous layers are not generated on the
319 # edges specified by the previous parameter (\a edges).
320 # @ingroup l3_hypos_additi
321 def ViscousLayers2D(self, thickness, numberOfLayers, stretchFactor,
322 edges=[], isEdgesToIgnore=True ):
323 if not isinstance(self.algo, SMESH._objref_SMESH_2D_Algo):
324 raise TypeError, "ViscousLayers2D are supported by 2D algorithms only"
325 if not "ViscousLayers2D" in self.GetCompatibleHypothesis():
326 raise TypeError, "ViscousLayers2D are not supported by %s"%self.algo.GetName()
327 if edges and isinstance( edges[0], geomBuilder.GEOM._objref_GEOM_Object ):
328 edges = [ self.mesh.geompyD.GetSubShapeID(self.mesh.geom, f) for f in edges ]
329 hyp = self.Hypothesis("ViscousLayers2D",
330 [thickness, numberOfLayers, stretchFactor, edges, isEdgesToIgnore],
332 hyp.SetTotalThickness(thickness)
333 hyp.SetNumberLayers(numberOfLayers)
334 hyp.SetStretchFactor(stretchFactor)
335 hyp.SetEdges(edges, isEdgesToIgnore)
336 self.mesh.AddHypothesis( hyp, self.geom )
339 ## Transform a list of either edges or tuples (edge, 1st_vertex_of_edge)
340 # into a list acceptable to SetReversedEdges() of some 1D hypotheses
341 # @ingroup l3_hypos_1dhyps
342 def ReversedEdgeIndices(self, reverseList):
343 from salome.smesh.smeshBuilder import FirstVertexOnCurve
345 geompy = self.mesh.geompyD
346 for i in reverseList:
347 if isinstance( i, int ):
348 s = geompy.SubShapes(self.mesh.geom, [i])[0]
349 if s.GetShapeType() != geomBuilder.GEOM.EDGE:
350 raise TypeError, "Not EDGE index given"
352 elif isinstance( i, geomBuilder.GEOM._objref_GEOM_Object ):
353 if i.GetShapeType() != geomBuilder.GEOM.EDGE:
354 raise TypeError, "Not an EDGE given"
355 resList.append( geompy.GetSubShapeID(self.mesh.geom, i ))
359 if not isinstance( e, geomBuilder.GEOM._objref_GEOM_Object ) or \
360 not isinstance( v, geomBuilder.GEOM._objref_GEOM_Object ):
361 raise TypeError, "A list item must be a tuple (edge, 1st_vertex_of_edge)"
362 if v.GetShapeType() == geomBuilder.GEOM.EDGE and \
363 e.GetShapeType() == geomBuilder.GEOM.VERTEX:
365 if e.GetShapeType() != geomBuilder.GEOM.EDGE or \
366 v.GetShapeType() != geomBuilder.GEOM.VERTEX:
367 raise TypeError, "A list item must be a tuple (edge, 1st_vertex_of_edge)"
368 vFirst = FirstVertexOnCurve( self.mesh, e )
369 tol = geompy.Tolerance( vFirst )[-1]
370 if geompy.MinDistance( v, vFirst ) > 1.5*tol:
371 resList.append( geompy.GetSubShapeID(self.mesh.geom, e ))
373 raise TypeError, "Item must be either an edge or tuple (edge, 1st_vertex_of_edge)"