1 # Copyright (C) 2007-2022 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
21 # @package NETGENPluginBuilder
22 # Python API for the NETGEN meshing plug-in module.
24 from salome.smesh.smesh_algorithm import Mesh_Algorithm
25 from salome.smesh.smeshBuilder import AssureGeomPublished, ParseParameters, IsEqual
27 # import NETGENPlugin module if possible
35 LIBRARY = "libNETGENEngine.so"
37 NETGEN_VERSION_MAJOR = NETGENPlugin.NETGEN_VERSION_MAJOR
39 #----------------------------
40 # Mesh algo type identifiers
41 #----------------------------
43 ## Algorithm type: Netgen tetrahedron 3D algorithm, see NETGEN_3D_Algorithm
44 NETGEN_3D = "NETGEN_3D"
45 ## Algorithm type: Netgen tetrahedron 1D-2D-3D algorithm, see NETGEN_1D2D3D_Algorithm
46 NETGEN_1D2D3D = "NETGEN_2D3D"
47 ## Algorithm type: Netgen triangle 1D-2D algorithm, see NETGEN_1D2D_Algorithm
48 NETGEN_1D2D = "NETGEN_2D"
49 ## Algorithm type: Netgen triangle 2D algorithm, see NETGEN_2D_Only_Algorithm
50 NETGEN_2D = "NETGEN_2D_ONLY"
51 ## Algorithm type: Synonim of NETGEN_1D2D3D, see NETGEN_1D2D3D_Algorithm
52 NETGEN_FULL = NETGEN_1D2D3D
53 ## Algorithm type: Synonim of NETGEN_3D, see NETGEN_3D_Algorithm
55 ## Algorithm type: Synonim of NETGEN_1D2D3D, see NETGEN_1D2D3D_Algorithm
56 FULL_NETGEN = NETGEN_FULL
58 #----------------------------
59 # Hypothesis type enumeration
60 #----------------------------
62 ## Hypothesis type enumeration: complex hypothesis
63 # (full set of parameters can be specified),
64 # see NETGEN_Algorithm.Parameters()
66 ## Hypothesis type enumeration: simple hypothesis
67 # (only major parameters are specified),
68 # see NETGEN_Algorithm.Parameters()
71 #----------------------
72 # Fineness enumeration
73 #----------------------
75 ## Fineness enumeration: very coarse quality of mesh,
76 # see NETGEN_Algorithm.SetFineness()
78 ## Fineness enumeration: coarse quality of mesh,
79 # see NETGEN_Algorithm.SetFineness()
81 ## Fineness enumeration: moderate quality of mesh,
82 # see NETGEN_Algorithm.SetFineness()
84 ## Fineness enumeration: fine quality of mesh,
85 # see NETGEN_Algorithm.SetFineness()
87 ## Fineness enumeration: very fine quality of mesh,
88 # see NETGEN_Algorithm.SetFineness()
90 ## Fineness enumeration: custom quality of mesh specified by other parameters),
91 # see NETGEN_Algorithm.SetFineness()
94 #----------------------
96 #----------------------
98 ## Base of all NETGEN algorithms.
100 # This class provides common methods for all algorithms implemented by NETGEN plugin.
101 # @note This class must not be instantiated directly.
102 class NETGEN_Algorithm(Mesh_Algorithm):
104 ## Private constructor
105 # @param mesh parent mesh object algorithm is assigned to
106 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
107 # if it is @c 0 (default), the algorithm is assigned to the main shape
108 def __init__(self, mesh, geom=0):
109 Mesh_Algorithm.__init__(self)
110 if noNETGENPlugin: print("Warning: NETGENPlugin module unavailable")
111 if not mesh.GetMesh().HasShapeToMesh() and \
112 self.meshMethod == "Triangle": # create a 2D remesher
113 self.Create(mesh, geom, "NETGEN_Remesher_2D", LIBRARY)
115 self.Create(mesh, geom, self.algoType, LIBRARY)
119 ## Sets @c MaxSize parameter
120 # @param theSize new value of the @c MaxSize parameter
121 def SetMaxSize(self, theSize):
122 if self.Parameters(): self.params.SetMaxSize(theSize)
125 ## Sets @c MinSize parameter
126 # @param theSize new value of the @c MinSize parameter
127 def SetMinSize(self, theSize):
128 if self.Parameters(): self.params.SetMinSize(theSize)
131 ## Sets @c Optimize flag
132 # @param theVal new value of the @c Optimize parameter
133 def SetOptimize(self, theVal):
134 if self.Parameters(): self.params.SetOptimize(theVal)
137 ## Sets @c Fineness parameter
138 # @param theFineness new value of the @c Fineness parameter; it can be:
139 # @ref VeryCoarse, @ref Coarse, @ref Moderate, @ref Fine, @ref VeryFine or @ref Custom
140 def SetFineness(self, theFineness):
141 if self.Parameters(): self.params.SetFineness(theFineness)
144 ## Sets @c GrowthRate parameter
145 # @param theRate new value of the @c GrowthRate parameter
146 def SetGrowthRate(self, theRate):
147 if self.Parameters(): self.params.SetGrowthRate(theRate)
150 ## Creates meshing hypothesis according to the chosen algorithm type
151 # and initializes it with default parameters
152 # @param which hypothesis type; can be either @ref SOLE (default) or @ref SIMPLE
153 # @return hypothesis object
154 def Parameters(self, which=SOLE):
155 if self.algoType == NETGEN_1D2D:
157 hypType = "NETGEN_SimpleParameters_2D"
159 hypType = "NETGEN_Parameters_2D"
160 elif self.algoType == NETGEN_1D2D3D:
162 hypType = "NETGEN_SimpleParameters_3D"
164 hypType = "NETGEN_Parameters"
165 elif self.algoType == NETGEN_2D:
166 hypType = "NETGEN_Parameters_2D_ONLY"
168 hypType = "NETGEN_Parameters_3D"
170 if self.algo.GetName() == "NETGEN_Remesher_2D":
171 hypType = "NETGEN_RemesherParameters_2D"
173 if self.params and self.params.GetName() != hypType:
174 self.mesh.RemoveHypothesis( self.params, self.geom )
177 self.params = self.Hypothesis(hypType, [], LIBRARY, UseExisting=0)
181 ## Defines a file specifying size of elements at points and lines
182 # @param file name of the file
183 def SetMeshSizeFile(self, file):
184 self.Parameters().SetMeshSizeFile(file)
187 ## Set size of elements on a shape
188 # @param shape - geometry
189 # @param size - element size
190 def SetLocalSizeOnShape(self, shape, size ):
191 self.Parameters().SetLocalSizeOnShape(shape, size)
195 pass # end of NETGEN_Algorithm class
198 ## Tetrahedron 1D-2D-3D algorithm.
200 # It can be created by calling smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN_1D2D3D, geom=0 ).
201 # This algorithm generates all 1D (edges), 2D (faces) and 3D (volumes) elements
202 # for given geometrical shape.
203 class NETGEN_1D2D3D_Algorithm(NETGEN_Algorithm):
205 ## name of the dynamic method in smeshBuilder.Mesh class
207 meshMethod = "Tetrahedron"
208 ## type of algorithm used with helper function in smeshBuilder.Mesh class
210 algoType = NETGEN_1D2D3D
211 ## doc string of the method
213 docHelper = "Creates tetrahedron 3D algorithm for solids"
215 ## Private constructor.
216 # @param mesh parent mesh object algorithm is assigned to
217 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
218 # if it is @c 0 (default), the algorithm is assigned to the main shape
219 def __init__(self, mesh, geom=0):
220 NETGEN_Algorithm.__init__(self, mesh, geom)
223 ## Sets @c SecondOrder flag
224 # @param theVal new value of the @c SecondOrder parameter
225 def SetSecondOrder(self, theVal):
226 if self.Parameters(): self.params.SetSecondOrder(theVal)
229 ## Sets @c NbSegPerEdge parameter
230 # @param theVal new value of the @c NbSegPerEdge parameter
231 def SetNbSegPerEdge(self, theVal):
232 if self.Parameters(): self.params.SetNbSegPerEdge(theVal)
235 ## Sets @c NbSegPerRadius parameter
236 # @param theVal new value of the @c NbSegPerRadius parameter
237 def SetNbSegPerRadius(self, theVal):
238 if self.Parameters(): self.params.SetNbSegPerRadius(theVal)
241 ## Sets @c ChordalError parameter
242 # @param theVal new value of the @c ChordalError parameter
243 def SetChordalError(self, theVal):
244 if self.Parameters():
245 self.params.SetChordalError(theVal)
246 self.params.SetChordalErrorEnabled( theVal > 0 )
249 ## Sets @c RidgeAngle parameter
250 # @param theVal new value of the @c RidgeAngle parameter
251 def SetRidgeAngle(self, theVal):
252 if self.Parameters():
253 self.params.SetRidgeAngle(theVal)
256 ## Sets @c QuadAllowed flag
257 # @param toAllow new value of the @c QuadAllowed parameter (@c True by default)
258 def SetQuadAllowed(self, toAllow=True):
259 if self.Parameters(): self.params.SetQuadAllowed(toAllow)
261 ## Sets @c UseSurfaceCurvature flag
262 # @param toUse new value of the @c UseSurfaceCurvature parameter (@c True by default)
263 def SetUseSurfaceCurvature(self, toUse=True):
264 if self.Parameters(): self.params.SetUseSurfaceCurvature(toUse)
266 ## Sets @c FuseEdges flag
267 # @param toFuse new value of the @c FuseEdges parameter (@c False by default)
268 def SetFuseEdges(self, toFuse=False):
269 if self.Parameters(): self.params.SetFuseEdges(toFuse)
272 ## Sets number of segments overriding the value set by SetLocalLength()
273 # @param theVal new value of number of segments parameter
274 def SetNumberOfSegments(self, theVal):
275 self.Parameters(SIMPLE).SetNumberOfSegments(theVal)
278 ## Sets number of segments overriding the value set by SetNumberOfSegments()
279 # @param theVal new value of local length parameter
280 def SetLocalLength(self, theVal):
281 self.Parameters(SIMPLE).SetLocalLength(theVal)
284 ## Defines @c MaxElementArea parameter of @c NETGEN_SimpleParameters_3D hypothesis.
285 # Overrides value set by LengthFromEdges()
286 # @param area new value of @c MaxElementArea parameter
287 def MaxElementArea(self, area):
288 self.Parameters(SIMPLE).SetMaxElementArea(area)
291 ## Defines @c LengthFromEdges parameter of @c NETGEN_SimpleParameters_3D hypothesis.
292 # Overrides value set by MaxElementArea()
293 def LengthFromEdges(self):
294 self.Parameters(SIMPLE).LengthFromEdges()
297 ## Defines @c LengthFromFaces parameter of @c NETGEN_SimpleParameters_3D hypothesis.
298 # Overrides value set by MaxElementVolume()
299 def LengthFromFaces(self):
300 self.Parameters(SIMPLE).LengthFromFaces()
303 ## Defines @c MaxElementVolume parameter of @c NETGEN_SimpleParameters_3D hypothesis.
304 # Overrides value set by LengthFromFaces()
305 # @param vol new value of @c MaxElementVolume parameter
306 def MaxElementVolume(self, vol):
307 self.Parameters(SIMPLE).SetMaxElementVolume(vol)
310 pass # end of NETGEN_1D2D3D_Algorithm class
313 ## Triangle NETGEN 1D-2D algorithm.
315 # It can be created by calling smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_1D2D, geom=0 )
317 # This algorithm generates 1D (edges) and 2D (faces) elements
318 # for given geometrical shape.
319 class NETGEN_1D2D_Algorithm(NETGEN_1D2D3D_Algorithm):
321 ## name of the dynamic method in smeshBuilder.Mesh class
323 meshMethod = "Triangle"
324 ## type of algorithm used with helper function in smeshBuilder.Mesh class
326 algoType = NETGEN_1D2D
327 ## doc string of the method
329 docHelper = "Creates triangle 2D algorithm for faces"
332 ## Private constructor.
333 # @param mesh parent mesh object algorithm is assigned to
334 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
335 # if it is @c 0 (default), the algorithm is assigned to the main shape
336 def __init__(self, mesh, geom=0):
337 NETGEN_1D2D3D_Algorithm.__init__(self, mesh, geom)
340 pass # end of NETGEN_1D2D_Algorithm class
343 ## Triangle NETGEN 2D algorithm
345 # It can be created by calling smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_2D, geom=0 )
347 # This algorithm generates only 2D (faces) elements for given geometrical shape
348 # and, in contrast to NETGEN_1D2D_Algorithm class, should be used in conjunction
349 # with other 1D meshing algorithm.
350 class NETGEN_2D_Only_Algorithm(NETGEN_Algorithm):
352 ## name of the dynamic method in smeshBuilder.Mesh class
354 meshMethod = "Triangle"
355 ## type of algorithm used with helper function in smeshBuilder.Mesh class
358 ## flag pointing whether this algorithm should be used by default in dynamic method
359 # of smeshBuilder.Mesh class
361 ## doc string of the method
363 docHelper = "Creates triangle 2D algorithm for faces"
367 ## Private constructor.
368 # @param mesh parent mesh object algorithm is assigned to
369 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
370 # if it is @c 0 (default), the algorithm is assigned to the main shape
371 def __init__(self, mesh, geom=0):
372 NETGEN_Algorithm.__init__(self, mesh, geom)
375 ## Defines @c MaxElementArea parameter of hypothesis basing on the definition of the
376 # maximum area of each triangle
377 # @param area maximum area value of each triangle
378 # @param UseExisting if \c True - searches for an existing hypothesis created with the
379 # same parameters, else (default) - creates a new one
380 # @return hypothesis object
381 def MaxElementArea(self, area, UseExisting=0):
382 compFun = lambda hyp, args: IsEqual(hyp.GetMaxElementArea(), args[0])
383 hyp = self.Hypothesis("MaxElementArea", [area], UseExisting=UseExisting,
384 CompareMethod=compFun)
385 hyp.SetMaxElementArea(area)
388 ## Defines @c LengthFromEdges hypothesis to build triangles
389 # based on the length of the edges taken from the wire
390 # @return hypothesis object
391 def LengthFromEdges(self):
392 hyp = self.Hypothesis("LengthFromEdges", UseExisting=1, CompareMethod=self.CompareEqualHyp)
395 ## Sets @c UseSurfaceCurvature flag
396 # @param toUse new value of the @c UseSurfaceCurvature parameter (@c True by default)
397 def SetUseSurfaceCurvature(self, toUse=True):
398 if self.Parameters(): self.params.SetUseSurfaceCurvature(toUse)
401 ## Sets @c QuadAllowed flag.
402 # @param toAllow new value of the @c QuadAllowed parameter (@c True by default)
403 # @return hypothesis object
404 def SetQuadAllowed(self, toAllow=True):
407 hasSimpleHyps = False
408 simpleHyps = ["QuadranglePreference","LengthFromEdges","MaxElementArea"]
409 for hyp in self.mesh.GetHypothesisList( self.geom ):
410 if hyp.GetName() in simpleHyps:
412 if hyp.GetName() == "QuadranglePreference":
413 if not toAllow: # remove QuadranglePreference
414 self.mesh.RemoveHypothesis( self.geom, hyp )
421 if toAllow: # add QuadranglePreference
422 return self.Hypothesis("QuadranglePreference", UseExisting=1, CompareMethod=self.CompareEqualHyp)
425 self.Parameters().SetQuadAllowed( toAllow )
428 pass # end of NETGEN_2D_Only_Algorithm class
431 ## Tetrahedron 3D algorithm
433 # It can be created by calling smeshBuilder.Mesh.Tetrahedron() or smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN, geom=0 )
435 # This algorithm generates only 3D (volumes) elements for given geometrical shape
436 # and, in contrast to NETGEN_1D2D3D_Algorithm class, should be used in conjunction
437 # with other 1D and 2D meshing algorithms.
438 class NETGEN_3D_Algorithm(NETGEN_Algorithm):
440 ## name of the dynamic method in smeshBuilder.Mesh class
442 meshMethod = "Tetrahedron"
443 ## type of algorithm used with helper function in smeshBuilder.Mesh class
446 ## flag pointing either this algorithm should be used by default in dynamic method
447 # of smeshBuilder.Mesh class
450 ## doc string of the method
452 docHelper = "Creates tetrahedron 3D algorithm for solids"
454 ## Private constructor.
455 # @param mesh parent mesh object algorithm is assigned to
456 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
457 # if it is @c 0 (default), the algorithm is assigned to the main shape
458 def __init__(self, mesh, geom=0):
459 NETGEN_Algorithm.__init__(self, mesh, geom)
462 ## Defines @c MaxElementVolume hypothesis to specify the maximum volume value of each tetrahedron
463 # @param vol maximum volume value of each tetrahedron
464 # @param UseExisting if \c True - searches for the existing hypothesis created with
465 # the same parameters, else (default) - creates a new one
466 # @return hypothesis object
467 def MaxElementVolume(self, vol, UseExisting=0):
468 compFun = lambda hyp, args: IsEqual(hyp.GetMaxElementVolume(), args[0])
469 hyp = self.Hypothesis("MaxElementVolume", [vol], UseExisting=UseExisting,
470 CompareMethod=compFun)
471 hyp.SetMaxElementVolume(vol)
474 pass # end of NETGEN_3D_Algorithm class
477 ## Triangle (helper) 1D-2D algorithm
479 # This is the helper class that is used just to allow creating of create NETGEN_1D2D algorithm
480 # by calling smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN, geom=0 ); this is required for backward compatibility
481 # with old Python scripts.
483 # @note This class (and corresponding smeshBuilder.Mesh function) is obsolete;
484 # use smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_1D2D, geom=0 ) instead.
485 class NETGEN_1D2D_Algorithm_2(NETGEN_1D2D_Algorithm):
487 ## name of the dynamic method in smeshBuilder.Mesh class
491 ## Private constructor.
492 # @param mesh parent mesh object algorithm is assigned to
493 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
494 # if it is @c 0 (default), the algorithm is assigned to the main shape
495 def __init__(self, mesh, geom=0):
496 self.algoType = NETGEN_1D2D
497 NETGEN_1D2D_Algorithm.__init__(self,mesh, geom)
500 pass # end of NETGEN_1D2D_Algorithm_2 class
503 ## Tetrahedron (helper) 1D-2D-3D algorithm.
505 # This is the helper class that is used just to allow creating of create NETGEN_1D2D3D
506 # by calling smeshBuilder.Mesh.Netgen(); this is required for backward compatibility with old Python scripts.
508 # @note This class (and corresponding smeshBuilder.Mesh function) is obsolete;
509 # use smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN_1D2D3D, geom=0 ) instead.
510 class NETGEN_1D2D3D_Algorithm_2(NETGEN_1D2D3D_Algorithm):
512 ## name of the dynamic method in smeshBuilder.Mesh class
514 meshMethod = "Netgen"
515 ## doc string of the method
517 docHelper = "Deprecated, used only for compatibility! See Tetrahedron() method."
519 ## Private constructor.
520 # @param mesh parent mesh object algorithm is assigned to
521 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
522 # if it is @c 0 (default), the algorithm is assigned to the main shape
523 def __init__(self, mesh, geom=0):
524 NETGEN_1D2D3D_Algorithm.__init__(self,mesh, geom)
527 pass # end of NETGEN_1D2D3D_Algorithm_2 class