Salome HOME
Using provider for occgeom and nglib
[plugins/netgenplugin.git] / src / NETGENPlugin / NETGENPluginBuilder.py
1 # Copyright (C) 2007-2022  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
332     ## Private constructor.
333     #  @param mesh parent mesh object algorithm is assigned to
334     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
335     #              if it is @c 0 (default), the algorithm is assigned to the main shape
336     def __init__(self, mesh, geom=0):
337         NETGEN_1D2D3D_Algorithm.__init__(self, mesh, geom)
338         pass
339
340     pass # end of NETGEN_1D2D_Algorithm class
341
342
343 ## Triangle NETGEN 2D algorithm
344 #
345 #  It can be created by calling smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_2D, geom=0 )
346 #
347 #  This algorithm generates only 2D (faces) elements for given geometrical shape
348 #  and, in contrast to NETGEN_1D2D_Algorithm class, should be used in conjunction
349 #  with other 1D meshing algorithm.
350 class NETGEN_2D_Only_Algorithm(NETGEN_Algorithm):
351
352     ## name of the dynamic method in smeshBuilder.Mesh class
353     #  @internal
354     meshMethod = "Triangle"
355     ## type of algorithm used with helper function in smeshBuilder.Mesh class
356     #  @internal
357     algoType = NETGEN_2D
358     ## flag pointing whether this algorithm should be used by default in dynamic method
359     #  of smeshBuilder.Mesh class
360     isDefault  = True
361     ## doc string of the method
362     #  @internal
363     docHelper  = "Creates triangle 2D algorithm for faces"
364
365     isDefault = True
366
367     ## Private constructor.
368     #  @param mesh parent mesh object algorithm is assigned to
369     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
370     #              if it is @c 0 (default), the algorithm is assigned to the main shape
371     def __init__(self, mesh, geom=0):
372         NETGEN_Algorithm.__init__(self, mesh, geom)
373         pass
374
375     ## Defines @c MaxElementArea parameter of hypothesis basing on the definition of the
376     #  maximum area of each triangle
377     #  @param area maximum area value of each triangle
378     #  @param UseExisting if \c True - searches for an existing hypothesis created with the
379     #                     same parameters, else (default) - creates a new one
380     #  @return hypothesis object
381     def MaxElementArea(self, area, UseExisting=0):
382         compFun = lambda hyp, args: IsEqual(hyp.GetMaxElementArea(), args[0])
383         hyp = self.Hypothesis("MaxElementArea", [area], UseExisting=UseExisting,
384                               CompareMethod=compFun)
385         hyp.SetMaxElementArea(area)
386         return hyp
387
388     ## Defines @c LengthFromEdges hypothesis to build triangles
389     #  based on the length of the edges taken from the wire
390     #  @return hypothesis object
391     def LengthFromEdges(self):
392         hyp = self.Hypothesis("LengthFromEdges", UseExisting=1, CompareMethod=self.CompareEqualHyp)
393         return hyp
394
395     ## Sets @c UseSurfaceCurvature flag
396     #  @param toUse new value of the @c UseSurfaceCurvature parameter (@c True by default)
397     def SetUseSurfaceCurvature(self, toUse=True):
398         if self.Parameters(): self.params.SetUseSurfaceCurvature(toUse)
399         pass
400
401     ## Sets @c QuadAllowed flag.
402     #  @param toAllow new value of the @c QuadAllowed parameter (@c True by default)
403     #  @return hypothesis object
404     def SetQuadAllowed(self, toAllow=True):
405         if not self.params:
406             # use simple hyps
407             hasSimpleHyps = False
408             simpleHyps = ["QuadranglePreference","LengthFromEdges","MaxElementArea"]
409             for hyp in self.mesh.GetHypothesisList( self.geom ):
410                 if hyp.GetName() in simpleHyps:
411                     hasSimpleHyps = True
412                     if hyp.GetName() == "QuadranglePreference":
413                         if not toAllow: # remove QuadranglePreference
414                             self.mesh.RemoveHypothesis( self.geom, hyp )
415                         else:
416                             return hyp
417                         return None
418                     pass
419                 pass
420             if hasSimpleHyps:
421                 if toAllow: # add QuadranglePreference
422                     return self.Hypothesis("QuadranglePreference", UseExisting=1, CompareMethod=self.CompareEqualHyp)
423                 return None
424             pass
425         self.Parameters().SetQuadAllowed( toAllow )
426         return self.params
427
428     pass # end of NETGEN_2D_Only_Algorithm class
429
430
431 ## Tetrahedron 3D algorithm
432 #
433 #  It can be created by calling smeshBuilder.Mesh.Tetrahedron() or smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN, geom=0 )
434 #
435 #  This algorithm generates only 3D (volumes) elements for given geometrical shape
436 #  and, in contrast to NETGEN_1D2D3D_Algorithm class, should be used in conjunction
437 #  with other 1D and 2D meshing algorithms.
438 class NETGEN_3D_Algorithm(NETGEN_Algorithm):
439
440     ## name of the dynamic method in smeshBuilder.Mesh class
441     #  @internal
442     meshMethod = "Tetrahedron"
443     ## type of algorithm used with helper function in smeshBuilder.Mesh class
444     #  @internal
445     algoType   = NETGEN
446     ## flag pointing either this algorithm should be used by default in dynamic method
447     #  of smeshBuilder.Mesh class
448     #  @internal
449     isDefault  = True
450     ## doc string of the method
451     #  @internal
452     docHelper  = "Creates tetrahedron 3D algorithm for solids"
453
454     ## Private constructor.
455     #  @param mesh parent mesh object algorithm is assigned to
456     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
457     #              if it is @c 0 (default), the algorithm is assigned to the main shape
458     def __init__(self, mesh, geom=0):
459         NETGEN_Algorithm.__init__(self, mesh, geom)
460         pass
461
462     ## Defines @c MaxElementVolume hypothesis to specify the maximum volume value of each tetrahedron
463     #  @param vol maximum volume value of each tetrahedron
464     #  @param UseExisting if \c True - searches for the existing hypothesis created with
465     #                   the same parameters, else (default) - creates a new one
466     #  @return hypothesis object
467     def MaxElementVolume(self, vol, UseExisting=0):
468         compFun = lambda hyp, args: IsEqual(hyp.GetMaxElementVolume(), args[0])
469         hyp = self.Hypothesis("MaxElementVolume", [vol], UseExisting=UseExisting,
470                               CompareMethod=compFun)
471         hyp.SetMaxElementVolume(vol)
472         return hyp
473
474     pass # end of NETGEN_3D_Algorithm class
475
476
477 ## Triangle (helper) 1D-2D algorithm
478 #
479 #  This is the helper class that is used just to allow creating of create NETGEN_1D2D algorithm
480 #  by calling smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN, geom=0 ); this is required for backward compatibility
481 #  with old Python scripts.
482 #
483 #  @note This class (and corresponding smeshBuilder.Mesh function) is obsolete;
484 #  use smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_1D2D, geom=0 ) instead.
485 class NETGEN_1D2D_Algorithm_2(NETGEN_1D2D_Algorithm):
486
487     ## name of the dynamic method in smeshBuilder.Mesh class
488     #  @internal
489     algoType = NETGEN
490
491     ## Private constructor.
492     #  @param mesh parent mesh object algorithm is assigned to
493     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
494     #              if it is @c 0 (default), the algorithm is assigned to the main shape
495     def __init__(self, mesh, geom=0):
496         self.algoType = NETGEN_1D2D
497         NETGEN_1D2D_Algorithm.__init__(self,mesh, geom)
498         pass
499
500     pass # end of NETGEN_1D2D_Algorithm_2 class
501
502
503 ## Tetrahedron (helper) 1D-2D-3D algorithm.
504 #
505 #  This is the helper class that is used just to allow creating of create NETGEN_1D2D3D
506 #  by calling smeshBuilder.Mesh.Netgen(); this is required for backward compatibility with old Python scripts.
507 #
508 #  @note This class (and corresponding smeshBuilder.Mesh function) is obsolete;
509 #  use smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN_1D2D3D, geom=0 ) instead.
510 class NETGEN_1D2D3D_Algorithm_2(NETGEN_1D2D3D_Algorithm):
511
512     ## name of the dynamic method in smeshBuilder.Mesh class
513     #  @internal
514     meshMethod = "Netgen"
515     ## doc string of the method
516     #  @internal
517     docHelper  = "Deprecated, used only for compatibility! See Tetrahedron() method."
518
519     ## Private constructor.
520     #  @param mesh parent mesh object algorithm is assigned to
521     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
522     #              if it is @c 0 (default), the algorithm is assigned to the main shape
523     def __init__(self, mesh, geom=0):
524         NETGEN_1D2D3D_Algorithm.__init__(self,mesh, geom)
525         pass
526
527     pass # end of NETGEN_1D2D3D_Algorithm_2 class