1 # Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License.
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 # File : salome_study.py
23 # Author : Paul RASCLE, EDF
30 from launchConfigureParser import verbose
32 #--------------------------------------------------------------------------
34 def DumpComponent(Study, SO, Builder,offset):
35 it = Study.NewChildIterator(SO)
38 a=offset*"--" + ">" + CSO.GetID()
39 find,AtName = Builder.FindAttribute(CSO, "AttributeName")
41 a=a+":"+AtName.Value()
42 find,AtIOR = Builder.FindAttribute(CSO, "AttributeIOR")
45 find,RefSO = CSO.ReferencedObject()
49 DumpComponent(Study, CSO, Builder,offset+2)
52 #--------------------------------------------------------------------------
56 Dump a study, given the ior
58 itcomp = Study.NewComponentIterator()
59 Builder = Study.NewBuilder()
62 name = SC.ComponentDataType()
63 print "-> ComponentDataType is " + name
64 DumpComponent(Study, SC,Builder, 1)
69 Dump all studies in a StudyManager
71 for name in myStudyManager.GetOpenStudies():
72 s=myStudyManager.GetStudyByName(name)
73 print "study:",name, s._get_StudyId()
77 #--------------------------------------------------------------------------
81 mySO = myStudy.FindObjectID(id);
83 ok, anAttr = mySO.FindAttribute("AttributeIOR")
85 AtIOR = anAttr._narrow(SALOMEDS.AttributeIOR)
86 if AtIOR.Value() != "":
87 myObj = orb.string_to_object(AtIOR.Value())
90 def ObjectToSObject(obj):
93 ior = orb.object_to_string(obj)
95 mySO = myStudy.FindObjectIOR(ior)
99 mySO = ObjectToSObject(obj)
105 mySO = myStudy.FindObjectID(id);
108 #--------------------------------------------------------------------------
110 def generateName(prefix = None):
112 int = whrandom.randint(1,1000);
114 return "Study" + str(int)
116 return prefix + str(int)
118 #--------------------------------------------------------------------------
120 def PersistentPresentation(theStudy, theSO, theWithID):
121 # put the sobject's content (with subchildren) to the string
123 attrs = theSO.GetAllAttributes()
126 for a in range(0,aLen):
128 if isinstance(attr,SALOMEDS._objref_AttributeTreeNode):
130 elif isinstance(attr,SALOMEDS._objref_AttributeTarget):
132 elif isinstance(attr,SALOMEDS._objref_AttributeReal) or \
133 isinstance(attr,SALOMEDS._objref_AttributeInteger) or \
134 isinstance(attr,SALOMEDS._objref_AttributeName) or \
135 isinstance(attr,SALOMEDS._objref_AttributeComment) or \
136 isinstance(attr,SALOMEDS._objref_AttributePersistentRef) or \
137 isinstance(attr,SALOMEDS._objref_AttributeLocalID) or \
138 isinstance(attr,SALOMEDS._objref_AttributeUserID):
139 aResult += " attribute value: " + str(attr.Value())
140 elif isinstance(attr,SALOMEDS._objref_AttributeIOR):
141 aResult += " attribute: IOR"
142 elif isinstance(attr,SALOMEDS._objref_AttributeSequenceOfReal) or \
143 isinstance(attr,SALOMEDS._objref_AttributeSequenceOfInteger):
144 aResult += " Sequence: " + str(attr.CorbaSequence())
145 elif isinstance(attr,SALOMEDS._objref_AttributeDrawable):
146 aResult += " Drawable: " + str(attr.IsDrawable())
147 elif isinstance(attr,SALOMEDS._objref_AttributeSelectable):
148 aResult += " Selectable: " + str(attr.IsSelectable())
149 elif isinstance(attr,SALOMEDS._objref_AttributeExpandable):
150 aResult += " Expandable: " + str(attr.IsExpandable())
151 elif isinstance(attr,SALOMEDS._objref_AttributeOpened):
152 aResult += " Opened: " + str(attr.IsOpened())
153 elif isinstance(attr,SALOMEDS._objref_AttributeTextColor):
154 aResult += " TextColor: " + str(attr.TextColor())
155 elif isinstance(attr,SALOMEDS._objref_AttributeTextHighlightColor):
156 aResult += " TextHighlightColor: " + str(attr.TextHighlightColor())
157 elif isinstance(attr,SALOMEDS._objref_AttributePixMap):
158 aResult += " PixMap: " + str(attr.GetPixMap())
159 elif isinstance(attr,SALOMEDS._objref_AttributeTableOfInteger) or \
160 isinstance(attr,SALOMEDS._objref_AttributeTableOfReal):
161 aResult += " Table with title: " + attr.GetTitle()
162 elif isinstance(attr,SALOMEDS._objref_AttributePythonObject):
163 aResult += " PythonObject: " + attr.GetObject()
166 aResult = "sobject: " + theSO.GetID() + " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
168 aResult = " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
169 anIter = theStudy.NewChildIterator(theSO)
171 aResult += PersistentPresentation(theStudy, anIter.Value(), theWithID)
175 #--------------------------------------------------------------------------
178 # returns the document list tree (as list)
179 aResult = [theSO.GetID()]
180 anIter = myStudy.NewChildIterator(theSO)
182 aResult += GetTree(anIter.Value())
186 #--------------------------------------------------------------------------
188 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 myStudyManager.CanCopy(theSO):
196 raise RuntimeError, "<CanCopy> for "+theInfo+" returns false"
198 if not myStudyManager.Copy(theSO):
199 raise RuntimeError, "<Copy> for "+theInfo+" returns false"
202 if not myStudyManager.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 myStudyManager.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 FindFileInDataDir(filename):
230 datadir = os.getenv("DATA_DIR")
231 if datadir is not None:
233 dirs = string.split(datadir, ":")
235 file = dir + "/" + filename
236 if os.path.exists(file):
238 datadir = os.getenv("KERNEL_ROOT_DIR") + "/examples/"
239 file = datadir + filename
240 if os.path.exists(file):
245 #--------------------------------------------------------------------------
249 def getActiveStudy(theStudyId=0):
250 global salome_study_ID
252 if verbose(): print "getActiveStudy"
253 if salome_study_ID == -1:
254 if salome_iapp.hasDesktop():
255 if verbose(): print "---in gui"
256 salome_study_ID = salome_iapp.sg.getActiveStudyId()
258 if verbose(): print "---outside gui"
260 aStudy=myStudyManager.GetStudyByID(theStudyId)
262 if verbose(): print "connection to existing study ", theStudyId
263 salome_study_ID = theStudyId
264 if salome_study_ID == -1:
265 salome_study_ID = createNewStudy()
266 if verbose(): print"--- Study Id ", salome_study_ID
267 return salome_study_ID
269 #--------------------------------------------------------------------------
271 def createNewStudy():
272 print "createNewStudy"
274 aStudyName = "noName"
276 listOfOpenStudies = myStudyManager.GetOpenStudies()
277 print listOfOpenStudies
278 while nameAlreadyInUse:
279 aStudyName = "extStudy_%d"%i
280 if aStudyName not in listOfOpenStudies:
285 theStudy = myStudyManager.NewStudy(aStudyName)
286 theStudyId = theStudy._get_StudyId()
287 print aStudyName, theStudyId
290 #--------------------------------------------------------------------------
292 salome_study_initial = 1
294 def salome_study_init(theStudyId=0):
296 Performs only once study creation or connection.
297 optional argument : theStudyId
298 When in embedded interpreter inside IAPP, theStudyId is not used
299 When used without GUI (external interpreter)
300 0 : create a new study (default).
301 n (>0) : try connection to study with Id = n, or create a new one
305 global salome_study_initial
306 global myStudyManager, myStudyId, myStudy, myStudyName
307 global orb, lcc, naming_service, cm
309 if salome_study_initial:
310 salome_study_initial = 0
312 orb, lcc, naming_service, cm = salome_kernel.salome_kernel_init()
314 # get Study Manager reference
315 if verbose(): print "looking for studyManager ..."
316 obj = naming_service.Resolve('myStudyManager')
317 myStudyManager = obj._narrow(SALOMEDS.StudyManager)
318 if verbose(): print "studyManager found"
320 # get active study Id, ref and name
321 myStudyId = getActiveStudy(theStudyId)
322 if verbose(): print "myStudyId",myStudyId
323 myStudy = myStudyManager.GetStudyByID(myStudyId)
324 myStudyName = myStudy._get_Name()
326 return myStudyManager, myStudyId, myStudy, myStudyName