Salome HOME
bos #26515 [CEA 24622] Failing SMESH tests
[plugins/netgenplugin.git] / src / NETGENPlugin / NETGENPluginBuilder.py
1 # Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
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.
7 #
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.
12 #
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
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 ##
21 # @package NETGENPluginBuilder
22 # Python API for the NETGEN meshing plug-in module.
23
24 from salome.smesh.smesh_algorithm import Mesh_Algorithm
25 from salome.smesh.smeshBuilder import AssureGeomPublished, ParseParameters, IsEqual
26
27 # import NETGENPlugin module if possible
28 noNETGENPlugin = 0
29 try:
30     import NETGENPlugin
31 except ImportError:
32     noNETGENPlugin = 1
33     pass
34
35 LIBRARY = "libNETGENEngine.so"
36
37 NETGEN_VERSION_MAJOR = NETGENPlugin.NETGEN_VERSION_MAJOR
38
39 #----------------------------
40 # Mesh algo type identifiers
41 #----------------------------
42
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 
54 NETGEN        = NETGEN_3D
55 ## Algorithm type: Synonim of NETGEN_1D2D3D, see NETGEN_1D2D3D_Algorithm 
56 FULL_NETGEN   = NETGEN_FULL
57
58 #----------------------------
59 # Hypothesis type enumeration
60 #----------------------------
61
62 ## Hypothesis type enumeration: complex hypothesis
63 #  (full set of parameters can be specified),
64 #  see NETGEN_Algorithm.Parameters()
65 SOLE   = 0
66 ## Hypothesis type enumeration: simple hypothesis
67 #  (only major parameters are specified),
68 #  see NETGEN_Algorithm.Parameters()
69 SIMPLE = 1
70
71 #----------------------
72 # Fineness enumeration
73 #----------------------
74
75 ## Fineness enumeration: very coarse quality of mesh,
76 #  see NETGEN_Algorithm.SetFineness()
77 VeryCoarse = 0
78 ## Fineness enumeration: coarse quality of mesh,
79 #  see NETGEN_Algorithm.SetFineness()
80 Coarse     = 1
81 ## Fineness enumeration: moderate quality of mesh,
82 #  see NETGEN_Algorithm.SetFineness()
83 Moderate   = 2
84 ## Fineness enumeration: fine quality of mesh,
85 #  see NETGEN_Algorithm.SetFineness()
86 Fine       = 3
87 ## Fineness enumeration: very fine quality of mesh,
88 #  see NETGEN_Algorithm.SetFineness()
89 VeryFine   = 4
90 ## Fineness enumeration: custom quality of mesh specified by other parameters),
91 #  see NETGEN_Algorithm.SetFineness()
92 Custom     = 5
93
94 #----------------------
95 # Algorithms
96 #----------------------
97
98 ## Base of all NETGEN algorithms.
99 #
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):
103
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)
114         else:
115             self.Create(mesh, geom, self.algoType, LIBRARY)
116         self.params = None
117         pass
118
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)
123         pass
124
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)
129         pass
130
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)
135         pass
136
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)
142         pass
143
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)
148         pass
149
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:
156             if which == SIMPLE:
157                 hypType = "NETGEN_SimpleParameters_2D"
158             else:
159                 hypType = "NETGEN_Parameters_2D"
160         elif self.algoType == NETGEN_1D2D3D:
161             if which == SIMPLE:
162                 hypType = "NETGEN_SimpleParameters_3D"
163             else:
164                 hypType = "NETGEN_Parameters"
165         elif self.algoType == NETGEN_2D:
166             hypType = "NETGEN_Parameters_2D_ONLY"
167         else:
168             hypType = "NETGEN_Parameters_3D"
169
170         if self.algo.GetName() == "NETGEN_Remesher_2D":
171             hypType = "NETGEN_RemesherParameters_2D"
172
173         if self.params and self.params.GetName() != hypType:
174             self.mesh.RemoveHypothesis( self.params, self.geom )
175             self.params = None
176         if not self.params:
177             self.params = self.Hypothesis(hypType, [], LIBRARY, UseExisting=0)
178
179         return self.params
180
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)
185         pass
186
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)
192         pass
193         
194
195     pass # end of NETGEN_Algorithm class
196
197
198 ## Tetrahedron 1D-2D-3D algorithm.
199 #
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):
204
205     ## name of the dynamic method in smeshBuilder.Mesh class
206     #  @internal
207     meshMethod = "Tetrahedron"
208     ## type of algorithm used with helper function in smeshBuilder.Mesh class
209     #  @internal
210     algoType   = NETGEN_1D2D3D
211     ## doc string of the method
212     #  @internal
213     docHelper  = "Creates tetrahedron 3D algorithm for solids"
214
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)
221         pass
222
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)
227         pass
228
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)
233         pass
234
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)
239         pass
240
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 )
247         pass
248
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)
254         pass
255
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)
260         pass
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)
265         pass
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)
270         pass
271
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)
276         pass
277
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)
282         pass
283
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)
289         pass
290
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()
295         pass
296
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()
301         pass
302
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)
308         pass
309
310     pass # end of NETGEN_1D2D3D_Algorithm class
311
312
313 ## Triangle NETGEN 1D-2D algorithm. 
314 #
315 #  It can be created by calling smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_1D2D, geom=0 )
316 #
317 #  This algorithm generates 1D (edges) and 2D (faces) elements
318 #  for given geometrical shape.
319 class NETGEN_1D2D_Algorithm(NETGEN_1D2D3D_Algorithm):
320
321     ## name of the dynamic method in smeshBuilder.Mesh class
322     #  @internal
323     meshMethod = "Triangle"
324     ## type of algorithm used with helper function in smeshBuilder.Mesh class
325     #  @internal
326     algoType   = NETGEN_1D2D
327     ## doc string of the method
328     #  @internal
329     docHelper  = "Creates triangle 2D algorithm for faces"
330
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)
337         pass
338
339     pass # end of NETGEN_1D2D_Algorithm class
340
341
342 ## Triangle NETGEN 2D algorithm
343 #
344 #  It can be created by calling smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_2D, geom=0 )
345 #
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):
350
351     ## name of the dynamic method in smeshBuilder.Mesh class
352     #  @internal
353     meshMethod = "Triangle"
354     ## type of algorithm used with helper function in smeshBuilder.Mesh class
355     #  @internal
356     algoType = NETGEN_2D
357     ## doc string of the method
358     #  @internal
359     docHelper  = "Creates triangle 2D algorithm for faces"
360     
361     ## Private constructor.
362     #  @param mesh parent mesh object algorithm is assigned to
363     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
364     #              if it is @c 0 (default), the algorithm is assigned to the main shape
365     def __init__(self, mesh, geom=0):
366         NETGEN_Algorithm.__init__(self, mesh, geom)
367         pass
368
369     ## Defines @c MaxElementArea parameter of hypothesis basing on the definition of the
370     #  maximum area of each triangle
371     #  @param area maximum area value of each triangle
372     #  @param UseExisting if \c True - searches for an existing hypothesis created with the
373     #                     same parameters, else (default) - creates a new one
374     #  @return hypothesis object
375     def MaxElementArea(self, area, UseExisting=0):
376         compFun = lambda hyp, args: IsEqual(hyp.GetMaxElementArea(), args[0])
377         hyp = self.Hypothesis("MaxElementArea", [area], UseExisting=UseExisting,
378                               CompareMethod=compFun)
379         hyp.SetMaxElementArea(area)
380         return hyp
381
382     ## Defines @c LengthFromEdges hypothesis to build triangles
383     #  based on the length of the edges taken from the wire
384     #  @return hypothesis object
385     def LengthFromEdges(self):
386         hyp = self.Hypothesis("LengthFromEdges", UseExisting=1, CompareMethod=self.CompareEqualHyp)
387         return hyp
388         
389     ## Sets @c UseSurfaceCurvature flag
390     #  @param toUse new value of the @c UseSurfaceCurvature parameter (@c True by default)
391     def SetUseSurfaceCurvature(self, toUse=True):
392         if self.Parameters(): self.params.SetUseSurfaceCurvature(toUse)
393         pass
394
395     ## Sets @c QuadAllowed flag.
396     #  @param toAllow new value of the @c QuadAllowed parameter (@c True by default)
397     #  @return hypothesis object
398     def SetQuadAllowed(self, toAllow=True):
399         if not self.params:
400             # use simple hyps
401             hasSimpleHyps = False
402             simpleHyps = ["QuadranglePreference","LengthFromEdges","MaxElementArea"]
403             for hyp in self.mesh.GetHypothesisList( self.geom ):
404                 if hyp.GetName() in simpleHyps:
405                     hasSimpleHyps = True
406                     if hyp.GetName() == "QuadranglePreference":
407                         if not toAllow: # remove QuadranglePreference
408                             self.mesh.RemoveHypothesis( self.geom, hyp )
409                         else:
410                             return hyp
411                         return None
412                     pass
413                 pass
414             if hasSimpleHyps:
415                 if toAllow: # add QuadranglePreference
416                     return self.Hypothesis("QuadranglePreference", UseExisting=1, CompareMethod=self.CompareEqualHyp)
417                 return None
418             pass
419         self.Parameters().SetQuadAllowed( toAllow )
420         return self.params
421
422     pass # end of NETGEN_2D_Only_Algorithm class
423
424
425 ## Tetrahedron 3D algorithm
426 #
427 #  It can be created by calling smeshBuilder.Mesh.Tetrahedron() or smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN, geom=0 )
428 #
429 #  This algorithm generates only 3D (volumes) elements for given geometrical shape
430 #  and, in contrast to NETGEN_1D2D3D_Algorithm class, should be used in conjunction
431 #  with other 1D and 2D meshing algorithms.
432 class NETGEN_3D_Algorithm(NETGEN_Algorithm):
433
434     ## name of the dynamic method in smeshBuilder.Mesh class
435     #  @internal
436     meshMethod = "Tetrahedron"
437     ## type of algorithm used with helper function in smeshBuilder.Mesh class
438     #  @internal
439     algoType   = NETGEN
440     ## flag pointing either this algorithm should be used by default in dynamic method
441     #  of smeshBuilder.Mesh class
442     #  @internal
443     isDefault  = True
444     ## doc string of the method
445     #  @internal
446     docHelper  = "Creates tetrahedron 3D algorithm for solids"
447
448     ## Private constructor.
449     #  @param mesh parent mesh object algorithm is assigned to
450     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
451     #              if it is @c 0 (default), the algorithm is assigned to the main shape
452     def __init__(self, mesh, geom=0):
453         NETGEN_Algorithm.__init__(self, mesh, geom)
454         pass
455
456     ## Defines @c MaxElementVolume hypothesis to specify the maximum volume value of each tetrahedron
457     #  @param vol maximum volume value of each tetrahedron
458     #  @param UseExisting if \c True - searches for the existing hypothesis created with
459     #                   the same parameters, else (default) - creates a new one
460     #  @return hypothesis object
461     def MaxElementVolume(self, vol, UseExisting=0):
462         compFun = lambda hyp, args: IsEqual(hyp.GetMaxElementVolume(), args[0])
463         hyp = self.Hypothesis("MaxElementVolume", [vol], UseExisting=UseExisting,
464                               CompareMethod=compFun)
465         hyp.SetMaxElementVolume(vol)
466         return hyp
467
468     pass # end of NETGEN_3D_Algorithm class
469
470
471 ## Triangle (helper) 1D-2D algorithm
472 #
473 #  This is the helper class that is used just to allow creating of create NETGEN_1D2D algorithm
474 #  by calling smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN, geom=0 ); this is required for backward compatibility
475 #  with old Python scripts.
476 #
477 #  @note This class (and corresponding smeshBuilder.Mesh function) is obsolete;
478 #  use smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_1D2D, geom=0 ) instead.
479 class NETGEN_1D2D_Algorithm_2(NETGEN_1D2D_Algorithm):
480
481     ## name of the dynamic method in smeshBuilder.Mesh class
482     #  @internal
483     algoType = NETGEN
484
485     ## Private constructor.
486     #  @param mesh parent mesh object algorithm is assigned to
487     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
488     #              if it is @c 0 (default), the algorithm is assigned to the main shape
489     def __init__(self, mesh, geom=0):
490         self.algoType = NETGEN_1D2D
491         NETGEN_1D2D_Algorithm.__init__(self,mesh, geom)
492         pass
493
494     pass # end of NETGEN_1D2D_Algorithm_2 class
495
496
497 ## Tetrahedron (helper) 1D-2D-3D algorithm.
498 #
499 #  This is the helper class that is used just to allow creating of create NETGEN_1D2D3D
500 #  by calling smeshBuilder.Mesh.Netgen(); this is required for backward compatibility with old Python scripts.
501 #
502 #  @note This class (and corresponding smeshBuilder.Mesh function) is obsolete;
503 #  use smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN_1D2D3D, geom=0 ) instead.
504 class NETGEN_1D2D3D_Algorithm_2(NETGEN_1D2D3D_Algorithm):
505
506     ## name of the dynamic method in smeshBuilder.Mesh class
507     #  @internal
508     meshMethod = "Netgen"
509     ## doc string of the method
510     #  @internal
511     docHelper  = "Deprecated, used only for compatibility! See Tetrahedron() method."
512
513     ## Private constructor.
514     #  @param mesh parent mesh object algorithm is assigned to
515     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
516     #              if it is @c 0 (default), the algorithm is assigned to the main shape
517     def __init__(self, mesh, geom=0):
518         NETGEN_1D2D3D_Algorithm.__init__(self,mesh, geom)
519         pass
520
521     pass # end of NETGEN_1D2D3D_Algorithm_2 class