Salome HOME
Fix omniORB::fatalException at SALOME termination after creating a Mesh in Python
[modules/smesh.git] / src / SMESH_SWIG / smeshBuilder.py
index e819c6630eb6a23358ed9e99ef504503302cb423..b82ba1ee5314c27e1d630b45e021985a19ee169c 100644 (file)
@@ -551,7 +551,9 @@ class smeshBuilder(object, SMESH._objref_SMESH_Gen):
         aMeshes = [ Mesh(self, self.geompyD, m) for m in aSmeshMeshes ]
         return aMeshes, aStatus
 
-    ## Creates a Mesh object importing data from the given GMF file
+    ## Creates a Mesh object importing data from the given GMF file.
+    #  GMF files must have .mesh extension for the ASCII format and .meshb for
+    #  the binary format.
     #  @return [ an instance of Mesh class, SMESH.ComputeError ]
     #  @ingroup l2_impexp
     def CreateMeshesFromGMF( self, theFileName ):
@@ -980,6 +982,7 @@ class smeshBuilder(object, SMESH._objref_SMESH_Gen):
         if not hasattr(src1, "_narrow"): return None
         src1 = src1._narrow(SMESH.SMESH_IDSource)
         if not src1: return None
+        unRegister = genObjUnRegister()
         if id1 != 0:
             m = src1.GetMesh()
             e = m.GetMeshEditor()
@@ -987,6 +990,7 @@ class smeshBuilder(object, SMESH._objref_SMESH_Gen):
                 src1 = e.MakeIDSource([id1], SMESH.FACE)
             else:
                 src1 = e.MakeIDSource([id1], SMESH.NODE)
+            unRegister.set( src1 )
             pass
         if hasattr(src2, "_narrow"):
             src2 = src2._narrow(SMESH.SMESH_IDSource)
@@ -997,11 +1001,12 @@ class smeshBuilder(object, SMESH._objref_SMESH_Gen):
                     src2 = e.MakeIDSource([id2], SMESH.FACE)
                 else:
                     src2 = e.MakeIDSource([id2], SMESH.NODE)
+                unRegister.set( src2 )
                 pass
             pass
         aMeasurements = self.CreateMeasurements()
+        unRegister.set( aMeasurements )
         result = aMeasurements.MinDistance(src1, src2)
-        aMeasurements.UnRegister()
         return result
 
     ## Get bounding box of the specified object(s)
@@ -1041,6 +1046,44 @@ class smeshBuilder(object, SMESH._objref_SMESH_Gen):
         aMeasurements.UnRegister()
         return result
 
+    ## Get sum of lengths of all 1D elements in the mesh object.
+    #  @param elemId obj mesh, submesh or group
+    #  @return sum of lengths of all 1D elements
+    #  @ingroup l1_measurements
+    def GetLength(self, obj):
+        if isinstance(obj, Mesh): obj = obj.mesh
+        if isinstance(obj, Mesh_Algorithm): obj = obj.GetSubMesh()
+        aMeasurements = self.CreateMeasurements()
+        value = aMeasurements.Length(obj)
+        aMeasurements.UnRegister()
+        return value
+
+    ## Get sum of areas of all 2D elements in the mesh object.
+    #  @param elemId obj mesh, submesh or group
+    #  @return sum of areas of all 2D elements
+    #  @ingroup l1_measurements
+    def GetArea(self, obj):
+        if isinstance(obj, Mesh): obj = obj.mesh
+        if isinstance(obj, Mesh_Algorithm): obj = obj.GetSubMesh()
+        aMeasurements = self.CreateMeasurements()
+        value = aMeasurements.Area(obj)
+        aMeasurements.UnRegister()
+        return value
+
+    ## Get sum of volumes of all 3D elements in the mesh object.
+    #  @param elemId obj mesh, submesh or group
+    #  @return sum of volumes of all 3D elements
+    #  @ingroup l1_measurements
+    def GetVolume(self, obj):
+        if isinstance(obj, Mesh): obj = obj.mesh
+        if isinstance(obj, Mesh_Algorithm): obj = obj.GetSubMesh()
+        aMeasurements = self.CreateMeasurements()
+        value = aMeasurements.Volume(obj)
+        aMeasurements.UnRegister()
+        return value
+
+    pass # end of class smeshBuilder
+
 import omniORB
 #Registering the new proxy for SMESH_Gen
 omniORB.registerObjref(SMESH._objref_SMESH_Gen._NP_RepositoryId, smeshBuilder)
