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