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 s = myStudyManager.GetStudyByName(listOpenStudies[0])
303 salome_study_ID = s._get_StudyId()
304 if verbose(): print("--- Study Id ", salome_study_ID)
305 return salome_study_ID
307 #--------------------------------------------------------------------------
309 def setCurrentStudy(theStudy):
311 Change current study : an existing one given by a study object.
313 :param theStudy: the study CORBA object to set as current study
315 Obsolete: only one study can be opened at the moment.
316 This function works properly if specified theStudy parameter
317 corresponds to the currently opened study.
318 Kept for backward compatibility only.
320 global myStudyId, myStudy, myStudyName
321 global salome_study_ID
323 myStudyId = theStudy._get_StudyId()
324 myStudyName = theStudy._get_Name()
325 return myStudyId, myStudy, myStudyName
327 #--------------------------------------------------------------------------
329 # *args are used here to support backward compatibility
330 # previously it was possible to pass theStudyId parameter to this function
331 # which is no more supported.
332 def setCurrentStudyId(*args):
334 Change current study : an existing or new one.
336 This function connects to the single opened study if there is any; otherwise
337 new empty study is created.
339 Obsolete: only one study can be opened at the moment.
340 Kept for backward compatibility only.
342 global myStudyManager, myStudyId, myStudy, myStudyName
343 global salome_study_ID
345 myStudyId = getActiveStudy()
347 myStudyId = createNewStudy()
348 if verbose(): print("myStudyId",myStudyId)
349 myStudy = myStudyManager.GetStudyByID(myStudyId)
350 myStudyName = myStudy._get_Name()
351 return myStudyId, myStudy, myStudyName
353 #--------------------------------------------------------------------------
355 def createNewStudy():
356 global myStudyManager
357 print("createNewStudy")
358 aStudyName = "extStudy"
359 theStudy = myStudyManager.NewStudy(aStudyName)
360 theStudyId = theStudy._get_StudyId()
361 print(aStudyName, theStudyId)
364 #--------------------------------------------------------------------------
366 def openStudy(theStudyPath):
367 global myStudyManager
369 theStudy = myStudyManager.Open(theStudyPath)
370 theStudyId = theStudy._get_StudyId()
371 print(theStudyPath, theStudyId)
374 #--------------------------------------------------------------------------
376 def salome_study_init(theStudyPath=None):
378 Performs only once study creation or connection.
379 optional argument : theStudyPath
380 None : attach to the currently active single study;
381 create new empty study if there is active study
382 <URL> (str) : open study with the given file name
384 global myStudyManager, myStudyId, myStudy, myStudyName
385 global orb, lcc, naming_service, cm
387 if verbose(): print("theStudyPath:", theStudyPath)
388 if not myStudyManager:
389 orb, lcc, naming_service, cm = salome_kernel.salome_kernel_init()
391 # get Study Manager reference
392 if verbose(): print("looking for studyManager ...")
393 obj = naming_service.Resolve('myStudyManager')
394 myStudyManager = obj._narrow(SALOMEDS.StudyManager)
395 if verbose(): print("studyManager found")
398 # get active study Id, ref and name
400 myStudyId = getActiveStudy()
401 if myStudyId == None :
403 if theStudyPath and type(theStudyPath) == bytes:
404 myStudyId = openStudy(theStudyPath)
406 myStudyId = createNewStudy()
407 if verbose(): print("myStudyId", myStudyId)
410 myStudy = myStudyManager.GetStudyByID(myStudyId)
411 myStudyName = myStudy._get_Name()
413 return myStudyManager, myStudyId, myStudy, myStudyName
415 def salome_study_close():
416 global salome_study_ID
417 global myStudyId, myStudy, myStudyName
419 myStudyId, myStudy, myStudyName = None, None, None