Salome HOME
Merge from V6_main 01/04/2013
[modules/kernel.git] / src / KERNEL_PY / batchmode_salome.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2013  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.
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 #import salome_shared_modules
27 #
28 from omniORB import CORBA
29 from LifeCycleCORBA import *
30 import SALOME
31 import SALOMEDS
32 from SALOME_NamingServicePy import *
33
34 #--------------------------------------------------------------------------
35
36 def ImportComponentGUI(ComponentName):
37     libName = "lib" + ComponentName + "_Swig"
38     command = "from " + libName + " import *"
39     exec ( command )
40     constructor = ComponentName + "GUI_Swig()"
41     command = "gui = " + constructor
42     exec ( command )
43     return gui
44
45     #--------------------------------------------------------------------------
46
47 def SalomeGUIgetAllSelected(self):
48     selNumber = self.SelectedCount()
49     listSelected = []
50     for i in range(selNumber):
51         listSelected.append(self.getSelected(i))
52     return listSelected
53     #--------------------------------------------------------------------------
54
55 def generateName(prefix = None):
56     import random;
57     int = random.randint(1,1000);
58     if prefix is None:
59         return "Study" + str(int)
60     else :
61         return prefix + str(int)
62
63 #WITHOUTIHMfrom libSALOME_Swig import *
64 ###from SalomePyQt import *
65 #WITHOUTIHMclass SalomeGUI(SALOMEGUI_Swig):
66     #WITHOUTIHMgetAllSelected = SalomeGUIgetAllSelected
67    
68     #WITHOUTIHMdef getDesktop(self) :
69 #       return SalomePyQt.getDesktop()
70         #WITHOUTIHMreturn None
71
72     #WITHOUTIHMdef getSelection(self) :
73 #       return SalomePyQt.getSelection()
74         #WITHOUTIHMreturn None
75
76     #--------------------------------------------------------------------------
77
78 def IDToObject(id):
79     myObj = None
80     mySO = myStudy.FindObjectID(id);
81     if mySO is not None:
82         ok, anAttr = mySO.FindAttribute("AttributeIOR")
83         if ok:
84             AtIOR = anAttr._narrow(SALOMEDS.AttributeIOR)
85             if AtIOR.Value() != "":
86                 myObj = orb.string_to_object(AtIOR.Value())
87     return myObj
88
89 def ObjectToSObject(obj):
90     mySO = None
91     if obj is not None:
92         ior =  orb.object_to_string(obj)
93         if ior != "":
94             mySO = myStudy.FindObjectIOR(ior)
95     return mySO
96
97 def ObjectToID(obj):
98     mySO = ObjectToSObject(obj)
99     if mySO:
100         return mySO.GetID()
101     return ""
102
103 def IDToSObject(id):
104     mySO = myStudy.FindObjectID(id);
105     return mySO
106
107     #--------------------------------------------------------------------------
108
109 def PersistentPresentation(theStudy, theSO, theWithID):
110     # put the sobject's content (with subchildren) to the string
111     aResult = ""
112     attrs = theSO.GetAllAttributes()
113     aLen = len(attrs)
114     anUncopied = 0
115     for a in range(0,aLen):
116         attr = attrs[a]
117         if isinstance(attr,SALOMEDS._objref_AttributeTreeNode):
118             anUncopied += 1
119         elif isinstance(attr,SALOMEDS._objref_AttributeTarget):
120             anUncopied += 1
121         elif isinstance(attr,SALOMEDS._objref_AttributeReal) or \
122              isinstance(attr,SALOMEDS._objref_AttributeInteger) or \
123              isinstance(attr,SALOMEDS._objref_AttributeName) or \
124              isinstance(attr,SALOMEDS._objref_AttributeComment) or \
125              isinstance(attr,SALOMEDS._objref_AttributePersistentRef) or \
126              isinstance(attr,SALOMEDS._objref_AttributeLocalID) or \
127              isinstance(attr,SALOMEDS._objref_AttributeUserID):
128             aResult += " attribute value: " + str(attr.Value())
129         elif isinstance(attr,SALOMEDS._objref_AttributeIOR):
130             aResult += " attribute: IOR"
131         elif isinstance(attr,SALOMEDS._objref_AttributeSequenceOfReal) or \
132              isinstance(attr,SALOMEDS._objref_AttributeSequenceOfInteger):
133             aResult += " Sequence: " + str(attr.CorbaSequence())
134         elif isinstance(attr,SALOMEDS._objref_AttributeDrawable):
135             aResult += " Drawable: " + str(attr.IsDrawable())
136         elif isinstance(attr,SALOMEDS._objref_AttributeSelectable):
137             aResult += " Selectable: " + str(attr.IsSelectable())
138         elif isinstance(attr,SALOMEDS._objref_AttributeExpandable):
139             aResult += " Expandable: " + str(attr.IsExpandable())
140         elif isinstance(attr,SALOMEDS._objref_AttributeOpened):
141             aResult += " Opened: " + str(attr.IsOpened())
142         elif isinstance(attr,SALOMEDS._objref_AttributeTextColor):
143             aResult += " TextColor: " + str(attr.TextColor())
144         elif isinstance(attr,SALOMEDS._objref_AttributeTextHighlightColor):
145             aResult += " TextHighlightColor: " + str(attr.TextHighlightColor())
146         elif isinstance(attr,SALOMEDS._objref_AttributePixMap):
147             aResult += " PixMap: " + str(attr.GetPixMap())
148         elif isinstance(attr,SALOMEDS._objref_AttributeTableOfInteger) or \
149              isinstance(attr,SALOMEDS._objref_AttributeTableOfReal):
150             aResult += " Table with title: " + attr.GetTitle()
151         elif isinstance(attr,SALOMEDS._objref_AttributePythonObject):
152             aResult += " PythonObject: " + attr.GetObject()
153
154     if theWithID:
155         aResult = "sobject: " + theSO.GetID() + " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
156     else:
157         aResult = " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
158     anIter = theStudy.NewChildIterator(theSO)
159     while anIter.More():
160         aResult += PersistentPresentation(theStudy, anIter.Value(), theWithID)
161         anIter.Next()
162     return aResult
163
164     #--------------------------------------------------------------------------
165
166 def GetTree(theSO):
167     # returns the document list tree (as list)
168     aResult = [theSO.GetID()]
169     anIter = myStudy.NewChildIterator(theSO)
170     while anIter.More():
171         aResult += GetTree(anIter.Value())
172         anIter.Next()
173     return aResult
174
175     #--------------------------------------------------------------------------
176
177 def CheckCopyPaste(theSO, theInfo ,theComponentPaste):
178     aRoot = theSO
179     while aRoot.GetID() != "0:":
180         aRoot = aRoot.GetFather()
181     aTree = GetTree(aRoot)
182     aStudyPersist = PersistentPresentation(myStudy, aRoot, 1)
183
184     if not myStudyManager.CanCopy(theSO):
185         raise RuntimeError, "<CanCopy> for "+theInfo+" returns false"
186     
187     if not myStudyManager.Copy(theSO):
188         raise RuntimeError, "<Copy> for "+theInfo+" returns false"
189
190     
191     if not myStudyManager.CanPaste(theSO):
192         raise RuntimeError, "<CanPaste> for "+theInfo+" returns false"
193
194     # check: before paste study is not changed check
195     if aStudyPersist != PersistentPresentation(myStudy, aRoot, 1):
196         raise RuntimeError, "Study is changed before Paste calling for "+theInfo
197     
198     aSObj = theSO
199     if theComponentPaste:
200         aSObj = theSO.GetFatherComponent()
201         theInfo = theInfo + "(paste for component)"
202     if myStudyManager.Paste(aSObj) == None:
203         raise RuntimeError, "<Paste> for "+theInfo+" returns None object"
204     aNewTree = GetTree(aRoot)
205     aLen = len(aTree)
206     for a in range(0,aLen):
207         if aTree[a] != aNewTree[a]:
208             return myStudy.FindObjectID(aNewTree[a])
209         
210     if aLen < len(aNewTree):
211         return myStudy.FindObjectID(aNewTree[aLen])
212     
213     raise RuntimeError, "After Copy calling the tree is not changed"
214     
215     #--------------------------------------------------------------------------
216 def FindFileInDataDir(filename):
217     import os
218     datadir = os.getenv("DATA_DIR")
219     if datadir is not None:
220         import string
221         dirs = string.split(datadir, ":")
222         for dir in dirs:
223             file = dir + "/" + filename
224             if os.path.exists(file):
225                 return file;
226     datadir = os.getenv("KERNEL_ROOT_DIR") + "/examples/"
227     file = datadir + filename
228     if os.path.exists(file):
229         return file;
230
231     return None
232         
233 #--------------------------------------------------------------------------
234 # initialise the ORB
235 orb = None
236
237 step = 0
238 sleeping_time = 0.01
239 sleeping_time_max = 1.0
240 while 1:
241     orb = CORBA.ORB_init([''], CORBA.ORB_ID)
242     if orb is not None: break
243     step = step + 1
244     if step > 100: break
245     time.sleep(sleeping_time)
246     sleeping_time = max(sleeping_time_max, 2*sleeping_time)
247     pass
248     
249 if orb is None:
250     print "Warning: ORB has not been initialized !!!"
251
252 # create an LifeCycleCORBA instance
253 lcc = LifeCycleCORBA(orb)
254
255 step = 0
256 sleeping_time = 0.01
257 sleeping_time_max = 1.0
258 while 1:
259     lcc = LifeCycleCORBA(orb)
260     if lcc is not None: break
261     step = step + 1
262     if step > 100: break
263     time.sleep(sleeping_time)
264     sleeping_time = max(sleeping_time_max, 2*sleeping_time)
265     pass
266     
267 if lcc is None:
268     print "Warning: LifeCycleCORBA object has not been initialized !!!"
269     
270 #create a naming service instance
271 naming_service = SALOME_NamingServicePy_i(orb)
272
273 # get Study Manager reference
274 obj = None
275
276 step = 0
277 sleeping_time = 0.01
278 sleeping_time_max = 1.0
279 while 1:
280     obj = naming_service.Resolve('myStudyManager')
281     if obj is not None:break
282     step = step + 1
283     if step > 100: break
284     time.sleep(sleeping_time)
285     sleeping_time = max(sleeping_time_max, 2*sleeping_time)
286     pass
287      
288 myStudyManager = obj._narrow(SALOMEDS.StudyManager)
289
290 if myStudyManager is None:
291     print "Warning: SALOMEDS.StudyManager has not been created !!!"
292
293 # create new study
294 aListOfOpenStudies = myStudyManager.GetOpenStudies();
295 myStudy = None;
296 if len(aListOfOpenStudies) == 0 :
297     myStudy = myStudyManager.NewStudy("Study1")
298 else:
299     myStudyName = aListOfOpenStudies[0]
300     myStudy = myStudyManager.GetStudyByName(myStudyName)
301     
302 myStudyName = myStudy._get_Name()
303
304 myStudyId = myStudy._get_StudyId()
305 #print myStudyId
306