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"
331 ## Private constructor.
332 # @param mesh parent mesh object algorithm is assigned to
333 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
334 # if it is @c 0 (default), the algorithm is assigned to the main shape
335 def __init__(self, mesh, geom=0):
336 NETGEN_1D2D3D_Algorithm.__init__(self, mesh, geom)
339 pass # end of NETGEN_1D2D_Algorithm class
342 ## Triangle NETGEN 2D algorithm
344 # It can be created by calling smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_2D, geom=0 )
346 # This algorithm generates only 2D (faces) elements for given geometrical shape
347 # and, in contrast to NETGEN_1D2D_Algorithm class, should be used in conjunction
348 # with other 1D meshing algorithm.
349 class NETGEN_2D_Only_Algorithm(NETGEN_Algorithm):
351 ## name of the dynamic method in smeshBuilder.Mesh class
353 meshMethod = "Triangle"
354 ## type of algorithm used with helper function in smeshBuilder.Mesh class
357 ## flag pointing whether this algorithm should be used by default in dynamic method
358 # of smeshBuilder.Mesh class
360 ## doc string of the method
362 docHelper = "Creates triangle 2D algorithm for faces"
364 ## Private constructor.
365 # @param mesh parent mesh object algorithm is assigned to
366 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
367 # if it is @c 0 (default), the algorithm is assigned to the main shape
368 def __init__(self, mesh, geom=0):
369 NETGEN_Algorithm.__init__(self, mesh, geom)
372 ## Defines @c MaxElementArea parameter of hypothesis basing on the definition of the
373 # maximum area of each triangle
374 # @param area maximum area value of each triangle
375 # @param UseExisting if \c True - searches for an existing hypothesis created with the
376 # same parameters, else (default) - creates a new one
377 # @return hypothesis object
378 def MaxElementArea(self, area, UseExisting=0):
379 compFun = lambda hyp, args: IsEqual(hyp.GetMaxElementArea(), args[0])
380 hyp = self.Hypothesis("MaxElementArea", [area], UseExisting=UseExisting,
381 CompareMethod=compFun)
382 hyp.SetMaxElementArea(area)
385 ## Defines @c LengthFromEdges hypothesis to build triangles
386 # based on the length of the edges taken from the wire
387 # @return hypothesis object
388 def LengthFromEdges(self):
389 hyp = self.Hypothesis("LengthFromEdges", UseExisting=1, CompareMethod=self.CompareEqualHyp)
392 ## Sets @c UseSurfaceCurvature flag
393 # @param toUse new value of the @c UseSurfaceCurvature parameter (@c True by default)
394 def SetUseSurfaceCurvature(self, toUse=True):
395 if self.Parameters(): self.params.SetUseSurfaceCurvature(toUse)
398 ## Sets @c QuadAllowed flag.
399 # @param toAllow new value of the @c QuadAllowed parameter (@c True by default)
400 # @return hypothesis object
401 def SetQuadAllowed(self, toAllow=True):
404 hasSimpleHyps = False
405 simpleHyps = ["QuadranglePreference","LengthFromEdges","MaxElementArea"]
406 for hyp in self.mesh.GetHypothesisList( self.geom ):
407 if hyp.GetName() in simpleHyps:
409 if hyp.GetName() == "QuadranglePreference":
410 if not toAllow: # remove QuadranglePreference
411 self.mesh.RemoveHypothesis( self.geom, hyp )
418 if toAllow: # add QuadranglePreference
419 return self.Hypothesis("QuadranglePreference", UseExisting=1, CompareMethod=self.CompareEqualHyp)
422 self.Parameters().SetQuadAllowed( toAllow )
425 pass # end of NETGEN_2D_Only_Algorithm class
428 ## Tetrahedron 3D algorithm
430 # It can be created by calling smeshBuilder.Mesh.Tetrahedron() or smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN, geom=0 )
432 # This algorithm generates only 3D (volumes) elements for given geometrical shape
433 # and, in contrast to NETGEN_1D2D3D_Algorithm class, should be used in conjunction
434 # with other 1D and 2D meshing algorithms.
435 class NETGEN_3D_Algorithm(NETGEN_Algorithm):
437 ## name of the dynamic method in smeshBuilder.Mesh class
439 meshMethod = "Tetrahedron"
440 ## type of algorithm used with helper function in smeshBuilder.Mesh class
443 ## flag pointing either this algorithm should be used by default in dynamic method
444 # of smeshBuilder.Mesh class
447 ## doc string of the method
449 docHelper = "Creates tetrahedron 3D algorithm for solids"
451 ## Private constructor.
452 # @param mesh parent mesh object algorithm is assigned to
453 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
454 # if it is @c 0 (default), the algorithm is assigned to the main shape
455 def __init__(self, mesh, geom=0):
456 NETGEN_Algorithm.__init__(self, mesh, geom)
459 ## Defines @c MaxElementVolume hypothesis to specify the maximum volume value of each tetrahedron
460 # @param vol maximum volume value of each tetrahedron
461 # @param UseExisting if \c True - searches for the existing hypothesis created with
462 # the same parameters, else (default) - creates a new one
463 # @return hypothesis object
464 def MaxElementVolume(self, vol, UseExisting=0):
465 compFun = lambda hyp, args: IsEqual(hyp.GetMaxElementVolume(), args[0])
466 hyp = self.Hypothesis("MaxElementVolume", [vol], UseExisting=UseExisting,
467 CompareMethod=compFun)
468 hyp.SetMaxElementVolume(vol)
471 pass # end of NETGEN_3D_Algorithm class
474 ## Triangle (helper) 1D-2D algorithm
476 # This is the helper class that is used just to allow creating of create NETGEN_1D2D algorithm
477 # by calling smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN, geom=0 ); this is required for backward compatibility
478 # with old Python scripts.
480 # @note This class (and corresponding smeshBuilder.Mesh function) is obsolete;
481 # use smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_1D2D, geom=0 ) instead.
482 class NETGEN_1D2D_Algorithm_2(NETGEN_1D2D_Algorithm):
484 ## name of the dynamic method in smeshBuilder.Mesh class
488 ## Private constructor.
489 # @param mesh parent mesh object algorithm is assigned to
490 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
491 # if it is @c 0 (default), the algorithm is assigned to the main shape
492 def __init__(self, mesh, geom=0):
493 self.algoType = NETGEN_1D2D
494 NETGEN_1D2D_Algorithm.__init__(self,mesh, geom)
497 pass # end of NETGEN_1D2D_Algorithm_2 class
500 ## Tetrahedron (helper) 1D-2D-3D algorithm.
502 # This is the helper class that is used just to allow creating of create NETGEN_1D2D3D
503 # by calling smeshBuilder.Mesh.Netgen(); this is required for backward compatibility with old Python scripts.
505 # @note This class (and corresponding smeshBuilder.Mesh function) is obsolete;
506 # use smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN_1D2D3D, geom=0 ) instead.
507 class NETGEN_1D2D3D_Algorithm_2(NETGEN_1D2D3D_Algorithm):
509 ## name of the dynamic method in smeshBuilder.Mesh class
511 meshMethod = "Netgen"
512 ## doc string of the method
514 docHelper = "Deprecated, used only for compatibility! See Tetrahedron() method."
516 ## Private constructor.
517 # @param mesh parent mesh object algorithm is assigned to
518 # @param geom geometry (shape/sub-shape) algorithm is assigned to;
519 # if it is @c 0 (default), the algorithm is assigned to the main shape
520 def __init__(self, mesh, geom=0):
521 NETGEN_1D2D3D_Algorithm.__init__(self,mesh, geom)
524 pass # end of NETGEN_1D2D3D_Algorithm_2 class