Salome HOME
Initial implementation of Fields support
[modules/shaper_study.git] / src / PY / SHAPERSTUDY_Object.py
index 4a8f4000c9b81d25082e2e1f9018b75bc2a8b3ba..1b69e8c1ab2b466bcd0904e086f13733503e7a7f 100644 (file)
 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 #
 
+import SHAPERSTUDY_ORB
 import SHAPERSTUDY_ORB__POA
 import GEOM
-from SHAPERSTUDY_utils import getEngine
+from SHAPERSTUDY_utils import getEngine, getStudy
+import salome
 
-class SHAPERSTUDY_BaseObject(SHAPERSTUDY_ORB__POA.BaseObject):
+import StudyData_Swig
+
+# converter from the integer values to idl shape_type enumerations
+__shape_types__ = {
+  0:GEOM.COMPOUND, 1:GEOM.COMPSOLID, 2:GEOM.SOLID,
+  3:GEOM.SHELL, 4:GEOM.FACE, 5:GEOM.WIRE,
+  6:GEOM.EDGE, 7:GEOM.VERTEX, 8:GEOM.SHAPE, 9:GEOM.FLAT}
+
+
+class SHAPERSTUDY_Object(SHAPERSTUDY_ORB__POA.SHAPER_Object):
     """
-    Construct an instance of SHAPER_BaseObject.
+    Construct an instance of SHAPERSTUDY Object.
     """
     def __init__ ( self, *args):
+        self.SO = None
+        self.data = None
+        self.entry = None
+        self.type = 1 # by default it is a shape (Import feature in GEOMImpl_Types.hxx)
         pass
 
+    def GetShapeType( self ):
+        """
+        Get a GEOM.shape_type of the object value.
+        """
+        if self.data is None:
+            return GEOM.SHAPE
+        global __shape_types__
+        return __shape_types__[self.data.type()];
+
+    def IsMainShape( self ):
+        """
+        Returns True if this object is not a sub-shape of another object.
+        """
+        return True
+
+    def GetSubShapeIndices( self ):
+        """
+        Get a list of ID's of sub-shapes in the main shape.
+        """
+        return []
+
+    def GetMainShape( self ):
+        """
+        Get a main shape object to which this object is a sub-shape.
+        """
+        return getShape()
+
+    def getShape( self ):
+        """
+        Get the TopoDS_Shape, for collocated case only.
+        Called by GEOM_Client to get TopoDS_Shape pointer
+        """
+        if self.data is None:
+            return 0
+        return self.data.shape()
+
+    def GetShapeStream( self ):
+        """
+        Get geometric shape of the object as a byte stream in BRep format
+        """
+        if self.data is None:
+            return b''
+        return self.data.shapeStream().encode()
+
+    def GetOldShapeStream( self ):
+        """
+        Get geometric shape of the object as a byte stream in BRep format
+        """
+        if self.data is None:
+            return b''
+        return self.data.oldShapeStream().encode()
+
+    def SetShapeByStream(self, theStream):
+        """
+        Sets geometric shape content of the object as a byte stream in BRep format
+        """
+        if self.data:
+          self.data.updateShape(theStream)
+        else:
+          self.data = StudyData_Swig.StudyData_Object(theStream)
+
+    """
+    Methods from BaseObject
+    """
     def GetName( self ):
         """
         Get name of the object associated with this object.
         """
-        return ""
+        return self.SO.GetName()
+
+    def SetEntry( self, theInternalEntry ):
+        """
+        Sets internal (unique) entry of the object in the component's data tree.
+        """
+        self.entry = theInternalEntry
 
     def GetEntry( self ):
         """
         Get internal (unique) entry of the object in the component's data tree.
         """
-        return '0:0:0:1'
+        return self.entry
 
     def GetType( self ):
         """
         Get internal type of operation created this object.
         In SMESH is used to find out if an object is GROUP (type == 37)
         """
-        return 1
+        return self.type
+
+    def SetType( self, theType ):
+        """
+        Sets internal type of operation created this object.
+        In SMESH is used to find out if an object is GROUP (type == 37, for shape it is default=1)
+        """
+        self.type = theType
 
     def GetTick( self ):
         """
         Get value of a modification counter of the object
         """
-        return 1
+        if self.data:
+          return self.data.getTick()
+        return 0
 
     def GetStudyEntry( self ):
         """
         Get a Study entry where this object was published.
         """