@@ -1133,12 +1176,12 @@ class Mesh:
                     else:
                         geo_name = "%s_%s to mesh"%(self.geom.GetShapeType(), id(self.geom)%100)
                     geompyD.addToStudy( self.geom, geo_name )
-                self.mesh = self.smeshpyD.CreateMesh(self.geom)
+                self.SetMesh( self.smeshpyD.CreateMesh(self.geom) )
 
             elif isinstance(obj, SMESH._objref_SMESH_Mesh):
                 self.SetMesh(obj)
         else:
-            self.mesh = self.smeshpyD.CreateEmptyMesh()
+            self.SetMesh( self.smeshpyD.CreateEmptyMesh() )
         if name:
             self.smeshpyD.SetName(self.mesh, name)
         elif objHasName:
@@ -1156,16 +1199,28 @@ class Mesh:
             if isinstance( attr, algoCreator ):
                 #print "algoCreator ", attrName
                 setattr( self, attrName, attr.copy( self ))
+                pass
+            pass
+        pass
 
+    ## Destructor. Clean-up resources
+    def __del__(self):
+        if self.mesh:
+            #self.mesh.UnRegister()
+            pass
+        pass
+        
     ## Initializes the Mesh object from an instance of SMESH_Mesh interface
     #  @param theMesh a SMESH_Mesh object
     #  @ingroup l2_construct
     def SetMesh(self, theMesh):
-        if self.mesh: self.mesh.UnRegister()
+        # do not call Register() as this prevents mesh servant deletion at closing study
+        #if self.mesh: self.mesh.UnRegister()
         self.mesh = theMesh
         if self.mesh:
-            self.mesh.Register()
+            #self.mesh.Register()
             self.geom = self.mesh.GetShapeToMesh()
+        pass
 
     ## Returns the mesh, that is an instance of SMESH_Mesh interface
     #  @return a SMESH_Mesh object
@@ -1577,8 +1632,10 @@ class Mesh:
     def ExportMED(self, f, auto_groups=0, version=MED_V2_2,
                   overwrite=1, meshPart=None, autoDimension=True):
         if meshPart:
+            unRegister = genObjUnRegister()
             if isinstance( meshPart, list ):
                 meshPart = self.GetIDSource( meshPart, SMESH.ALL )
+                unRegister.set( meshPart )
             self.mesh.ExportPartToMED( meshPart, f, auto_groups, version, overwrite, autoDimension)
         else:
             self.mesh.ExportToMEDX(f, auto_groups, version, overwrite, autoDimension)
@@ -1598,8 +1655,10 @@ class Mesh:
     #  @ingroup l2_impexp
     def ExportDAT(self, f, meshPart=None):
         if meshPart:
+            unRegister = genObjUnRegister()
             if isinstance( meshPart, list ):
                 meshPart = self.GetIDSource( meshPart, SMESH.ALL )
+                unRegister.set( meshPart )
             self.mesh.ExportPartToDAT( meshPart, f )
         else:
             self.mesh.ExportDAT(f)
@@ -1610,8 +1669,10 @@ class Mesh:
     #  @ingroup l2_impexp
     def ExportUNV(self, f, meshPart=None):
         if meshPart:
+            unRegister = genObjUnRegister()
             if isinstance( meshPart, list ):
                 meshPart = self.GetIDSource( meshPart, SMESH.ALL )
+                unRegister.set( meshPart )
             self.mesh.ExportPartToUNV( meshPart, f )
         else:
             self.mesh.ExportUNV(f)
@@ -1623,8 +1684,10 @@ class Mesh:
     #  @ingroup l2_impexp
     def ExportSTL(self, f, ascii=1, meshPart=None):
         if meshPart:
+            unRegister = genObjUnRegister()
             if isinstance( meshPart, list ):
                 meshPart = self.GetIDSource( meshPart, SMESH.ALL )
