Salome HOME
Merge from V6_main_20120808 08Aug12
[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 from smesh import Mesh_Algorithm, AssureGeomPublished, ParseParameters, IsEqual
21
22 # import NETGENPlugin module if possible
23 noNETGENPlugin = 0
24 try:
25     import NETGENPlugin
26 except ImportError:
27     noNETGENPlugin = 1
28     pass
29
30 # Types of algorithms
31 NETGEN_3D     = "NETGEN_3D"
32 NETGEN_1D2D3D = "NETGEN_2D3D"
33 NETGEN_1D2D   = "NETGEN_2D"
34 NETGEN_2D     = "NETGEN_2D_ONLY"
35 NETGEN_FULL   = NETGEN_1D2D3D
36 NETGEN        = NETGEN_3D
37 FULL_NETGEN   = NETGEN_FULL
38
39 SOLE   = 0
40 SIMPLE = 1
41
42 # Fineness enumeration
43 VeryCoarse = 0
44 Coarse     = 1
45 Moderate   = 2
46 Fine       = 3
47 VeryFine   = 4
48 Custom     = 5
49
50 ## Base of all NETGEN algorithms.
51 #
52 class NETGEN_Algorithm(Mesh_Algorithm):
53
54     def __init__(self, mesh, geom=0):
55         Mesh_Algorithm.__init__(self)
56         if noNETGENPlugin: print "Warning: NETGENPlugin module unavailable"
57         self.Create(mesh, geom, self.algoType, "libNETGENEngine.so")
58         self.params = None
59
60     ## Sets MaxSize
61     #
62     def SetMaxSize(self, theSize):
63         if self.Parameters():
64             self.params.SetMaxSize(theSize)
65
66     ## Sets MinSize
67     #
68     def SetMinSize(self, theSize):
69         if self.Parameters():
70             self.params.SetMinSize(theSize)
71
72
73     ## Sets Optimize flag
74     #
75     def SetOptimize(self, theVal):
76         if self.Parameters():
77             self.params.SetOptimize(theVal)
78
79     ## Sets Fineness
80     #  @param theFineness is:
81     #  VeryCoarse, Coarse, Moderate, Fine, VeryFine or Custom
82     #
83     def SetFineness(self, theFineness):
84         if self.Parameters():
85             self.params.SetFineness(theFineness)
86
87     ## Sets GrowthRate
88     #
89     def SetGrowthRate(self, theRate):
90         if self.Parameters():
91             self.params.SetGrowthRate(theRate)
92
93     ## Defines hypothesis having several parameters
94     #
95     def Parameters(self, which=SOLE):
96         if self.algoType == NETGEN_1D2D:
97             if which == SIMPLE:
98                 hypType = "NETGEN_SimpleParameters_2D"
99             else:
100                 hypType = "NETGEN_Parameters_2D"
101         elif self.algoType == NETGEN_1D2D3D:
102             if which == SIMPLE:
103                 hypType = "NETGEN_SimpleParameters_3D"
104             else:
105                 hypType = "NETGEN_Parameters"
106         elif self.algoType == NETGEN_2D:
107             hypType = "NETGEN_Parameters_2D_ONLY"
108         else:
109             hypType = "NETGEN_Parameters_3D"
110
111         if self.params and self.params.GetName() != hypType:
112             self.mesh.RemoveHypothesis( self.params, self.geom )
113             self.params = None
114         if not self.params:
115             self.params = self.Hypothesis(hypType, [],"libNETGENEngine.so",UseExisting=0)
116
117         return self.params
118
119
120
121 ## Defines a tetrahedron 1D-2D-3D algorithm
122 #  It is created by calling Mesh.Triangle( NETGEN_1D2D3D, geom=0 )
123 #
124 class NETGEN_1D2D3D_Algorithm(NETGEN_Algorithm):
125
126     meshMethod = "Tetrahedron"
127     algoType   = NETGEN_1D2D3D
128
129     ## Private constructor.
130     def __init__(self, mesh, geom=0):
131         NETGEN_Algorithm.__init__(self, mesh, geom)
132
133     ## Sets SecondOrder flag
134     #
135     def SetSecondOrder(self, theVal):
136         if self.Parameters():
137             self.params.SetSecondOrder(theVal)
138
139     ## Sets NbSegPerEdge
140     #
141     def SetNbSegPerEdge(self, theVal):
142         if self.Parameters():
143             self.params.SetNbSegPerEdge(theVal)
144
145     ## Sets NbSegPerRadius
146     #
147     def SetNbSegPerRadius(self, theVal):
148         if self.Parameters():
149             self.params.SetNbSegPerRadius(theVal)
150
151     ## Sets QuadAllowed flag.
152     def SetQuadAllowed(self, toAllow=True):
153         if self.Parameters():
154             self.params.SetQuadAllowed(toAllow)
155
156
157     ## Sets number of segments overriding the value set by SetLocalLength()
158     #
159     def SetNumberOfSegments(self, theVal):
160         self.Parameters(SIMPLE).SetNumberOfSegments(theVal)
161
162     ## Sets number of segments overriding the value set by SetNumberOfSegments()
163     #
164     def SetLocalLength(self, theVal):
165         self.Parameters(SIMPLE).SetLocalLength(theVal)
166
167     ## Defines "MaxElementArea" parameter of NETGEN_SimpleParameters_3D hypothesis.
168     #  Overrides value set by LengthFromEdges()
169     def MaxElementArea(self, area):
170         self.Parameters(SIMPLE).SetMaxElementArea(area)
171
172     ## Defines "LengthFromEdges" parameter of NETGEN_SimpleParameters_3D hypothesis
173     #  Overrides value set by MaxElementArea()
174     def LengthFromEdges(self):
175         self.Parameters(SIMPLE).LengthFromEdges()
176
177     ## Defines "LengthFromFaces" parameter of NETGEN_SimpleParameters_3D hypothesis
178     #  Overrides value set by MaxElementVolume()
179     def LengthFromFaces(self):
180         self.Parameters(SIMPLE).LengthFromFaces()
181
182     ## Defines "MaxElementVolume" parameter of NETGEN_SimpleParameters_3D hypothesis
183     #  Overrides value set by LengthFromFaces()
184     def MaxElementVolume(self, vol):
185         self.Parameters(SIMPLE).SetMaxElementVolume(vol)
186
187
188 ## Triangle NETGEN 1D-2D algorithm. 
189 #  It is created by calling Mesh.Triangle( NETGEN_1D2D, geom=0 )
190 #
191 class NETGEN_1D2D_Algorithm(NETGEN_1D2D3D_Algorithm):
192
193     meshMethod = "Triangle"
194     algoType   = NETGEN_1D2D
195
196     ## Private constructor.
197     def __init__(self, mesh, geom=0):
198         NETGEN_1D2D3D_Algorithm.__init__(self, mesh, geom)
199
200
201
202 ## Triangle NETGEN 2D algorithm
203 #  It is created by calling Mesh.Triangle( NETGEN_2D, geom=0 )
204 #
205 class NETGEN_2D_Only_Algorithm(NETGEN_Algorithm):
206
207     meshMethod = "Triangle"
208     algoType = NETGEN_2D
209     
210     ## Private constructor.
211     def __init__(self, mesh, geom=0):
212         NETGEN_Algorithm.__init__(self, mesh, geom)
213
214     ## Sets QuadAllowed flag.
215     def SetQuadAllowed(self, toAllow=True):
216         if self.Parameters():
217             self.params.SetQuadAllowed(toAllow)
218
219     ## Defines "MaxElementArea" hypothesis basing on the definition of the maximum area of each triangle
220     #  @param area for the maximum area of each triangle
221     #  @param UseExisting if ==true - searches for an  existing hypothesis created with the
222     #                     same parameters, else (default) - creates a new one
223     #
224     def MaxElementArea(self, area, UseExisting=0):
225         compFun = lambda hyp, args: IsEqual(hyp.GetMaxElementArea(), args[0])
226         hyp = self.Hypothesis("MaxElementArea", [area], UseExisting=UseExisting,
227                               CompareMethod=compFun)
228         hyp.SetMaxElementArea(area)
229         return hyp
230
231     ## Defines "LengthFromEdges" hypothesis to build triangles
232     #  based on the length of the edges taken from the wire
233     #
234     def LengthFromEdges(self):
235         hyp = self.Hypothesis("LengthFromEdges", UseExisting=1, CompareMethod=self.CompareEqualHyp)
236         return hyp
237
238     ## Sets QuadAllowed flag.
239     def SetQuadAllowed(self, toAllow=True):
240         if not self.params:
241             # use simple hyps
242             hasSimpleHyps = False
243             simpleHyps = ["QuadranglePreference","LengthFromEdges","MaxElementArea"]
244             for hyp in self.mesh.GetHypothesisList( self.geom ):
245                 if hyp.GetName() in simpleHyps:
246                     hasSimpleHyps = True
247                     if hyp.GetName() == "QuadranglePreference":
248                         if not toAllow: # remove QuadranglePreference
249                             self.mesh.RemoveHypothesis( self.geom, hyp )
250                         else:
251                             return hyp
252                         return None
253                     pass
254                 pass
255             if hasSimpleHyps:
256                 if toAllow: # add QuadranglePreference
257                     return self.Hypothesis("QuadranglePreference", UseExisting=1, CompareMethod=self.CompareEqualHyp)
258                 return None
259             pass
260         self.Parameters().SetQuadAllowed( toAllow )
261         return self.params
262
263 ## Defines a tetrahedron 3D algorithm
264 #  It is created by calling Mesh.Tetrahedron()
265 #
266 class NETGEN_3D_Algorithm(NETGEN_Algorithm):
267
268     meshMethod = "Tetrahedron"
269     algoType   = NETGEN
270     isDefault  = True
271
272     ## Private constructor.
273     def __init__(self, mesh, geom=0):
274         NETGEN_Algorithm.__init__(self, mesh, geom)
275
276     ## Defines "MaxElementVolume" hypothesis to give the maximun volume of each tetrahedron
277     #  @param vol for the maximum volume of each tetrahedron
278     #  @param UseExisting if ==true - searches for the existing hypothesis created with
279     #                   the same parameters, else (default) - creates a new one
280     def MaxElementVolume(self, vol, UseExisting=0):
281         compFun = lambda hyp, args: IsEqual(hyp.GetMaxElementVolume(), args[0])
282         hyp = self.Hypothesis("MaxElementVolume", [vol], UseExisting=UseExisting,
283                               CompareMethod=compFun)
284         hyp.SetMaxElementVolume(vol)
285         return hyp
286
287
288 # Class just to create NETGEN_1D2D by calling Mesh.Triangle(NETGEN)
289 class NETGEN_1D2D_Algorithm_2(NETGEN_1D2D_Algorithm):
290
291     algoType = NETGEN
292
293     ## Private constructor.
294     def __init__(self, mesh, geom=0):
295         self.algoType = NETGEN_1D2D
296         NETGEN_1D2D_Algorithm.__init__(self,mesh, geom)
297
298
299 # Class just to create NETGEN_1D2D3D by calling Mesh.Netgen()
300 class NETGEN_1D2D3D_Algorithm_2(NETGEN_1D2D3D_Algorithm):
301
302     meshMethod = "Netgen"
303
304     ## Private constructor.
305     def __init__(self, mesh, geom=0):
306         NETGEN_1D2D3D_Algorithm.__init__(self,mesh, geom)