1 # Copyright (C) 2007-2024 CEA, EDF, 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 NETGEN_3D_Remote = "NETGEN_3D_Remote"
46 ## Algorithm type: Netgen tetrahedron 1D-2D-3D algorithm, see NETGEN_1D2D3D_Algorithm
47 NETGEN_1D2D3D = "NETGEN_2D3D"
48 ## Algorithm type: Netgen triangle 1D-2D algorithm, see NETGEN_1D2D_Algorithm
49 NETGEN_1D2D = "NETGEN_2D"
50 ## Algorithm type: Netgen triangle 2D algorithm, see NETGEN_2D_Only_Algorithm
51 NETGEN_2D = "NETGEN_2D_ONLY"
52 NETGEN_2D_Remote = "NETGEN_2D_Remote"
53 ## Algorithm type: Synonim of NETGEN_1D2D3D, see NETGEN_1D2D3D_Algorithm
54 NETGEN_FULL = NETGEN_1D2D3D
55 ## Algorithm type: Synonim of NETGEN_3D, see NETGEN_3D_Algorithm
57 ## Algorithm type: Synonim of NETGEN_1D2D3D, see NETGEN_1D2D3D_Algorithm
58 FULL_NETGEN = NETGEN_FULL
60 #----------------------------
61 # Hypothesis type enumeration
62 #----------------------------
64 ## Hypothesis type enumeration: complex hypothesis
65 # (full set of parameters can be specified),
66 # see NETGEN_Algorithm.Parameters()
68 ## Hypothesis type enumeration: simple hypothesis
69 # (only major parameters are specified),
70 # see NETGEN_Algorithm.Parameters()
73 #----------------------
74 # Fineness enumeration
75 #----------------------
77 ## Fineness enumeration: very coarse quality of mesh,
78 # see NETGEN_Algorithm.SetFineness()
80 ## Fineness enumeration: coarse quality of mesh,
81 # see NETGEN_Algorithm.SetFineness()
83 ## Fineness enumeration: moderate quality of mesh,
84 # see NETGEN_Algorithm.SetFineness()
86 ## Fineness enumeration: fine quality of mesh,
87 # see NETGEN_Algorithm.SetFineness()
89 ## Fineness enumeration: very fine quality of mesh,
90 # see NETGEN_Algorithm.SetFineness()
92 ## Fineness enumeration: custom quality of mesh specified by other parameters),
93 # see NETGEN_Algorithm.SetFineness()
96 #----------------------
98 #----------------------
100 ## Base of all NETGEN algorithms.
102 # This class provides common methods for all algorithms implemented by NETGEN plugin.
103 # @note This class must not be instantiated directly.
104 class NETGEN_Algorithm(Mesh_Algorithm):
106 ## Private constructor
107 # @param mesh parent mesh object algorithm is assigned to
108 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
109 # if it is @c 0 (default), the algorithm is assigned to the main shape
110 def __init__(self, mesh, geom=0):
111 Mesh_Algorithm.__init__(self)
112 if noNETGENPlugin: print("Warning: NETGENPlugin module unavailable")
113 if not mesh.GetMesh().HasShapeToMesh() and \
114 self.meshMethod == "Triangle": # create a 2D remesher
115 self.Create(mesh, geom, "NETGEN_Remesher_2D", LIBRARY)
117 self.Create(mesh, geom, self.algoType, LIBRARY)
121 ## Sets @c MaxSize parameter
122 # @param theSize new value of the @c MaxSize parameter
123 def SetMaxSize(self, theSize):
124 if self.Parameters(): self.params.SetMaxSize(theSize)
127 ## Sets @c MinSize parameter
128 # @param theSize new value of the @c MinSize parameter
129 def SetMinSize(self, theSize):
130 if self.Parameters(): self.params.SetMinSize(theSize)
133 ## Sets @c Optimize flag
134 # @param theVal new value of the @c Optimize parameter
135 def SetOptimize(self, theVal):
136 if self.Parameters(): self.params.SetOptimize(theVal)
139 ## Sets @c Fineness parameter
140 # @param theFineness new value of the @c Fineness parameter; it can be:
141 # @ref VeryCoarse, @ref Coarse, @ref Moderate, @ref Fine, @ref VeryFine or @ref Custom
142 def SetFineness(self, theFineness):
143 if self.Parameters(): self.params.SetFineness(theFineness)
146 ## Sets @c GrowthRate parameter
147 # @param theRate new value of the @c GrowthRate parameter
148 def SetGrowthRate(self, theRate):
149 if self.Parameters(): self.params.SetGrowthRate(theRate)
152 ## Sets @c NbThreads parameter
153 # @param theRate new value of the @c NbThreads parameter
154 def SetNbThreads(self, theNumber):
155 if self.Parameters(): self.params.SetNbThreads(theNumber)
158 ## Creates meshing hypothesis according to the chosen algorithm type
159 # and initializes it with default parameters
160 # @param which hypothesis type; can be either @ref SOLE (default) or @ref SIMPLE
161 # @return hypothesis object
162 def Parameters(self, which=SOLE):
163 if self.algoType == NETGEN_1D2D:
165 hypType = "NETGEN_SimpleParameters_2D"
167 hypType = "NETGEN_Parameters_2D"
168 elif self.algoType == NETGEN_1D2D3D:
170 hypType = "NETGEN_SimpleParameters_3D"
172 hypType = "NETGEN_Parameters"
173 elif self.algoType in [NETGEN_2D, NETGEN_2D_Remote]:
174 hypType = "NETGEN_Parameters_2D_ONLY"
176 hypType = "NETGEN_Parameters_3D"
178 if self.algo.GetName() == "NETGEN_Remesher_2D":
179 hypType = "NETGEN_RemesherParameters_2D"
181 if self.params and self.params.GetName() != hypType:
182 self.mesh.RemoveHypothesis( self.params, self.geom )
185 self.params = self.Hypothesis(hypType, [], LIBRARY, UseExisting=0)
189 ## Defines a file specifying size of elements at points and lines
190 # @param file name of the file
191 def SetMeshSizeFile(self, file):
192 self.Parameters().SetMeshSizeFile(file)
195 ## Set size of elements on a shape
196 # @param shape - geometry
197 # @param size - element size
198 def SetLocalSizeOnShape(self, shape, size ):
199 self.Parameters().SetLocalSizeOnShape(shape, size)
203 pass # end of NETGEN_Algorithm class
206 ## Tetrahedron 1D-2D-3D algorithm.
208 # It can be created by calling smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN_1D2D3D, geom=0 ).
209 # This algorithm generates all 1D (edges), 2D (faces) and 3D (volumes) elements
210 # for given geometrical shape.
211 class NETGEN_1D2D3D_Algorithm(NETGEN_Algorithm):
213 ## name of the dynamic method in smeshBuilder.Mesh class
215 meshMethod = "Tetrahedron"
216 ## type of algorithm used with helper function in smeshBuilder.Mesh class
218 algoType = NETGEN_1D2D3D
219 ## doc string of the method
221 docHelper = "Creates tetrahedron 3D algorithm for solids"
223 ## Private constructor.
224 # @param mesh parent mesh object algorithm is assigned to
225 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
226 # if it is @c 0 (default), the algorithm is assigned to the main shape
227 def __init__(self, mesh, geom=0):
228 NETGEN_Algorithm.__init__(self, mesh, geom)
231 ## Sets @c SecondOrder flag
232 # @param theVal new value of the @c SecondOrder parameter
233 def SetSecondOrder(self, theVal):
234 if self.Parameters(): self.params.SetSecondOrder(theVal)
237 ## Sets @c NbSegPerEdge parameter
238 # @param theVal new value of the @c NbSegPerEdge parameter
239 def SetNbSegPerEdge(self, theVal):
240 if self.Parameters(): self.params.SetNbSegPerEdge(theVal)
243 ## Sets @c NbSegPerRadius parameter
244 # @param theVal new value of the @c NbSegPerRadius parameter
245 def SetNbSegPerRadius(self, theVal):
246 if self.Parameters(): self.params.SetNbSegPerRadius(theVal)
249 ## Sets @c ChordalError parameter
250 # @param theVal new value of the @c ChordalError parameter
251 def SetChordalError(self, theVal):
252 if self.Parameters():
253 self.params.SetChordalError(theVal)
254 self.params.SetChordalErrorEnabled( theVal > 0 )
257 ## Sets @c RidgeAngle parameter
258 # @param theVal new value of the @c RidgeAngle parameter
259 def SetRidgeAngle(self, theVal):
260 if self.Parameters():
261 self.params.SetRidgeAngle(theVal)
264 ## Sets @c QuadAllowed flag
265 # @param toAllow new value of the @c QuadAllowed parameter (@c True by default)
266 def SetQuadAllowed(self, toAllow=True):
267 if self.Parameters(): self.params.SetQuadAllowed(toAllow)
269 ## Sets @c UseSurfaceCurvature flag
270 # @param toUse new value of the @c UseSurfaceCurvature parameter (@c True by default)
271 def SetUseSurfaceCurvature(self, toUse=True):
272 if self.Parameters(): self.params.SetUseSurfaceCurvature(toUse)
274 ## Sets @c FuseEdges flag
275 # @param toFuse new value of the @c FuseEdges parameter (@c False by default)
276 def SetFuseEdges(self, toFuse=False):
277 if self.Parameters(): self.params.SetFuseEdges(toFuse)
280 ## Sets number of segments overriding the value set by SetLocalLength()
281 # @param theVal new value of number of segments parameter
282 def SetNumberOfSegments(self, theVal):
283 self.Parameters(SIMPLE).SetNumberOfSegments(theVal)
286 ## Sets number of segments overriding the value set by SetNumberOfSegments()
287 # @param theVal new value of local length parameter
288 def SetLocalLength(self, theVal):
289 self.Parameters(SIMPLE).SetLocalLength(theVal)
292 ## Defines @c MaxElementArea parameter of @c NETGEN_SimpleParameters_3D hypothesis.
293 # Overrides value set by LengthFromEdges()
294 # @param area new value of @c MaxElementArea parameter
295 def MaxElementArea(self, area):
296 self.Parameters(SIMPLE).SetMaxElementArea(area)
299 ## Defines @c LengthFromEdges parameter of @c NETGEN_SimpleParameters_3D hypothesis.
300 # Overrides value set by MaxElementArea()
301 def LengthFromEdges(self):
302 self.Parameters(SIMPLE).LengthFromEdges()
305 ## Defines @c LengthFromFaces parameter of @c NETGEN_SimpleParameters_3D hypothesis.
306 # Overrides value set by MaxElementVolume()
307 def LengthFromFaces(self):
308 self.Parameters(SIMPLE).LengthFromFaces()
311 ## Defines @c MaxElementVolume parameter of @c NETGEN_SimpleParameters_3D hypothesis.
312 # Overrides value set by LengthFromFaces()
313 # @param vol new value of @c MaxElementVolume parameter
314 def MaxElementVolume(self, vol):
315 self.Parameters(SIMPLE).SetMaxElementVolume(vol)
318 pass # end of NETGEN_1D2D3D_Algorithm class
321 ## Triangle NETGEN 1D-2D algorithm.
323 # It can be created by calling smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_1D2D, geom=0 )
325 # This algorithm generates 1D (edges) and 2D (faces) elements
326 # for given geometrical shape.
327 class NETGEN_1D2D_Algorithm(NETGEN_1D2D3D_Algorithm):
329 ## name of the dynamic method in smeshBuilder.Mesh class
331 meshMethod = "Triangle"
332 ## type of algorithm used with helper function in smeshBuilder.Mesh class
334 algoType = NETGEN_1D2D
335 ## doc string of the method
337 docHelper = "Creates triangle 2D algorithm for faces"
340 ## Private constructor.
341 # @param mesh parent mesh object algorithm is assigned to
342 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
343 # if it is @c 0 (default), the algorithm is assigned to the main shape
344 def __init__(self, mesh, geom=0):
345 NETGEN_1D2D3D_Algorithm.__init__(self, mesh, geom)
348 pass # end of NETGEN_1D2D_Algorithm class
351 ## Triangle NETGEN 2D algorithm
353 # It can be created by calling smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_2D, geom=0 )
355 # This algorithm generates only 2D (faces) elements for given geometrical shape
356 # and, in contrast to NETGEN_1D2D_Algorithm class, should be used in conjunction
357 # with other 1D meshing algorithm.
358 class NETGEN_2D_Only_Algorithm(NETGEN_Algorithm):
360 ## name of the dynamic method in smeshBuilder.Mesh class
362 meshMethod = "Triangle"
363 ## type of algorithm used with helper function in smeshBuilder.Mesh class
366 ## flag pointing whether this algorithm should be used by default in dynamic method
367 # of smeshBuilder.Mesh class
369 ## doc string of the method
371 docHelper = "Creates triangle 2D algorithm for faces"
375 ## Private constructor.
376 # @param mesh parent mesh object algorithm is assigned to
377 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
378 # if it is @c 0 (default), the algorithm is assigned to the main shape
379 def __init__(self, mesh, geom=0):
380 NETGEN_Algorithm.__init__(self, mesh, geom)
383 ## Defines @c MaxElementArea parameter of hypothesis basing on the definition of the
384 # maximum area of each triangle
385 # @param area maximum area value of each triangle
386 # @param UseExisting if \c True - searches for an existing hypothesis created with the
387 # same parameters, else (default) - creates a new one
388 # @return hypothesis object
389 def MaxElementArea(self, area, UseExisting=0):
390 compFun = lambda hyp, args: IsEqual(hyp.GetMaxElementArea(), args[0])
391 hyp = self.Hypothesis("MaxElementArea", [area], UseExisting=UseExisting,
392 CompareMethod=compFun)
393 hyp.SetMaxElementArea(area)
396 ## Defines @c LengthFromEdges hypothesis to build triangles
397 # based on the length of the edges taken from the wire
398 # @return hypothesis object
399 def LengthFromEdges(self):
400 hyp = self.Hypothesis("LengthFromEdges", UseExisting=1, CompareMethod=self.CompareEqualHyp)
403 ## Sets @c UseSurfaceCurvature flag
404 # @param toUse new value of the @c UseSurfaceCurvature parameter (@c True by default)
405 def SetUseSurfaceCurvature(self, toUse=True):
406 if self.Parameters(): self.params.SetUseSurfaceCurvature(toUse)
409 ## Sets @c QuadAllowed flag.
410 # @param toAllow new value of the @c QuadAllowed parameter (@c True by default)
411 # @return hypothesis object
412 def SetQuadAllowed(self, toAllow=True):
415 hasSimpleHyps = False
416 simpleHyps = ["QuadranglePreference","LengthFromEdges","MaxElementArea"]
417 for hyp in self.mesh.GetHypothesisList( self.geom ):
418 if hyp.GetName() in simpleHyps:
420 if hyp.GetName() == "QuadranglePreference":
421 if not toAllow: # remove QuadranglePreference
422 self.mesh.RemoveHypothesis( self.geom, hyp )
429 if toAllow: # add QuadranglePreference
430 return self.Hypothesis("QuadranglePreference", UseExisting=1, CompareMethod=self.CompareEqualHyp)
433 self.Parameters().SetQuadAllowed( toAllow )
436 pass # end of NETGEN_2D_Only_Algorithm class
439 ## Tetrahedron 3D algorithm
441 # It can be created by calling smeshBuilder.Mesh.Tetrahedron() or smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN, geom=0 )
443 # This algorithm generates only 3D (volumes) elements for given geometrical shape
444 # and, in contrast to NETGEN_1D2D3D_Algorithm class, should be used in conjunction
445 # with other 1D and 2D meshing algorithms.
446 class NETGEN_3D_Algorithm(NETGEN_Algorithm):
448 ## name of the dynamic method in smeshBuilder.Mesh class
450 meshMethod = "Tetrahedron"
451 ## type of algorithm used with helper function in smeshBuilder.Mesh class
454 ## flag pointing either this algorithm should be used by default in dynamic method
455 # of smeshBuilder.Mesh class
458 ## doc string of the method
460 docHelper = "Creates tetrahedron 3D algorithm for solids"
462 ## Private constructor.
463 # @param mesh parent mesh object algorithm is assigned to
464 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
465 # if it is @c 0 (default), the algorithm is assigned to the main shape
466 def __init__(self, mesh, geom=0):
467 NETGEN_Algorithm.__init__(self, mesh, geom)
470 ## Defines @c MaxElementVolume hypothesis to specify the maximum volume value of each tetrahedron
471 # @param vol maximum volume value of each tetrahedron
472 # @param UseExisting if \c True - searches for the existing hypothesis created with
473 # the same parameters, else (default) - creates a new one
474 # @return hypothesis object
475 def MaxElementVolume(self, vol, UseExisting=0):
476 compFun = lambda hyp, args: IsEqual(hyp.GetMaxElementVolume(), args[0])
477 hyp = self.Hypothesis("MaxElementVolume", [vol], UseExisting=UseExisting,
478 CompareMethod=compFun)
479 hyp.SetMaxElementVolume(vol)
482 pass # end of NETGEN_3D_Algorithm class
484 ## Tetrahedron 3D algorithm
486 # It can be created by calling smeshBuilder.Mesh.Tetrahedron() or smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN, geom=0 )
488 # This algorithm generates only 3D (volumes) elements for given geometrical shape
489 # and, in contrast to NETGEN_1D2D3D_Algorithm class, should be used in conjunction
490 # with other 1D and 2D meshing algorithms.
491 class NETGEN_3D_Remote_Algorithm(NETGEN_3D_Algorithm):
493 ## type of algorithm used with helper function in smeshBuilder.Mesh class
495 algoType = NETGEN_3D_Remote
496 ## flag pointing either this algorithm should be used by default in dynamic method
497 # of smeshBuilder.Mesh class
500 ## doc string of the method
502 docHelper = "Remotely Creates tetrahedron 3D algorithm for solids"
504 ## Private constructor.
505 # @param mesh parent mesh object algorithm is assigned to
506 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
507 # if it is @c 0 (default), the algorithm is assigned to the main shape
508 def __init__(self, mesh, geom=0):
509 NETGEN_3D_Algorithm.__init__(self, mesh, geom)
512 pass # end of NETGEN_3D_Remote_Algorithm class
514 ## Tetrahedron 2D algorithm
516 # It can be created by calling smeshBuilder.Mesh.Triangle() or smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN, geom=0 )
518 class NETGEN_2D_Remote_Algorithm(NETGEN_2D_Only_Algorithm):
520 ## type of algorithm used with helper function in smeshBuilder.Mesh class
522 algoType = NETGEN_2D_Remote
523 ## flag pointing either this algorithm should be used by default in dynamic method
524 # of smeshBuilder.Mesh class
527 ## doc string of the method
529 docHelper = "Remotely Creates triangles in face of solids"
531 ## Private constructor.
532 # @param mesh parent mesh object algorithm is assigned to
533 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
534 # if it is @c 0 (default), the algorithm is assigned to the main shape
535 def __init__(self, mesh, geom=0):
536 self.algoType = NETGEN_2D_Remote
537 NETGEN_2D_Only_Algorithm.__init__(self, mesh, geom)
540 pass # end of NETGEN_2D_Remote_Algorithm class
543 ## Triangle (helper) 1D-2D algorithm
545 # This is the helper class that is used just to allow creating of create NETGEN_1D2D algorithm
546 # by calling smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN, geom=0 ); this is required for backward compatibility
547 # with old Python scripts.
549 # @note This class (and corresponding smeshBuilder.Mesh function) is obsolete;
550 # use smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_1D2D, geom=0 ) instead.
551 class NETGEN_1D2D_Algorithm_2(NETGEN_1D2D_Algorithm):
553 ## name of the dynamic method in smeshBuilder.Mesh class
557 ## Private constructor.
558 # @param mesh parent mesh object algorithm is assigned to
559 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
560 # if it is @c 0 (default), the algorithm is assigned to the main shape
561 def __init__(self, mesh, geom=0):
562 self.algoType = NETGEN_1D2D
563 NETGEN_1D2D_Algorithm.__init__(self,mesh, geom)
566 pass # end of NETGEN_1D2D_Algorithm_2 class
569 ## Tetrahedron (helper) 1D-2D-3D algorithm.
571 # This is the helper class that is used just to allow creating of create NETGEN_1D2D3D
572 # by calling smeshBuilder.Mesh.Netgen(); this is required for backward compatibility with old Python scripts.
574 # @note This class (and corresponding smeshBuilder.Mesh function) is obsolete;
575 # use smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN_1D2D3D, geom=0 ) instead.
576 class NETGEN_1D2D3D_Algorithm_2(NETGEN_1D2D3D_Algorithm):
578 ## name of the dynamic method in smeshBuilder.Mesh class
580 meshMethod = "Netgen"
581 ## doc string of the method
583 docHelper = "Deprecated, used only for compatibility! See Tetrahedron() method."
585 ## Private constructor.
586 # @param mesh parent mesh object algorithm is assigned to
587 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
588 # if it is @c 0 (default), the algorithm is assigned to the main shape
589 def __init__(self, mesh, geom=0):
590 NETGEN_1D2D3D_Algorithm.__init__(self,mesh, geom)
593 pass # end of NETGEN_1D2D3D_Algorithm_2 class