Salome HOME
Merge from V6_main (04/10/2012)
[plugins/netgenplugin.git] / src / NETGENPlugin / NETGENPluginDC.py
1 # Copyright (C) 2007-2012  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.
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 NETGENPluginDC
22 # Python API for the NETGEN meshing plug-in module.
23
24 from smesh_algorithm import Mesh_Algorithm
25 from smesh 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 smesh.Mesh.Tetrahedron( smesh.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 smesh.Mesh class
181     #  @internal
182     meshMethod = "Tetrahedron"
183     ## type of algorithm used with helper function in smesh.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
222     ## Sets number of segments overriding the value set by SetLocalLength()
223     #  @param theVal new value of number of segments parameter
224     def SetNumberOfSegments(self, theVal):
225         self.Parameters(SIMPLE).SetNumberOfSegments(theVal)
226         pass
227
228     ## Sets number of segments overriding the value set by SetNumberOfSegments()
229     #  @param theVal new value of local length parameter
230     def SetLocalLength(self, theVal):
231         self.Parameters(SIMPLE).SetLocalLength(theVal)
232         pass
233
234     ## Defines @c MaxElementArea parameter of @c NETGEN_SimpleParameters_3D hypothesis.
235     #  Overrides value set by LengthFromEdges()
236     #  @param area new value of @c MaxElementArea parameter
237     def MaxElementArea(self, area):
238         self.Parameters(SIMPLE).SetMaxElementArea(area)
239         pass
240
241     ## Defines @c LengthFromEdges parameter of @c NETGEN_SimpleParameters_3D hypothesis.
242     #  Overrides value set by MaxElementArea()
243     def LengthFromEdges(self):
244         self.Parameters(SIMPLE).LengthFromEdges()
245         pass
246
247     ## Defines @c LengthFromFaces parameter of @c NETGEN_SimpleParameters_3D hypothesis.
248     #  Overrides value set by MaxElementVolume()
249     def LengthFromFaces(self):
250         self.Parameters(SIMPLE).LengthFromFaces()
251         pass
252
253     ## Defines @c MaxElementVolume parameter of @c NETGEN_SimpleParameters_3D hypothesis.
254     #  Overrides value set by LengthFromFaces()
255     #  @param vol new value of @c MaxElementVolume parameter
256     def MaxElementVolume(self, vol):
257         self.Parameters(SIMPLE).SetMaxElementVolume(vol)
258         pass
259
260     pass # end of NETGEN_1D2D3D_Algorithm class
261
262
263 ## Triangle NETGEN 1D-2D algorithm. 
264 #
265 #  It can be created by calling smesh.Mesh.Triangle( smesh.NETGEN_1D2D, geom=0 )
266 #
267 #  This algorithm generates 1D (edges) and 2D (faces) elements
268 #  for given geometrical shape.
269 class NETGEN_1D2D_Algorithm(NETGEN_1D2D3D_Algorithm):
270
271     ## name of the dynamic method in smesh.Mesh class
272     #  @internal
273     meshMethod = "Triangle"
274     ## type of algorithm used with helper function in smesh.Mesh class
275     #  @internal
276     algoType   = NETGEN_1D2D
277     ## doc string of the method
278     #  @internal
279     docHelper  = "Creates triangle 2D algorithm for faces"
280
281     ## Private constructor.
282     #  @param mesh parent mesh object algorithm is assigned to
283     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
284     #              if it is @c 0 (default), the algorithm is assigned to the main shape
285     def __init__(self, mesh, geom=0):
286         NETGEN_1D2D3D_Algorithm.__init__(self, mesh, geom)
287         pass
288
289     pass # end of NETGEN_1D2D_Algorithm class
290
291
292 ## Triangle NETGEN 2D algorithm
293 #
294 #  It can be created by calling smesh.Mesh.Triangle( smesh.NETGEN_2D, geom=0 )
295 #
296 #  This algorithm generates only 2D (faces) elements for given geometrical shape
297 #  and, in contrast to NETGEN_1D2D_Algorithm class, should be used in conjunction
298 #  with other 1D meshing algorithm.
299 class NETGEN_2D_Only_Algorithm(NETGEN_Algorithm):
300
301     ## name of the dynamic method in smesh.Mesh class
302     #  @internal
303     meshMethod = "Triangle"
304     ## type of algorithm used with helper function in smesh.Mesh class
305     #  @internal
306     algoType = NETGEN_2D
307     ## doc string of the method
308     #  @internal
309     docHelper  = "Creates triangle 2D algorithm for faces"
310     
311     ## Private constructor.
312     #  @param mesh parent mesh object algorithm is assigned to
313     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
314     #              if it is @c 0 (default), the algorithm is assigned to the main shape
315     def __init__(self, mesh, geom=0):
316         NETGEN_Algorithm.__init__(self, mesh, geom)
317         pass
318
319     ## Defines @c MaxElementArea parameter of hypothesis basing on the definition of the
320     #  maximum area of each triangle
321     #  @param area maximum area value of each triangle
322     #  @param UseExisting if \c True - searches for an existing hypothesis created with the
323     #                     same parameters, else (default) - creates a new one
324     #  @return hypothesis object
325     def MaxElementArea(self, area, UseExisting=0):
326         compFun = lambda hyp, args: IsEqual(hyp.GetMaxElementArea(), args[0])
327         hyp = self.Hypothesis("MaxElementArea", [area], UseExisting=UseExisting,
328                               CompareMethod=compFun)
329         hyp.SetMaxElementArea(area)
330         return hyp
331
332     ## Defines @c LengthFromEdges hypothesis to build triangles
333     #  based on the length of the edges taken from the wire
334     #  @return hypothesis object
335     def LengthFromEdges(self):
336         hyp = self.Hypothesis("LengthFromEdges", UseExisting=1, CompareMethod=self.CompareEqualHyp)
337         return hyp
338
339     ## Sets @c QuadAllowed flag.
340     #  @param toAllow new value of the @c QuadAllowed parameter (@c True by default)
341     #  @return hypothesis object
342     def SetQuadAllowed(self, toAllow=True):
343         if not self.params:
344             # use simple hyps
345             hasSimpleHyps = False
346             simpleHyps = ["QuadranglePreference","LengthFromEdges","MaxElementArea"]
347             for hyp in self.mesh.GetHypothesisList( self.geom ):
348                 if hyp.GetName() in simpleHyps:
349                     hasSimpleHyps = True
350                     if hyp.GetName() == "QuadranglePreference":
351                         if not toAllow: # remove QuadranglePreference
352                             self.mesh.RemoveHypothesis( self.geom, hyp )
353                         else:
354                             return hyp
355                         return None
356                     pass
357                 pass
358             if hasSimpleHyps:
359                 if toAllow: # add QuadranglePreference
360                     return self.Hypothesis("QuadranglePreference", UseExisting=1, CompareMethod=self.CompareEqualHyp)
361                 return None
362             pass
363         self.Parameters().SetQuadAllowed( toAllow )
364         return self.params
365
366     pass # end of NETGEN_2D_Only_Algorithm class
367
368
369 ## Tetrahedron 3D algorithm
370 #
371 #  It can be created by calling smesh.Mesh.Tetrahedron() or smesh.Mesh.Tetrahedron( smesh.NETGEN, geom=0 )
372 #
373 #  This algorithm generates only 3D (volumes) elements for given geometrical shape
374 #  and, in contrast to NETGEN_1D2D3D_Algorithm class, should be used in conjunction
375 #  with other 1D and 2D meshing algorithms.
376 class NETGEN_3D_Algorithm(NETGEN_Algorithm):
377
378     ## name of the dynamic method in smesh.Mesh class
379     #  @internal
380     meshMethod = "Tetrahedron"
381     ## type of algorithm used with helper function in smesh.Mesh class
382     #  @internal
383     algoType   = NETGEN
384     ## flag pointing either this algorithm should be used by default in dynamic method
385     #  of smesh.Mesh class
386     #  @internal
387     isDefault  = True
388     ## doc string of the method
389     #  @internal
390     docHelper  = "Creates tetrahedron 3D algorithm for solids"
391
392     ## Private constructor.
393     #  @param mesh parent mesh object algorithm is assigned to
394     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
395     #              if it is @c 0 (default), the algorithm is assigned to the main shape
396     def __init__(self, mesh, geom=0):
397         NETGEN_Algorithm.__init__(self, mesh, geom)
398         pass
399
400     ## Defines @c MaxElementVolume hypothesis to specify the maximum volume value of each tetrahedron
401     #  @param vol maximum volume value of each tetrahedron
402     #  @param UseExisting if \c True - searches for the existing hypothesis created with
403     #                   the same parameters, else (default) - creates a new one
404     #  @return hypothesis object
405     def MaxElementVolume(self, vol, UseExisting=0):
406         compFun = lambda hyp, args: IsEqual(hyp.GetMaxElementVolume(), args[0])
407         hyp = self.Hypothesis("MaxElementVolume", [vol], UseExisting=UseExisting,
408                               CompareMethod=compFun)
409         hyp.SetMaxElementVolume(vol)
410         return hyp
411
412     pass # end of NETGEN_3D_Algorithm class
413
414
415 ## Triangle (helper) 1D-2D algorithm
416 #
417 #  This is the helper class that is used just to allow creating of create NETGEN_1D2D algorithm
418 #  by calling smesh.Mesh.Triangle( smesh.NETGEN, geom=0 ); this is required for backward compatibility
419 #  with old Python scripts.
420 #
421 #  @note This class (and corresponding smesh.Mesh function) is obsolete;
422 #  use smesh.Mesh.Triangle( smesh.NETGEN_1D2D, geom=0 ) instead.
423 class NETGEN_1D2D_Algorithm_2(NETGEN_1D2D_Algorithm):
424
425     ## name of the dynamic method in smesh.Mesh class
426     #  @internal
427     algoType = NETGEN
428
429     ## Private constructor.
430     #  @param mesh parent mesh object algorithm is assigned to
431     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
432     #              if it is @c 0 (default), the algorithm is assigned to the main shape
433     def __init__(self, mesh, geom=0):
434         self.algoType = NETGEN_1D2D
435         NETGEN_1D2D_Algorithm.__init__(self,mesh, geom)
436         pass
437
438     pass # end of NETGEN_1D2D_Algorithm_2 class
439
440
441 ## Tetrahedron (helper) 1D-2D-3D algorithm.
442 #
443 #  This is the helper class that is used just to allow creating of create NETGEN_1D2D3D
444 #  by calling smesh.Mesh.Netgen(); this is required for backward compatibility with old Python scripts.
445 #
446 #  @note This class (and corresponding smesh.Mesh function) is obsolete;
447 #  use smesh.Mesh.Tetrahedron( smesh.NETGEN_1D2D3D, geom=0 ) instead.
448 class NETGEN_1D2D3D_Algorithm_2(NETGEN_1D2D3D_Algorithm):
449
450     ## name of the dynamic method in smesh.Mesh class
451     #  @internal
452     meshMethod = "Netgen"
453     ## doc string of the method
454     #  @internal
455     docHelper  = "Deprecated, used only for compatibility! See Tetrahedron() method."
456
457     ## Private constructor.
458     #  @param mesh parent mesh object algorithm is assigned to
459     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
460     #              if it is @c 0 (default), the algorithm is assigned to the main shape
461     def __init__(self, mesh, geom=0):
462         NETGEN_1D2D3D_Algorithm.__init__(self,mesh, geom)
463         pass
464
465     pass # end of NETGEN_1D2D3D_Algorithm_2 class