+                unRegister.set( meshPart )
             self.mesh.ExportPartToSTL( meshPart, f, ascii )
         else:
             self.mesh.ExportSTL(f, ascii)
@@ -1635,21 +1698,27 @@ class Mesh:
     #  @param meshPart a part of mesh (group, sub-mesh) to export instead of the mesh
     #  @ingroup l2_impexp
     def ExportCGNS(self, f, overwrite=1, meshPart=None):
+        unRegister = genObjUnRegister()
         if isinstance( meshPart, list ):
             meshPart = self.GetIDSource( meshPart, SMESH.ALL )
+            unRegister.set( meshPart )
         if isinstance( meshPart, Mesh ):
             meshPart = meshPart.mesh
         elif not meshPart:
             meshPart = self.mesh
         self.mesh.ExportCGNS(meshPart, f, overwrite)
 
-    ## Exports the mesh in a file in GMF format
+    ## Exports the mesh in a file in GMF format.
+    #  GMF files must have .mesh extension for the ASCII format and .meshb for
+    #  the bynary format. Other extensions are not allowed.
     #  @param f is the file name
     #  @param meshPart a part of mesh (group, sub-mesh) to export instead of the mesh
     #  @ingroup l2_impexp
     def ExportGMF(self, f, meshPart=None):
+        unRegister = genObjUnRegister()
         if isinstance( meshPart, list ):
             meshPart = self.GetIDSource( meshPart, SMESH.ALL )
+            unRegister.set( meshPart )
         if isinstance( meshPart, Mesh ):
             meshPart = meshPart.mesh
         elif not meshPart:
@@ -1664,9 +1733,15 @@ class Mesh:
     #  @param opt boolean parameter for creating/not creating
     #         the groups Group_On_All_Nodes, Group_On_All_Faces, ...
     #  @param overwrite boolean parameter for overwriting/not overwriting the file
+    #  @param autoDimension: if @c True (default), a space dimension of a MED mesh can be either
+    #         - 1D if all mesh nodes lie on OX coordinate axis, or
+    #         - 2D if all mesh nodes lie on XOY coordinate plane, or
+    #         - 3D in the rest cases.
+    #
+    #         If @a autoDimension is @c False, the space dimension is always 3.
     #  @ingroup l2_impexp
-    def ExportToMED(self, f, version, opt=0, overwrite=1):
-        self.mesh.ExportToMEDX(f, opt, version, overwrite)
+    def ExportToMED(self, f, version, opt=0, overwrite=1, autoDimension=True):
+        self.mesh.ExportToMEDX(f, opt, version, overwrite, autoDimension)
 
     # Operations with groups:
     # ----------------------
@@ -1972,12 +2047,6 @@ class Mesh:
     def GetIDSource(self, ids, elemType):
         return self.editor.MakeIDSource(ids, elemType)
 
-    ## Gets MED Mesh
-    #  @return an instance of SALOME_MED::MESH
-    #  @ingroup l1_auxiliary
-    def GetMEDMesh(self):
-        return self.mesh.GetMEDMesh()
-
 
     # Get informations about mesh contents:
     # ------------------------------------
@@ -2434,7 +2503,7 @@ class Mesh:
 
         aMeasurements = self.smeshpyD.CreateMeasurements()
         aMeasure = aMeasurements.MinDistance(id1, id2)
-        aMeasurements.UnRegister()
+        genObjUnRegister([aMeasurements,id1, id2])
         return aMeasure
 
     ## Get bounding box of the specified object(s)
@@ -2467,6 +2536,7 @@ class Mesh:
         if len(IDs) > 0 and isinstance(IDs[0], int):
             IDs = [IDs]
         srclist = []
+        unRegister = genObjUnRegister()
         for o in IDs:
             if isinstance(o, Mesh):
                 srclist.append(o.mesh)
@@ -2479,11 +2549,12 @@ class Mesh:
                     srclist.append(self.editor.MakeIDSource(o, SMESH.FACE))
                 else:
                     srclist.append(self.editor.MakeIDSource(o, SMESH.NODE))
