Salome HOME
[EDF30062] : Forward of current directory mecanism
[modules/kernel.git] / src / KERNEL_PY / salome_study.py
old mode 100755 (executable)
new mode 100644 (file)
index 7269f8b..048c67d
@@ -1,5 +1,5 @@
 #  -*- coding: iso-8859-1 -*-
-# Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+# Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
 #
 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -36,8 +36,9 @@ myStudyName = None
 
 #--------------------------------------------------------------------------
 
-def DumpComponent(Study, SO, Builder,offset):
-  it = Study.NewChildIterator(SO)
+def DumpComponent(SO, Builder,offset):
+  global myStudy
+  it = myStudy.NewChildIterator(SO)
   while it.More():
     CSO = it.Value()
     a=offset*"--" + ">" + CSO.GetID()
@@ -50,23 +51,24 @@ def DumpComponent(Study, SO, Builder,offset):
     find,RefSO = CSO.ReferencedObject()
     if find:
       a=a+":"+RefSO.GetID()
-    print a
-    DumpComponent(Study, CSO, Builder,offset+2)
+    print(a)
+    DumpComponent(CSO, Builder,offset+2)
     it.Next()
 
 #--------------------------------------------------------------------------
 
-def DumpStudy(Study):
+def DumpStudy():
     """
     Dump a study, given the ior
     """
-    itcomp = Study.NewComponentIterator()
-    Builder = Study.NewBuilder()
+    global myStudy
+    itcomp = myStudy.NewComponentIterator()
+    Builder = myStudy.NewBuilder()
     while itcomp.More():
       SC = itcomp.Value()
       name = SC.ComponentDataType()
-      print "-> ComponentDataType is " + name
-      DumpComponent(Study, SC,Builder, 1)
+      print("-> ComponentDataType is " + name)
+      DumpComponent(SC,Builder, 1)
       itcomp.Next()
 
 #--------------------------------------------------------------------------
@@ -84,6 +86,7 @@ def IDToObject(id):
     return myObj
 
 def ObjectToSObject(obj):
+    global myStudy
     mySO = None
     if obj is not None:
         ior =  orb.object_to_string(obj)
@@ -114,7 +117,8 @@ def generateName(prefix = None):
 
     #--------------------------------------------------------------------------
 
-def PersistentPresentation(theStudy, theSO, theWithID):
+def PersistentPresentation(theSO, theWithID):
+    global myStudy
     # put the sobject's content (with subchildren) to the string
     aResult = ""
     attrs = theSO.GetAllAttributes()
@@ -163,9 +167,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
 
@@ -190,28 +194,28 @@ def CheckCopyPaste(theSO, theInfo ,theComponentPaste):
     while aRoot.GetID() != "0:":
         aRoot = aRoot.GetFather()
     aTree = GetTree(aRoot)
-    aStudyPersist = PersistentPresentation(myStudy, aRoot, 1)
+    aStudyPersist = PersistentPresentation(aRoot, 1)
 
     if not myStudy.CanCopy(theSO):
-        raise RuntimeError, "<CanCopy> for "+theInfo+" returns false"
+        raise RuntimeError("<CanCopy> for "+theInfo+" returns false")
     
     if not myStudy.Copy(theSO):
-        raise RuntimeError, "<Copy> for "+theInfo+" returns false"
+        raise RuntimeError("<Copy> for "+theInfo+" returns false")
 
     
     if not myStudy.CanPaste(theSO):
-        raise RuntimeError, "<CanPaste> for "+theInfo+" returns false"
+        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 myStudy.Paste(aSObj) == None:
-        raise RuntimeError, "<Paste> for "+theInfo+" returns None object"
+        raise RuntimeError("<Paste> for "+theInfo+" returns None object")
     aNewTree = GetTree(aRoot)
     aLen = len(aTree)
     for a in range(0,aLen):
@@ -221,7 +225,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")
     
     #--------------------------------------------------------------------------
 
@@ -266,39 +270,44 @@ def FindFileInDataDir(filename):
 
     #--------------------------------------------------------------------------
 
-def setCurrentStudy(theStudy):
-    """
-    Change current study : an existing one given by a study object.
+def openStudy(theStudyPath):
+    global myStudy, myStudyName
+    myStudy.Open(theStudyPath)
+    myStudyName = myStudy._get_Name()
 
-    :param theStudy: the study CORBA object to set as current study
+    #--------------------------------------------------------------------------
 
-    Obsolete: only one study can be opened at the moment.
-    This function works properly if specified theStudy parameter
-    corresponds to the currently opened study.
-    Kept for backward compatibility only.
+def salome_study_init_without_session(theStudyPath=None):
+    """
+    Performs only once study creation or connection.
+    optional argument : theStudyPath
+        None        : attach to the currently active single study;
+                      create new empty study if there is active study
+        <URL> (str) : open study with the given file name
     """
     global myStudy, myStudyName
-    myStudy = theStudy
-    myStudyName = theStudy._get_Name()
-    return myStudy, myStudyName
+    global orb, lcc, naming_service, cm
 
-    #--------------------------------------------------------------------------
+    if not myStudy:
+        import CORBA
+        orb = CORBA.ORB_init([''])
 
-def openStudy(theStudyPath):
-    global myStudy
-    print "openStudy"
-    theStudy = myStudy.Open(theStudyPath)
-    theStudyName = theStudy._get_Name()
-    print theStudyPath, theStudyName
-    return theStudyName
+        # get Study reference
+        import KernelDS
+        myStudy = KernelDS.myStudy()
+        pass
 
-    #--------------------------------------------------------------------------
-def clearStudy():
-    global myStudy
-    print "clearStudy"
-    myStudy.Clear()
-    myStudy = None
-    pass
+    import types
+    if theStudyPath and isinstance(theStudyPath, (str, bytes)):
+        if isinstance(theStudyPath, bytes):
+            theStudyPath = str(theStudyPath, 'UTF8')
+        openStudy(theStudyPath)
+    else:
+        myStudy.Init()
+
+    myStudyName = myStudy._get_Name()
+
+    return myStudy, myStudyName
 
     #--------------------------------------------------------------------------
 
@@ -313,23 +322,26 @@ def salome_study_init(theStudyPath=None):
     global myStudy, myStudyName
     global orb, lcc, naming_service, cm
 
-    if verbose(): print "theStudyPath:", theStudyPath
     if not myStudy:
-        orb, lcc, naming_service, cm = salome_kernel.salome_kernel_init()
+        orb, lcc, naming_service, cm, _, _, _ = salome_kernel.salome_kernel_init()
 
         # get Study reference
-        if verbose(): print "looking for study..."
-        obj = naming_service.Resolve('Study')
+        obj = naming_service.Resolve('/Study')
         myStudy = obj._narrow(SALOMEDS.Study)
-        if verbose(): print "Study found"
         pass
 
-    # get study name
+    import types
+    if theStudyPath and isinstance(theStudyPath, (str, bytes)):
+        if isinstance(theStudyPath, bytes):
+            theStudyPath = str(theStudyPath, 'UTF8')
+        openStudy(theStudyPath)
+    else:
+        myStudy.Init()
+
     myStudyName = myStudy._get_Name()
 
     return myStudy, myStudyName
 
 def salome_study_close():
-    global myStudy, myStudyName
-    myStudy, myStudyName = None, None
-    pass
+  global myStudy, myStudyName
+  myStudy, myStudyName = None, None