1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
4 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 # File : salome_study.py
25 # Author : Paul RASCLE, EDF
32 from launchConfigureParser import verbose
39 #--------------------------------------------------------------------------
41 def DumpComponent(Study, SO, Builder,offset):
42 it = Study.NewChildIterator(SO)
45 a=offset*"--" + ">" + CSO.GetID()
46 find,AtName = Builder.FindAttribute(CSO, "AttributeName")
48 a=a+":"+AtName.Value()
49 find,AtIOR = Builder.FindAttribute(CSO, "AttributeIOR")
52 find,RefSO = CSO.ReferencedObject()
56 DumpComponent(Study, CSO, Builder,offset+2)
59 #--------------------------------------------------------------------------
63 Dump a study, given the ior
65 itcomp = Study.NewComponentIterator()
66 Builder = Study.NewBuilder()
69 name = SC.ComponentDataType()
70 print("-> ComponentDataType is " + name)
71 DumpComponent(Study, SC,Builder, 1)
76 Dump all studies in a StudyManager
79 for name in myStudyManager.GetOpenStudies():
80 s = myStudyManager.GetStudyByName(name)
81 print("study:",name, s._get_StudyId())
85 #--------------------------------------------------------------------------
90 mySO = myStudy.FindObjectID(id);
92 ok, anAttr = mySO.FindAttribute("AttributeIOR")
94 AtIOR = anAttr._narrow(SALOMEDS.AttributeIOR)
95 if AtIOR.Value() != "":
96 myObj = orb.string_to_object(AtIOR.Value())
99 def ObjectToSObject(obj):
102 ior = orb.object_to_string(obj)
104 mySO = myStudy.FindObjectIOR(ior)
108 mySO = ObjectToSObject(obj)
115 mySO = myStudy.FindObjectID(id);
118 #--------------------------------------------------------------------------
120 def generateName(prefix = None):
122 int = random.randint(1,1000);
124 return "Study" + str(int)
126 return prefix + str(int)
128 #--------------------------------------------------------------------------
130 def PersistentPresentation(theStudy, theSO, theWithID):
131 # put the sobject's content (with subchildren) to the string
133 attrs = theSO.GetAllAttributes()
136 for a in range(0,aLen):
138 if isinstance(attr,SALOMEDS._objref_AttributeTreeNode):
140 elif isinstance(attr,SALOMEDS._objref_AttributeTarget):
142 elif isinstance(attr,SALOMEDS._objref_AttributeReal) or \
143 isinstance(attr,SALOMEDS._objref_AttributeInteger) or \
144 isinstance(attr,SALOMEDS._objref_AttributeName) or \
145 isinstance(attr,SALOMEDS._objref_AttributeComment) or \
146 isinstance(attr,SALOMEDS._objref_AttributePersistentRef) or \
147 isinstance(attr,SALOMEDS._objref_AttributeLocalID) or \
148 isinstance(attr,SALOMEDS._objref_AttributeUserID):
149 aResult += " attribute value: " + str(attr.Value())
150 elif isinstance(attr,SALOMEDS._objref_AttributeIOR):
151 aResult += " attribute: IOR"
152 elif isinstance(attr,SALOMEDS._objref_AttributeSequenceOfReal) or \
153 isinstance(attr,SALOMEDS._objref_AttributeSequenceOfInteger):
154 aResult += " Sequence: " + str(attr.CorbaSequence())
155 elif isinstance(attr,SALOMEDS._objref_AttributeDrawable):
156 aResult += " Drawable: " + str(attr.IsDrawable())
157 elif isinstance(attr,SALOMEDS._objref_AttributeSelectable):
158 aResult += " Selectable: " + str(attr.IsSelectable())
159 elif isinstance(attr,SALOMEDS._objref_AttributeExpandable):
160 aResult += " Expandable: " + str(attr.IsExpandable())
161 elif isinstance(attr,SALOMEDS._objref_AttributeOpened):
162 aResult += " Opened: " + str(attr.IsOpened())
163 elif isinstance(attr,SALOMEDS._objref_AttributeTextColor):
164 aResult += " TextColor: " + str(attr.TextColor())
165 elif isinstance(attr,SALOMEDS._objref_AttributeTextHighlightColor):
166 aResult += " TextHighlightColor: " + str(attr.TextHighlightColor())
167 elif isinstance(attr,SALOMEDS._objref_AttributePixMap):
168 aResult += " PixMap: " + str(attr.GetPixMap())
169 elif isinstance(attr,SALOMEDS._objref_AttributeTableOfInteger) or \
170 isinstance(attr,SALOMEDS._objref_AttributeTableOfReal):
171 aResult += " Table with title: " + attr.GetTitle()
172 elif isinstance(attr,SALOMEDS._objref_AttributePythonObject):
173 aResult += " PythonObject: " + attr.GetObject()
176 aResult = "sobject: " + theSO.GetID() + " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
178 aResult = " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
179 anIter = theStudy.NewChildIterator(theSO)
181 aResult += PersistentPresentation(theStudy, anIter.Value(), theWithID)
185 #--------------------------------------------------------------------------
188 # returns the document list tree (as list)
190 aResult = [theSO.GetID()]
191 anIter = myStudy.NewChildIterator(theSO)
193 aResult += GetTree(anIter.Value())
197 #--------------------------------------------------------------------------
199 def CheckCopyPaste(theSO, theInfo ,theComponentPaste):
200 global myStudyManager, myStudy
203 while aRoot.GetID() != "0:":
204 aRoot = aRoot.GetFather()
205 aTree = GetTree(aRoot)
206 aStudyPersist = PersistentPresentation(myStudy, aRoot, 1)
208 if not myStudyManager.CanCopy(theSO):
209 raise RuntimeError("<CanCopy> for "+theInfo+" returns false")
211 if not myStudyManager.Copy(theSO):
212 raise RuntimeError("<Copy> for "+theInfo+" returns false")
215 if not myStudyManager.CanPaste(theSO):
216 raise RuntimeError("<CanPaste> for "+theInfo+" returns false")
218 # check: before paste study is not changed check
219 if aStudyPersist != PersistentPresentation(myStudy, aRoot, 1):
220 raise RuntimeError("Study is changed before Paste calling for "+theInfo)
223 if theComponentPaste:
224 aSObj = theSO.GetFatherComponent()
225 theInfo = theInfo + "(paste for component)"
226 if myStudyManager.Paste(aSObj) == None:
227 raise RuntimeError("<Paste> for "+theInfo+" returns None object")
228 aNewTree = GetTree(aRoot)
230 for a in range(0,aLen):
231 if aTree[a] != aNewTree[a]:
232 return myStudy.FindObjectID(aNewTree[a])
234 if aLen < len(aNewTree):
235 return myStudy.FindObjectID(aNewTree[aLen])
237 raise RuntimeError("After Copy calling the tree is not changed")
239 #--------------------------------------------------------------------------
241 def GetComponentVersion(theComponent, all_versions = False):
242 # returns the document list tree (as list)
244 props = myStudy.GetProperties()
245 stored_components = props.GetStoredComponents()
246 version = "no component data" # vsr: better raise an exception in this case?
247 if theComponent in stored_components:
249 version = props.GetComponentVersions(theComponent)
250 for i in range(len(version)):
251 if not version[i]: version[i] = "unknown"
255 version = props.GetComponentVersion(theComponent)
256 if not version: version = "unknown"
261 #--------------------------------------------------------------------------
263 def FindFileInDataDir(filename):
265 datadir = os.getenv("DATA_DIR")
266 if datadir is not None:
268 dirs = string.split(datadir, ":")
270 file = dir + "/" + filename
271 if os.path.exists(file):
273 datadir = os.getenv("KERNEL_ROOT_DIR") + "/examples/"
274 file = datadir + filename
275 if os.path.exists(file):
280 #--------------------------------------------------------------------------
284 # *args are used here to support backward compatibility
285 # previously it was possible to pass theStudyId parameter to this function
286 # which is no more supported.
287 def getActiveStudy(*args):
288 global myStudyManager
289 global salome_study_ID
291 if not myStudyManager:
292 print("No active study")
296 if verbose(): print("getActiveStudy")
297 if salome_study_ID == -1:
298 listOpenStudies = myStudyManager.GetOpenStudies()
299 if len(listOpenStudies) == 0:
302 print(listOpenStudies[0])
303 s = myStudyManager.GetStudyByName(listOpenStudies[0])
304 salome_study_ID = s._get_StudyId()
305 if verbose(): print("--- Study Id ", salome_study_ID)
306 return salome_study_ID
308 #--------------------------------------------------------------------------
310 def setCurrentStudy(theStudy):
312 Change current study : an existing one given by a study object.
314 :param theStudy: the study CORBA object to set as current study
316 Obsolete: only one study can be opened at the moment.
317 This function works properly if specified theStudy parameter
318 corresponds to the currently opened study.
319 Kept for backward compatibility only.
321 global myStudyId, myStudy, myStudyName
322 global salome_study_ID
324 myStudyId = theStudy._get_StudyId()
325 myStudyName = theStudy._get_Name()
326 return myStudyId, myStudy, myStudyName
328 #--------------------------------------------------------------------------
330 # *args are used here to support backward compatibility
331 # previously it was possible to pass theStudyId parameter to this function
332 # which is no more supported.
333 def setCurrentStudyId(*args):
335 Change current study : an existing or new one.
337 This function connects to the single opened study if there is any; otherwise
338 new empty study is created.
340 Obsolete: only one study can be opened at the moment.
341 Kept for backward compatibility only.
343 global myStudyManager, myStudyId, myStudy, myStudyName
344 global salome_study_ID
346 myStudyId = getActiveStudy()
348 myStudyId = createNewStudy()
349 if verbose(): print("myStudyId",myStudyId)
350 myStudy = myStudyManager.GetStudyByID(myStudyId)
351 myStudyName = myStudy._get_Name()
352 return myStudyId, myStudy, myStudyName
354 #--------------------------------------------------------------------------
356 def createNewStudy():
357 global myStudyManager
358 print("createNewStudy")
359 aStudyName = "étudeExt"
360 theStudy = myStudyManager.NewStudy(aStudyName)
361 theStudyId = theStudy._get_StudyId()
362 print(aStudyName, theStudyId)
365 #--------------------------------------------------------------------------
367 def openStudy(theStudyPath):
368 global myStudyManager
370 theStudy = myStudyManager.Open(theStudyPath)
371 theStudyId = theStudy._get_StudyId()
372 print(theStudyPath, theStudyId)
375 #--------------------------------------------------------------------------
377 def salome_study_init(theStudyPath=None):
379 Performs only once study creation or connection.
380 optional argument : theStudyPath
381 None : attach to the currently active single study;
382 create new empty study if there is active study
383 <URL> (str) : open study with the given file name
385 global myStudyManager, myStudyId, myStudy, myStudyName
386 global orb, lcc, naming_service, cm
388 if verbose(): print("theStudyPath:", theStudyPath)
389 if not myStudyManager:
390 orb, lcc, naming_service, cm = salome_kernel.salome_kernel_init()
392 # get Study Manager reference
393 if verbose(): print("looking for studyManager ...")
394 obj = naming_service.Resolve('myStudyManager')
395 myStudyManager = obj._narrow(SALOMEDS.StudyManager)
396 if verbose(): print("studyManager found")
399 # get active study Id, ref and name
401 myStudyId = getActiveStudy()
402 if myStudyId == None :
403 if theStudyPath and isinstance(theStudyPath, (str, bytes)):
404 if isinstance(theStudyPath, bytes):
405 theStudyPath = str(theStudyPath, 'UTF8')
406 myStudyId = openStudy(theStudyPath)
408 myStudyId = createNewStudy()
409 if verbose(): print("myStudyId", myStudyId)
412 myStudy = myStudyManager.GetStudyByID(myStudyId)
413 myStudyName = myStudy._get_Name()
415 return myStudyManager, myStudyId, myStudy, myStudyName
417 def salome_study_close():
418 global salome_study_ID
419 global myStudyId, myStudy, myStudyName
421 myStudyId, myStudy, myStudyName = None, None, None