+                unRegister.set( srclist[-1] )
                 pass
             pass
         aMeasurements = self.smeshpyD.CreateMeasurements()
+        unRegister.set( aMeasurements )
         aMeasure = aMeasurements.BoundingBox(srclist)
-        aMeasurements.UnRegister()
         return aMeasure
 
     # Mesh edition (SMESH_MeshEditor functionality):
@@ -2536,10 +2607,12 @@ class Mesh:
     #          can be retrieved from the returned object by calling GetIDs()
     #  @ingroup l2_modif_add
     def Add0DElementsToAllNodes(self, theObject, theGroupName=""):
+        unRegister = genObjUnRegister()
         if isinstance( theObject, Mesh ):
             theObject = theObject.GetMesh()
         if isinstance( theObject, list ):
             theObject = self.GetIDSource( theObject, SMESH.ALL )
+            unRegister.set( theObject )
         return self.editor.Create0DElementsOnAllNodes( theObject, theGroupName )
 
     ## Creates a ball element on a node with given ID.
@@ -2809,11 +2882,13 @@ class Mesh:
     #  @return number of reoriented faces
     #  @ingroup l2_modif_changori
     def Reorient2D(self, the2DObject, theDirection, theFaceOrPoint ):
+        unRegister = genObjUnRegister()
         # check the2DObject
         if isinstance( the2DObject, Mesh ):
             the2DObject = the2DObject.GetMesh()
         if isinstance( the2DObject, list ):
             the2DObject = self.GetIDSource( the2DObject, SMESH.FACE )
+            unRegister.set( the2DObject )
         # check theDirection
         if isinstance( theDirection, geomBuilder.GEOM._objref_GEOM_Object):
             theDirection = self.smeshpyD.GetDirStruct( theDirection )
@@ -2903,12 +2978,14 @@ class Mesh:
     #         group or a list of face IDs. By default all quadrangles are split
     #  @ingroup l2_modif_cutquadr
     def QuadTo4Tri (self, theElements=[]):
+        unRegister = genObjUnRegister()
         if isinstance( theElements, Mesh ):
             theElements = theElements.mesh
         elif not theElements:
             theElements = self.mesh
         elif isinstance( theElements, list ):
             theElements = self.GetIDSource( theElements, SMESH.FACE )
+            unRegister.set( theElements )
         return self.editor.QuadTo4Tri( theElements )
 
     ## Splits quadrangles into triangles.
@@ -2948,10 +3025,12 @@ class Mesh:
     #         Hex_5Tet - split the hexahedron into 5 tetrahedrons, etc
     #  @ingroup l2_modif_cutquadr
     def SplitVolumesIntoTetra(self, elemIDs, method=smeshBuilder.Hex_5Tet ):
+        unRegister = genObjUnRegister()
         if isinstance( elemIDs, Mesh ):
             elemIDs = elemIDs.GetMesh()
         if ( isinstance( elemIDs, list )):
             elemIDs = self.editor.MakeIDSource(elemIDs, SMESH.VOLUME)
+            unRegister.set( elemIDs )
         self.editor.SplitVolumesIntoTetra(elemIDs, method)
 
     ## Splits quadrangle faces near triangular facets of volumes
@@ -3218,16 +3297,18 @@ class Mesh:
     #     the new mesh else only boundary elements will be copied into the new mesh
     #  @param toCopyExistingBondary - if true, not only new but also pre-existing
     #     boundary elements will be copied into the new mesh
-    #  @return tuple (mesh, group) where bondary elements were added to
+    #  @return tuple (mesh, group) where boundary elements were added to
     #  @ingroup l2_modif_edit
     def MakeBoundaryMesh(self, elements, dimension=SMESH.BND_2DFROM3D, groupName="", meshName="",
                          toCopyElements=False, toCopyExistingBondary=False):
+        unRegister = genObjUnRegister()
         if isinstance( elements, Mesh ):
             elements = elements.GetMesh()
         if ( isinstance( elements, list )):
             elemType = SMESH.ALL
             if elements: elemType = self.GetElementType( elements[0], iselem=True)
             elements = self.editor.MakeIDSource(elements, elemType)
