Salome HOME
Merge branch 'omu/Launcher9'
[modules/kernel.git] / src / KERNEL_PY / batchmode_salome.py
index c19046ccb9264215f5360223d432879b7486c7c8..59a0bfc8543bb563bc310df5117ae8891112693c 100755 (executable)
@@ -1,12 +1,29 @@
-#  Copyright (C) 2003  CEA/DEN, EDF R&D
-#  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+#  -*- coding: iso-8859-1 -*-
+# Copyright (C) 2007-2016  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
 #
+# 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, 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
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+#
+# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+#
+
 #  File   : batchmode_salome.py
 #  Module : SALOME
 
-import salome_shared_modules
-
 from omniORB import CORBA
 from LifeCycleCORBA import *
 import SALOME
@@ -18,10 +35,10 @@ from SALOME_NamingServicePy import *
 def ImportComponentGUI(ComponentName):
     libName = "lib" + ComponentName + "_Swig"
     command = "from " + libName + " import *"
-    exec ( command )
+    exec (command, globals())
     constructor = ComponentName + "GUI_Swig()"
     command = "gui = " + constructor
-    exec ( command )
+    exec (command, globals())
     return gui
 
     #--------------------------------------------------------------------------
@@ -35,8 +52,8 @@ def SalomeGUIgetAllSelected(self):
     #--------------------------------------------------------------------------
 
 def generateName(prefix = None):
-    import whrandom;
-    int = whrandom.randint(1,1000);
+    import random;
+    int = random.randint(1,1000);
     if prefix is None:
         return "Study" + str(int)
     else :
@@ -48,12 +65,12 @@ def generateName(prefix = None):
     #WITHOUTIHMgetAllSelected = SalomeGUIgetAllSelected
    
     #WITHOUTIHMdef getDesktop(self) :
-#      return SalomePyQt.getDesktop()
-       #WITHOUTIHMreturn None
+#       return SalomePyQt.getDesktop()
+        #WITHOUTIHMreturn None
 
     #WITHOUTIHMdef getSelection(self) :
-#      return SalomePyQt.getSelection()
-       #WITHOUTIHMreturn None
+#       return SalomePyQt.getSelection()
+        #WITHOUTIHMreturn None
 
     #--------------------------------------------------------------------------
 
@@ -88,7 +105,7 @@ def IDToSObject(id):
 
     #--------------------------------------------------------------------------
 
-def PersistentPresentation(theStudy, theSO, theWithID):
+def PersistentPresentation(theSO, theWithID):
     # put the sobject's content (with subchildren) to the string
     aResult = ""
     attrs = theSO.GetAllAttributes()
@@ -137,9 +154,9 @@ def PersistentPresentation(theStudy, theSO, theWithID):
         aResult = "sobject: " + theSO.GetID() + " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
     else:
         aResult = " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
-    anIter = theStudy.NewChildIterator(theSO)
+    anIter = myStudy.NewChildIterator(theSO)
     while anIter.More():
-        aResult += PersistentPresentation(theStudy, anIter.Value(), theWithID)
+        aResult += PersistentPresentation(anIter.Value(), theWithID)
         anIter.Next()
     return aResult
 
@@ -161,28 +178,30 @@ def CheckCopyPaste(theSO, theInfo ,theComponentPaste):
     while aRoot.GetID() != "0:":
         aRoot = aRoot.GetFather()
     aTree = GetTree(aRoot)
+    aStudyPersist = PersistentPresentation(aRoot, 1)
+
     aStudyPersist = PersistentPresentation(myStudy, aRoot, 1)
 
-    if not myStudyManager.CanCopy(theSO):
-        raise RuntimeError, "<CanCopy> for "+theInfo+" returns false"
+    if not myStudy.CanCopy(theSO):
+        raise RuntimeError("<CanCopy> for "+theInfo+" returns false")
     
-    if not myStudyManager.Copy(theSO):
-        raise RuntimeError, "<Copy> for "+theInfo+" returns false"
+    if not myStudy.Copy(theSO):
+        raise RuntimeError("<Copy> for "+theInfo+" returns false")
 
     
-    if not myStudyManager.CanPaste(theSO):
-        raise RuntimeError, "<CanPaste> for "+theInfo+" returns false"
+    if not myStudy.CanPaste(theSO):
+        raise RuntimeError("<CanPaste> for "+theInfo+" returns false")
 
     # check: before paste study is not changed check
