Back to index

salome-kernel  6.5.0
salome_study.py
Go to the documentation of this file.
00001 #  -*- coding: iso-8859-1 -*-
00002 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00003 #
00004 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00005 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00006 #
00007 # This library is free software; you can redistribute it and/or
00008 # modify it under the terms of the GNU Lesser General Public
00009 # License as published by the Free Software Foundation; either
00010 # version 2.1 of the License.
00011 #
00012 # This library is distributed in the hope that it will be useful,
00013 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 # Lesser General Public License for more details.
00016 #
00017 # You should have received a copy of the GNU Lesser General Public
00018 # License along with this library; if not, write to the Free Software
00019 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00020 #
00021 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00022 #
00023 
00024 #  File   : salome_study.py
00025 #  Author : Paul RASCLE, EDF
00026 #  Module : SALOME
00027 #  $Header: /home/server/cvs/KERNEL/KERNEL_SRC/src/KERNEL_PY/salome_study.py,v 1.6.2.2.14.4.12.1 2012-04-12 14:05:13 vsr Exp $
00028 #
00029 import salome_kernel
00030 import SALOMEDS
00031 import salome_iapp
00032 from launchConfigureParser import verbose
00033 
00034 #--------------------------------------------------------------------------
00035 
00036 def DumpComponent(Study, SO, Builder,offset):
00037   it = Study.NewChildIterator(SO)
00038   while it.More():
00039     CSO = it.Value()
00040     a=offset*"--" + ">" + CSO.GetID()
00041     find,AtName = Builder.FindAttribute(CSO, "AttributeName")
00042     if find:
00043       a=a+":"+AtName.Value()
00044     find,AtIOR = Builder.FindAttribute(CSO, "AttributeIOR")
00045     if find:
00046       a=a+":"+AtIOR.Value()
00047     find,RefSO = CSO.ReferencedObject()
00048     if find:
00049       a=a+":"+RefSO.GetID()
00050     print a
00051     DumpComponent(Study, CSO, Builder,offset+2)
00052     it.Next()
00053 
00054 #--------------------------------------------------------------------------
00055 
00056 def DumpStudy(Study):
00057     """
00058     Dump a study, given the ior
00059     """
00060     itcomp = Study.NewComponentIterator()
00061     Builder = Study.NewBuilder()
00062     while itcomp.More():
00063       SC = itcomp.Value()
00064       name = SC.ComponentDataType()
00065       print "-> ComponentDataType is " + name
00066       DumpComponent(Study, SC,Builder, 1)
00067       itcomp.Next()
00068 
00069 def DumpStudies():
00070   """
00071     Dump all studies in a StudyManager
00072   """
00073   for name in myStudyManager.GetOpenStudies():
00074     s=myStudyManager.GetStudyByName(name)
00075     print "study:",name, s._get_StudyId()
00076     DumpStudy(s)
00077 
00078 
00079 #--------------------------------------------------------------------------
00080 
00081 def IDToObject(id):
00082     myObj = None
00083     mySO = myStudy.FindObjectID(id);
00084     if mySO is not None:
00085         ok, anAttr = mySO.FindAttribute("AttributeIOR")
00086         if ok:
00087             AtIOR = anAttr._narrow(SALOMEDS.AttributeIOR)
00088             if AtIOR.Value() != "":
00089                 myObj = orb.string_to_object(AtIOR.Value())
00090     return myObj
00091 
00092 def ObjectToSObject(obj):
00093     mySO = None
00094     if obj is not None:
00095         ior =  orb.object_to_string(obj)
00096         if ior != "":
00097             mySO = myStudy.FindObjectIOR(ior)
00098     return mySO
00099 
00100 def ObjectToID(obj):
00101     mySO = ObjectToSObject(obj)
00102     if mySO:
00103         return mySO.GetID()
00104     return ""
00105 
00106 def IDToSObject(id):
00107     mySO = myStudy.FindObjectID(id);
00108     return mySO
00109 
00110     #--------------------------------------------------------------------------
00111 
00112 def generateName(prefix = None):
00113     import random;
00114     int = random.randint(1,1000);
00115     if prefix is None:
00116         return "Study" + str(int)
00117     else :
00118         return prefix + str(int)
00119 
00120     #--------------------------------------------------------------------------
00121 
00122 def PersistentPresentation(theStudy, theSO, theWithID):
00123     # put the sobject's content (with subchildren) to the string
00124     aResult = ""
00125     attrs = theSO.GetAllAttributes()
00126     aLen = len(attrs)
00127     anUncopied = 0
00128     for a in range(0,aLen):
00129         attr = attrs[a]
00130         if isinstance(attr,SALOMEDS._objref_AttributeTreeNode):
00131             anUncopied += 1
00132         elif isinstance(attr,SALOMEDS._objref_AttributeTarget):
00133             anUncopied += 1
00134         elif isinstance(attr,SALOMEDS._objref_AttributeReal) or \
00135              isinstance(attr,SALOMEDS._objref_AttributeInteger) or \
00136              isinstance(attr,SALOMEDS._objref_AttributeName) or \
00137              isinstance(attr,SALOMEDS._objref_AttributeComment) or \
00138              isinstance(attr,SALOMEDS._objref_AttributePersistentRef) or \
00139              isinstance(attr,SALOMEDS._objref_AttributeLocalID) or \
00140              isinstance(attr,SALOMEDS._objref_AttributeUserID):
00141             aResult += " attribute value: " + str(attr.Value())
00142         elif isinstance(attr,SALOMEDS._objref_AttributeIOR):
00143             aResult += " attribute: IOR"
00144         elif isinstance(attr,SALOMEDS._objref_AttributeSequenceOfReal) or \
00145              isinstance(attr,SALOMEDS._objref_AttributeSequenceOfInteger):
00146             aResult += " Sequence: " + str(attr.CorbaSequence())
00147         elif isinstance(attr,SALOMEDS._objref_AttributeDrawable):
00148             aResult += " Drawable: " + str(attr.IsDrawable())
00149         elif isinstance(attr,SALOMEDS._objref_AttributeSelectable):
00150             aResult += " Selectable: " + str(attr.IsSelectable())
00151         elif isinstance(attr,SALOMEDS._objref_AttributeExpandable):
00152             aResult += " Expandable: " + str(attr.IsExpandable())
00153         elif isinstance(attr,SALOMEDS._objref_AttributeOpened):
00154             aResult += " Opened: " + str(attr.IsOpened())
00155         elif isinstance(attr,SALOMEDS._objref_AttributeTextColor):
00156             aResult += " TextColor: " + str(attr.TextColor())
00157         elif isinstance(attr,SALOMEDS._objref_AttributeTextHighlightColor):
00158             aResult += " TextHighlightColor: " + str(attr.TextHighlightColor())
00159         elif isinstance(attr,SALOMEDS._objref_AttributePixMap):
00160             aResult += " PixMap: " + str(attr.GetPixMap())
00161         elif isinstance(attr,SALOMEDS._objref_AttributeTableOfInteger) or \
00162              isinstance(attr,SALOMEDS._objref_AttributeTableOfReal):
00163             aResult += " Table with title: " + attr.GetTitle()
00164         elif isinstance(attr,SALOMEDS._objref_AttributePythonObject):
00165             aResult += " PythonObject: " + attr.GetObject()
00166 
00167     if theWithID:
00168         aResult = "sobject: " + theSO.GetID() + " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
00169     else:
00170         aResult = " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
00171     anIter = theStudy.NewChildIterator(theSO)
00172     while anIter.More():
00173         aResult += PersistentPresentation(theStudy, anIter.Value(), theWithID)
00174         anIter.Next()
00175     return aResult
00176 
00177     #--------------------------------------------------------------------------
00178 
00179 def GetTree(theSO):
00180     # returns the document list tree (as list)
00181     aResult = [theSO.GetID()]
00182     anIter = myStudy.NewChildIterator(theSO)
00183     while anIter.More():
00184         aResult += GetTree(anIter.Value())
00185         anIter.Next()
00186     return aResult
00187 
00188     #--------------------------------------------------------------------------
00189 
00190 def CheckCopyPaste(theSO, theInfo ,theComponentPaste):
00191     aRoot = theSO
00192     while aRoot.GetID() != "0:":
00193         aRoot = aRoot.GetFather()
00194     aTree = GetTree(aRoot)
00195     aStudyPersist = PersistentPresentation(myStudy, aRoot, 1)
00196 
00197     if not myStudyManager.CanCopy(theSO):
00198         raise RuntimeError, "<CanCopy> for "+theInfo+" returns false"
00199     
00200     if not myStudyManager.Copy(theSO):
00201         raise RuntimeError, "<Copy> for "+theInfo+" returns false"
00202 
00203     
00204     if not myStudyManager.CanPaste(theSO):
00205         raise RuntimeError, "<CanPaste> for "+theInfo+" returns false"
00206 
00207     # check: before paste study is not changed check
00208     if aStudyPersist != PersistentPresentation(myStudy, aRoot, 1):
00209         raise RuntimeError, "Study is changed before Paste calling for "+theInfo
00210     
00211     aSObj = theSO
00212     if theComponentPaste:
00213         aSObj = theSO.GetFatherComponent()
00214         theInfo = theInfo + "(paste for component)"
00215     if myStudyManager.Paste(aSObj) == None:
00216         raise RuntimeError, "<Paste> for "+theInfo+" returns None object"
00217     aNewTree = GetTree(aRoot)
00218     aLen = len(aTree)
00219     for a in range(0,aLen):
00220         if aTree[a] != aNewTree[a]:
00221             return myStudy.FindObjectID(aNewTree[a])
00222         
00223     if aLen < len(aNewTree):
00224         return myStudy.FindObjectID(aNewTree[aLen])
00225     
00226     raise RuntimeError, "After Copy calling the tree is not changed"
00227     
00228     #--------------------------------------------------------------------------
00229 
00230 def FindFileInDataDir(filename):
00231     import os
00232     datadir = os.getenv("DATA_DIR")
00233     if datadir is not None:
00234         import string
00235         dirs = string.split(datadir, ":")
00236         for dir in dirs:
00237             file = dir + "/" + filename
00238             if os.path.exists(file):
00239                 return file;
00240     datadir = os.getenv("KERNEL_ROOT_DIR") + "/examples/"
00241     file = datadir + filename
00242     if os.path.exists(file):
00243         return file;
00244 
00245     return None
00246 
00247     #--------------------------------------------------------------------------
00248 
00249 salome_study_ID = -1
00250 
00251 def getActiveStudy(theStudyId=0):
00252     global salome_study_ID
00253 
00254     if verbose(): print "getActiveStudy"
00255     if salome_study_ID == -1:
00256         if salome_iapp.hasDesktop():
00257             if verbose(): print "---in gui"
00258             salome_study_ID = salome_iapp.sg.getActiveStudyId()
00259         else:
00260             if verbose(): print "---outside gui"
00261             if theStudyId:
00262                 aStudy=myStudyManager.GetStudyByID(theStudyId)
00263                 if aStudy:
00264                     if verbose(): print "connection to existing study ", theStudyId
00265                     salome_study_ID = theStudyId
00266             if salome_study_ID == -1:
00267                 salome_study_ID = createNewStudy()
00268             if verbose(): print"--- Study Id ", salome_study_ID
00269     return salome_study_ID
00270 
00271     #--------------------------------------------------------------------------
00272 
00273 def setCurrentStudy(theStudy):
00274     """
00275     Change current study : an existing one given by a study object.
00276 
00277     :param theStudy: the study CORBA object to set as current study
00278     """
00279     global myStudyId, myStudy, myStudyName
00280     global salome_study_ID
00281     myStudy=theStudy
00282     myStudyId=theStudy._get_StudyId()
00283     myStudyName=theStudy._get_Name()
00284     return myStudyId, myStudy, myStudyName
00285 
00286     #--------------------------------------------------------------------------
00287 
00288 def setCurrentStudyId(theStudyId=0):
00289     """
00290     Change current study : an existing or new one.
00291     optional argument : theStudyId
00292         0      : create a new study (default).
00293         n (>0) : try connection to study with Id = n, or create a new one
00294                  if study not found.
00295     """
00296     global myStudyId, myStudy, myStudyName
00297     global salome_study_ID
00298     salome_study_ID = -1
00299     myStudyId = getActiveStudy(theStudyId)
00300     if verbose(): print "myStudyId",myStudyId
00301     myStudy = myStudyManager.GetStudyByID(myStudyId)
00302     myStudyName = myStudy._get_Name()
00303 
00304     return myStudyId, myStudy, myStudyName
00305 
00306     #--------------------------------------------------------------------------
00307 
00308 def createNewStudy():
00309     print "createNewStudy"
00310     i=1
00311     aStudyName = "noName"
00312     nameAlreadyInUse = 1
00313     listOfOpenStudies = myStudyManager.GetOpenStudies()
00314     print listOfOpenStudies
00315     while nameAlreadyInUse:
00316         aStudyName = "extStudy_%d"%i
00317         if aStudyName not in listOfOpenStudies:
00318             nameAlreadyInUse=0
00319         else:
00320             i = i+1
00321 
00322     theStudy = myStudyManager.NewStudy(aStudyName)
00323     theStudyId = theStudy._get_StudyId()
00324     print aStudyName, theStudyId
00325     return theStudyId
00326 
00327     #--------------------------------------------------------------------------
00328 
00329 salome_study_initial = 1
00330 
00331 def salome_study_init(theStudyId=0):
00332     """
00333     Performs only once study creation or connection.
00334     optional argument : theStudyId
00335       When in embedded interpreter inside IAPP, theStudyId is not used
00336       When used without GUI (external interpreter)
00337         0      : create a new study (default).
00338         n (>0) : try connection to study with Id = n, or create a new one
00339                  if study not found.
00340     """
00341 
00342     global salome_study_initial
00343     global myStudyManager, myStudyId, myStudy, myStudyName
00344     global orb, lcc, naming_service, cm
00345 
00346     if salome_study_initial:
00347         salome_study_initial = 0
00348 
00349         orb, lcc, naming_service, cm = salome_kernel.salome_kernel_init()
00350 
00351         # get Study Manager reference
00352         if verbose(): print "looking for studyManager ..."
00353         obj = naming_service.Resolve('myStudyManager')
00354         myStudyManager = obj._narrow(SALOMEDS.StudyManager)
00355         if verbose(): print "studyManager found"
00356 
00357         # get active study Id, ref and name
00358         myStudyId = getActiveStudy(theStudyId)
00359         if verbose(): print "myStudyId",myStudyId
00360         myStudy = myStudyManager.GetStudyByID(myStudyId)
00361         myStudyName = myStudy._get_Name()
00362 
00363     return myStudyManager, myStudyId, myStudy, myStudyName
00364