1 # Copyright (C) 2007-2023 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 ## 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 ## Sets @c NbThreads parameter
152 # @param theRate new value of the @c NbThreads parameter
153 def SetNbThreads(self, theNumber):
154 if self.Parameters(): self.params.SetNbThreads(theNumber)
157 ## Creates meshing hypothesis according to the chosen algorithm type
158 # and initializes it with default parameters
159 # @param which hypothesis type; can be either @ref SOLE (default) or @ref SIMPLE
160 # @return hypothesis object
161 def Parameters(self, which=SOLE):
162 if self.algoType == NETGEN_1D2D:
164 hypType = "NETGEN_SimpleParameters_2D"
166 hypType = "NETGEN_Parameters_2D"
167 elif self.algoType == NETGEN_1D2D3D:
169 hypType = "NETGEN_SimpleParameters_3D"
171 hypType = "NETGEN_Parameters"
172 elif self.algoType == NETGEN_2D:
173 hypType = "NETGEN_Parameters_2D_ONLY"
175 hypType = "NETGEN_Parameters_3D"
177 if self.algo.GetName() == "NETGEN_Remesher_2D":
178 hypType = "NETGEN_RemesherParameters_2D"
180 if self.params and self.params.GetName() != hypType:
181 self.mesh.RemoveHypothesis( self.params, self.geom )
184 self.params = self.Hypothesis(hypType, [], LIBRARY, UseExisting=0)
188 ## Defines a file specifying size of elements at points and lines
189 # @param file name of the file
190 def SetMeshSizeFile(self, file):
191 self.Parameters().SetMeshSizeFile(file)
194 ## Set size of elements on a shape
195 # @param shape - geometry
196 # @param size - element size
197 def SetLocalSizeOnShape(self, shape, size ):
198 self.Parameters().SetLocalSizeOnShape(shape, size)
202 pass # end of NETGEN_Algorithm class
205 ## Tetrahedron 1D-2D-3D algorithm.
207 # It can be created by calling smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN_1D2D3D, geom=0 ).
208 # This algorithm generates all 1D (edges), 2D (faces) and 3D (volumes) elements
209 # for given geometrical shape.
210 class NETGEN_1D2D3D_Algorithm(NETGEN_Algorithm):
212 ## name of the dynamic method in smeshBuilder.Mesh class
214 meshMethod = "Tetrahedron"
215 ## type of algorithm used with helper function in smeshBuilder.Mesh class
217 algoType = NETGEN_1D2D3D
218 ## doc string of the method
220 docHelper = "Creates tetrahedron 3D algorithm for solids"
222 ## Private constructor.
223 # @param mesh parent mesh object algorithm is assigned to
224 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
225 # if it is @c 0 (default), the algorithm is assigned to the main shape
226 def __init__(self, mesh, geom=0):
227 NETGEN_Algorithm.__init__(self, mesh, geom)
230 ## Sets @c SecondOrder flag
231 # @param theVal new value of the @c SecondOrder parameter
232 def SetSecondOrder(self, theVal):
233 if self.Parameters(): self.params.SetSecondOrder(theVal)
236 ## Sets @c NbSegPerEdge parameter
237 # @param theVal new value of the @c NbSegPerEdge parameter
238 def SetNbSegPerEdge(self, theVal):
239 if self.Parameters(): self.params.SetNbSegPerEdge(theVal)
242 ## Sets @c NbSegPerRadius parameter
243 # @param theVal new value of the @c NbSegPerRadius parameter
244 def SetNbSegPerRadius(self, theVal):
245 if self.Parameters(): self.params.SetNbSegPerRadius(theVal)
248 ## Sets @c ChordalError parameter
249 # @param theVal new value of the @c ChordalError parameter
250 def SetChordalError(self, theVal):
251 if self.Parameters():
252 self.params.SetChordalError(theVal)
253 self.params.SetChordalErrorEnabled( theVal > 0 )
256 ## Sets @c RidgeAngle parameter
257 # @param theVal new value of the @c RidgeAngle parameter
258 def SetRidgeAngle(self, theVal):
259 if self.Parameters():
260 self.params.SetRidgeAngle(theVal)
263 ## Sets @c QuadAllowed flag
264 # @param toAllow new value of the @c QuadAllowed parameter (@c True by default)
265 def SetQuadAllowed(self, toAllow=True):
266 if self.Parameters(): self.params.SetQuadAllowed(toAllow)
268 ## Sets @c UseSurfaceCurvature flag
269 # @param toUse new value of the @c UseSurfaceCurvature parameter (@c True by default)
270 def SetUseSurfaceCurvature(self, toUse=True):
271 if self.Parameters(): self.params.SetUseSurfaceCurvature(toUse)
273 ## Sets @c FuseEdges flag
274 # @param toFuse new value of the @c FuseEdges parameter (@c False by default)
275 def SetFuseEdges(self, toFuse=False):
276 if self.Parameters(): self.params.SetFuseEdges(toFuse)
279 ## Sets number of segments overriding the value set by SetLocalLength()
280 # @param theVal new value of number of segments parameter
281 def SetNumberOfSegments(self, theVal):
282 self.Parameters(SIMPLE).SetNumberOfSegments(theVal)
285 ## Sets number of segments overriding the value set by SetNumberOfSegments()
286 # @param theVal new value of local length parameter
287 def SetLocalLength(self, theVal):
288 self.Parameters(SIMPLE).SetLocalLength(theVal)
291 ## Defines @c MaxElementArea parameter of @c NETGEN_SimpleParameters_3D hypothesis.
292 # Overrides value set by LengthFromEdges()
293 # @param area new value of @c MaxElementArea parameter
294 def MaxElementArea(self, area):
295 self.Parameters(SIMPLE).SetMaxElementArea(area)
298 ## Defines @c LengthFromEdges parameter of @c NETGEN_SimpleParameters_3D hypothesis.
299 # Overrides value set by MaxElementArea()
300 def LengthFromEdges(self):
301 self.Parameters(SIMPLE).LengthFromEdges()
304 ## Defines @c LengthFromFaces parameter of @c NETGEN_SimpleParameters_3D hypothesis.
305 # Overrides value set by MaxElementVolume()
306 def LengthFromFaces(self):
307 self.Parameters(SIMPLE).LengthFromFaces()
310 ## Defines @c MaxElementVolume parameter of @c NETGEN_SimpleParameters_3D hypothesis.
311 # Overrides value set by LengthFromFaces()
312 # @param vol new value of @c MaxElementVolume parameter
313 def MaxElementVolume(self, vol):
314 self.Parameters(SIMPLE).SetMaxElementVolume(vol)
317 pass # end of NETGEN_1D2D3D_Algorithm class
320 ## Triangle NETGEN 1D-2D algorithm.
322 # It can be created by calling smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_1D2D, geom=0 )
324 # This algorithm generates 1D (edges) and 2D (faces) elements
325 # for given geometrical shape.
326 class NETGEN_1D2D_Algorithm(NETGEN_1D2D3D_Algorithm):
328 ## name of the dynamic method in smeshBuilder.Mesh class
330 meshMethod = "Triangle"
331 ## type of algorithm used with helper function in smeshBuilder.Mesh class
333 algoType = NETGEN_1D2D
334 ## doc string of the method
336 docHelper = "Creates triangle 2D algorithm for faces"
339 ## Private constructor.
340 # @param mesh parent mesh object algorithm is assigned to
341 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
342 # if it is @c 0 (default), the algorithm is assigned to the main shape
343 def __init__(self, mesh, geom=0):
344 NETGEN_1D2D3D_Algorithm.__init__(self, mesh, geom)
347 pass # end of NETGEN_1D2D_Algorithm class
350 ## Triangle NETGEN 2D algorithm
352 # It can be created by calling smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_2D, geom=0 )
354 # This algorithm generates only 2D (faces) elements for given geometrical shape
355 # and, in contrast to NETGEN_1D2D_Algorithm class, should be used in conjunction
356 # with other 1D meshing algorithm.
357 class NETGEN_2D_Only_Algorithm(NETGEN_Algorithm):
359 ## name of the dynamic method in smeshBuilder.Mesh class
361 meshMethod = "Triangle"
362 ## type of algorithm used with helper function in smeshBuilder.Mesh class
365 ## flag pointing whether this algorithm should be used by default in dynamic method
366 # of smeshBuilder.Mesh class
368 ## doc string of the method
370 docHelper = "Creates triangle 2D algorithm for faces"
374 ## Private constructor.
375 # @param mesh parent mesh object algorithm is assigned to
376 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
377 # if it is @c 0 (default), the algorithm is assigned to the main shape
378 def __init__(self, mesh, geom=0):
379 NETGEN_Algorithm.__init__(self, mesh, geom)
382 ## Defines @c MaxElementArea parameter of hypothesis basing on the definition of the
383 # maximum area of each triangle
384 # @param area maximum area value of each triangle
385 # @param UseExisting if \c True - searches for an existing hypothesis created with the
386 # same parameters, else (default) - creates a new one
387 # @return hypothesis object
388 def MaxElementArea(self, area, UseExisting=0):
389 compFun = lambda hyp, args: IsEqual(hyp.GetMaxElementArea(), args[0])
390 hyp = self.Hypothesis("MaxElementArea", [area], UseExisting=UseExisting,
391 CompareMethod=compFun)
392 hyp.SetMaxElementArea(area)
395 ## Defines @c LengthFromEdges hypothesis to build triangles
396 # based on the length of the edges taken from the wire
397 # @return hypothesis object
398 def LengthFromEdges(self):
399 hyp = self.Hypothesis("LengthFromEdges", UseExisting=1, CompareMethod=self.CompareEqualHyp)
402 ## Sets @c UseSurfaceCurvature flag
403 # @param toUse new value of the @c UseSurfaceCurvature parameter (@c True by default)
404 def SetUseSurfaceCurvature(self, toUse=True):
405 if self.Parameters(): self.params.SetUseSurfaceCurvature(toUse)
408 ## Sets @c QuadAllowed flag.
409 # @param toAllow new value of the @c QuadAllowed parameter (@c True by default)
410 # @return hypothesis object
411 def SetQuadAllowed(self, toAllow=True):
414 hasSimpleHyps = False
415 simpleHyps = ["QuadranglePreference","LengthFromEdges","MaxElementArea"]
416 for hyp in self.mesh.GetHypothesisList( self.geom ):
417 if hyp.GetName() in simpleHyps:
419 if hyp.GetName() == "QuadranglePreference":
420 if not toAllow: # remove QuadranglePreference
421 self.mesh.RemoveHypothesis( self.geom, hyp )
428 if toAllow: # add QuadranglePreference
429 return self.Hypothesis("QuadranglePreference", UseExisting=1, CompareMethod=self.CompareEqualHyp)
432 self.Parameters().SetQuadAllowed( toAllow )
435 pass # end of NETGEN_2D_Only_Algorithm class
438 ## Tetrahedron 3D algorithm
440 # It can be created by calling smeshBuilder.Mesh.Tetrahedron() or smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN, geom=0 )
442 # This algorithm generates only 3D (volumes) elements for given geometrical shape
443 # and, in contrast to NETGEN_1D2D3D_Algorithm class, should be used in conjunction
444 # with other 1D and 2D meshing algorithms.
445 class NETGEN_3D_Algorithm(NETGEN_Algorithm):
447 ## name of the dynamic method in smeshBuilder.Mesh class
449 meshMethod = "Tetrahedron"
450 ## type of algorithm used with helper function in smeshBuilder.Mesh class
453 ## flag pointing either this algorithm should be used by default in dynamic method
454 # of smeshBuilder.Mesh class
457 ## doc string of the method
459 docHelper = "Creates tetrahedron 3D algorithm for solids"
461 ## Private constructor.
462 # @param mesh parent mesh object algorithm is assigned to
463 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
464 # if it is @c 0 (default), the algorithm is assigned to the main shape
465 def __init__(self, mesh, geom=0):
466 NETGEN_Algorithm.__init__(self, mesh, geom)
469 ## Defines @c MaxElementVolume hypothesis to specify the maximum volume value of each tetrahedron
470 # @param vol maximum volume value of each tetrahedron
471 # @param UseExisting if \c True - searches for the existing hypothesis created with
472 # the same parameters, else (default) - creates a new one
473 # @return hypothesis object
474 def MaxElementVolume(self, vol, UseExisting=0):
475 compFun = lambda hyp, args: IsEqual(hyp.GetMaxElementVolume(), args[0])
476 hyp = self.Hypothesis("MaxElementVolume", [vol], UseExisting=UseExisting,
477 CompareMethod=compFun)
478 hyp.SetMaxElementVolume(vol)
481 pass # end of NETGEN_3D_Algorithm class
483 ## Tetrahedron 3D algorithm
485 # It can be created by calling smeshBuilder.Mesh.Tetrahedron() or smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN, geom=0 )
487 # This algorithm generates only 3D (volumes) elements for given geometrical shape
488 # and, in contrast to NETGEN_1D2D3D_Algorithm class, should be used in conjunction
489 # with other 1D and 2D meshing algorithms.
490 class NETGEN_3D_Remote_Algorithm(NETGEN_3D_Algorithm):
492 ## type of algorithm used with helper function in smeshBuilder.Mesh class
494 algoType = NETGEN_3D_Remote
495 ## flag pointing either this algorithm should be used by default in dynamic method
496 # of smeshBuilder.Mesh class
499 ## doc string of the method
501 docHelper = "Remotely Creates tetrahedron 3D algorithm for solids"
503 ## Private constructor.
504 # @param mesh parent mesh object algorithm is assigned to
505 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
506 # if it is @c 0 (default), the algorithm is assigned to the main shape
507 def __init__(self, mesh, geom=0):
508 NETGEN_3D_Algorithm.__init__(self, mesh, geom)
511 pass # end of NETGEN_3D_Remote_Algorithm class
515 ## Triangle (helper) 1D-2D algorithm
517 # This is the helper class that is used just to allow creating of create NETGEN_1D2D algorithm
518 # by calling smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN, geom=0 ); this is required for backward compatibility
519 # with old Python scripts.
521 # @note This class (and corresponding smeshBuilder.Mesh function) is obsolete;
522 # use smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_1D2D, geom=0 ) instead.
523 class NETGEN_1D2D_Algorithm_2(NETGEN_1D2D_Algorithm):
525 ## name of the dynamic method in smeshBuilder.Mesh class
529 ## Private constructor.
530 # @param mesh parent mesh object algorithm is assigned to
531 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
532 # if it is @c 0 (default), the algorithm is assigned to the main shape
533 def __init__(self, mesh, geom=0):
534 self.algoType = NETGEN_1D2D
535 NETGEN_1D2D_Algorithm.__init__(self,mesh, geom)
538 pass # end of NETGEN_1D2D_Algorithm_2 class
541 ## Tetrahedron (helper) 1D-2D-3D algorithm.
543 # This is the helper class that is used just to allow creating of create NETGEN_1D2D3D
544 # by calling smeshBuilder.Mesh.Netgen(); this is required for backward compatibility with old Python scripts.
546 # @note This class (and corresponding smeshBuilder.Mesh function) is obsolete;
547 # use smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN_1D2D3D, geom=0 ) instead.
548 class NETGEN_1D2D3D_Algorithm_2(NETGEN_1D2D3D_Algorithm):
550 ## name of the dynamic method in smeshBuilder.Mesh class
552 meshMethod = "Netgen"
553 ## doc string of the method
555 docHelper = "Deprecated, used only for compatibility! See Tetrahedron() method."
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 NETGEN_1D2D3D_Algorithm.__init__(self,mesh, geom)
565 pass # end of NETGEN_1D2D3D_Algorithm_2 class