-        return '0:0:0:1'
+        return self.SO.GetID()
 
     def IsShape( self ):
         """
-        Return true if geom object representes a shape.
+        Return true if geom object represents a shape.
         For example, method return false for GEOM_MARKER
         """
-        return False
+        return True
 
     def IsSame( self, other ):
         """
         Return true if passed object is identical to this object
         """
-        return False
+        return self.GetType() == other.GetType() and self.GetEntry() == other.GetEntry()
 
     def GetGen( self ):
         """
@@ -81,58 +175,212 @@ class SHAPERSTUDY_BaseObject(SHAPERSTUDY_ORB__POA.BaseObject):
         """
         return getEngine()
 
-    pass
+    def SetSO( self, theSO ):
+        """
+        Sets SObject of this object (when it is published)
+        """
+        self.SO = theSO
+        
+    def GetSO( self ):
+        """
+        Returns SObject of this object
+        """
+        return self.SO
 
+    def IsParametrical(self):
+        """
+        Returns true if the current object has connection to a parametrical model
+        which can be modified by parameters change.
+        """
+        return not self.IsDead() and self.type == 1 # only break link for shapes are accessible now
 
+    def IsDead(self):
+        """
+        Returns true if the shape is dead - no parametrical link to the SHAPER exists
+        """
+        return self.GetEntry().startswith("dead")
 
-class SHAPERSTUDY_Object(SHAPERSTUDY_ORB__POA.SHAPER_Object):
+    def MakeDead(self):
+        """
+        Makes the dead-copy of the shape and returns it.
+        """
+        aStudy = getStudy()
+        aBuilder = aStudy.NewBuilder()
+        aRes, aHistSO = self.SO.FindSubObject(2)
+        if not aRes: # create a "history" folder if it does not exist
+          aHistSO = aBuilder.NewObjectToTag(self.SO, 2)
+          aHistSO.SetAttrString("AttributeName", "History")
+
+        aDeadSO = aBuilder.NewObject(aHistSO)
+        anIndex = aDeadSO.Tag()
+        aDeadSO.SetAttrString("AttributeName", self.SO.GetName() + " (" + str(anIndex) + ")")
+        aDead = SHAPERSTUDY_Object()
+        aDeadEntry = "dead" + str(anIndex) + "_" + self.GetEntry()
+        aDead.SetEntry(aDeadEntry)
+        aDead.SetShapeByStream(self.data.oldShapeStream())
+        aDeadObj = aDead._this()
+        anIOR = salome.orb.object_to_string(aDeadObj)
+        aDeadSO.SetAttrString("AttributeIOR", anIOR)
+        aDead.SetSO(aDeadSO)
+        if self.GetTick() > 2:
+          aDead.data.setTick(self.GetTick() - 1) # set the tick of an old shape
+        # make dead-copy also sub-groups
+        aSOIter = aStudy.NewChildIterator(self.SO)
+        while aSOIter.More():
+          aGroupSO = aSOIter.Value()
+          anIOR = aGroupSO.GetIOR()
+          if len(anIOR):
+            aGroup = salome.orb.string_to_object(anIOR)
+            if isinstance(aGroup, SHAPERSTUDY_ORB._objref_SHAPER_Group):
+              aDeadGroup = SHAPERSTUDY_Group()
+              aDeadGroupEntry = "dead" + str(anIndex) + "_" + aGroup.GetEntry()
+              aDeadGroup.SetEntry(aDeadGroupEntry)
+              aDeadGroup.SetShapeByPointer(aGroup.getShape())
+              aDeadGroup.SetSelectionType(aGroup.GetSelectionType())
+              aDeadGroup.SetSelection(aGroup.GetSelection())
+              aDeadGroupSO = aBuilder.NewObject(aDeadSO)
+              aDeadGroup.SetSO(aDeadGroupSO)
+              aDeadGroupSO.SetAttrString("AttributeName", aGroupSO.GetName() + " (" + str(anIndex) + ")")
+              aDeadGroupObj = aDeadGroup._this()
+              anIOR = salome.orb.object_to_string(aDeadGroupObj)
+              aDeadGroupSO.SetAttrString("AttributeIOR", anIOR)
+          aSOIter.Next()
+
+        return aDeadObj
+    
+    def SetShapeByPointer(self, theShape):
+        """
+        Sets the shape by the pointer to the TopoDS_Shape
+        """
+        if not self.data:
+          self.data = StudyData_Swig.StudyData_Object()
+        self.data.SetShapeByPointer(theShape)
+
+    pass
+
+class SHAPERSTUDY_Group(SHAPERSTUDY_ORB__POA.SHAPER_Group, SHAPERSTUDY_Object):
     """
-    Construct an instance of SHAPERSTUDY Object.
+    Construct an instance of SHAPERSTUDY Group
     """
     def __init__ ( self, *args):
