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