Salome HOME
35bb8b7cd5b0bdcf2f906b6792ac743fe6109e6b
[plugins/ghs3dplugin.git] / src / GHS3DPlugin / GHS3DPluginBuilder.py
1 # Copyright (C) 2007-2020  CEA/DEN, EDF R&D
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 GHS3DPluginBuilder
22 # Python API for the MG-Tetra meshing plug-in module.
23
24 import omniORB
25 from salome.smesh.smesh_algorithm import Mesh_Algorithm
26 from salome.smesh.smeshBuilder import AssureGeomPublished
27
28 # import GHS3DPlugin module if possible
29 noGHS3DPlugin = 0
30 try:
31     import GHS3DPlugin
32 except ImportError:
33     noGHS3DPlugin = 1
34     pass
35
36 # Optimization level of MG-Tetra
37 # V3.1
38 None_Optimization, Light_Optimization, Medium_Optimization, Strong_Optimization = 0,1,2,3
39 # V4.1 (partialy redefines V3.1). Issue 0020574
40 None_Optimization, Light_Optimization, Standard_Optimization, StandardPlus_Optimization, Strong_Optimization = 0,1,2,3,4
41
42 # import items of enums
43 for e in GHS3DPlugin.Mode._items: exec('%s = GHS3DPlugin.%s'%(e,e))
44 for e in GHS3DPlugin.PThreadsMode._items: exec('%s = GHS3DPlugin.%s'%(e,e))
45 Mode_NO, Mode_YES, Mode_ONLY = GHS3DPlugin.Mode._items
46 Mode_SAFE, Mode_AGGRESSIVE, Mode_NONE = GHS3DPlugin.PThreadsMode._items
47
48 #----------------------------
49 # Mesh algo type identifiers
50 #----------------------------
51
52 ## Algorithm type: MG-Tetra tetrahedron 3D algorithm, see GHS3D_Algorithm
53 MG_Tetra = "MG-Tetra"
54 GHS3D = MG_Tetra
55 MG_Tetra_Optimization = "MG-Tetra Optimization"
56
57 ## Tetrahedron MG-Tetra 3D algorithm
58 #  
59 #  It can be created by calling smeshBuilder.Mesh.Tetrahedron( smeshBuilder.MG_Tetra, geom=0 )
60 class GHS3D_Algorithm(Mesh_Algorithm):
61
62     ## name of the dynamic method in smeshBuilder.Mesh class
63     #  @internal
64     meshMethod = "Tetrahedron"
65     ## type of algorithm used with helper function in smeshBuilder.Mesh class
66     #  @internal
67     algoType   = MG_Tetra
68     ## doc string of the method in smeshBuilder.Mesh class
69     #  @internal
70     docHelper  = "Creates tetrahedron 3D algorithm"
71
72     ## Private constructor.
73     #  @param mesh parent mesh object algorithm is assigned to
74     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
75     #              if it is @c 0 (default), the algorithm is assigned to the main shape
76     def __init__(self, mesh, geom=0):
77         Mesh_Algorithm.__init__(self)
78         if noGHS3DPlugin: print("Warning: GHS3DPlugin module unavailable")
79         self.Create(mesh, geom, self.algoType, "libGHS3DEngine.so")
80         self.params = None
81         pass
82
83     ## Defines hypothesis having several parameters
84     #  @return hypothesis object
85     def Parameters(self):
86         if not self.params:
87             self.params = self.Hypothesis("MG-Tetra Parameters", [],
88                                           "libGHS3DEngine.so", UseExisting=0)
89             pass
90         return self.params
91
92     ## Set lower boundary of mesh element size
93     #  Set it to zero to deactivate this option
94     def SetMinSize(self, theMinSize):
95         self.Parameters().SetMinSize(theMinSize)
96         return
97
98     ## Set upper boundary of mesh element size
99     #  Set it to zero to deactivate this option
100     def SetMaxSize(self, theMaxSize):
101         self.Parameters().SetMaxSize(theMaxSize)
102         return
103
104     ## Activate/deactivate volume proximity computation
105     #
106     def SetVolumeProximity(self, toUse ):
107         self.Parameters().SetVolumeProximity(toUse)
108         return
109
110     ## Set number of surface element layers to be generated due to volume proximity
111     #
112     def SetNbVolumeProximityLayers(self, nbLayers ):
113         self.Parameters().SetNbVolumeProximityLayers(nbLayers)
114         return
115
116     ## To mesh "holes" in a solid or not. Default is to mesh.
117     #  @param toMesh "mesh holes" flag value
118     def SetToMeshHoles(self, toMesh):
119         self.Parameters().SetToMeshHoles(toMesh)
120         pass
121
122     ## To make groups of volumes of different domains when mesh is generated from skin.
123     #  Default is to make groups.
124     # This option works only (1) for the mesh w/o shape and (2) if GetToMeshHoles() == true
125     #  @param toMakeGroups "Make groups of domains" flag value
126     def SetToMakeGroupsOfDomains(self, toMakeGroups):
127         self.Parameters().SetToMakeGroupsOfDomains(toMakeGroups)
128         pass
129
130     ## Set Optimization level:
131     #  @param level optimization level, one of the following values
132     #  - None_Optimization
133     #  - Light_Optimization
134     #  - Standard_Optimization
135     #  - StandardPlus_Optimization
136     #  - Strong_Optimization.
137     #  .
138     #  Default is Standard_Optimization
139     def SetOptimizationLevel(self, level):
140         self.Parameters().SetOptimizationLevel(level)
141         pass
142
143     ## Set maximal size of memory to be used by the algorithm (in Megabytes).
144     #  @param MB maximal size of memory
145     def SetMaximumMemory(self, MB):
146         self.Parameters().SetMaximumMemory(MB)
147         pass
148
149     ## Set initial size of memory to be used by the algorithm (in Megabytes) in
150     #  automatic memory adjustment mode.
151     #  @param MB initial size of memory
152     def SetInitialMemory(self, MB):
153         self.Parameters().SetInitialMemory(MB)
154         pass
155
156     ## Set path to working directory.
157     #  @param path working directory
158     def SetWorkingDirectory(self, path):
159         self.Parameters().SetWorkingDirectory(path)
160         pass
161
162     ## To keep working files or remove them.
163     #  @param toKeep "keep working files" flag value
164     def SetKeepFiles(self, toKeep):
165         self.Parameters().SetKeepFiles(toKeep)
166         pass
167     
168     ## Remove or not the log file (if any) in case of successful computation.
169     #  The log file remains in case of errors anyway. If 
170     #  the "keep working files" flag is set to true, this option
171     #  has no effect.
172     #  @param toRemove "remove log on success" flag value
173     def SetRemoveLogOnSuccess(self, toRemove):
174         self.Parameters().SetRemoveLogOnSuccess(toRemove)
175         pass
176     
177     ## Print the the log in a file. If set to false, the
178     # log is printed on the standard output
179     #  @param toPrintLogInFile "print log in a file" flag value
180     def SetPrintLogInFile(self, toPrintLogInFile):
181         self.Parameters().SetStandardOutputLog(not toPrintLogInFile)
182         pass
183
184     ## Set verbosity level [0-10].
185     #  @param level verbosity level
186     #  - 0 - no standard output,
187     #  - 2 - prints the data, quality statistics of the skin and final meshes and
188     #    indicates when the final mesh is being saved. In addition the software
189     #    gives indication regarding the CPU time.
190     #  - 10 - same as 2 plus the main steps in the computation, quality statistics
191     #    histogram of the skin mesh, quality statistics histogram together with
192     #    the characteristics of the final mesh.
193     def SetVerboseLevel(self, level):
194         self.Parameters().SetVerboseLevel(level)
195         pass
196
197     ## To create new nodes.
198     #  @param toCreate "create new nodes" flag value
199     def SetToCreateNewNodes(self, toCreate):
200         self.Parameters().SetToCreateNewNodes(toCreate)
201         pass
202
203     ## To use boundary recovery version which tries to create mesh on a very poor
204     #  quality surface mesh.
205     #  @param toUse "use boundary recovery version" flag value
206     def SetToUseBoundaryRecoveryVersion(self, toUse):
207         self.Parameters().SetToUseBoundaryRecoveryVersion(toUse)
208         pass
209
210     ## Applies finite-element correction by replacing overconstrained elements where
211     #  it is possible. The process is cutting first the overconstrained edges and
212     #  second the overconstrained facets. This insure that no edges have two boundary
213     #  vertices and that no facets have three boundary vertices.
214     #  @param toUseFem "apply finite-element correction" flag value
215     def SetFEMCorrection(self, toUseFem):
216         self.Parameters().SetFEMCorrection(toUseFem)
217         pass
218
219     ## To remove initial central point.
220     #  @param toRemove "remove initial central point" flag value
221     def SetToRemoveCentralPoint(self, toRemove):
222         self.Parameters().SetToRemoveCentralPoint(toRemove)
223         pass
224
225     ## To set an enforced vertex.
226     #  @param x            : x coordinate
227     #  @param y            : y coordinate
228     #  @param z            : z coordinate
229     #  @param size         : size of 1D element around enforced vertex
230     #  @param vertexName   : name of the enforced vertex
231     #  @param groupName    : name of the group
232     def SetEnforcedVertex(self, x, y, z, size, vertexName = "", groupName = ""):
233         if vertexName == "":
234             if groupName == "":
235                 return self.Parameters().SetEnforcedVertex(x, y, z, size)
236             else:
237                 return self.Parameters().SetEnforcedVertexWithGroup(x, y, z, size, groupName)
238             pass
239         else:
240             if groupName == "":
241                 return self.Parameters().SetEnforcedVertexNamed(x, y, z, size, vertexName)
242             else:
243                 return self.Parameters().SetEnforcedVertexNamedWithGroup(x, y, z, size, vertexName, groupName)
244             pass
245         pass
246
247     ## To set an enforced vertex given a GEOM vertex, group or compound.
248     #  @param theVertex    : GEOM vertex (or group, compound) to be projected on theFace.
249     #  @param size         : size of 1D element around enforced vertex
250     #  @param groupName    : name of the group
251     def SetEnforcedVertexGeom(self, theVertex, size, groupName = ""):
252         AssureGeomPublished( self.mesh, theVertex )
253         if groupName == "":
254             return self.Parameters().SetEnforcedVertexGeom(theVertex, size)
255         else:
256             return self.Parameters().SetEnforcedVertexGeomWithGroup(theVertex, size, groupName)
257         pass
258
259     ## To remove an enforced vertex.
260     #  @param x            : x coordinate
261     #  @param y            : y coordinate
262     #  @param z            : z coordinate
263     def RemoveEnforcedVertex(self, x, y, z):
264         return self.Parameters().RemoveEnforcedVertex(x, y, z)
265
266     ## To remove an enforced vertex given a GEOM vertex, group or compound.
267     #  @param theVertex    : GEOM vertex (or group, compound) to be projected on theFace.
268     def RemoveEnforcedVertexGeom(self, theVertex):
269         AssureGeomPublished( self.mesh, theVertex )
270         return self.Parameters().RemoveEnforcedVertexGeom(theVertex)
271
272     ## To set an enforced mesh with given size and add the enforced elements in the group "groupName".
273     #  @param theSource    : source mesh which provides constraint elements/nodes
274     #  @param elementType  : SMESH.ElementType (NODE, EDGE or FACE)
275     #  @param size         : size of elements around enforced elements. Unused if -1.
276     #  @param groupName    : group in which enforced elements will be added. Unused if "".
277     def SetEnforcedMesh(self, theSource, elementType, size = -1, groupName = ""):
278         if size < 0:
279             if groupName == "":
280                 return self.Parameters().SetEnforcedMesh(theSource, elementType)
281             else:
282                 return self.Parameters().SetEnforcedMeshWithGroup(theSource, elementType, groupName)
283             pass
284         else:
285             if groupName == "":
286                 return self.Parameters().SetEnforcedMeshSize(theSource, elementType, size)
287             else:
288                 return self.Parameters().SetEnforcedMeshSizeWithGroup(theSource, elementType, size, groupName)
289             pass
290         pass
291
292     ## Set advanced option value
293     #  @param optionName option name
294     #  @param optionValue option value
295     def SetOptionValue(self, optionName, optionValue):
296         self.Parameters().SetOptionValue( optionName, optionValue )
297         pass
298     
299     ## Sets command line option as text.
300     #  @param optionAndValue command line option in a form "option value"
301     def SetAdvancedOption(self, optionAndValue):
302         self.Parameters().SetAdvancedOption(optionAndValue)
303         pass
304     
305     ## OBSOLETE Sets command line option as text.
306     #  @param option command line option
307     def SetTextOption(self, option):
308         self.Parameters().SetAdvancedOption(option)
309         pass
310     
311     pass # end of GHS3D_Algorithm class
312
313
314 ## MG-Tetra Optimization algorithm - optimizer of tetrahedral meshes
315 #
316 #  It can be created by calling smeshBuilder.Mesh.Tetrahedron( smeshBuilder.MG_Tetra_Optimization )
317 class GHS3D_Optimizer(GHS3D_Algorithm):
318
319     ## name of the dynamic method in smeshBuilder.Mesh class
320     #  @internal
321     meshMethod = "Tetrahedron"
322     ## type of algorithm used with helper function in smeshBuilder.Mesh class
323     #  @internal
324     algoType   = MG_Tetra_Optimization
325     ## doc string of the method in smeshBuilder.Mesh class
326     #  @internal
327     docHelper  = "Creates MG-Tetra optimizer of tetrahedral meshes"
328
329     ## Private constructor.
330     #  @param mesh parent mesh object algorithm is assigned to
331     #  @param geom - not used
332     def __init__(self, mesh, geom=0):
333         GHS3D_Algorithm.__init__(self, mesh)
334
335         # remove some inherited methods
336         # del self.SetToMeshHoles
337         # del self.SetToMakeGroupsOfDomains
338         # del self.SetToUseBoundaryRecoveryVersion
339         # del self.SetFEMCorrection
340         # del self.SetToRemoveCentralPoint
341         # del self.SetEnforcedVertex
342         # del self.SetEnforcedVertexGeom
343         # del self.RemoveEnforcedVertex
344         # del self.RemoveEnforcedVertexGeom
345         # del self.SetEnforcedMesh
346         # del self.SetTextOption
347         pass
348
349     ## Defines hypothesis having several parameters
350     #  @return hypothesis object
351     def Parameters(self):
352         if not self.params:
353             self.params = self.Hypothesis("MG-Tetra Optimization Parameters", [],
354                                           "libGHS3DEngine.so", UseExisting=0)
355             pass
356         return self.params
357
358     ## Set Optimization mode
359     #  @param optMode optimization mode, one of the following values:
360     #  smeshBuilder.Mode_NO,
361     #  smeshBuilder.Mode_YES (default),
362     #  smeshBuilder.MODE_ONLY
363     def SetOptimizationOnly(self, optMode ):
364         self.Parameters().SetOptimizationOnly(optMode)
365         pass
366
367     ## Set mode of splitting over-constrained elements
368     #  @param ovcMode, one of the following values
369     #  smeshBuilder.Mode_NO (default),
370     #  smeshBuilder.Mode_YES,
371     #  smeshBuilder.Mode_ONLY
372     def SetSplitOverConstrained(self, ovcMode ):
373         self.Parameters().SetSplitOverConstrained(ovcMode)
374         pass
375
376     ## Activate smoothing sliver elements:
377     #  @param toSmooth - Boolean flag
378     def SetSmoothOffSlivers(self, toSmooth ):
379         self.Parameters().SetSmoothOffSlivers(toSmooth)
380         pass
381
382     ## Set multithread mode
383     #  @param mode - the mode, one of the following values:
384     #  smeshBuilder.Mode_SAFE,
385     #  smeshBuilder.Mode_AGGRESSIVE,
386     #  smeshBuilder.Mode_NONE (default)
387     def SetPThreadsMode(self, mode ):
388         self.Parameters().SetPThreadsMode(mode)
389         pass
390
391     ## Set maximal number of threads
392     #  @param nb - number of threads
393     def SetMaximalNumberOfThreads(self, nb ):
394         self.Parameters().SetMaximalNumberOfThreads(nb)
395         pass
396
397
398     ## Set Optimization level:
399     #  @param level optimization level, one of the following values
400     #  - None_Optimization
401     #  - Light_Optimization
402     #  - Standard_Optimization
403     #  - StandardPlus_Optimization
404     #  - Strong_Optimization.
405     #  .
406     #  Default is Standard_Optimization
407     def SetOptimizationLevel(self, level):
408         self.Parameters().SetOptimizationLevel(level)
409         pass
410
411     ## Set maximal size of memory to be used by the algorithm (in Megabytes).
412     #  @param MB maximal size of memory
413     def SetMaximumMemory(self, MB):
414         self.Parameters().SetMaximumMemory(MB)
415         pass
416
417     ## Set initial size of memory to be used by the algorithm (in Megabytes) in
418     #  automatic memory adjustment mode.
419     #  @param MB initial size of memory
420     def SetInitialMemory(self, MB):
421         self.Parameters().SetInitialMemory(MB)
422         pass
423
424     ## Set path to working directory.
425     #  @param path working directory
426     def SetWorkingDirectory(self, path):
427         self.Parameters().SetWorkingDirectory(path)
428         pass
429
430     ## To keep working files or remove them.
431     #  @param toKeep "keep working files" flag value
432     def SetKeepFiles(self, toKeep):
433         self.Parameters().SetKeepFiles(toKeep)
434         pass
435
436     ## Remove or not the log file (if any) in case of successful computation.
437     #  The log file remains in case of errors anyway. If
438     #  the "keep working files" flag is set to true, this option
439     #  has no effect.
440     #  @param toRemove "remove log on success" flag value
441     def SetRemoveLogOnSuccess(self, toRemove):
442         self.Parameters().SetRemoveLogOnSuccess(toRemove)
443         pass
444
445     ## Print the the log in a file. If set to false, the
446     # log is printed on the standard output
447     #  @param toPrintLogInFile "print log in a file" flag value
448     def SetPrintLogInFile(self, toPrintLogInFile):
449         self.Parameters().SetStandardOutputLog(not toPrintLogInFile)
450         pass
451
452     ## Set verbosity level [0-10].
453     #  @param level verbosity level
454     #  - 0 - no standard output,
455     #  - 2 - prints the data, quality statistics of the skin and final meshes and
456     #    indicates when the final mesh is being saved. In addition the software
457     #    gives indication regarding the CPU time.
458     #  - 10 - same as 2 plus the main steps in the computation, quality statistics
459     #    histogram of the skin mesh, quality statistics histogram together with
460     #    the characteristics of the final mesh.
461     def SetVerboseLevel(self, level):
462         self.Parameters().SetVerboseLevel(level)
463         pass
464
465     ## To create new nodes.
466     #  @param toCreate "create new nodes" flag value
467     def SetToCreateNewNodes(self, toCreate):
468         self.Parameters().SetToCreateNewNodes(toCreate)
469         pass
470
471     ## Sets command line option as text.
472     #  @param option command line option
473     def SetAdvancedOption(self, option):
474         self.Parameters().SetAdvancedOption(option)
475         pass
476
477     pass # end of GHS3D_Optimizer class
478
479 class hypoProxy(GHS3DPlugin._objref_GHS3DPlugin_Hypothesis):
480     """
481     Private class wrapping to provide backward compatibility with deprecated API.
482     """
483     def __init__(self, *args):
484         GHS3DPlugin._objref_GHS3DPlugin_Hypothesis.__init__(self, *args)
485     def __deepcopy__(self, memo=None):
486         new = self.__class__(self)
487         return new
488     def SetPrintLogInFile(self, value):
489         self.SetStandardOutputLog(not value)
490 omniORB.registerObjref(GHS3DPlugin._objref_GHS3DPlugin_Hypothesis._NP_RepositoryId, hypoProxy)
491 del hypoProxy