+        self.seltype = None
+        self.selection = []
+        self.SO = None
+        self.data = None
+        self.entry = None
+        self.type = 37 # a group type
         pass
 
-    def GetShapeType( self ):
+    def SetSelectionType(self, theType):
         """
-        Get a GEOM.shape_type of the object value.
+        Sets what is returned in the GEOM_IGroupOperations::GetType
         """
-        return GEOM.SHAPE
+        self.seltype = theType
 
-    def IsMainShape( self ):
+    def GetSelectionType(self):
         """
-        Returns True if this object is not a sub-shape of another object.
+        Returns the type of the selected sub-shapes
         """
-        return False
+        return self.seltype
 
-    def GetSubShapeIndices( self ):
+    def SetSelection(self, theSelection):
         """
-        Get a list of ID's of sub-shapes in the main shape.
+        Sets what is returned in the GEOM_IGroupOperations::GetObjects
         """
-        return [1]
+        self.data = None # nullify the cashed shape when selection is changed
+        self.selection = theSelection
 
-    def GetMainShape( self ):
+    def GetSelection(self):
         """
-        Get a main shape object to which this object is a sub-shape.
+        Returns the selected sub-shapes indices
         """
-        return
+        return self.selection
 
-    def getShape( self ):
+    def GetMainShape( self ):
         """
-        Get the TopoDS_Shape, for colocated case only.
-        Called by GEOM_Client to get TopoDS_Shape pointer
+        Main shape is groups owner
         """
-        return 0
+        return self.SO.GetFather().GetObject()
 
-    def GetShapeStream( self ):
+    def getShape( self ):
         """
-        Get geometric shape of the object as a byte stream in BRep format
+        Redefinition of the getShape method: here it creates a shape by the
+        main shape and the group index.
         """
-        return ;
+        if not self.data:
+          self.data = StudyData_Swig.StudyData_Object()
+        # convert selection to long list
+        anArg = StudyData_Swig.LongList()
+        for l in self.selection:
+          anArg.append(l)
+        return self.data.groupShape(self.GetMainShape().getShape(), anArg)
 
-    def SetShapeByStream(self, theStream):
-        """
-        Sets geometric shape content of the object as a byte stream in BRep format
-        """
-        return ;
+    pass
+
+class SHAPERSTUDY_Field(SHAPERSTUDY_ORB__POA.SHAPER_Field, SHAPERSTUDY_Group):
+    """
+    Construct an instance of SHAPERSTUDY Field (inherits selection from a Group object)
+    """
+    def __init__ ( self, *args):
+        self.seltype = None
+        self.selection = []
+        self.SO = None
+        self.data = None
+        self.entry = None
+        self.type = 52 # a field type
+        self.valtype = None # type of the values
+        self.steps = [] # list of long
+        self.components = [] # string array, names of the components
+        self.name = None # name, string
+        pass
+
+    def SetValuesType( self, theType ):
+      """
+      Sets the type of values in the field
+      """
+      self.valtype = theType
+
+    def GetDataType( self ):
+      """
+      Returns the type of values in the field in terms of GEOM enumeration
+      """
+      if self.valtype == 0:
+        return GEOM.FDT_Bool
+      elif self.valtype == 1:
+        return GEOM.FDT_Int
+      elif self.valtype == 2:
+        return GEOM.FDT_Double
+      elif self.valtype == 3:
+        return GEOM.FDT_String
+      return None # unknown case
+
+    def SetSteps( self, theSteps ):
+      self.steps = theSteps
+
+    def GetSteps( self ):
+      return self.steps
+
+    def SetComponents( self, theComponents ):
+      self.components = theComponents
+    
+    def GetComponents( self ):
+      return self.components
+
+    def GetDimension( self ):
+      aShapeType = SHAPERSTUDY_Group.GetSelectionType()
+      if aShapeType == 8:
+        return -1 # whole part
+      elif aShapeType == 7:
+        return 0 # vertex
+      elif aShapeType == 6:
+        return 1 # edge
+      elif aShapeType == 4:
+        return 2 # face
+      elif aShapeType == 2:
+        return 3 # solid
+      return None # unknown case
+
+    def GetShape( self ):
+      return SHAPERSTUDY_Group.getShape()
 
     pass