Salome HOME
updated copyright message
[modules/kernel.git] / src / KERNEL_PY / batchmode_salome.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2023  CEA, EDF, 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, globals())
39     constructor = ComponentName + "GUI_Swig()"
40     command = "gui = " + constructor
41     exec (command, globals())
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     aStudyPersist = PersistentPresentation(myStudy, aRoot, 1)
184
185     if not myStudy.CanCopy(theSO):
186         raise RuntimeError("<CanCopy> for "+theInfo+" returns false")
187     
188     if not myStudy.Copy(theSO):
189         raise RuntimeError("<Copy> for "+theInfo+" returns false")
190
191     
192     if not myStudy.CanPaste(theSO):
193         raise RuntimeError("<CanPaste> for "+theInfo+" returns false")
194
195     # check: before paste study is not changed check
196     if aStudyPersist != PersistentPresentation(aRoot, 1):
197         raise RuntimeError("Study is changed before Paste calling for "+theInfo)
198     
199     aSObj = theSO
200     if theComponentPaste:
201         aSObj = theSO.GetFatherComponent()
202         theInfo = theInfo + "(paste for component)"
203     if myStudy.Paste(aSObj) == None:
204         raise RuntimeError("<Paste> for "+theInfo+" returns None object")
205     aNewTree = GetTree(aRoot)
206     aLen = len(aTree)
207     for a in range(0,aLen):
208         if aTree[a] != aNewTree[a]:
209             return myStudy.FindObjectID(aNewTree[a])
210         
211     if aLen < len(aNewTree):
212         return myStudy.FindObjectID(aNewTree[aLen])
213     
214     raise RuntimeError("After Copy calling the tree is not changed")
215     
216     #--------------------------------------------------------------------------
217 def FindFileInDataDir(filename):
218     import os
219     datadir = os.getenv("DATA_DIR")
220     if datadir is not None:
221         import string
222         dirs = string.split(datadir, ":")
223         for dir in dirs:
224             file = dir + "/" + filename
225             if os.path.exists(file):
226                 return file;
227     datadir = os.getenv("KERNEL_ROOT_DIR") + "/examples/"
228     file = datadir + filename
229     if os.path.exists(file):
230         return file;
231
232     return None
233         
234 #--------------------------------------------------------------------------
235 # initialise the ORB
236 orb = None
237
238 step = 0
239 sleeping_time = 0.01
240 sleeping_time_max = 1.0
241 while 1:
242     orb = CORBA.ORB_init([''], CORBA.ORB_ID)
243     if orb is not None: break
244     step = step + 1
245     if step > 100: break
246     time.sleep(sleeping_time)
247     sleeping_time = max(sleeping_time_max, 2*sleeping_time)
248     pass
249     
250 if orb is None:
251     print("Warning: ORB has not been initialized !!!")
252
253 # create an LifeCycleCORBA instance
254 lcc = LifeCycleCORBA(orb)
255
256 step = 0
257 sleeping_time = 0.01
258 sleeping_time_max = 1.0
259 while 1:
260     lcc = LifeCycleCORBA(orb)
261     if lcc is not None: break
262     step = step + 1
263     if step > 100: break
264     time.sleep(sleeping_time)
265     sleeping_time = max(sleeping_time_max, 2*sleeping_time)
266     pass
267     
268 if lcc is None:
269     print("Warning: LifeCycleCORBA object has not been initialized !!!")
270     
271 #create a naming service instance
272 naming_service = SALOME_NamingServicePy_i(orb)
273
274 # get Study reference
275 obj = None
276
277 step = 0
278 sleeping_time = 0.01
279 sleeping_time_max = 1.0
280 while 1:
281     obj = naming_service.Resolve('/Study')
282     if obj is not None:break
283     step = step + 1
284     if step > 100: break
285     time.sleep(sleeping_time)
286     sleeping_time = max(sleeping_time_max, 2*sleeping_time)
287     pass
288     
289 myStudy = obj._narrow(SALOMEDS.Study)
290
291
292 if myStudy is None:
293     print("Warning: SALOMEDS.Study has not been created !!!")
294     
295 myStudyName = myStudy._get_Name()