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 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 ## Algorithm type: Synonim of NETGEN_1D2D3D, see NETGEN_1D2D3D_Algorithm
53 NETGEN_FULL = NETGEN_1D2D3D
54 ## Algorithm type: Synonim of NETGEN_3D, see NETGEN_3D_Algorithm
56 ## Algorithm type: Synonim of NETGEN_1D2D3D, see NETGEN_1D2D3D_Algorithm
57 FULL_NETGEN = NETGEN_FULL
59 #----------------------------
60 # Hypothesis type enumeration
61 #----------------------------
63 ## Hypothesis type enumeration: complex hypothesis
64 # (full set of parameters can be specified),
65 # see NETGEN_Algorithm.Parameters()
67 ## Hypothesis type enumeration: simple hypothesis
68 # (only major parameters are specified),
69 # see NETGEN_Algorithm.Parameters()
72 #----------------------
73 # Fineness enumeration
74 #----------------------
76 ## Fineness enumeration: very coarse quality of mesh,
77 # see NETGEN_Algorithm.SetFineness()
79 ## Fineness enumeration: coarse quality of mesh,
80 # see NETGEN_Algorithm.SetFineness()
82 ## Fineness enumeration: moderate quality of mesh,
83 # see NETGEN_Algorithm.SetFineness()
85 ## Fineness enumeration: fine quality of mesh,
86 # see NETGEN_Algorithm.SetFineness()
88 ## Fineness enumeration: very fine quality of mesh,
89 # see NETGEN_Algorithm.SetFineness()
91 ## Fineness enumeration: custom quality of mesh specified by other parameters),
92 # see NETGEN_Algorithm.SetFineness()
95 #----------------------
97 #----------------------
99 ## Base of all NETGEN algorithms.
101 # This class provides common methods for all algorithms implemented by NETGEN plugin.
102 # @note This class must not be instantiated directly.
103 class NETGEN_Algorithm(Mesh_Algorithm):
105 ## Private constructor
106 # @param mesh parent mesh object algorithm is assigned to
107 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
108 # if it is @c 0 (default), the algorithm is assigned to the main shape
109 def __init__(self, mesh, geom=0):
110 Mesh_Algorithm.__init__(self)
111 if noNETGENPlugin: print("Warning: NETGENPlugin module unavailable")
112 if not mesh.GetMesh().HasShapeToMesh() and \
113 self.meshMethod == "Triangle": # create a 2D remesher
114 self.Create(mesh, geom, "NETGEN_Remesher_2D", LIBRARY)
116 self.Create(mesh, geom, self.algoType, LIBRARY)
120 ## Sets @c MaxSize parameter
121 # @param theSize new value of the @c MaxSize parameter
122 def SetMaxSize(self, theSize):
123 if self.Parameters(): self.params.SetMaxSize(theSize)
126 ## Sets @c MinSize parameter
127 # @param theSize new value of the @c MinSize parameter
128 def SetMinSize(self, theSize):
129 if self.Parameters(): self.params.SetMinSize(theSize)
132 ## Sets @c Optimize flag
133 # @param theVal new value of the @c Optimize parameter
134 def SetOptimize(self, theVal):
135 if self.Parameters(): self.params.SetOptimize(theVal)
138 ## Sets @c Fineness parameter
139 # @param theFineness new value of the @c Fineness parameter; it can be:
140 # @ref VeryCoarse, @ref Coarse, @ref Moderate, @ref Fine, @ref VeryFine or @ref Custom
141 def SetFineness(self, theFineness):
142 if self.Parameters(): self.params.SetFineness(theFineness)
145 ## Sets @c GrowthRate parameter
146 # @param theRate new value of the @c GrowthRate parameter
147 def SetGrowthRate(self, theRate):
148 if self.Parameters(): self.params.SetGrowthRate(theRate)
151 ## Creates meshing hypothesis according to the chosen algorithm type
152 # and initializes it with default parameters
153 # @param which hypothesis type; can be either @ref SOLE (default) or @ref SIMPLE
154 # @return hypothesis object
155 def Parameters(self, which=SOLE):
156 if self.algoType == NETGEN_1D2D:
158 hypType = "NETGEN_SimpleParameters_2D"
160 hypType = "NETGEN_Parameters_2D"
161 elif self.algoType == NETGEN_1D2D3D:
163 hypType = "NETGEN_SimpleParameters_3D"
165 hypType = "NETGEN_Parameters"
166 elif self.algoType == NETGEN_2D:
167 hypType = "NETGEN_Parameters_2D_ONLY"
169 hypType = "NETGEN_Parameters_3D"
171 if self.algo.GetName() == "NETGEN_Remesher_2D":
172 hypType = "NETGEN_RemesherParameters_2D"
174 if self.params and self.params.GetName() != hypType:
175 self.mesh.RemoveHypothesis( self.params, self.geom )
178 self.params = self.Hypothesis(hypType, [], LIBRARY, UseExisting=0)
182 ## Defines a file specifying size of elements at points and lines
183 # @param file name of the file
184 def SetMeshSizeFile(self, file):
185 self.Parameters().SetMeshSizeFile(file)
188 ## Set size of elements on a shape
189 # @param shape - geometry
190 # @param size - element size
191 def SetLocalSizeOnShape(self, shape, size ):
192 self.Parameters().SetLocalSizeOnShape(shape, size)
196 pass # end of NETGEN_Algorithm class
199 ## Tetrahedron 1D-2D-3D algorithm.
201 # It can be created by calling smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN_1D2D3D, geom=0 ).
202 # This algorithm generates all 1D (edges), 2D (faces) and 3D (volumes) elements
203 # for given geometrical shape.
204 class NETGEN_1D2D3D_Algorithm(NETGEN_Algorithm):
206 ## name of the dynamic method in smeshBuilder.Mesh class
208 meshMethod = "Tetrahedron"
209 ## type of algorithm used with helper function in smeshBuilder.Mesh class
211 algoType = NETGEN_1D2D3D
212 ## doc string of the method
214 docHelper = "Creates tetrahedron 3D algorithm for solids"
216 ## Private constructor.
217 # @param mesh parent mesh object algorithm is assigned to
218 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
219 # if it is @c 0 (default), the algorithm is assigned to the main shape
220 def __init__(self, mesh, geom=0):
221 NETGEN_Algorithm.__init__(self, mesh, geom)
224 ## Sets @c SecondOrder flag
225 # @param theVal new value of the @c SecondOrder parameter
226 def SetSecondOrder(self, theVal):
227 if self.Parameters(): self.params.SetSecondOrder(theVal)
230 ## Sets @c NbSegPerEdge parameter
231 # @param theVal new value of the @c NbSegPerEdge parameter
232 def SetNbSegPerEdge(self, theVal):
233 if self.Parameters(): self.params.SetNbSegPerEdge(theVal)
236 ## Sets @c NbSegPerRadius parameter
237 # @param theVal new value of the @c NbSegPerRadius parameter
238 def SetNbSegPerRadius(self, theVal):
239 if self.Parameters(): self.params.SetNbSegPerRadius(theVal)
242 ## Sets @c ChordalError parameter
243 # @param theVal new value of the @c ChordalError parameter
244 def SetChordalError(self, theVal):
245 if self.Parameters():
246 self.params.SetChordalError(theVal)
247 self.params.SetChordalErrorEnabled( theVal > 0 )
250 ## Sets @c RidgeAngle parameter
251 # @param theVal new value of the @c RidgeAngle parameter
252 def SetRidgeAngle(self, theVal):
253 if self.Parameters():
254 self.params.SetRidgeAngle(theVal)
257 ## Sets @c QuadAllowed flag
258 # @param toAllow new value of the @c QuadAllowed parameter (@c True by default)
259 def SetQuadAllowed(self, toAllow=True):
260 if self.Parameters(): self.params.SetQuadAllowed(toAllow)
262 ## Sets @c UseSurfaceCurvature flag
263 # @param toUse new value of the @c UseSurfaceCurvature parameter (@c True by default)
264 def SetUseSurfaceCurvature(self, toUse=True):
265 if self.Parameters(): self.params.SetUseSurfaceCurvature(toUse)
267 ## Sets @c FuseEdges flag
268 # @param toFuse new value of the @c FuseEdges parameter (@c False by default)
269 def SetFuseEdges(self, toFuse=False):
270 if self.Parameters(): self.params.SetFuseEdges(toFuse)
273 ## Sets number of segments overriding the value set by SetLocalLength()
274 # @param theVal new value of number of segments parameter
275 def SetNumberOfSegments(self, theVal):
276 self.Parameters(SIMPLE).SetNumberOfSegments(theVal)
279 ## Sets number of segments overriding the value set by SetNumberOfSegments()
280 # @param theVal new value of local length parameter
281 def SetLocalLength(self, theVal):
282 self.Parameters(SIMPLE).SetLocalLength(theVal)
285 ## Defines @c MaxElementArea parameter of @c NETGEN_SimpleParameters_3D hypothesis.
286 # Overrides value set by LengthFromEdges()
287 # @param area new value of @c MaxElementArea parameter
288 def MaxElementArea(self, area):
289 self.Parameters(SIMPLE).SetMaxElementArea(area)
292 ## Defines @c LengthFromEdges parameter of @c NETGEN_SimpleParameters_3D hypothesis.
293 # Overrides value set by MaxElementArea()
294 def LengthFromEdges(self):
295 self.Parameters(SIMPLE).LengthFromEdges()
298 ## Defines @c LengthFromFaces parameter of @c NETGEN_SimpleParameters_3D hypothesis.
299 # Overrides value set by MaxElementVolume()
300 def LengthFromFaces(self):
301 self.Parameters(SIMPLE).LengthFromFaces()
304 ## Defines @c MaxElementVolume parameter of @c NETGEN_SimpleParameters_3D hypothesis.
305 # Overrides value set by LengthFromFaces()
306 # @param vol new value of @c MaxElementVolume parameter
307 def MaxElementVolume(self, vol):
308 self.Parameters(SIMPLE).SetMaxElementVolume(vol)
311 pass # end of NETGEN_1D2D3D_Algorithm class
314 ## Triangle NETGEN 1D-2D algorithm.
316 # It can be created by calling smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_1D2D, geom=0 )
318 # This algorithm generates 1D (edges) and 2D (faces) elements
319 # for given geometrical shape.
320 class NETGEN_1D2D_Algorithm(NETGEN_1D2D3D_Algorithm):
322 ## name of the dynamic method in smeshBuilder.Mesh class
324 meshMethod = "Triangle"
325 ## type of algorithm used with helper function in smeshBuilder.Mesh class
327 algoType = NETGEN_1D2D
328 ## doc string of the method
330 docHelper = "Creates triangle 2D algorithm for faces"
333 ## Private constructor.
334 # @param mesh parent mesh object algorithm is assigned to
335 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
336 # if it is @c 0 (default), the algorithm is assigned to the main shape
337 def __init__(self, mesh, geom=0):
338 NETGEN_1D2D3D_Algorithm.__init__(self, mesh, geom)
341 pass # end of NETGEN_1D2D_Algorithm class
344 ## Triangle NETGEN 2D algorithm
346 # It can be created by calling smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_2D, geom=0 )
348 # This algorithm generates only 2D (faces) elements for given geometrical shape
349 # and, in contrast to NETGEN_1D2D_Algorithm class, should be used in conjunction
350 # with other 1D meshing algorithm.
351 class NETGEN_2D_Only_Algorithm(NETGEN_Algorithm):
353 ## name of the dynamic method in smeshBuilder.Mesh class
355 meshMethod = "Triangle"
356 ## type of algorithm used with helper function in smeshBuilder.Mesh class
359 ## flag pointing whether this algorithm should be used by default in dynamic method
360 # of smeshBuilder.Mesh class
362 ## doc string of the method
364 docHelper = "Creates triangle 2D algorithm for faces"
368 ## Private constructor.
369 # @param mesh parent mesh object algorithm is assigned to
370 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
371 # if it is @c 0 (default), the algorithm is assigned to the main shape
372 def __init__(self, mesh, geom=0):
373 NETGEN_Algorithm.__init__(self, mesh, geom)
376 ## Defines @c MaxElementArea parameter of hypothesis basing on the definition of the
377 # maximum area of each triangle
378 # @param area maximum area value of each triangle
379 # @param UseExisting if \c True - searches for an existing hypothesis created with the
380 # same parameters, else (default) - creates a new one
381 # @return hypothesis object
382 def MaxElementArea(self, area, UseExisting=0):
383 compFun = lambda hyp, args: IsEqual(hyp.GetMaxElementArea(), args[0])
384 hyp = self.Hypothesis("MaxElementArea", [area], UseExisting=UseExisting,
385 CompareMethod=compFun)
386 hyp.SetMaxElementArea(area)
389 ## Defines @c LengthFromEdges hypothesis to build triangles
390 # based on the length of the edges taken from the wire
391 # @return hypothesis object
392 def LengthFromEdges(self):
393 hyp = self.Hypothesis("LengthFromEdges", UseExisting=1, CompareMethod=self.CompareEqualHyp)
396 ## Sets @c UseSurfaceCurvature flag
397 # @param toUse new value of the @c UseSurfaceCurvature parameter (@c True by default)
398 def SetUseSurfaceCurvature(self, toUse=True):
399 if self.Parameters(): self.params.SetUseSurfaceCurvature(toUse)
402 ## Sets @c QuadAllowed flag.
403 # @param toAllow new value of the @c QuadAllowed parameter (@c True by default)
404 # @return hypothesis object
405 def SetQuadAllowed(self, toAllow=True):
408 hasSimpleHyps = False
409 simpleHyps = ["QuadranglePreference","LengthFromEdges","MaxElementArea"]
410 for hyp in self.mesh.GetHypothesisList( self.geom ):
411 if hyp.GetName() in simpleHyps:
413 if hyp.GetName() == "QuadranglePreference":
414 if not toAllow: # remove QuadranglePreference
415 self.mesh.RemoveHypothesis( self.geom, hyp )
422 if toAllow: # add QuadranglePreference
423 return self.Hypothesis("QuadranglePreference", UseExisting=1, CompareMethod=self.CompareEqualHyp)
426 self.Parameters().SetQuadAllowed( toAllow )
429 pass # end of NETGEN_2D_Only_Algorithm class
432 ## Tetrahedron 3D algorithm
434 # It can be created by calling smeshBuilder.Mesh.Tetrahedron() or smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN, geom=0 )
436 # This algorithm generates only 3D (volumes) elements for given geometrical shape
437 # and, in contrast to NETGEN_1D2D3D_Algorithm class, should be used in conjunction
438 # with other 1D and 2D meshing algorithms.
439 class NETGEN_3D_Algorithm(NETGEN_Algorithm):
441 ## name of the dynamic method in smeshBuilder.Mesh class
443 meshMethod = "Tetrahedron"
444 ## type of algorithm used with helper function in smeshBuilder.Mesh class
447 ## flag pointing either this algorithm should be used by default in dynamic method
448 # of smeshBuilder.Mesh class
451 ## doc string of the method
453 docHelper = "Creates tetrahedron 3D algorithm for solids"
455 ## Private constructor.
456 # @param mesh parent mesh object algorithm is assigned to
457 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
458 # if it is @c 0 (default), the algorithm is assigned to the main shape
459 def __init__(self, mesh, geom=0):
460 NETGEN_Algorithm.__init__(self, mesh, geom)
463 ## Defines @c MaxElementVolume hypothesis to specify the maximum volume value of each tetrahedron
464 # @param vol maximum volume value of each tetrahedron
465 # @param UseExisting if \c True - searches for the existing hypothesis created with
466 # the same parameters, else (default) - creates a new one
467 # @return hypothesis object
468 def MaxElementVolume(self, vol, UseExisting=0):
469 compFun = lambda hyp, args: IsEqual(hyp.GetMaxElementVolume(), args[0])
470 hyp = self.Hypothesis("MaxElementVolume", [vol], UseExisting=UseExisting,
471 CompareMethod=compFun)
472 hyp.SetMaxElementVolume(vol)
475 pass # end of NETGEN_3D_Algorithm class
477 ## Tetrahedron 3D algorithm
479 # It can be created by calling smeshBuilder.Mesh.Tetrahedron() or smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN, geom=0 )
481 # This algorithm generates only 3D (volumes) elements for given geometrical shape
482 # and, in contrast to NETGEN_1D2D3D_Algorithm class, should be used in conjunction
483 # with other 1D and 2D meshing algorithms.
484 class NETGEN_3D_Remote_Algorithm(NETGEN_3D_Algorithm):
486 ## type of algorithm used with helper function in smeshBuilder.Mesh class
488 algoType = NETGEN_3D_Remote
489 ## flag pointing either this algorithm should be used by default in dynamic method
490 # of smeshBuilder.Mesh class
493 ## doc string of the method
495 docHelper = "Remotely Creates tetrahedron 3D algorithm for solids"
497 ## Private constructor.
498 # @param mesh parent mesh object algorithm is assigned to
499 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
500 # if it is @c 0 (default), the algorithm is assigned to the main shape
501 def __init__(self, mesh, geom=0):
502 NETGEN_3D_Algorithm.__init__(self, mesh, geom)
505 pass # end of NETGEN_3D_Remote_Algorithm class
509 ## Triangle (helper) 1D-2D algorithm
511 # This is the helper class that is used just to allow creating of create NETGEN_1D2D algorithm
512 # by calling smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN, geom=0 ); this is required for backward compatibility
513 # with old Python scripts.
515 # @note This class (and corresponding smeshBuilder.Mesh function) is obsolete;
516 # use smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_1D2D, geom=0 ) instead.
517 class NETGEN_1D2D_Algorithm_2(NETGEN_1D2D_Algorithm):
519 ## name of the dynamic method in smeshBuilder.Mesh class
523 ## Private constructor.
524 # @param mesh parent mesh object algorithm is assigned to
525 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
526 # if it is @c 0 (default), the algorithm is assigned to the main shape
527 def __init__(self, mesh, geom=0):
528 self.algoType = NETGEN_1D2D
529 NETGEN_1D2D_Algorithm.__init__(self,mesh, geom)
532 pass # end of NETGEN_1D2D_Algorithm_2 class
535 ## Tetrahedron (helper) 1D-2D-3D algorithm.
537 # This is the helper class that is used just to allow creating of create NETGEN_1D2D3D
538 # by calling smeshBuilder.Mesh.Netgen(); this is required for backward compatibility with old Python scripts.
540 # @note This class (and corresponding smeshBuilder.Mesh function) is obsolete;
541 # use smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN_1D2D3D, geom=0 ) instead.
542 class NETGEN_1D2D3D_Algorithm_2(NETGEN_1D2D3D_Algorithm):
544 ## name of the dynamic method in smeshBuilder.Mesh class
546 meshMethod = "Netgen"
547 ## doc string of the method
549 docHelper = "Deprecated, used only for compatibility! See Tetrahedron() method."
551 ## Private constructor.
552 # @param mesh parent mesh object algorithm is assigned to
553 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
554 # if it is @c 0 (default), the algorithm is assigned to the main shape
555 def __init__(self, mesh, geom=0):
556 NETGEN_1D2D3D_Algorithm.__init__(self,mesh, geom)
559 pass # end of NETGEN_1D2D3D_Algorithm_2 class