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