X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FKERNEL_PY%2Fsalome_study.py;h=048c67d1b0cb3e5edbc61009cd4bd6ee2346c1f9;hb=15a97ac169f8acd9cfcb6c4f9ef236d79c9d6a32;hp=c084373255be678b239bc6187924f7babd1d13f5;hpb=e429ce02076e083051c6520e0d7113022bd67b18;p=modules%2Fkernel.git diff --git a/src/KERNEL_PY/salome_study.py b/src/KERNEL_PY/salome_study.py old mode 100755 new mode 100644 index c08437325..048c67d1b --- a/src/KERNEL_PY/salome_study.py +++ b/src/KERNEL_PY/salome_study.py @@ -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 @@ -31,15 +31,14 @@ import SALOMEDS import salome_iapp from launchConfigureParser import verbose -myStudyManager = None -myStudyId = None myStudy = None 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() @@ -52,36 +51,26 @@ 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() -def DumpStudies(): - """ - Dump all studies in a StudyManager - """ - global myStudyManager - for name in myStudyManager.GetOpenStudies(): - s = myStudyManager.GetStudyByName(name) - print "study:",name, s._get_StudyId() - DumpStudy(s) - - #-------------------------------------------------------------------------- def IDToObject(id): @@ -97,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) @@ -127,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() @@ -176,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 @@ -197,34 +188,34 @@ def GetTree(theSO): #-------------------------------------------------------------------------- def CheckCopyPaste(theSO, theInfo ,theComponentPaste): - global myStudyManager, myStudy + global myStudy aRoot = theSO while aRoot.GetID() != "0:": aRoot = aRoot.GetFather() aTree = GetTree(aRoot) - aStudyPersist = PersistentPresentation(myStudy, aRoot, 1) + aStudyPersist = PersistentPresentation(aRoot, 1) - if not myStudyManager.CanCopy(theSO): - raise RuntimeError, " for "+theInfo+" returns false" + if not myStudy.CanCopy(theSO): + raise RuntimeError(" for "+theInfo+" returns false") - if not myStudyManager.Copy(theSO): - raise RuntimeError, " for "+theInfo+" returns false" + if not myStudy.Copy(theSO): + raise RuntimeError(" for "+theInfo+" returns false") - if not myStudyManager.CanPaste(theSO): - raise RuntimeError, " for "+theInfo+" returns false" + if not myStudy.CanPaste(theSO): + raise RuntimeError(" 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, " for "+theInfo+" returns None object" + if myStudy.Paste(aSObj) == None: + raise RuntimeError(" for "+theInfo+" returns None object") aNewTree = GetTree(aRoot) aLen = len(aTree) for a in range(0,aLen): @@ -234,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") #-------------------------------------------------------------------------- @@ -279,97 +270,44 @@ def FindFileInDataDir(filename): #-------------------------------------------------------------------------- -salome_study_ID = -1 - -# *args are used here to support backward compatibility -# previously it was possible to pass theStudyId parameter to this function -# which is no more supported. -def getActiveStudy(*args): - global myStudyManager - global salome_study_ID - - if not myStudyManager: - print "No active study" - return None - pass - - if verbose(): print "getActiveStudy" - if salome_study_ID == -1: - listOpenStudies = myStudyManager.GetOpenStudies() - if len(listOpenStudies) == 0: - return None - else: - s = myStudyManager.GetStudyByName(listOpenStudies[0]) - salome_study_ID = s._get_StudyId() - if verbose(): print"--- Study Id ", salome_study_ID - return salome_study_ID +def openStudy(theStudyPath): + global myStudy, myStudyName + myStudy.Open(theStudyPath) + myStudyName = myStudy._get_Name() #-------------------------------------------------------------------------- -def setCurrentStudy(theStudy): +def salome_study_init_without_session(theStudyPath=None): """ - Change current study : an existing one given by a study object. - - :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. + 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 + (str) : open study with the given file name """ - global myStudyId, myStudy, myStudyName - global salome_study_ID - myStudy = theStudy - myStudyId = theStudy._get_StudyId() - myStudyName = theStudy._get_Name() - return myStudyId, myStudy, myStudyName + global myStudy, myStudyName + global orb, lcc, naming_service, cm - #-------------------------------------------------------------------------- + if not myStudy: + import CORBA + orb = CORBA.ORB_init(['']) -# *args are used here to support backward compatibility -# previously it was possible to pass theStudyId parameter to this function -# which is no more supported. -def setCurrentStudyId(*args): - """ - Change current study : an existing or new one. + # get Study reference + import KernelDS + myStudy = KernelDS.myStudy() + pass - This function connects to the single opened study if there is any; otherwise - new empty study is created. + import types + if theStudyPath and isinstance(theStudyPath, (str, bytes)): + if isinstance(theStudyPath, bytes): + theStudyPath = str(theStudyPath, 'UTF8') + openStudy(theStudyPath) + else: + myStudy.Init() - Obsolete: only one study can be opened at the moment. - Kept for backward compatibility only. - """ - global myStudyManager, myStudyId, myStudy, myStudyName - global salome_study_ID - salome_study_ID = -1 - myStudyId = getActiveStudy() - if not myStudyId: - myStudyId = createNewStudy() - if verbose(): print "myStudyId",myStudyId - myStudy = myStudyManager.GetStudyByID(myStudyId) myStudyName = myStudy._get_Name() - return myStudyId, myStudy, myStudyName - #-------------------------------------------------------------------------- - -def createNewStudy(): - global myStudyManager - print "createNewStudy" - aStudyName = "extStudy" - theStudy = myStudyManager.NewStudy(aStudyName) - theStudyId = theStudy._get_StudyId() - print aStudyName, theStudyId - return theStudyId - - #-------------------------------------------------------------------------- - -def openStudy(theStudyPath): - global myStudyManager - print "openStudy" - theStudy = myStudyManager.Open(theStudyPath) - theStudyId = theStudy._get_StudyId() - print theStudyPath, theStudyId - return theStudyId + return myStudy, myStudyName #-------------------------------------------------------------------------- @@ -381,40 +319,29 @@ def salome_study_init(theStudyPath=None): create new empty study if there is active study (str) : open study with the given file name """ - global myStudyManager, myStudyId, myStudy, myStudyName + global myStudy, myStudyName global orb, lcc, naming_service, cm - if verbose(): print "theStudyPath:", theStudyPath - if not myStudyManager: - orb, lcc, naming_service, cm = salome_kernel.salome_kernel_init() + if not myStudy: + orb, lcc, naming_service, cm, _, _, _ = salome_kernel.salome_kernel_init() - # get Study Manager reference - if verbose(): print "looking for studyManager ..." - obj = naming_service.Resolve('myStudyManager') - myStudyManager = obj._narrow(SALOMEDS.StudyManager) - if verbose(): print "studyManager found" + # get Study reference + obj = naming_service.Resolve('/Study') + myStudy = obj._narrow(SALOMEDS.Study) pass - # get active study Id, ref and name - myStudy = None - myStudyId = getActiveStudy() - if myStudyId == None : - import types - if theStudyPath and type(theStudyPath) == types.StringType: - myStudyId = openStudy(theStudyPath) - else: - myStudyId = createNewStudy() - if verbose(): print "myStudyId", myStudyId - - if myStudy == None: - myStudy = myStudyManager.GetStudyByID(myStudyId) + 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 myStudyManager, myStudyId, myStudy, myStudyName + return myStudy, myStudyName def salome_study_close(): - global salome_study_ID - global myStudyId, myStudy, myStudyName - salome_study_ID = -1 - myStudyId, myStudy, myStudyName = None, None, None - pass + global myStudy, myStudyName + myStudy, myStudyName = None, None