Salome HOME
Copyright update 2020
[modules/shaper_study.git] / src / PY / SHAPERSTUDY_IOperations.py
old mode 100644 (file)
new mode 100755 (executable)
index 079c9f5..b9edf64
@@ -1,7 +1,4 @@
-# Copyright (C) 2007-2019  CEA/DEN, EDF R&D, OPEN CASCADE
-#
-# Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-# CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+# Copyright (C) 2019-2020  CEA/DEN, EDF R&D
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -24,6 +21,7 @@ import SHAPERSTUDY_ORB__POA
 import SHAPERSTUDY_ORB
 import SHAPERSTUDY_Object
 import GEOM
+import salome
 from SHAPERSTUDY_utils import getStudy
 
 import StudyData_Swig
@@ -34,8 +32,10 @@ class SHAPERSTUDY_IShapesOperations(SHAPERSTUDY_ORB__POA.IShapesOperations,
     Construct an instance of SHAPERSTUDY IShapesOperations.
     """
     def __init__ ( self, *args):
+        SHAPERSTUDY_Object.SHAPERSTUDY_GenericObject.__init__(self)
         self.done = False
         self.myop = StudyData_Swig.StudyData_Operation()
+        self.errorcode = ""
         pass
 
     def GetAllSubShapesIDs( self, theShape, theShapeType, isSorted ):
@@ -49,7 +49,7 @@ class SHAPERSTUDY_IShapesOperations(SHAPERSTUDY_ORB__POA.IShapesOperations,
             sorted by coordinates of their gravity centers.
         """
         aList = self.myop.GetAllSubShapesIDs(theShape.getShape(), theShapeType, isSorted)
-        self.done = not aList.empty()
+        self.done = True
         aResult = []
         for i in aList:
           aResult.append(i)
@@ -65,7 +65,7 @@ class SHAPERSTUDY_IShapesOperations(SHAPERSTUDY_ORB__POA.IShapesOperations,
             theShapeType Type of sub-shapes to be retrieved.
         """
         aList = self.myop.GetSharedShapes(theShape1.getShape(), theShape2.getShape(), theShapeType)
-        self.done = not aList.empty()
+        self.done = True
         aResult = []
         for i in aList:
           aResult.append(i)
@@ -76,7 +76,7 @@ class SHAPERSTUDY_IShapesOperations(SHAPERSTUDY_ORB__POA.IShapesOperations,
         Get global index of theSubShape in theMainShape.
         """
         anIndex = self.myop.GetSubShapeIndex(theMainShape.getShape(), theSubShape.getShape())
-        self.done = anIndex != 0
+        self.done = True
         return anIndex
 
     def GetSubShape( self, theMainShape, theID ):
@@ -93,12 +93,139 @@ class SHAPERSTUDY_IShapesOperations(SHAPERSTUDY_ORB__POA.IShapesOperations,
         aShapeObj.SetShapeByPointer(aShape)
         return aShapeObj._this()
 
+    def ExtractSubShapes(self, aShape, aType, isSorted):
+        """
+        Extract shapes (excluding the main shape) of given type.
+
+        Parameters:
+            aShape The shape.
+            aType  The shape type (see geompy.ShapeType)
+            isSorted Boolean flag to switch sorting on/off.
+
+        Returns:
+            List of sub-shapes of type aType, contained in aShape.
+        """
+        shapes = self.myop.ExtractSubShapes( aShape.getShape(), aType, isSorted )
+        resultList = []
+        for s in shapes:
+            aShapeObj = SHAPERSTUDY_Object.SHAPERSTUDY_Object()
+            aShapeObj.SetShapeByPointer( s )
+            resultList.append( aShapeObj._this() )
+        self.done = True
+        return resultList
+
+
+    def NumberOfEdges(self, theShape):
+        """
+        Gives quantity of edges in the given shape.
+
+        Parameters:
+            theShape Shape to count edges of.
+
+        Returns:
+            Quantity of edges.
+        """
+        nb = self.myop.NumberOfEdges( theShape.getShape() )
+        self.done = ( nb >= 0 )
+        return nb
+
+    def NumberOfFaces(self, theShape):
+        """
+        Gives quantity of faces in the given shape.
+
+        Parameters:
+            theShape Shape to count faces of.
+
+        Returns:
+            Quantity of faces.
+        """
+        nb = self.myop.NumberOfFaces( theShape.getShape() )
+        self.done = ( nb >= 0 )
+        return nb
+
+    def MakeAllSubShapes(self, aShape, aType):
+        """
+        Explode a shape on sub-shapes of a given type.
+        If the shape itself matches the type, it is also returned.
+
+        Parameters:
+            aShape Shape to be exploded.
+            aType Type of sub-shapes to be retrieved (see geompy.ShapeType)
+
+        Returns:
+            List of sub-shapes of type theShapeType, contained in theShape.
+        """
+        self.done = True
+        return self.myop.MakeAllSubShapes(aShape.getShape(), aType)
+
+    def MakeSubShapes(self, aShape, anIDs):
+        """
+        Get a set of sub-shapes defined by their unique IDs inside theMainShape
+
+        Parameters:
+            aShape Main shape.
+            anIDs List of unique IDs of sub-shapes inside theMainShape.
+
+        Returns:
+            List of GEOM.GEOM_Object, corresponding to found sub-shapes.
+        """
+        self.done = True
+        return self.myop.MakeSubShapes(aShape.getShape(), anIDs)
+
+    def GetExistingSubObjects(self, theShape, theGroupsOnly = False):
+        """
+        Get all sub-shapes and groups of theShape,
+        that were created already by any other methods.
+
+        Parameters:
+            theShape Any shape.
+            theGroupsOnly If this parameter is TRUE, only groups will be
+                             returned, else all found sub-shapes and groups.
+
+        Returns:
+            List of existing sub-objects of theShape.
+        """
+        ListObj = []
+        self.done = False
+        SObj = salome.ObjectToSObject( theShape )
+        if not SObj: return ListObj
+        soIter = salome.myStudy.NewChildIterator( SObj )
+        while soIter.More():
+            soChild = soIter.Value()
+            soIter.Next()
+            obj = soChild.GetObject()
+            if theGroupsOnly:
+                if isinstance( obj, SHAPERSTUDY_ORB._objref_SHAPER_Group):
+                    ListObj.append( obj )
+            elif isinstance( obj, SHAPERSTUDY_ORB._objref_SHAPER_Object ):
+                ListObj.append( obj )
+        self.done = True
+        return ListObj
+
+    def GetTopologyIndex(self, aMainObj, aSubObj):
+        """
+        Return index of a sub-shape
+        """
+        i = self.myop.GetTopologyIndex(aMainObj.getShape(), aSubObj.getShape())
+        self.done = ( i > 0 )
+        return i
+
+    def GetShapeTypeString(self,aSubObj):
+        """
+        Return a shape type as a string
+        """
+        s = "%s" % aSubObj.GetShapeType()
+        t = s[5:]
+        return t
+        
+
     def GetInPlace( self, theShapeWhere, theShapeWhat ):
         """
         Get sub-shape(s) of \a theShapeWhere, which are
         coincident with \a theShapeWhat or could be a part of it.
         """
-        # not done
+        self.done = False
+        self.errorcode = "Not implemented"
         return SHAPERSTUDY_Object()._this()
         
     def GetInPlaceMap( self, theShapeWhere, theShapeWhat ):
@@ -106,7 +233,8 @@ class SHAPERSTUDY_IShapesOperations(SHAPERSTUDY_ORB__POA.IShapesOperations,
         A sort of GetInPlace functionality, returning for each sub-shape ID of
         \a theShapeWhat a list of corresponding sub-shape IDs of \a theShapeWhere.
         """
-        # not done
+        self.done = False
+        self.errorcode = "Not implemented"
         return [[]]
 
     def IsDone( self ):
@@ -117,11 +245,21 @@ class SHAPERSTUDY_IShapesOperations(SHAPERSTUDY_ORB__POA.IShapesOperations,
 
     pass
 
-class SHAPERSTUDY_IGroupOperations(SHAPERSTUDY_ORB__POA.IGroupOperations):
+    def GetErrorCode( self ):
+        """
+        To know a failure reason
+        """
+        return self.errorcode
+
+    pass
+
+class SHAPERSTUDY_IGroupOperations(SHAPERSTUDY_ORB__POA.IGroupOperations,
+                                   SHAPERSTUDY_IShapesOperations):
     """
     Construct an instance of SHAPERSTUDY IShapesOperations.
     """
     def __init__ ( self, *args):
+        SHAPERSTUDY_IShapesOperations.__init__(self)
         self.done = False
         pass
 
@@ -158,27 +296,38 @@ class SHAPERSTUDY_IGroupOperations(SHAPERSTUDY_ORB__POA.IGroupOperations):
     def UnionList( self, theGroup, theSubShapes ):
         """
         Adds to the group all the given shapes. No errors, if some shapes are already included.
-
-        Parameters:
-            theGroup is a GEOM group to which the new sub-shapes are added.
-            theSubShapes is a list of sub-shapes to be added.
         """
-        # Not needed while SHAPER-STUDY has no sub-shapes in the structure, so, 
-        # theSubShapes can not be filled or treated
+        self.done = False
+        indices = theGroup.GetSelection()
+        mainShape = self.GetMainShape( theGroup )
+        if not mainShape:
+            self.errorcode = "No main shape"
+            return
+        groupType = self.GetType( theGroup )
+        from shaperBuilder import EnumToLong
+        for shape in theSubShapes:
+            shapeType = EnumToLong( shape.GetShapeType() )
+            if not groupType == shapeType:
+                self.errorcode = "Group type and shape type mismatch"
+                return
+            i = self.myop.GetSubShapeIndex( mainShape.getShape(), shape.getShape() )
+            if not i in indices:
+                indices.append( i )
+        theGroup.SetSelection( indices )
+        self.done = True
         return
 
-    def IsDone( self ):
-        """
-        To know, if the operation was successfully performed
-        """
-        return self.done
-
     def GetMainShape( self, theGroup ):
         """
         Returns a main shape associated with the group
         """
         aSO = theGroup.GetSO()
+        if not aSO:
+            return None
         aFatherSO = aSO.GetFather()
+        if not aFatherSO:
+            return None
+        anObj = aFatherSO.GetObject()
         if isinstance( anObj, SHAPERSTUDY_ORB._objref_SHAPER_Object ):
             return anObj
         else:
@@ -198,11 +347,13 @@ class SHAPERSTUDY_IGroupOperations(SHAPERSTUDY_ORB__POA.IGroupOperations):
 
     pass
 
-class SHAPERSTUDY_IFieldOperations(SHAPERSTUDY_ORB__POA.IFieldOperations):
+class SHAPERSTUDY_IFieldOperations(SHAPERSTUDY_ORB__POA.IFieldOperations,
+                                   SHAPERSTUDY_IShapesOperations):
     """
     Construct an instance of SHAPERSTUDY IFieldOperations.
     """
     def __init__ ( self, *args):
+        SHAPERSTUDY_IShapesOperations.__init__(self)
         pass
 
     def CreateFieldByType( self, theMainShape, theShapeType):
@@ -251,11 +402,14 @@ class SHAPERSTUDY_IFieldOperations(SHAPERSTUDY_ORB__POA.IFieldOperations):
     pass
 
 
-class SHAPERSTUDY_IMeasureOperations(SHAPERSTUDY_ORB__POA.IMeasureOperations):
+class SHAPERSTUDY_IMeasureOperations(SHAPERSTUDY_ORB__POA.IMeasureOperations,
+                                     SHAPERSTUDY_IShapesOperations):
     """
     Construct an instance of SHAPERSTUDY IMeasureOperations.
     """
     def __init__ ( self, *args):
+        SHAPERSTUDY_IShapesOperations.__init__(self)
+        self.myop = StudyData_Swig.StudyData_Operation()
         pass
 
     def GetVertexByIndex( self, theShape, theIndex, theUseOri ):
@@ -267,6 +421,56 @@ class SHAPERSTUDY_IMeasureOperations(SHAPERSTUDY_ORB__POA.IMeasureOperations):
         theIndex Index to find vertex by this index (starting from zero)
         theUseOri To consider edge/wire orientation or not
         """
-        return [ SHAPERSTUDY_Field() ]
+        v = self.myop.GetVertexByIndex( theShape.getShape(), theIndex, theUseOri )
+        self.done = ( v > 0 )
+        if self.done:
+            aShapeObj = SHAPERSTUDY_Object.SHAPERSTUDY_Object()
+            aShapeObj.SetShapeByPointer( v )
+            return aShapeObj._this()
+        return None
+
+    def GetMinDistance(self, theShape1, theShape2):
+        """
+        Get minimal distance between the given shapes.
+
+        Parameters:
+            theShape1,theShape2 Shapes to find minimal distance between.
+
+        Returns:
+            Value of the minimal distance between the given shapes.
+        """
+        d = self.myop.MinDistance(theShape1.getShape(), theShape2.getShape())
+        self.done = ( d >= 0 )
+        return d, 0,0,0, 0,0,0
+
+    def PointCoordinates(self,Point):
+        """
+        Get point coordinates
+
+        Returns:
+            [x, y, z]
+        """
+        d = self.myop.PointCoordinates(Point.getShape())
+        self.done = len( d )
+        if self.done == 3:
+            return d[0],d[1],d[2]
+        return [0,0,0]
+
+    def GetTolerance(self,theShape):
+        """
+        Get min and max tolerances of sub-shapes of theShape
+
+        Parameters:
+            theShape Shape, to get tolerances of.
+
+        Returns:
+            [FaceMin,FaceMax, EdgeMin,EdgeMax, VertMin,VertMax]
+             FaceMin,FaceMax: Min and max tolerances of the faces.
+             EdgeMin,EdgeMax: Min and max tolerances of the edges.
+             VertMin,VertMax: Min and max tolerances of the vertices.
+        """
+        tol = self.myop.GetTolerance(theShape.getShape())
+        self.done = tol > 0;
+        return tol,tol, tol,tol, tol,tol
 
     pass