Back to index

salome-kernel  6.5.0
batchmode_salome.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   : batchmode_salome.py
00025 #  Module : SALOME
00026 #import salome_shared_modules
00027 #
00028 from omniORB import CORBA
00029 from LifeCycleCORBA import *
00030 import SALOME
00031 import SALOMEDS
00032 from SALOME_NamingServicePy import *
00033 
00034 #--------------------------------------------------------------------------
00035 
00036 def ImportComponentGUI(ComponentName):
00037     libName = "lib" + ComponentName + "_Swig"
00038     command = "from " + libName + " import *"
00039     exec ( command )
00040     constructor = ComponentName + "GUI_Swig()"
00041     command = "gui = " + constructor
00042     exec ( command )
00043     return gui
00044 
00045     #--------------------------------------------------------------------------
00046 
00047 def SalomeGUIgetAllSelected(self):
00048     selNumber = self.SelectedCount()
00049     listSelected = []
00050     for i in range(selNumber):
00051         listSelected.append(self.getSelected(i))
00052     return listSelected
00053     #--------------------------------------------------------------------------
00054 
00055 def generateName(prefix = None):
00056     import random;
00057     int = random.randint(1,1000);
00058     if prefix is None:
00059         return "Study" + str(int)
00060     else :
00061         return prefix + str(int)
00062 
00063 #WITHOUTIHMfrom libSALOME_Swig import *
00064 ###from SalomePyQt import *
00065 #WITHOUTIHMclass SalomeGUI(SALOMEGUI_Swig):
00066     #WITHOUTIHMgetAllSelected = SalomeGUIgetAllSelected
00067    
00068     #WITHOUTIHMdef getDesktop(self) :
00069 #      return SalomePyQt.getDesktop()
00070        #WITHOUTIHMreturn None
00071 
00072     #WITHOUTIHMdef getSelection(self) :
00073 #      return SalomePyQt.getSelection()
00074        #WITHOUTIHMreturn None
00075 
00076     #--------------------------------------------------------------------------
00077 
00078 def IDToObject(id):
00079     myObj = None
00080     mySO = myStudy.FindObjectID(id);
00081     if mySO is not None:
00082         ok, anAttr = mySO.FindAttribute("AttributeIOR")
00083         if ok:
00084             AtIOR = anAttr._narrow(SALOMEDS.AttributeIOR)
00085             if AtIOR.Value() != "":
00086                 myObj = orb.string_to_object(AtIOR.Value())
00087     return myObj
00088 
00089 def ObjectToSObject(obj):
00090     mySO = None
00091     if obj is not None:
00092         ior =  orb.object_to_string(obj)
00093         if ior != "":
00094             mySO = myStudy.FindObjectIOR(ior)
00095     return mySO
00096 
00097 def ObjectToID(obj):
00098     mySO = ObjectToSObject(obj)
00099     if mySO:
00100         return mySO.GetID()
00101     return ""
00102 
00103 def IDToSObject(id):
00104     mySO = myStudy.FindObjectID(id);
00105     return mySO
00106 
00107     #--------------------------------------------------------------------------
00108 
00109 def PersistentPresentation(theStudy, theSO, theWithID):
00110     # put the sobject's content (with subchildren) to the string
00111     aResult = ""
00112     attrs = theSO.GetAllAttributes()
00113     aLen = len(attrs)
00114     anUncopied = 0
00115     for a in range(0,aLen):
00116         attr = attrs[a]
00117         if isinstance(attr,SALOMEDS._objref_AttributeTreeNode):
00118             anUncopied += 1
00119         elif isinstance(attr,SALOMEDS._objref_AttributeTarget):
00120             anUncopied += 1
00121         elif isinstance(attr,SALOMEDS._objref_AttributeReal) or \
00122              isinstance(attr,SALOMEDS._objref_AttributeInteger) or \
00123              isinstance(attr,SALOMEDS._objref_AttributeName) or \
00124              isinstance(attr,SALOMEDS._objref_AttributeComment) or \
00125              isinstance(attr,SALOMEDS._objref_AttributePersistentRef) or \
00126              isinstance(attr,SALOMEDS._objref_AttributeLocalID) or \
00127              isinstance(attr,SALOMEDS._objref_AttributeUserID):
00128             aResult += " attribute value: " + str(attr.Value())
00129         elif isinstance(attr,SALOMEDS._objref_AttributeIOR):
00130             aResult += " attribute: IOR"
00131         elif isinstance(attr,SALOMEDS._objref_AttributeSequenceOfReal) or \
00132              isinstance(attr,SALOMEDS._objref_AttributeSequenceOfInteger):
00133             aResult += " Sequence: " + str(attr.CorbaSequence())
00134         elif isinstance(attr,SALOMEDS._objref_AttributeDrawable):
00135             aResult += " Drawable: " + str(attr.IsDrawable())
00136         elif isinstance(attr,SALOMEDS._objref_AttributeSelectable):
00137             aResult += " Selectable: " + str(attr.IsSelectable())
00138         elif isinstance(attr,SALOMEDS._objref_AttributeExpandable):
00139             aResult += " Expandable: " + str(attr.IsExpandable())
00140         elif isinstance(attr,SALOMEDS._objref_AttributeOpened):
00141             aResult += " Opened: " + str(attr.IsOpened())
00142         elif isinstance(attr,SALOMEDS._objref_AttributeTextColor):
00143             aResult += " TextColor: " + str(attr.TextColor())
00144         elif isinstance(attr,SALOMEDS._objref_AttributeTextHighlightColor):
00145             aResult += " TextHighlightColor: " + str(attr.TextHighlightColor())
00146         elif isinstance(attr,SALOMEDS._objref_AttributePixMap):
00147             aResult += " PixMap: " + str(attr.GetPixMap())
00148         elif isinstance(attr,SALOMEDS._objref_AttributeTableOfInteger) or \
00149              isinstance(attr,SALOMEDS._objref_AttributeTableOfReal):
00150             aResult += " Table with title: " + attr.GetTitle()
00151         elif isinstance(attr,SALOMEDS._objref_AttributePythonObject):
00152             aResult += " PythonObject: " + attr.GetObject()
00153 
00154     if theWithID:
00155         aResult = "sobject: " + theSO.GetID() + " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
00156     else:
00157         aResult = " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
00158     anIter = theStudy.NewChildIterator(theSO)
00159     while anIter.More():
00160         aResult += PersistentPresentation(theStudy, anIter.Value(), theWithID)
00161         anIter.Next()
00162     return aResult
00163 
00164     #--------------------------------------------------------------------------
00165 
00166 def GetTree(theSO):
00167     # returns the document list tree (as list)
00168     aResult = [theSO.GetID()]
00169     anIter = myStudy.NewChildIterator(theSO)
00170     while anIter.More():
00171         aResult += GetTree(anIter.Value())
00172         anIter.Next()
00173     return aResult
00174 
00175     #--------------------------------------------------------------------------
00176 
00177 def CheckCopyPaste(theSO, theInfo ,theComponentPaste):
00178     aRoot = theSO
00179     while aRoot.GetID() != "0:":
00180         aRoot = aRoot.GetFather()
00181     aTree = GetTree(aRoot)
00182     aStudyPersist = PersistentPresentation(myStudy, aRoot, 1)
00183 
00184     if not myStudyManager.CanCopy(theSO):
00185         raise RuntimeError, "<CanCopy> for "+theInfo+" returns false"
00186     
00187     if not myStudyManager.Copy(theSO):
00188         raise RuntimeError, "<Copy> for "+theInfo+" returns false"
00189 
00190     
00191     if not myStudyManager.CanPaste(theSO):
00192         raise RuntimeError, "<CanPaste> for "+theInfo+" returns false"
00193 
00194     # check: before paste study is not changed check
00195     if aStudyPersist != PersistentPresentation(myStudy, aRoot, 1):
00196         raise RuntimeError, "Study is changed before Paste calling for "+theInfo
00197     
00198     aSObj = theSO
00199     if theComponentPaste:
00200         aSObj = theSO.GetFatherComponent()
00201         theInfo = theInfo + "(paste for component)"
00202     if myStudyManager.Paste(aSObj) == None:
00203         raise RuntimeError, "<Paste> for "+theInfo+" returns None object"
00204     aNewTree = GetTree(aRoot)
00205     aLen = len(aTree)
00206     for a in range(0,aLen):
00207         if aTree[a] != aNewTree[a]:
00208             return myStudy.FindObjectID(aNewTree[a])
00209         
00210     if aLen < len(aNewTree):
00211         return myStudy.FindObjectID(aNewTree[aLen])
00212     
00213     raise RuntimeError, "After Copy calling the tree is not changed"
00214     
00215     #--------------------------------------------------------------------------
00216 def FindFileInDataDir(filename):
00217     import os
00218     datadir = os.getenv("DATA_DIR")
00219     if datadir is not None:
00220         import string
00221         dirs = string.split(datadir, ":")
00222         for dir in dirs:
00223             file = dir + "/" + filename
00224             if os.path.exists(file):
00225                 return file;
00226     datadir = os.getenv("KERNEL_ROOT_DIR") + "/examples/"
00227     file = datadir + filename
00228     if os.path.exists(file):
00229         return file;
00230 
00231     return None
00232         
00233 #--------------------------------------------------------------------------
00234 # initialise the ORB
00235 orb = None
00236 
00237 step = 0
00238 sleeping_time = 0.01
00239 sleeping_time_max = 1.0
00240 while 1:
00241     orb = CORBA.ORB_init([''], CORBA.ORB_ID)
00242     if orb is not None: break
00243     step = step + 1
00244     if step > 100: break
00245     time.sleep(sleeping_time)
00246     sleeping_time = max(sleeping_time_max, 2*sleeping_time)
00247     pass
00248     
00249 if orb is None:
00250     print "Warning: ORB has not been initialized !!!"
00251 
00252 # create an LifeCycleCORBA instance
00253 lcc = LifeCycleCORBA(orb)
00254 
00255 step = 0
00256 sleeping_time = 0.01
00257 sleeping_time_max = 1.0
00258 while 1:
00259     lcc = LifeCycleCORBA(orb)
00260     if lcc is not None: break
00261     step = step + 1
00262     if step > 100: break
00263     time.sleep(sleeping_time)
00264     sleeping_time = max(sleeping_time_max, 2*sleeping_time)
00265     pass
00266     
00267 if lcc is None:
00268     print "Warning: LifeCycleCORBA object has not been initialized !!!"
00269     
00270 #create a naming service instance
00271 naming_service = SALOME_NamingServicePy_i(orb)
00272 
00273 # get Study Manager reference
00274 obj = None
00275 
00276 step = 0
00277 sleeping_time = 0.01
00278 sleeping_time_max = 1.0
00279 while 1:
00280     obj = naming_service.Resolve('myStudyManager')
00281     if obj is not None:break
00282     step = step + 1
00283     if step > 100: break
00284     time.sleep(sleeping_time)
00285     sleeping_time = max(sleeping_time_max, 2*sleeping_time)
00286     pass
00287      
00288 myStudyManager = obj._narrow(SALOMEDS.StudyManager)
00289 
00290 if myStudyManager is None:
00291     print "Warning: SALOMEDS.StudyManager has not been created !!!"
00292 
00293 # create new study
00294 aListOfOpenStudies = myStudyManager.GetOpenStudies();
00295 myStudy = None;
00296 if len(aListOfOpenStudies) == 0 :
00297     myStudy = myStudyManager.NewStudy("Study1")
00298 else:
00299     myStudyName = aListOfOpenStudies[0]
00300     myStudy = myStudyManager.GetStudyByName(myStudyName)
00301     
00302 myStudyName = myStudy._get_Name()
00303 
00304 myStudyId = myStudy._get_StudyId()
00305 #print myStudyId
00306