Salome HOME
Merge branch 'master' of https://codev-tuleap.cea.fr/plugins/git/salome/netgenplugin
[plugins/netgenplugin.git] / src / NETGENPlugin / NETGENPluginBuilder.py
1 # Copyright (C) 2007-2019  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         if not mesh.GetMesh().HasShapeToMesh() and \
110            self.meshMethod == "Triangle": # create a 2D remesher
111             self.Create(mesh, geom, "NETGEN_Remesher_2D", LIBRARY)
112         else:
113             self.Create(mesh, geom, self.algoType, LIBRARY)
114         self.params = None
115         pass
116
117     ## Sets @c MaxSize parameter
118     #  @param theSize new value of the @c MaxSize parameter
119     def SetMaxSize(self, theSize):
120         if self.Parameters(): self.params.SetMaxSize(theSize)
121         pass
122
123     ## Sets @c MinSize parameter
124     #  @param theSize new value of the @c MinSize parameter
125     def SetMinSize(self, theSize):
126         if self.Parameters(): self.params.SetMinSize(theSize)
127         pass
128
129     ## Sets @c Optimize flag
130     #  @param theVal new value of the @c Optimize parameter
131     def SetOptimize(self, theVal):
132         if self.Parameters(): self.params.SetOptimize(theVal)
133         pass
134
135     ## Sets @c Fineness parameter
136     #  @param theFineness new value of the @c Fineness parameter; it can be:
137     #  @ref VeryCoarse, @ref Coarse, @ref Moderate, @ref Fine, @ref VeryFine or @ref Custom
138     def SetFineness(self, theFineness):
139         if self.Parameters(): self.params.SetFineness(theFineness)
140         pass
141
142     ## Sets @c GrowthRate parameter
143     #  @param theRate new value of the @c GrowthRate parameter
144     def SetGrowthRate(self, theRate):
145         if self.Parameters(): self.params.SetGrowthRate(theRate)
146         pass
147
148     ## Creates meshing hypothesis according to the chosen algorithm type
149     #  and initializes it with default parameters
150     #  @param which hypothesis type; can be either @ref SOLE (default) or @ref SIMPLE
151     #  @return hypothesis object
152     def Parameters(self, which=SOLE):
153         if self.algoType == NETGEN_1D2D:
154             if which == SIMPLE:
155                 hypType = "NETGEN_SimpleParameters_2D"
156             else:
157                 hypType = "NETGEN_Parameters_2D"
158         elif self.algoType == NETGEN_1D2D3D:
159             if which == SIMPLE:
160                 hypType = "NETGEN_SimpleParameters_3D"
161             else:
162                 hypType = "NETGEN_Parameters"
163         elif self.algoType == NETGEN_2D:
164             hypType = "NETGEN_Parameters_2D_ONLY"
165         else:
166             hypType = "NETGEN_Parameters_3D"
167
168         if self.algo.GetName() == "NETGEN_Remesher_2D":
169             hypType = "NETGEN_RemesherParameters_2D"
170
171         if self.params and self.params.GetName() != hypType:
172             self.mesh.RemoveHypothesis( self.params, self.geom )
173             self.params = None
174         if not self.params:
175             self.params = self.Hypothesis(hypType, [], LIBRARY, UseExisting=0)
176
177         return self.params
178
179     ## Defines a file specifying size of elements at points and lines
180     #  @param file name of the file
181     def SetMeshSizeFile(self, file):
182         self.Parameters().SetMeshSizeFile(file)
183         pass
184
185     ## Set size of elements on a shape
186     #  @param shape - geometry
187     #  @param size - element size
188     def SetLocalSizeOnShape(self, shape, size ):
189         self.Parameters().SetLocalSizeOnShape(shape, size)
190         pass
191         
192
193     pass # end of NETGEN_Algorithm class
194
195
196 ## Tetrahedron 1D-2D-3D algorithm.
197 #
198 #  It can be created by calling smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN_1D2D3D, geom=0 ).
199 #  This algorithm generates all 1D (edges), 2D (faces) and 3D (volumes) elements
200 #  for given geometrical shape.
201 class NETGEN_1D2D3D_Algorithm(NETGEN_Algorithm):
202
203     ## name of the dynamic method in smeshBuilder.Mesh class
204     #  @internal
205     meshMethod = "Tetrahedron"
206     ## type of algorithm used with helper function in smeshBuilder.Mesh class
207     #  @internal
208     algoType   = NETGEN_1D2D3D
209     ## doc string of the method
210     #  @internal
211     docHelper  = "Creates tetrahedron 3D algorithm for solids"
212
213     ## Private constructor.
214     #  @param mesh parent mesh object algorithm is assigned to
215     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
216     #              if it is @c 0 (default), the algorithm is assigned to the main shape
217     def __init__(self, mesh, geom=0):
218         NETGEN_Algorithm.__init__(self, mesh, geom)
219         pass
220
221     ## Sets @c SecondOrder flag
222     #  @param theVal new value of the @c SecondOrder parameter
223     def SetSecondOrder(self, theVal):
224         if self.Parameters(): self.params.SetSecondOrder(theVal)
225         pass
226
227     ## Sets @c NbSegPerEdge parameter
228     #  @param theVal new value of the @c NbSegPerEdge parameter
229     def SetNbSegPerEdge(self, theVal):
230         if self.Parameters(): self.params.SetNbSegPerEdge(theVal)
231         pass
232
233     ## Sets @c NbSegPerRadius parameter
234     #  @param theVal new value of the @c NbSegPerRadius parameter
235     def SetNbSegPerRadius(self, theVal):
236         if self.Parameters(): self.params.SetNbSegPerRadius(theVal)
237         pass
238
239     ## Sets @c ChordalError parameter
240     #  @param theVal new value of the @c ChordalError parameter
241     def SetChordalError(self, theVal):
242         if self.Parameters():
243             self.params.SetChordalError(theVal)
244             self.params.SetChordalErrorEnabled( theVal > 0 )
245         pass
246
247     ## Sets @c RidgeAngle parameter
248     #  @param theVal new value of the @c RidgeAngle parameter
249     def SetRidgeAngle(self, theVal):
250         if self.Parameters():
251             self.params.SetRidgeAngle(theVal)
252         pass
253
254     ## Sets @c QuadAllowed flag
255     #  @param toAllow new value of the @c QuadAllowed parameter (@c True by default)
256     def SetQuadAllowed(self, toAllow=True):
257         if self.Parameters(): self.params.SetQuadAllowed(toAllow)
258         pass
259     ## Sets @c UseSurfaceCurvature flag
260     #  @param toUse new value of the @c UseSurfaceCurvature parameter (@c True by default)
261     def SetUseSurfaceCurvature(self, toUse=True):
262         if self.Parameters(): self.params.SetUseSurfaceCurvature(toUse)
263         pass
264     ## Sets @c FuseEdges flag
265     #  @param toFuse new value of the @c FuseEdges parameter (@c False by default)
266     def SetFuseEdges(self, toFuse=False):
267         if self.Parameters(): self.params.SetFuseEdges(toFuse)
268         pass
269
270     ## Sets number of segments overriding the value set by SetLocalLength()
271     #  @param theVal new value of number of segments parameter
272     def SetNumberOfSegments(self, theVal):
273         self.Parameters(SIMPLE).SetNumberOfSegments(theVal)
274         pass
275
276     ## Sets number of segments overriding the value set by SetNumberOfSegments()
277     #  @param theVal new value of local length parameter
278     def SetLocalLength(self, theVal):
279         self.Parameters(SIMPLE).SetLocalLength(theVal)
280         pass
281
282     ## Defines @c MaxElementArea parameter of @c NETGEN_SimpleParameters_3D hypothesis.
283     #  Overrides value set by LengthFromEdges()
284     #  @param area new value of @c MaxElementArea parameter
285     def MaxElementArea(self, area):
286         self.Parameters(SIMPLE).SetMaxElementArea(area)
287         pass
288
289     ## Defines @c LengthFromEdges parameter of @c NETGEN_SimpleParameters_3D hypothesis.
290     #  Overrides value set by MaxElementArea()
291     def LengthFromEdges(self):
292         self.Parameters(SIMPLE).LengthFromEdges()
293         pass
294
295     ## Defines @c LengthFromFaces parameter of @c NETGEN_SimpleParameters_3D hypothesis.
296     #  Overrides value set by MaxElementVolume()
297     def LengthFromFaces(self):
298         self.Parameters(SIMPLE).LengthFromFaces()
299         pass
300
301     ## Defines @c MaxElementVolume parameter of @c NETGEN_SimpleParameters_3D hypothesis.
302     #  Overrides value set by LengthFromFaces()
303     #  @param vol new value of @c MaxElementVolume parameter
304     def MaxElementVolume(self, vol):
305         self.Parameters(SIMPLE).SetMaxElementVolume(vol)
306         pass
307
308     pass # end of NETGEN_1D2D3D_Algorithm class
309
310
311 ## Triangle NETGEN 1D-2D algorithm. 
312 #
313 #  It can be created by calling smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_1D2D, geom=0 )
314 #
315 #  This algorithm generates 1D (edges) and 2D (faces) elements
316 #  for given geometrical shape.
317 class NETGEN_1D2D_Algorithm(NETGEN_1D2D3D_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_1D2D
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_1D2D3D_Algorithm.__init__(self, mesh, geom)
335         pass
336
337     pass # end of NETGEN_1D2D_Algorithm class
338
339
340 ## Triangle NETGEN 2D algorithm
341 #
342 #  It can be created by calling smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_2D, geom=0 )
343 #
344 #  This algorithm generates only 2D (faces) elements for given geometrical shape
345 #  and, in contrast to NETGEN_1D2D_Algorithm class, should be used in conjunction
346 #  with other 1D meshing algorithm.
347 class NETGEN_2D_Only_Algorithm(NETGEN_Algorithm):
348
349     ## name of the dynamic method in smeshBuilder.Mesh class
350     #  @internal
351     meshMethod = "Triangle"
352     ## type of algorithm used with helper function in smeshBuilder.Mesh class
353     #  @internal
354     algoType = NETGEN_2D
355     ## doc string of the method
356     #  @internal
357     docHelper  = "Creates triangle 2D algorithm for faces"
358     
359     ## Private constructor.
360     #  @param mesh parent mesh object algorithm is assigned to
361     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
362     #              if it is @c 0 (default), the algorithm is assigned to the main shape
363     def __init__(self, mesh, geom=0):
364         NETGEN_Algorithm.__init__(self, mesh, geom)
365         pass
366
367     ## Defines @c MaxElementArea parameter of hypothesis basing on the definition of the
368     #  maximum area of each triangle
369     #  @param area maximum area value of each triangle
370     #  @param UseExisting if \c True - searches for an existing hypothesis created with the
371     #                     same parameters, else (default) - creates a new one
372     #  @return hypothesis object
373     def MaxElementArea(self, area, UseExisting=0):
374         compFun = lambda hyp, args: IsEqual(hyp.GetMaxElementArea(), args[0])
375         hyp = self.Hypothesis("MaxElementArea", [area], UseExisting=UseExisting,
376                               CompareMethod=compFun)
377         hyp.SetMaxElementArea(area)
378         return hyp
379
380     ## Defines @c LengthFromEdges hypothesis to build triangles
381     #  based on the length of the edges taken from the wire
382     #  @return hypothesis object
383     def LengthFromEdges(self):
384         hyp = self.Hypothesis("LengthFromEdges", UseExisting=1, CompareMethod=self.CompareEqualHyp)
385         return hyp
386         
387     ## Sets @c UseSurfaceCurvature flag
388     #  @param toUse new value of the @c UseSurfaceCurvature parameter (@c True by default)
389     def SetUseSurfaceCurvature(self, toUse=True):
390         if self.Parameters(): self.params.SetUseSurfaceCurvature(toUse)
391         pass
392
393     ## Sets @c QuadAllowed flag.
394     #  @param toAllow new value of the @c QuadAllowed parameter (@c True by default)
395     #  @return hypothesis object
396     def SetQuadAllowed(self, toAllow=True):
397         if not self.params:
398             # use simple hyps
399             hasSimpleHyps = False
400             simpleHyps = ["QuadranglePreference","LengthFromEdges","MaxElementArea"]
401             for hyp in self.mesh.GetHypothesisList( self.geom ):
402                 if hyp.GetName() in simpleHyps:
403                     hasSimpleHyps = True
404                     if hyp.GetName() == "QuadranglePreference":
405                         if not toAllow: # remove QuadranglePreference
406                             self.mesh.RemoveHypothesis( self.geom, hyp )
407                         else:
408                             return hyp
409                         return None
410                     pass
411                 pass
412             if hasSimpleHyps:
413                 if toAllow: # add QuadranglePreference
414                     return self.Hypothesis("QuadranglePreference", UseExisting=1, CompareMethod=self.CompareEqualHyp)
415                 return None
416             pass
417         self.Parameters().SetQuadAllowed( toAllow )
418         return self.params
419
420     pass # end of NETGEN_2D_Only_Algorithm class
421
422
423 ## Tetrahedron 3D algorithm
424 #
425 #  It can be created by calling smeshBuilder.Mesh.Tetrahedron() or smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN, geom=0 )
426 #
427 #  This algorithm generates only 3D (volumes) elements for given geometrical shape
428 #  and, in contrast to NETGEN_1D2D3D_Algorithm class, should be used in conjunction
429 #  with other 1D and 2D meshing algorithms.
430 class NETGEN_3D_Algorithm(NETGEN_Algorithm):
431
432     ## name of the dynamic method in smeshBuilder.Mesh class
433     #  @internal
434     meshMethod = "Tetrahedron"
435     ## type of algorithm used with helper function in smeshBuilder.Mesh class
436     #  @internal
437     algoType   = NETGEN
438     ## flag pointing either this algorithm should be used by default in dynamic method
439     #  of smeshBuilder.Mesh class
440     #  @internal
441     isDefault  = True
442     ## doc string of the method
443     #  @internal
444     docHelper  = "Creates tetrahedron 3D algorithm for solids"
445
446     ## Private constructor.
447     #  @param mesh parent mesh object algorithm is assigned to
448     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
449     #              if it is @c 0 (default), the algorithm is assigned to the main shape
450     def __init__(self, mesh, geom=0):
451         NETGEN_Algorithm.__init__(self, mesh, geom)
452         pass
453
454     ## Defines @c MaxElementVolume hypothesis to specify the maximum volume value of each tetrahedron
455     #  @param vol maximum volume value of each tetrahedron
456     #  @param UseExisting if \c True - searches for the existing hypothesis created with
457     #                   the same parameters, else (default) - creates a new one
458     #  @return hypothesis object
459     def MaxElementVolume(self, vol, UseExisting=0):
460         compFun = lambda hyp, args: IsEqual(hyp.GetMaxElementVolume(), args[0])
461         hyp = self.Hypothesis("MaxElementVolume", [vol], UseExisting=UseExisting,
462                               CompareMethod=compFun)
463         hyp.SetMaxElementVolume(vol)
464         return hyp
465
466     pass # end of NETGEN_3D_Algorithm class
467
468
469 ## Triangle (helper) 1D-2D algorithm
470 #
471 #  This is the helper class that is used just to allow creating of create NETGEN_1D2D algorithm
472 #  by calling smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN, geom=0 ); this is required for backward compatibility
473 #  with old Python scripts.
474 #
475 #  @note This class (and corresponding smeshBuilder.Mesh function) is obsolete;
476 #  use smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_1D2D, geom=0 ) instead.
477 class NETGEN_1D2D_Algorithm_2(NETGEN_1D2D_Algorithm):
478
479     ## name of the dynamic method in smeshBuilder.Mesh class
480     #  @internal
481     algoType = NETGEN
482
483     ## Private constructor.
484     #  @param mesh parent mesh object algorithm is assigned to
485     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
486     #              if it is @c 0 (default), the algorithm is assigned to the main shape
487     def __init__(self, mesh, geom=0):
488         self.algoType = NETGEN_1D2D
489         NETGEN_1D2D_Algorithm.__init__(self,mesh, geom)
490         pass
491
492     pass # end of NETGEN_1D2D_Algorithm_2 class
493
494
495 ## Tetrahedron (helper) 1D-2D-3D algorithm.
496 #
497 #  This is the helper class that is used just to allow creating of create NETGEN_1D2D3D
498 #  by calling smeshBuilder.Mesh.Netgen(); this is required for backward compatibility with old Python scripts.
499 #
500 #  @note This class (and corresponding smeshBuilder.Mesh function) is obsolete;
501 #  use smeshBuilder.Mesh.Tetrahedron( smeshBuilder.NETGEN_1D2D3D, geom=0 ) instead.
502 class NETGEN_1D2D3D_Algorithm_2(NETGEN_1D2D3D_Algorithm):
503
504     ## name of the dynamic method in smeshBuilder.Mesh class
505     #  @internal
506     meshMethod = "Netgen"
507     ## doc string of the method
508     #  @internal
509     docHelper  = "Deprecated, used only for compatibility! See Tetrahedron() method."
510
511     ## Private constructor.
512     #  @param mesh parent mesh object algorithm is assigned to
513     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
514     #              if it is @c 0 (default), the algorithm is assigned to the main shape
515     def __init__(self, mesh, geom=0):
516         NETGEN_1D2D3D_Algorithm.__init__(self,mesh, geom)
517         pass
518
519     pass # end of NETGEN_1D2D3D_Algorithm_2 class