Salome HOME
Merge remote-tracking branch 'origin/V8_3_BR' into gdd/python3_dev
[plugins/netgenplugin.git] / src / NETGENPlugin / NETGENPluginBuilder.py
1 # Copyright (C) 2007-2016  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 #----------------------------
38 # Mesh algo type identifiers
39 #----------------------------
40
41 ## Algorithm type: Netgen tetrahedron 3D algorithm, see NETGEN_3D_Algorithm 
42 NETGEN_3D     = "NETGEN_3D"
43 ## Algorithm type: Netgen tetrahedron 1D-2D-3D algorithm, see NETGEN_1D2D3D_Algorithm 
44 NETGEN_1D2D3D = "NETGEN_2D3D"
45 ## Algorithm type: Netgen triangle 1D-2D algorithm, see NETGEN_1D2D_Algorithm 
46 NETGEN_1D2D   = "NETGEN_2D"
47 ## Algorithm type: Netgen triangle 2D algorithm, see NETGEN_2D_Only_Algorithm
48 NETGEN_2D     = "NETGEN_2D_ONLY"
49 ## Algorithm type: Synonim of NETGEN_1D2D3D, see NETGEN_1D2D3D_Algorithm 
50 NETGEN_FULL   = NETGEN_1D2D3D
51 ## Algorithm type: Synonim of NETGEN_3D, see NETGEN_3D_Algorithm 
52 NETGEN        = NETGEN_3D
53 ## Algorithm type: Synonim of NETGEN_1D2D3D, see NETGEN_1D2D3D_Algorithm 
54 FULL_NETGEN   = NETGEN_FULL
55
56 #----------------------------
57 # Hypothesis type enumeration
58 #----------------------------
59
60 ## Hypothesis type enumeration: complex hypothesis
61 #  (full set of parameters can be specified),
62 #  see NETGEN_Algorithm.Parameters()
63 SOLE   = 0
64 ## Hypothesis type enumeration: simple hypothesis
65 #  (only major parameters are specified),
66 #  see NETGEN_Algorithm.Parameters()
67 SIMPLE = 1
68
69 #----------------------
70 # Fineness enumeration
71 #----------------------
72
73 ## Fineness enumeration: very coarse quality of mesh,
74 #  see NETGEN_Algorithm.SetFineness()
75 VeryCoarse = 0
76 ## Fineness enumeration: coarse quality of mesh,
77 #  see NETGEN_Algorithm.SetFineness()
78 Coarse     = 1
79 ## Fineness enumeration: moderate quality of mesh,
80 #  see NETGEN_Algorithm.SetFineness()
81 Moderate   = 2
82 ## Fineness enumeration: fine quality of mesh,
83 #  see NETGEN_Algorithm.SetFineness()
84 Fine       = 3
85 ## Fineness enumeration: very fine quality of mesh,
86 #  see NETGEN_Algorithm.SetFineness()
87 VeryFine   = 4
88 ## Fineness enumeration: custom quality of mesh specified by other parameters),
89 #  see NETGEN_Algorithm.SetFineness()
90 Custom     = 5
91
92 #----------------------
93 # Algorithms
94 #----------------------
95
96 ## Base of all NETGEN algorithms.
97 #
98 #  This class provides common methods for all algorithms implemented by NETGEN plugin.
99 #  @note This class must not be instantiated directly.
100 class NETGEN_Algorithm(Mesh_Algorithm):
101
102     ## Private constructor
103     #  @param mesh parent mesh object algorithm is assigned to
104     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
105     #              if it is @c 0 (default), the algorithm is assigned to the main shape
106     def __init__(self, mesh, geom=0):
107         Mesh_Algorithm.__init__(self)
108         if noNETGENPlugin: print("Warning: NETGENPlugin module unavailable")
109         self.Create(mesh, geom, self.algoType, LIBRARY)
110         self.params = None
111         pass
112
113     ## Sets @c MaxSize parameter
114     #  @param theSize new value of the @c MaxSize parameter
115     def SetMaxSize(self, theSize):
116         if self.Parameters(): self.params.SetMaxSize(theSize)
117         pass
118
119     ## Sets @c MinSize parameter
120     #  @param theSize new value of the @c MinSize parameter
121     def SetMinSize(self, theSize):
122         if self.Parameters(): self.params.SetMinSize(theSize)
123         pass
124
125     ## Sets @c Optimize flag
126     #  @param theVal new value of the @c Optimize parameter
127     def SetOptimize(self, theVal):
128         if self.Parameters(): self.params.SetOptimize(theVal)
129         pass
130
131     ## Sets @c Fineness parameter
132     #  @param theFineness new value of the @c Fineness parameter; it can be:
133     #  @ref VeryCoarse, @ref Coarse, @ref Moderate, @ref Fine, @ref VeryFine or @ref Custom
134     def SetFineness(self, theFineness):
135         if self.Parameters(): self.params.SetFineness(theFineness)
136         pass
137
138     ## Sets @c GrowthRate parameter
139     #  @param theRate new value of the @c GrowthRate parameter
140     def SetGrowthRate(self, theRate):
141         if self.Parameters(): self.params.SetGrowthRate(theRate)
142         pass
143
144     ## Creates meshing hypothesis according to the chosen algorithm type
145     #  and initializes it with default parameters
146     #  @param which hypothesis type; can be either @ref SOLE (default) or @ref SIMPLE
147     #  @return hypothesis object
148     def Parameters(self, which=SOLE):
149         if self.algoType == NETGEN_1D2D:
150             if which == SIMPLE:
151                 hypType = "NETGEN_SimpleParameters_2D"
152             else:
153                 hypType = "NETGEN_Parameters_2D"
154         elif self.algoType == NETGEN_1D2D3D:
155             if which == SIMPLE:
156                 hypType = "NETGEN_SimpleParameters_3D"
157             else:
158                 hypType = "NETGEN_Parameters"
159         elif self.algoType == NETGEN_2D:
160             hypType = "NETGEN_Parameters_2D_ONLY"
161         else:
162             hypType = "NETGEN_Parameters_3D"
163
164         if self.params and self.params.GetName() != hypType:
165             self.mesh.RemoveHypothesis( self.params, self.geom )
166             self.params = None
167         if not self.params:
168             self.params = self.Hypothesis(hypType, [], LIBRARY, UseExisting=0)
169
170         return self.params
171
172     ## Defines a file specifying size of elements at points and lines
173     #  @param file name of the file
174     def SetMeshSizeFile(self, file):
175         self.Parameters().SetMeshSizeFile(file)
176         pass
177
178     pass # end of NETGEN_Algorithm class
179
180
181 ## Tetrahedron 1D-2D-3D algorithm.
182 #
183 #  It can be created by calling smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN_1D2D3D, geom=0 ).
184 #  This algorithm generates all 1D (edges), 2D (faces) and 3D (volumes) elements
185 #  for given geometrical shape.
186 class NETGEN_1D2D3D_Algorithm(NETGEN_Algorithm):
187
188     ## name of the dynamic method in smeshBuilder.Mesh class
189     #  @internal
190     meshMethod = "Tetrahedron"
191     ## type of algorithm used with helper function in smeshBuilder.Mesh class
192     #  @internal
193     algoType   = NETGEN_1D2D3D
194     ## doc string of the method
195     #  @internal
196     docHelper  = "Creates tetrahedron 3D algorithm for solids"
197
198     ## Private constructor.
199     #  @param mesh parent mesh object algorithm is assigned to
200     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
201     #              if it is @c 0 (default), the algorithm is assigned to the main shape
202     def __init__(self, mesh, geom=0):
203         NETGEN_Algorithm.__init__(self, mesh, geom)
204         pass
205
206     ## Sets @c SecondOrder flag
207     #  @param theVal new value of the @c SecondOrder parameter
208     def SetSecondOrder(self, theVal):
209         if self.Parameters(): self.params.SetSecondOrder(theVal)
210         pass
211
212     ## Sets @c NbSegPerEdge parameter
213     #  @param theVal new value of the @c NbSegPerEdge parameter
214     def SetNbSegPerEdge(self, theVal):
215         if self.Parameters(): self.params.SetNbSegPerEdge(theVal)
216         pass
217
218     ## Sets @c NbSegPerRadius parameter
219     #  @param theVal new value of the @c NbSegPerRadius parameter
220     def SetNbSegPerRadius(self, theVal):
221         if self.Parameters(): self.params.SetNbSegPerRadius(theVal)
222         pass
223
224     ## Sets @c QuadAllowed flag
225     #  @param toAllow new value of the @c QuadAllowed parameter (@c True by default)
226     def SetQuadAllowed(self, toAllow=True):
227         if self.Parameters(): self.params.SetQuadAllowed(toAllow)
228         pass
229     ## Sets @c UseSurfaceCurvature flag
230     #  @param toUse new value of the @c UseSurfaceCurvature parameter (@c True by default)
231     def SetUseSurfaceCurvature(self, toUse=True):
232         if self.Parameters(): self.params.SetUseSurfaceCurvature(toUse)
233         pass
234     ## Sets @c FuseEdges flag
235     #  @param toFuse new value of the @c FuseEdges parameter (@c False by default)
236     def SetFuseEdges(self, toFuse=False):
237         if self.Parameters(): self.params.SetFuseEdges(toFuse)
238         pass
239
240     ## Sets number of segments overriding the value set by SetLocalLength()
241     #  @param theVal new value of number of segments parameter
242     def SetNumberOfSegments(self, theVal):
243         self.Parameters(SIMPLE).SetNumberOfSegments(theVal)
244         pass
245
246     ## Sets number of segments overriding the value set by SetNumberOfSegments()
247     #  @param theVal new value of local length parameter
248     def SetLocalLength(self, theVal):
249         self.Parameters(SIMPLE).SetLocalLength(theVal)
250         pass
251
252     ## Defines @c MaxElementArea parameter of @c NETGEN_SimpleParameters_3D hypothesis.
253     #  Overrides value set by LengthFromEdges()
254     #  @param area new value of @c MaxElementArea parameter
255     def MaxElementArea(self, area):
256         self.Parameters(SIMPLE).SetMaxElementArea(area)
257         pass
258
259     ## Defines @c LengthFromEdges parameter of @c NETGEN_SimpleParameters_3D hypothesis.
260     #  Overrides value set by MaxElementArea()
261     def LengthFromEdges(self):
262         self.Parameters(SIMPLE).LengthFromEdges()
263         pass
264
265     ## Defines @c LengthFromFaces parameter of @c NETGEN_SimpleParameters_3D hypothesis.
266     #  Overrides value set by MaxElementVolume()
267     def LengthFromFaces(self):
268         self.Parameters(SIMPLE).LengthFromFaces()
269         pass
270
271     ## Defines @c MaxElementVolume parameter of @c NETGEN_SimpleParameters_3D hypothesis.
272     #  Overrides value set by LengthFromFaces()
273     #  @param vol new value of @c MaxElementVolume parameter
274     def MaxElementVolume(self, vol):
275         self.Parameters(SIMPLE).SetMaxElementVolume(vol)
276         pass
277
278     pass # end of NETGEN_1D2D3D_Algorithm class
279
280
281 ## Triangle NETGEN 1D-2D algorithm. 
282 #
283 #  It can be created by calling smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_1D2D, geom=0 )
284 #
285 #  This algorithm generates 1D (edges) and 2D (faces) elements
286 #  for given geometrical shape.
287 class NETGEN_1D2D_Algorithm(NETGEN_1D2D3D_Algorithm):
288
289     ## name of the dynamic method in smeshBuilder.Mesh class
290     #  @internal
291     meshMethod = "Triangle"
292     ## type of algorithm used with helper function in smeshBuilder.Mesh class
293     #  @internal
294     algoType   = NETGEN_1D2D
295     ## doc string of the method
296     #  @internal
297     docHelper  = "Creates triangle 2D algorithm for faces"
298
299     ## Private constructor.
300     #  @param mesh parent mesh object algorithm is assigned to
301     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
302     #              if it is @c 0 (default), the algorithm is assigned to the main shape
303     def __init__(self, mesh, geom=0):
304         NETGEN_1D2D3D_Algorithm.__init__(self, mesh, geom)
305         pass
306
307     pass # end of NETGEN_1D2D_Algorithm class
308
309
310 ## Triangle NETGEN 2D algorithm
311 #
312 #  It can be created by calling smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_2D, geom=0 )
313 #
314 #  This algorithm generates only 2D (faces) elements for given geometrical shape
315 #  and, in contrast to NETGEN_1D2D_Algorithm class, should be used in conjunction
316 #  with other 1D meshing algorithm.
317 class NETGEN_2D_Only_Algorithm(NETGEN_Algorithm):
318
319     ## name of the dynamic method in smeshBuilder.Mesh class
320     #  @internal
321     meshMethod = "Triangle"
322     ## type of algorithm used with helper function in smeshBuilder.Mesh class
323     #  @internal
324     algoType = NETGEN_2D
325     ## doc string of the method
326     #  @internal
327     docHelper  = "Creates triangle 2D algorithm for faces"
328     
329     ## Private constructor.
330     #  @param mesh parent mesh object algorithm is assigned to
331     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
332     #              if it is @c 0 (default), the algorithm is assigned to the main shape
333     def __init__(self, mesh, geom=0):
334         NETGEN_Algorithm.__init__(self, mesh, geom)
335         pass
336
337     ## Defines @c MaxElementArea parameter of hypothesis basing on the definition of the
338     #  maximum area of each triangle
339     #  @param area maximum area value of each triangle
340     #  @param UseExisting if \c True - searches for an existing hypothesis created with the
341     #                     same parameters, else (default) - creates a new one
342     #  @return hypothesis object
343     def MaxElementArea(self, area, UseExisting=0):
344         compFun = lambda hyp, args: IsEqual(hyp.GetMaxElementArea(), args[0])
345         hyp = self.Hypothesis("MaxElementArea", [area], UseExisting=UseExisting,
346                               CompareMethod=compFun)
347         hyp.SetMaxElementArea(area)
348         return hyp
349
350     ## Defines @c LengthFromEdges hypothesis to build triangles
351     #  based on the length of the edges taken from the wire
352     #  @return hypothesis object
353     def LengthFromEdges(self):
354         hyp = self.Hypothesis("LengthFromEdges", UseExisting=1, CompareMethod=self.CompareEqualHyp)
355         return hyp
356         
357     ## Sets @c UseSurfaceCurvature flag
358     #  @param toUse new value of the @c UseSurfaceCurvature parameter (@c True by default)
359     def SetUseSurfaceCurvature(self, toUse=True):
360         if self.Parameters(): self.params.SetUseSurfaceCurvature(toUse)
361         pass
362
363     ## Sets @c QuadAllowed flag.
364     #  @param toAllow new value of the @c QuadAllowed parameter (@c True by default)
365     #  @return hypothesis object
366     def SetQuadAllowed(self, toAllow=True):
367         if not self.params:
368             # use simple hyps
369             hasSimpleHyps = False
370             simpleHyps = ["QuadranglePreference","LengthFromEdges","MaxElementArea"]
371             for hyp in self.mesh.GetHypothesisList( self.geom ):
372                 if hyp.GetName() in simpleHyps:
373                     hasSimpleHyps = True
374                     if hyp.GetName() == "QuadranglePreference":
375                         if not toAllow: # remove QuadranglePreference
376                             self.mesh.RemoveHypothesis( self.geom, hyp )
377                         else:
378                             return hyp
379                         return None
380                     pass
381                 pass
382             if hasSimpleHyps:
383                 if toAllow: # add QuadranglePreference
384                     return self.Hypothesis("QuadranglePreference", UseExisting=1, CompareMethod=self.CompareEqualHyp)
385                 return None
386             pass
387         self.Parameters().SetQuadAllowed( toAllow )
388         return self.params
389
390     pass # end of NETGEN_2D_Only_Algorithm class
391
392
393 ## Tetrahedron 3D algorithm
394 #
395 #  It can be created by calling smeshBuilder.Mesh.Tetrahedron() or smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN, geom=0 )
396 #
397 #  This algorithm generates only 3D (volumes) elements for given geometrical shape
398 #  and, in contrast to NETGEN_1D2D3D_Algorithm class, should be used in conjunction
399 #  with other 1D and 2D meshing algorithms.
400 class NETGEN_3D_Algorithm(NETGEN_Algorithm):
401
402     ## name of the dynamic method in smeshBuilder.Mesh class
403     #  @internal
404     meshMethod = "Tetrahedron"
405     ## type of algorithm used with helper function in smeshBuilder.Mesh class
406     #  @internal
407     algoType   = NETGEN
408     ## flag pointing either this algorithm should be used by default in dynamic method
409     #  of smeshBuilder.Mesh class
410     #  @internal
411     isDefault  = True
412     ## doc string of the method
413     #  @internal
414     docHelper  = "Creates tetrahedron 3D algorithm for solids"
415
416     ## Private constructor.
417     #  @param mesh parent mesh object algorithm is assigned to
418     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
419     #              if it is @c 0 (default), the algorithm is assigned to the main shape
420     def __init__(self, mesh, geom=0):
421         NETGEN_Algorithm.__init__(self, mesh, geom)
422         pass
423
424     ## Defines @c MaxElementVolume hypothesis to specify the maximum volume value of each tetrahedron
425     #  @param vol maximum volume value of each tetrahedron
426     #  @param UseExisting if \c True - searches for the existing hypothesis created with
427     #                   the same parameters, else (default) - creates a new one
428     #  @return hypothesis object
429     def MaxElementVolume(self, vol, UseExisting=0):
430         compFun = lambda hyp, args: IsEqual(hyp.GetMaxElementVolume(), args[0])
431         hyp = self.Hypothesis("MaxElementVolume", [vol], UseExisting=UseExisting,
432                               CompareMethod=compFun)
433         hyp.SetMaxElementVolume(vol)
434         return hyp
435
436     pass # end of NETGEN_3D_Algorithm class
437
438
439 ## Triangle (helper) 1D-2D algorithm
440 #
441 #  This is the helper class that is used just to allow creating of create NETGEN_1D2D algorithm
442 #  by calling smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN, geom=0 ); this is required for backward compatibility
443 #  with old Python scripts.
444 #
445 #  @note This class (and corresponding smeshBuilder.Mesh function) is obsolete;
446 #  use smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_1D2D, geom=0 ) instead.
447 class NETGEN_1D2D_Algorithm_2(NETGEN_1D2D_Algorithm):
448
449     ## name of the dynamic method in smeshBuilder.Mesh class
450     #  @internal
451     algoType = NETGEN
452
453     ## Private constructor.
454     #  @param mesh parent mesh object algorithm is assigned to
455     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
456     #              if it is @c 0 (default), the algorithm is assigned to the main shape
457     def __init__(self, mesh, geom=0):
458         self.algoType = NETGEN_1D2D
459         NETGEN_1D2D_Algorithm.__init__(self,mesh, geom)
460         pass
461
462     pass # end of NETGEN_1D2D_Algorithm_2 class
463
464
465 ## Tetrahedron (helper) 1D-2D-3D algorithm.
466 #
467 #  This is the helper class that is used just to allow creating of create NETGEN_1D2D3D
468 #  by calling smeshBuilder.Mesh.Netgen(); this is required for backward compatibility with old Python scripts.
469 #
470 #  @note This class (and corresponding smeshBuilder.Mesh function) is obsolete;
471 #  use smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN_1D2D3D, geom=0 ) instead.
472 class NETGEN_1D2D3D_Algorithm_2(NETGEN_1D2D3D_Algorithm):
473
474     ## name of the dynamic method in smeshBuilder.Mesh class
475     #  @internal
476     meshMethod = "Netgen"
477     ## doc string of the method
478     #  @internal
479     docHelper  = "Deprecated, used only for compatibility! See Tetrahedron() method."
480
481     ## Private constructor.
482     #  @param mesh parent mesh object algorithm is assigned to
483     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
484     #              if it is @c 0 (default), the algorithm is assigned to the main shape
485     def __init__(self, mesh, geom=0):
486         NETGEN_1D2D3D_Algorithm.__init__(self,mesh, geom)
487         pass
488
489     pass # end of NETGEN_1D2D3D_Algorithm_2 class