Salome HOME
Increment version: 7.6.0
[samples/pyhello.git] / src / PYHELLO / PYHELLO.py
index 07d16ca8f53fd50510c02eee9b8eddf31f92a76f..7bfac5498fbcc52864092703c0c957895801e76e 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
+# Copyright (C) 2007-2015  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
@@ -6,7 +6,7 @@
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
 # License as published by the Free Software Foundation; either
-# version 2.1 of the License.
+# version 2.1 of the License, or (at your option) any later version.
 #
 # This library is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -28,6 +28,8 @@
 import PYHELLO_ORB__POA
 import SALOME_ComponentPy
 import SALOME_DriverPy
+import SALOMEDS
+from SALOME_DataContainerPy import *
 
 from PYHELLO_utils import *
 
@@ -51,6 +53,13 @@ class PYHELLO(PYHELLO_ORB__POA.PYHELLO_Gen,
         #
         pass
 
+    """
+    Get version information.
+    """
+    def getVersion( self ):
+        import salome_version
+        return salome_version.getVersion("PYHELLO", True)
+
     """
     Generate hello banner.
     """
@@ -58,10 +67,19 @@ class PYHELLO(PYHELLO_ORB__POA.PYHELLO_Gen,
         banner = "Hello %s!" % name
         return banner
 
+    """
+    Intentionnally raises an exception for test purposes.
+    """
+    def raiseAnException( self ):
+        import SALOME
+        exData = SALOME.ExceptionStruct( SALOME.BAD_PARAM, "Test exception in raiseAnException()",'',0)
+        raise SALOME.SALOME_Exception( exData )
+      
     """
     Create object.
     """
     def createObject( self, study, name ):
+        self._createdNew = True # used for getModifiedData method
         builder = study.NewBuilder()
         father  = findOrCreateComponent( study )
         object  = builder.NewObject( father )
@@ -74,9 +92,8 @@ class PYHELLO(PYHELLO_ORB__POA.PYHELLO_Gen,
     """
     Dump module data to the Python script.
     """
-    def DumpPython( self, study, isPublished ):
+    def DumpPython( self, study, isPublished, isMultiFile ):
         abuffer = []
-        abuffer.append( "def RebuildData( theStudy ):" )
         names = []
         father = study.FindComponent( moduleName() )
         if father:
@@ -88,14 +105,54 @@ class PYHELLO(PYHELLO_ORB__POA.PYHELLO_Gen,
                 pass
             pass
         if names:
-            abuffer += [ "  from batchmode_salome import lcc" ]
-            abuffer += [ "  import PYHELLO_ORB" ]
-            abuffer += [ "  " ]
-            abuffer += [ "  pyhello = lcc.FindOrLoadComponent( 'FactoryServerPy', '%s' )" % moduleName() ]
-            abuffer += [ "  " ]
-            abuffer += [ "  pyhello.createObject( theStudy, '%s' )" % name for name in names ]
+            abuffer += [ "from salome import lcc" ]
+            abuffer += [ "import PYHELLO_ORB" ]
+            abuffer += [ "" ]
+            abuffer += [ "pyhello = lcc.FindOrLoadComponent( 'FactoryServerPy', '%s' )" % moduleName() ]
+            abuffer += [ "" ]
+            abuffer += [ "pyhello.createObject( theStudy, '%s' )" % name for name in names ]
+            abuffer += [ "" ]
             pass
-        abuffer += [ "  " ]
-        abuffer.append( "  pass" )
-        abuffer.append( "\0" )
+        if isMultiFile:
+            abuffer       = [ "  " + s for s in abuffer ]
+            abuffer[0:0]  = [ "def RebuildData( theStudy ):" ]
+            abuffer      += [ "  pass" ]
+        abuffer += [ "\0" ]
         return ("\n".join( abuffer ), 1)
+
+    """
+    Import file to restore module data
+    """
+    def importData(self, studyId, dataContainer, options):
+      # get study by Id
+      obj = self._naming_service.Resolve("myStudyManager")
+      myStudyManager = obj._narrow(SALOMEDS.StudyManager)
+      study = myStudyManager.GetStudyByID(studyId)
+      # create all objects from the imported stream
+      stream = dataContainer.get()
+      for objname in stream.split("\n"):
+        if len(objname) != 0:
+          self.createObject(study, objname)
+      self._createdNew = False # to store the modification of the study information later
+      return ["objects"] # identifier what is in this file
+
+    def getModifiedData(self, studyId):
+      if self._createdNew:
+        # get study by Id
+        obj = self._naming_service.Resolve("myStudyManager")
+        myStudyManager = obj._narrow(SALOMEDS.StudyManager)
+        study = myStudyManager.GetStudyByID(studyId)
+        # iterate all objects to get their names and store this information in stream
+        stream=""
+        father = study.FindComponent( moduleName() )
+        if father:
+            iter = study.NewChildIterator( father )
+            while iter.More():
+                name = iter.Value().GetName()
+                stream += name + "\n"
+                iter.Next()
+        # store stream to the temporary file to send it in DataContainer
+        dataContainer = SALOME_DataContainerPy_i(stream, "", "objects", False, True)
+        aVar = dataContainer._this()
+        return [aVar]
+      return []