+            unRegister.set( elements )
         mesh, group = self.editor.MakeBoundaryMesh(elements,dimension,groupName,meshName,
                                                    toCopyElements,toCopyExistingBondary)
         if mesh: mesh = self.smeshpyD.Mesh(mesh)
@@ -3536,8 +3617,10 @@ class Mesh:
     #  @param LinearVariation forces the computation of rotation angles as linear
     #                         variation of the given Angles along path steps
     #  @param HasRefPoint allows using the reference point
-    #  @param RefPoint the point around which the shape is rotated (the mass center of the shape by default).
+    #  @param RefPoint the point around which the elements are rotated (the mass
+    #         center of the elements by default).
     #         The User can specify any point as the Reference Point.
+    #         RefPoint can be either GEOM Vertex, [x,y,z] or SMESH.PointStruct
     #  @param MakeGroups forces the generation of new groups from existing ones
     #  @param ElemType type of elements for extrusion (if param Base is a mesh)
     #  @return list of created groups (SMESH_GroupBase) and SMESH::Extrusion_Error if MakeGroups=True,
@@ -3546,9 +3629,12 @@ class Mesh:
     def ExtrusionAlongPathX(self, Base, Path, NodeStart,
                             HasAngles, Angles, LinearVariation,
                             HasRefPoint, RefPoint, MakeGroups, ElemType):
-        if ( isinstance( RefPoint, geomBuilder.GEOM._objref_GEOM_Object)):
+        if isinstance( RefPoint, geomBuilder.GEOM._objref_GEOM_Object):
             RefPoint = self.smeshpyD.GetPointStruct(RefPoint)
             pass
+        elif isinstance( RefPoint, list ):
+            RefPoint = PointStruct(*RefPoint)
+            pass
         Angles,AnglesParameters,hasVars = ParseAngles(Angles)
         Parameters = AnglesParameters + var_separator + RefPoint.parameters
         self.mesh.SetParameters(Parameters)
@@ -3905,10 +3991,12 @@ class Mesh:
     #  @return list of created groups (SMESH_GroupBase) if MakeGroups=True,
     #          empty list otherwise
     def Scale(self, theObject, thePoint, theScaleFact, Copy, MakeGroups=False):
+        unRegister = genObjUnRegister()
         if ( isinstance( theObject, Mesh )):
             theObject = theObject.GetMesh()
         if ( isinstance( theObject, list )):
             theObject = self.GetIDSource(theObject, SMESH.ALL)
+            unRegister.set( theObject )
         if ( isinstance( theScaleFact, float )):
              theScaleFact = [theScaleFact]
         if ( isinstance( theScaleFact, int )):
@@ -3929,10 +4017,12 @@ class Mesh:
     #  @param NewMeshName - the name of the newly created mesh
     #  @return instance of Mesh class
     def ScaleMakeMesh(self, theObject, thePoint, theScaleFact, MakeGroups=False, NewMeshName=""):
+        unRegister = genObjUnRegister()
         if (isinstance(theObject, Mesh)):
             theObject = theObject.GetMesh()
         if ( isinstance( theObject, list )):
             theObject = self.GetIDSource(theObject,SMESH.ALL)
+            unRegister.set( theObject )
         if ( isinstance( theScaleFact, float )):
              theScaleFact = [theScaleFact]
         if ( isinstance( theScaleFact, int )):
@@ -4041,12 +4131,14 @@ class Mesh:
     #  @return the list of groups of nodes
     #  @ingroup l2_modif_trsf
     def FindCoincidentNodesOnPart (self, SubMeshOrGroup, Tolerance, exceptNodes=[]):
+        unRegister = genObjUnRegister()
         if (isinstance( SubMeshOrGroup, Mesh )):
             SubMeshOrGroup = SubMeshOrGroup.GetMesh()
         if not isinstance( exceptNodes, list):
             exceptNodes = [ exceptNodes ]
         if exceptNodes and isinstance( exceptNodes[0], int):
             exceptNodes = [ self.GetIDSource( exceptNodes, SMESH.NODE)]
+            unRegister.set( exceptNodes )
         return self.editor.FindCoincidentNodesOnPartBut(SubMeshOrGroup, Tolerance,exceptNodes)
 
     ## Merges nodes
