Salome HOME
fix problem with opening study by salome.openStudy(hdf) method
[modules/kernel.git] / src / KERNEL_PY / batchmode_salome.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20 #
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23
24 #  File   : batchmode_salome.py
25 #  Module : SALOME
26
27 from omniORB import CORBA
28 from LifeCycleCORBA import *
29 import SALOME
30 import SALOMEDS
31 from SALOME_NamingServicePy import *
32
33 #--------------------------------------------------------------------------
34
35 def ImportComponentGUI(ComponentName):
36     libName = "lib" + ComponentName + "_Swig"
37     command = "from " + libName + " import *"
38     exec ( command )
39     constructor = ComponentName + "GUI_Swig()"
40     command = "gui = " + constructor
41     exec ( command )
42     return gui
43
44     #--------------------------------------------------------------------------
45
46 def SalomeGUIgetAllSelected(self):
47     selNumber = self.SelectedCount()
48     listSelected = []
49     for i in range(selNumber):
50         listSelected.append(self.getSelected(i))
51     return listSelected
52     #--------------------------------------------------------------------------
53
54 def generateName(prefix = None):
55     import random;
56     int = random.randint(1,1000);
57     if prefix is None:
58         return "Study" + str(int)
59     else :
60         return prefix + str(int)
61
62 #WITHOUTIHMfrom libSALOME_Swig import *
63 ###from SalomePyQt import *
64 #WITHOUTIHMclass SalomeGUI(SALOMEGUI_Swig):
65     #WITHOUTIHMgetAllSelected = SalomeGUIgetAllSelected
66    
67     #WITHOUTIHMdef getDesktop(self) :
68 #       return SalomePyQt.getDesktop()
69         #WITHOUTIHMreturn None
70
71     #WITHOUTIHMdef getSelection(self) :
72 #       return SalomePyQt.getSelection()
73         #WITHOUTIHMreturn None
74
75     #--------------------------------------------------------------------------
76
77 def IDToObject(id):
78     myObj = None
79     mySO = myStudy.FindObjectID(id);
80     if mySO is not None:
81         ok, anAttr = mySO.FindAttribute("AttributeIOR")
82         if ok:
83             AtIOR = anAttr._narrow(SALOMEDS.AttributeIOR)
84             if AtIOR.Value() != "":
85                 myObj = orb.string_to_object(AtIOR.Value())
86     return myObj
87
88 def ObjectToSObject(obj):
89     mySO = None
90     if obj is not None:
91         ior =  orb.object_to_string(obj)
92         if ior != "":
93             mySO = myStudy.FindObjectIOR(ior)
94     return mySO
95
96 def ObjectToID(obj):
97     mySO = ObjectToSObject(obj)
98     if mySO:
99         return mySO.GetID()
100     return ""
101
102 def IDToSObject(id):
103     mySO = myStudy.FindObjectID(id);
104     return mySO
105
106     #--------------------------------------------------------------------------
107
108 def PersistentPresentation(theSO, theWithID):
109     # put the sobject's content (with subchildren) to the string
110     aResult = ""
111     attrs = theSO.GetAllAttributes()
112     aLen = len(attrs)
113     anUncopied = 0
114     for a in range(0,aLen):
115         attr = attrs[a]
116         if isinstance(attr,SALOMEDS._objref_AttributeTreeNode):
117             anUncopied += 1
118         elif isinstance(attr,SALOMEDS._objref_AttributeTarget):
119             anUncopied += 1
120         elif isinstance(attr,SALOMEDS._objref_AttributeReal) or \
121              isinstance(attr,SALOMEDS._objref_AttributeInteger) or \
122              isinstance(attr,SALOMEDS._objref_AttributeName) or \
123              isinstance(attr,SALOMEDS._objref_AttributeComment) or \
124              isinstance(attr,SALOMEDS._objref_AttributePersistentRef) or \
125              isinstance(attr,SALOMEDS._objref_AttributeLocalID) or \
126              isinstance(attr,SALOMEDS._objref_AttributeUserID):
127             aResult += " attribute value: " + str(attr.Value())
128         elif isinstance(attr,SALOMEDS._objref_AttributeIOR):
129             aResult += " attribute: IOR"
130         elif isinstance(attr,SALOMEDS._objref_AttributeSequenceOfReal) or \
131              isinstance(attr,SALOMEDS._objref_AttributeSequenceOfInteger):
132             aResult += " Sequence: " + str(attr.CorbaSequence())
133         elif isinstance(attr,SALOMEDS._objref_AttributeDrawable):
134             aResult += " Drawable: " + str(attr.IsDrawable())
135         elif isinstance(attr,SALOMEDS._objref_AttributeSelectable):
136             aResult += " Selectable: " + str(attr.IsSelectable())
137         elif isinstance(attr,SALOMEDS._objref_AttributeExpandable):
138             aResult += " Expandable: " + str(attr.IsExpandable())
139         elif isinstance(attr,SALOMEDS._objref_AttributeOpened):
140             aResult += " Opened: " + str(attr.IsOpened())
141         elif isinstance(attr,SALOMEDS._objref_AttributeTextColor):
142             aResult += " TextColor: " + str(attr.TextColor())
143         elif isinstance(attr,SALOMEDS._objref_AttributeTextHighlightColor):
144             aResult += " TextHighlightColor: " + str(attr.TextHighlightColor())
145         elif isinstance(attr,SALOMEDS._objref_AttributePixMap):
146             aResult += " PixMap: " + str(attr.GetPixMap())
147         elif isinstance(attr,SALOMEDS._objref_AttributeTableOfInteger) or \
148              isinstance(attr,SALOMEDS._objref_AttributeTableOfReal):
149             aResult += " Table with title: " + attr.GetTitle()
150         elif isinstance(attr,SALOMEDS._objref_AttributePythonObject):
151             aResult += " PythonObject: " + attr.GetObject()
152
153     if theWithID:
154         aResult = "sobject: " + theSO.GetID() + " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
155     else:
156         aResult = " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
157     anIter = myStudy.NewChildIterator(theSO)
158     while anIter.More():
159         aResult += PersistentPresentation(anIter.Value(), theWithID)
160         anIter.Next()
161     return aResult
162
163     #--------------------------------------------------------------------------
164
165 def GetTree(theSO):
166     # returns the document list tree (as list)
167     aResult = [theSO.GetID()]
168     anIter = myStudy.NewChildIterator(theSO)
169     while anIter.More():
170         aResult += GetTree(anIter.Value())
171         anIter.Next()
172     return aResult
173
174     #--------------------------------------------------------------------------
175
176 def CheckCopyPaste(theSO, theInfo ,theComponentPaste):
177     aRoot = theSO
178     while aRoot.GetID() != "0:":
179         aRoot = aRoot.GetFather()
180     aTree = GetTree(aRoot)
181     aStudyPersist = PersistentPresentation(aRoot, 1)
182
183     if not myStudy.CanCopy(theSO):
184         raise RuntimeError, "<CanCopy> for "+theInfo+" returns false"
185     
186     if not myStudy.Copy(theSO):
187         raise RuntimeError, "<Copy> for "+theInfo+" returns false"
188
189     
190     if not myStudy.CanPaste(theSO):
191         raise RuntimeError, "<CanPaste> for "+theInfo+" returns false"
192
193     # check: before paste study is not changed check
194     if aStudyPersist != PersistentPresentation(aRoot, 1):
195         raise RuntimeError, "Study is changed before Paste calling for "+theInfo
196     
197     aSObj = theSO
198     if theComponentPaste:
199         aSObj = theSO.GetFatherComponent()
200         theInfo = theInfo + "(paste for component)"
201     if myStudy.Paste(aSObj) == None:
202         raise RuntimeError, "<Paste> for "+theInfo+" returns None object"
203     aNewTree = GetTree(aRoot)
204     aLen = len(aTree)
205     for a in range(0,aLen):
206         if aTree[a] != aNewTree[a]:
207             return myStudy.FindObjectID(aNewTree[a])
208         
209     if aLen < len(aNewTree):
210         return myStudy.FindObjectID(aNewTree[aLen])
211     
212     raise RuntimeError, "After Copy calling the tree is not changed"
213     
214     #--------------------------------------------------------------------------
215 def FindFileInDataDir(filename):
216     import os
217     datadir = os.getenv("DATA_DIR")
218     if datadir is not None:
219         import string
220         dirs = string.split(datadir, ":")
221         for dir in dirs:
222             file = dir + "/" + filename
223             if os.path.exists(file):
224                 return file;
225     datadir = os.getenv("KERNEL_ROOT_DIR") + "/examples/"
226     file = datadir + filename
227     if os.path.exists(file):
228         return file;
229
230     return None
231         
232 #--------------------------------------------------------------------------
233 # initialise the ORB
234 orb = None
235
236 step = 0
237 sleeping_time = 0.01
238 sleeping_time_max = 1.0
239 while 1:
240     orb = CORBA.ORB_init([''], CORBA.ORB_ID)
241     if orb is not None: break
242     step = step + 1
243     if step > 100: break
244     time.sleep(sleeping_time)
245     sleeping_time = max(sleeping_time_max, 2*sleeping_time)
246     pass
247     
248 if orb is None:
249     print "Warning: ORB has not been initialized !!!"
250
251 # create an LifeCycleCORBA instance
252 lcc = LifeCycleCORBA(orb)
253
254 step = 0
255 sleeping_time = 0.01
256 sleeping_time_max = 1.0
257 while 1:
258     lcc = LifeCycleCORBA(orb)
259     if lcc is not None: break
260     step = step + 1
261     if step > 100: break
262     time.sleep(sleeping_time)
263     sleeping_time = max(sleeping_time_max, 2*sleeping_time)
264     pass
265     
266 if lcc is None:
267     print "Warning: LifeCycleCORBA object has not been initialized !!!"
268     
269 #create a naming service instance
270 naming_service = SALOME_NamingServicePy_i(orb)
271
272 # get Study reference
273 obj = None
274
275 step = 0
276 sleeping_time = 0.01
277 sleeping_time_max = 1.0
278 while 1:
279     obj = naming_service.Resolve('/Study')
280     if obj is not None:break
281     step = step + 1
282     if step > 100: break
283     time.sleep(sleeping_time)
284     sleeping_time = max(sleeping_time_max, 2*sleeping_time)
285     pass
286      
287 myStudy = obj._narrow(SALOMEDS.Study)
288
289 if myStudy is None:
290     print "Warning: SALOMEDS.Study has not been created !!!"
291     
292 myStudyName = myStudy._get_Name()