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