@@ -4157,10 +4249,12 @@ class Mesh:
     # @return a group where the new elements are added. None if theGroupName == "".
     #  @ingroup l2_modif_edit
     def DoubleElements(self, theElements, theGroupName=""):
+        unRegister = genObjUnRegister()
         if isinstance( theElements, Mesh ):
             theElements = theElements.mesh
         elif isinstance( theElements, list ):
             theElements = self.GetIDSource( theElements, SMESH.ALL )
+            unRegister.set( theElements )
         return self.editor.DoubleElements(theElements, theGroupName)
 
     ## Creates a hole in a mesh by doubling the nodes of some particular elements
@@ -4353,26 +4447,41 @@ class Mesh:
             val = 0
         return val
 
-    ## Get length of 1D element.
-    #  @param elemId mesh element ID
-    #  @return element's length value
+    ## Get length of 1D element or sum of lengths of all 1D mesh elements
+    #  @param elemId mesh element ID (if not defined - sum of length of all 1D elements will be calculated)
+    #  @return element's length value if \a elemId is specified or sum of all 1D mesh elements' lengths otherwise
     #  @ingroup l1_measurements
-    def GetLength(self, elemId):
-        return self._valueFromFunctor(SMESH.FT_Length, elemId)
+    def GetLength(self, elemId=None):
+        length = 0
+        if elemId == None:
+            length = self.smeshpyD.GetLength(self)
+        else:
+            length = self._valueFromFunctor(SMESH.FT_Length, elemId)
+        return length
 
-    ## Get area of 2D element.
-    #  @param elemId mesh element ID
-    #  @return element's area value
+    ## Get area of 2D element or sum of areas of all 2D mesh elements
+    #  @param elemId mesh element ID (if not defined - sum of areas of all 2D elements will be calculated)
+    #  @return element's area value if \a elemId is specified or sum of all 2D mesh elements' areas otherwise
     #  @ingroup l1_measurements
-    def GetArea(self, elemId):
-        return self._valueFromFunctor(SMESH.FT_Area, elemId)
+    def GetArea(self, elemId=None):
+        area = 0
+        if elemId == None:
+            area = self.smeshpyD.GetArea(self)
+        else:
+            area = self._valueFromFunctor(SMESH.FT_Area, elemId)
+        return area
 
-    ## Get volume of 3D element.
-    #  @param elemId mesh element ID
-    #  @return element's volume value
+    ## Get volume of 3D element or sum of volumes of all 3D mesh elements
+    #  @param elemId mesh element ID (if not defined - sum of volumes of all 3D elements will be calculated)
+    #  @return element's volume value if \a elemId is specified or sum of all 3D mesh elements' volumes otherwise
     #  @ingroup l1_measurements
-    def GetVolume(self, elemId):
-        return self._valueFromFunctor(SMESH.FT_Volume3D, elemId)
+    def GetVolume(self, elemId=None):
+        volume = 0
+        if elemId == None:
+            volume = self.smeshpyD.GetVolume(self)
+        else:
+            volume = self._valueFromFunctor(SMESH.FT_Volume3D, elemId)
+        return volume
 
     ## Get maximum element length.
     #  @param elemId mesh element ID
@@ -4516,6 +4625,28 @@ class hypMethodWrapper:
                 raise ValueError, detail # wrong variable name
 
         return result
+    pass
+
+# A helper class that call UnRegister() of SALOME.GenericObj'es stored in it
+class genObjUnRegister:
+
+    def __init__(self, genObj=None):
+        self.genObjList = []
+        self.set( genObj )
+        return
+
+    def set(self, genObj):
+        "Store one or a list of of SALOME.GenericObj'es"
+        if isinstance( genObj, list ):
+            self.genObjList.extend( genObj )
+        else:
+            self.genObjList.append( genObj )
+        return
+
+    def __del__(self):
+        for genObj in self.genObjList:
+            if genObj and hasattr( genObj, "UnRegister" ):
+                genObj.UnRegister()
 
 for pluginName in os.environ[ "SMESH_MeshersList" ].split( ":" ):
     #