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
37 #--------------------------------------------------------------------------
39 def DumpComponent(Study, SO, Builder,offset):
40 it = Study.NewChildIterator(SO)
43 a=offset*"--" + ">" + CSO.GetID()
44 find,AtName = Builder.FindAttribute(CSO, "AttributeName")
46 a=a+":"+AtName.Value()
47 find,AtIOR = Builder.FindAttribute(CSO, "AttributeIOR")
50 find,RefSO = CSO.ReferencedObject()
54 DumpComponent(Study, CSO, Builder,offset+2)
57 #--------------------------------------------------------------------------
61 Dump a study, given the ior
63 itcomp = Study.NewComponentIterator()
64 Builder = Study.NewBuilder()
67 name = SC.ComponentDataType()
68 print "-> ComponentDataType is " + name
69 DumpComponent(Study, SC,Builder, 1)
72 #--------------------------------------------------------------------------
77 mySO = myStudy.FindObjectID(id);
79 ok, anAttr = mySO.FindAttribute("AttributeIOR")
81 AtIOR = anAttr._narrow(SALOMEDS.AttributeIOR)
82 if AtIOR.Value() != "":
83 myObj = orb.string_to_object(AtIOR.Value())
86 def ObjectToSObject(obj):
89 ior = orb.object_to_string(obj)
91 mySO = myStudy.FindObjectIOR(ior)
95 mySO = ObjectToSObject(obj)
102 mySO = myStudy.FindObjectID(id);
105 #--------------------------------------------------------------------------
107 def generateName(prefix = None):
109 int = random.randint(1,1000);
111 return "Study" + str(int)
113 return prefix + str(int)
115 #--------------------------------------------------------------------------
117 def PersistentPresentation(theStudy, theSO, theWithID):
118 # put the sobject's content (with subchildren) to the string
120 attrs = theSO.GetAllAttributes()
123 for a in range(0,aLen):
125 if isinstance(attr,SALOMEDS._objref_AttributeTreeNode):
127 elif isinstance(attr,SALOMEDS._objref_AttributeTarget):
129 elif isinstance(attr,SALOMEDS._objref_AttributeReal) or \
130 isinstance(attr,SALOMEDS._objref_AttributeInteger) or \
131 isinstance(attr,SALOMEDS._objref_AttributeName) or \
132 isinstance(attr,SALOMEDS._objref_AttributeComment) or \
133 isinstance(attr,SALOMEDS._objref_AttributePersistentRef) or \
134 isinstance(attr,SALOMEDS._objref_AttributeLocalID) or \
135 isinstance(attr,SALOMEDS._objref_AttributeUserID):
136 aResult += " attribute value: " + str(attr.Value())
137 elif isinstance(attr,SALOMEDS._objref_AttributeIOR):
138 aResult += " attribute: IOR"
139 elif isinstance(attr,SALOMEDS._objref_AttributeSequenceOfReal) or \
140 isinstance(attr,SALOMEDS._objref_AttributeSequenceOfInteger):
141 aResult += " Sequence: " + str(attr.CorbaSequence())
142 elif isinstance(attr,SALOMEDS._objref_AttributeDrawable):
143 aResult += " Drawable: " + str(attr.IsDrawable())
144 elif isinstance(attr,SALOMEDS._objref_AttributeSelectable):
145 aResult += " Selectable: " + str(attr.IsSelectable())
146 elif isinstance(attr,SALOMEDS._objref_AttributeExpandable):
147 aResult += " Expandable: " + str(attr.IsExpandable())
148 elif isinstance(attr,SALOMEDS._objref_AttributeOpened):
149 aResult += " Opened: " + str(attr.IsOpened())
150 elif isinstance(attr,SALOMEDS._objref_AttributeTextColor):
151 aResult += " TextColor: " + str(attr.TextColor())
152 elif isinstance(attr,SALOMEDS._objref_AttributeTextHighlightColor):
153 aResult += " TextHighlightColor: " + str(attr.TextHighlightColor())
154 elif isinstance(attr,SALOMEDS._objref_AttributePixMap):
155 aResult += " PixMap: " + str(attr.GetPixMap())
156 elif isinstance(attr,SALOMEDS._objref_AttributeTableOfInteger) or \
157 isinstance(attr,SALOMEDS._objref_AttributeTableOfReal):
158 aResult += " Table with title: " + attr.GetTitle()
159 elif isinstance(attr,SALOMEDS._objref_AttributePythonObject):
160 aResult += " PythonObject: " + attr.GetObject()
163 aResult = "sobject: " + theSO.GetID() + " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
165 aResult = " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
166 anIter = theStudy.NewChildIterator(theSO)
168 aResult += PersistentPresentation(theStudy, anIter.Value(), theWithID)
172 #--------------------------------------------------------------------------
175 # returns the document list tree (as list)
177 aResult = [theSO.GetID()]
178 anIter = myStudy.NewChildIterator(theSO)
180 aResult += GetTree(anIter.Value())
184 #--------------------------------------------------------------------------
186 def CheckCopyPaste(theSO, theInfo ,theComponentPaste):
190 while aRoot.GetID() != "0:":
191 aRoot = aRoot.GetFather()
192 aTree = GetTree(aRoot)
193 aStudyPersist = PersistentPresentation(myStudy, aRoot, 1)
195 if not myStudy.CanCopy(theSO):
196 raise RuntimeError, "<CanCopy> for "+theInfo+" returns false"
198 if not myStudy.Copy(theSO):
199 raise RuntimeError, "<Copy> for "+theInfo+" returns false"
202 if not myStudy.CanPaste(theSO):
203 raise RuntimeError, "<CanPaste> for "+theInfo+" returns false"
205 # check: before paste study is not changed check
206 if aStudyPersist != PersistentPresentation(myStudy, aRoot, 1):
207 raise RuntimeError, "Study is changed before Paste calling for "+theInfo
210 if theComponentPaste:
211 aSObj = theSO.GetFatherComponent()
212 theInfo = theInfo + "(paste for component)"
213 if myStudy.Paste(aSObj) == None:
214 raise RuntimeError, "<Paste> for "+theInfo+" returns None object"
215 aNewTree = GetTree(aRoot)
217 for a in range(0,aLen):
218 if aTree[a] != aNewTree[a]:
219 return myStudy.FindObjectID(aNewTree[a])
221 if aLen < len(aNewTree):
222 return myStudy.FindObjectID(aNewTree[aLen])
224 raise RuntimeError, "After Copy calling the tree is not changed"
226 #--------------------------------------------------------------------------
228 def GetComponentVersion(theComponent, all_versions = False):
229 # returns the document list tree (as list)
231 props = myStudy.GetProperties()
232 stored_components = props.GetStoredComponents()
233 version = "no component data" # vsr: better raise an exception in this case?
234 if theComponent in stored_components:
236 version = props.GetComponentVersions(theComponent)
237 for i in range(len(version)):
238 if not version[i]: version[i] = "unknown"
242 version = props.GetComponentVersion(theComponent)
243 if not version: version = "unknown"
248 #--------------------------------------------------------------------------
250 def FindFileInDataDir(filename):
252 datadir = os.getenv("DATA_DIR")
253 if datadir is not None:
255 dirs = string.split(datadir, ":")
257 file = dir + "/" + filename
258 if os.path.exists(file):
260 datadir = os.getenv("KERNEL_ROOT_DIR") + "/examples/"
261 file = datadir + filename
262 if os.path.exists(file):
267 #--------------------------------------------------------------------------
269 def setCurrentStudy(theStudy):
271 Change current study : an existing one given by a study object.
273 :param theStudy: the study CORBA object to set as current study
275 Obsolete: only one study can be opened at the moment.
276 This function works properly if specified theStudy parameter
277 corresponds to the currently opened study.
278 Kept for backward compatibility only.
280 global myStudy, myStudyName
282 myStudyName = theStudy._get_Name()
283 return myStudy, myStudyName
285 #--------------------------------------------------------------------------
287 def openStudy(theStudyPath):
290 theStudy = myStudy.Open(theStudyPath)
291 theStudyName = theStudy._get_Name()
292 print theStudyPath, theStudyName
295 #--------------------------------------------------------------------------
303 #--------------------------------------------------------------------------
305 def salome_study_init(theStudyPath=None):
307 Performs only once study creation or connection.
308 optional argument : theStudyPath
309 None : attach to the currently active single study;
310 create new empty study if there is active study
311 <URL> (str) : open study with the given file name
313 global myStudy, myStudyName
314 global orb, lcc, naming_service, cm
316 if verbose(): print "theStudyPath:", theStudyPath
318 orb, lcc, naming_service, cm = salome_kernel.salome_kernel_init()
320 # get Study reference
321 if verbose(): print "looking for study..."
322 obj = naming_service.Resolve('Study')
323 myStudy = obj._narrow(SALOMEDS.Study)
324 if verbose(): print "Study found"
328 myStudyName = myStudy._get_Name()
330 return myStudy, myStudyName
332 def salome_study_close():
333 global myStudy, myStudyName
334 myStudy, myStudyName = None, None