2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
5 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License.
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 # 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
34 #--------------------------------------------------------------------------
36 def DumpComponent(Study, SO, Builder,offset):
37 it = Study.NewChildIterator(SO)
40 a=offset*"--" + ">" + CSO.GetID()
41 find,AtName = Builder.FindAttribute(CSO, "AttributeName")
43 a=a+":"+AtName.Value()
44 find,AtIOR = Builder.FindAttribute(CSO, "AttributeIOR")
47 find,RefSO = CSO.ReferencedObject()
51 DumpComponent(Study, CSO, Builder,offset+2)
54 #--------------------------------------------------------------------------
58 Dump a study, given the ior
60 itcomp = Study.NewComponentIterator()
61 Builder = Study.NewBuilder()
64 name = SC.ComponentDataType()
65 print "-> ComponentDataType is " + name
66 DumpComponent(Study, SC,Builder, 1)
71 Dump all studies in a StudyManager
73 for name in myStudyManager.GetOpenStudies():
74 s=myStudyManager.GetStudyByName(name)
75 print "study:",name, s._get_StudyId()
79 #--------------------------------------------------------------------------
83 mySO = myStudy.FindObjectID(id);
85 ok, anAttr = mySO.FindAttribute("AttributeIOR")
87 AtIOR = anAttr._narrow(SALOMEDS.AttributeIOR)
88 if AtIOR.Value() != "":
89 myObj = orb.string_to_object(AtIOR.Value())
92 def ObjectToSObject(obj):
95 ior = orb.object_to_string(obj)
97 mySO = myStudy.FindObjectIOR(ior)
101 mySO = ObjectToSObject(obj)
107 mySO = myStudy.FindObjectID(id);
110 #--------------------------------------------------------------------------
112 def generateName(prefix = None):
114 int = whrandom.randint(1,1000);
116 return "Study" + str(int)
118 return prefix + str(int)
120 #--------------------------------------------------------------------------
122 def PersistentPresentation(theStudy, theSO, theWithID):
123 # put the sobject's content (with subchildren) to the string
125 attrs = theSO.GetAllAttributes()
128 for a in range(0,aLen):
130 if isinstance(attr,SALOMEDS._objref_AttributeTreeNode):
132 elif isinstance(attr,SALOMEDS._objref_AttributeTarget):
134 elif isinstance(attr,SALOMEDS._objref_AttributeReal) or \
135 isinstance(attr,SALOMEDS._objref_AttributeInteger) or \
136 isinstance(attr,SALOMEDS._objref_AttributeName) or \
137 isinstance(attr,SALOMEDS._objref_AttributeComment) or \
138 isinstance(attr,SALOMEDS._objref_AttributePersistentRef) or \
139 isinstance(attr,SALOMEDS._objref_AttributeLocalID) or \
140 isinstance(attr,SALOMEDS._objref_AttributeUserID):
141 aResult += " attribute value: " + str(attr.Value())
142 elif isinstance(attr,SALOMEDS._objref_AttributeIOR):
143 aResult += " attribute: IOR"
144 elif isinstance(attr,SALOMEDS._objref_AttributeSequenceOfReal) or \
145 isinstance(attr,SALOMEDS._objref_AttributeSequenceOfInteger):
146 aResult += " Sequence: " + str(attr.CorbaSequence())
147 elif isinstance(attr,SALOMEDS._objref_AttributeDrawable):
148 aResult += " Drawable: " + str(attr.IsDrawable())
149 elif isinstance(attr,SALOMEDS._objref_AttributeSelectable):
150 aResult += " Selectable: " + str(attr.IsSelectable())
151 elif isinstance(attr,SALOMEDS._objref_AttributeExpandable):
152 aResult += " Expandable: " + str(attr.IsExpandable())
153 elif isinstance(attr,SALOMEDS._objref_AttributeOpened):
154 aResult += " Opened: " + str(attr.IsOpened())
155 elif isinstance(attr,SALOMEDS._objref_AttributeTextColor):
156 aResult += " TextColor: " + str(attr.TextColor())
157 elif isinstance(attr,SALOMEDS._objref_AttributeTextHighlightColor):
158 aResult += " TextHighlightColor: " + str(attr.TextHighlightColor())
159 elif isinstance(attr,SALOMEDS._objref_AttributePixMap):
160 aResult += " PixMap: " + str(attr.GetPixMap())
161 elif isinstance(attr,SALOMEDS._objref_AttributeTableOfInteger) or \
162 isinstance(attr,SALOMEDS._objref_AttributeTableOfReal):
163 aResult += " Table with title: " + attr.GetTitle()
164 elif isinstance(attr,SALOMEDS._objref_AttributePythonObject):
165 aResult += " PythonObject: " + attr.GetObject()
168 aResult = "sobject: " + theSO.GetID() + " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
170 aResult = " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
171 anIter = theStudy.NewChildIterator(theSO)
173 aResult += PersistentPresentation(theStudy, anIter.Value(), theWithID)
177 #--------------------------------------------------------------------------
180 # returns the document list tree (as list)
181 aResult = [theSO.GetID()]
182 anIter = myStudy.NewChildIterator(theSO)
184 aResult += GetTree(anIter.Value())
188 #--------------------------------------------------------------------------
190 def CheckCopyPaste(theSO, theInfo ,theComponentPaste):
192 while aRoot.GetID() != "0:":
193 aRoot = aRoot.GetFather()
194 aTree = GetTree(aRoot)
195 aStudyPersist = PersistentPresentation(myStudy, aRoot, 1)
197 if not myStudyManager.CanCopy(theSO):
198 raise RuntimeError, "<CanCopy> for "+theInfo+" returns false"
200 if not myStudyManager.Copy(theSO):
201 raise RuntimeError, "<Copy> for "+theInfo+" returns false"
204 if not myStudyManager.CanPaste(theSO):
205 raise RuntimeError, "<CanPaste> for "+theInfo+" returns false"
207 # check: before paste study is not changed check
208 if aStudyPersist != PersistentPresentation(myStudy, aRoot, 1):
209 raise RuntimeError, "Study is changed before Paste calling for "+theInfo
212 if theComponentPaste:
213 aSObj = theSO.GetFatherComponent()
214 theInfo = theInfo + "(paste for component)"
215 if myStudyManager.Paste(aSObj) == None:
216 raise RuntimeError, "<Paste> for "+theInfo+" returns None object"
217 aNewTree = GetTree(aRoot)
219 for a in range(0,aLen):
220 if aTree[a] != aNewTree[a]:
221 return myStudy.FindObjectID(aNewTree[a])
223 if aLen < len(aNewTree):
224 return myStudy.FindObjectID(aNewTree[aLen])
226 raise RuntimeError, "After Copy calling the tree is not changed"
228 #--------------------------------------------------------------------------
230 def FindFileInDataDir(filename):
232 datadir = os.getenv("DATA_DIR")
233 if datadir is not None:
235 dirs = string.split(datadir, ":")
237 file = dir + "/" + filename
238 if os.path.exists(file):
240 datadir = os.getenv("KERNEL_ROOT_DIR") + "/examples/"
241 file = datadir + filename
242 if os.path.exists(file):
247 #--------------------------------------------------------------------------
251 def getActiveStudy(theStudyId=0):
252 global salome_study_ID
254 if verbose(): print "getActiveStudy"
255 if salome_study_ID == -1:
256 if salome_iapp.hasDesktop():
257 if verbose(): print "---in gui"
258 salome_study_ID = salome_iapp.sg.getActiveStudyId()
260 if verbose(): print "---outside gui"
262 aStudy=myStudyManager.GetStudyByID(theStudyId)
264 if verbose(): print "connection to existing study ", theStudyId
265 salome_study_ID = theStudyId
266 if salome_study_ID == -1:
267 salome_study_ID = createNewStudy()
268 if verbose(): print"--- Study Id ", salome_study_ID
269 return salome_study_ID
271 #--------------------------------------------------------------------------
273 def createNewStudy():
274 print "createNewStudy"
276 aStudyName = "noName"
278 listOfOpenStudies = myStudyManager.GetOpenStudies()
279 print listOfOpenStudies
280 while nameAlreadyInUse:
281 aStudyName = "extStudy_%d"%i
282 if aStudyName not in listOfOpenStudies:
287 theStudy = myStudyManager.NewStudy(aStudyName)
288 theStudyId = theStudy._get_StudyId()
289 print aStudyName, theStudyId
292 #--------------------------------------------------------------------------
294 salome_study_initial = 1
296 def salome_study_init(theStudyId=0):
298 Performs only once study creation or connection.
299 optional argument : theStudyId
300 When in embedded interpreter inside IAPP, theStudyId is not used
301 When used without GUI (external interpreter)
302 0 : create a new study (default).
303 n (>0) : try connection to study with Id = n, or create a new one
307 global salome_study_initial
308 global myStudyManager, myStudyId, myStudy, myStudyName
309 global orb, lcc, naming_service, cm
311 if salome_study_initial:
312 salome_study_initial = 0
314 orb, lcc, naming_service, cm = salome_kernel.salome_kernel_init()
316 # get Study Manager reference
317 if verbose(): print "looking for studyManager ..."
318 obj = naming_service.Resolve('myStudyManager')
319 myStudyManager = obj._narrow(SALOMEDS.StudyManager)
320 if verbose(): print "studyManager found"
322 # get active study Id, ref and name
323 myStudyId = getActiveStudy(theStudyId)
324 if verbose(): print "myStudyId",myStudyId
325 myStudy = myStudyManager.GetStudyByID(myStudyId)
326 myStudyName = myStudy._get_Name()
328 return myStudyManager, myStudyId, myStudy, myStudyName