-    if aStudyPersist != PersistentPresentation(myStudy, aRoot, 1):
-        raise RuntimeError, "Study is changed before Paste calling for "+theInfo
+    if aStudyPersist != PersistentPresentation(aRoot, 1):
+        raise RuntimeError("Study is changed before Paste calling for "+theInfo)
     
     aSObj = theSO
     if theComponentPaste:
         aSObj = theSO.GetFatherComponent()
         theInfo = theInfo + "(paste for component)"
-    if myStudyManager.Paste(aSObj) == None:
-        raise RuntimeError, "<Paste> for "+theInfo+" returns None object"
+    if myStudy.Paste(aSObj) == None:
+        raise RuntimeError("<Paste> for "+theInfo+" returns None object")
     aNewTree = GetTree(aRoot)
     aLen = len(aTree)
     for a in range(0,aLen):
@@ -192,7 +211,7 @@ def CheckCopyPaste(theSO, theInfo ,theComponentPaste):
     if aLen < len(aNewTree):
         return myStudy.FindObjectID(aNewTree[aLen])
     
-    raise RuntimeError, "After Copy calling the tree is not changed"
+    raise RuntimeError("After Copy calling the tree is not changed")
     
     #--------------------------------------------------------------------------
 def FindFileInDataDir(filename):
@@ -217,54 +236,60 @@ def FindFileInDataDir(filename):
 orb = None
 
 step = 0
-while step < 100 and orb is None:
+sleeping_time = 0.01
+sleeping_time_max = 1.0
+while 1:
     orb = CORBA.ORB_init([''], CORBA.ORB_ID)
+    if orb is not None: break
     step = step + 1
-    time.sleep(4)
-
+    if step > 100: break
+    time.sleep(sleeping_time)
+    sleeping_time = max(sleeping_time_max, 2*sleeping_time)
+    pass
+    
 if orb is None:
-    print "Warning: ORB has not been initialized !!!"
+    print("Warning: ORB has not been initialized !!!")
 
 # create an LifeCycleCORBA instance
 lcc = LifeCycleCORBA(orb)
 
 step = 0
-while step < 100 and lcc is None:
+sleeping_time = 0.01
+sleeping_time_max = 1.0
+while 1:
     lcc = LifeCycleCORBA(orb)
+    if lcc is not None: break
     step = step + 1
-    time.sleep(4)
+    if step > 100: break
+    time.sleep(sleeping_time)
+    sleeping_time = max(sleeping_time_max, 2*sleeping_time)
+    pass
     
 if lcc is None:
-    print "Warning: LifeCycleCORBA object has not been initialized !!!"
+    print("Warning: LifeCycleCORBA object has not been initialized !!!")
     
 #create a naming service instance
 naming_service = SALOME_NamingServicePy_i(orb)
 
-# get Study Manager reference
+# get Study reference
 obj = None
 
 step = 0
-while step < 100 and obj == None:
-    obj = naming_service.Resolve('myStudyManager')
+sleeping_time = 0.01
+sleeping_time_max = 1.0
+while 1:
+    obj = naming_service.Resolve('/Study')
+    if obj is not None:break
     step = step + 1
-    time.sleep(4)
-     
-myStudyManager = obj._narrow(SALOMEDS.StudyManager)
-
-if myStudyManager is None:
-    print "Warning: SALOMEDS.StudyManager has not been created !!!"
-
-# create new study
-aListOfOpenStudies = myStudyManager.GetOpenStudies();
-myStudy = None;
-if len(aListOfOpenStudies) == 0 :
-    myStudy = myStudyManager.NewStudy("Study1")
-else:
-    myStudyName = aListOfOpenStudies[0]
-    myStudy = myStudyManager.GetStudyByName(myStudyName)
+    if step > 100: break
+    time.sleep(sleeping_time)
+    sleeping_time = max(sleeping_time_max, 2*sleeping_time)
+    pass
     
-myStudyName = myStudy._get_Name()
+myStudy = obj._narrow(SALOMEDS.Study)
 
-myStudyId = myStudy._get_StudyId()
-print myStudyId
 
+if myStudy is None:
+    print("Warning: SALOMEDS.Study has not been created !!!")
+    
+myStudyName = myStudy._get_Name()