1 # Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # 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, offset):
35 it = Study.NewChildIterator(SO)
36 Builder = Study.NewBuilder()
40 anAttr = Builder.FindOrCreateAttribute(CSO, "AttributeName")
41 AtName = anAttr._narrow(SALOMEDS.AttributeName)
42 t_name = AtName.Value()
49 MESSAGE( a + ">" + str(CSO.GetID()) + " " + str(t_name[1]) )
50 t_RefSO = CSO.ReferencedObject()
58 MESSAGE( a + ">" + str(RefSO.GetID()) )
59 DumpComponent(Study, CSO, offset+2)
61 #--------------------------------------------------------------------------
65 Dump a study, given the ior
67 itcomp = Study.NewComponentIterator()
71 name = SC.ComponentDataType()
72 MESSAGE( "-> ComponentDataType is " + name )
73 DumpComponent(Study, SC, 1)
76 #--------------------------------------------------------------------------
80 mySO = myStudy.FindObjectID(id);
82 ok, anAttr = mySO.FindAttribute("AttributeIOR")
84 AtIOR = anAttr._narrow(SALOMEDS.AttributeIOR)
85 if AtIOR.Value() != "":
86 myObj = orb.string_to_object(AtIOR.Value())
89 def ObjectToSObject(obj):
92 ior = orb.object_to_string(obj)
94 mySO = myStudy.FindObjectIOR(ior)
98 mySO = ObjectToSObject(obj)
104 mySO = myStudy.FindObjectID(id);
107 #--------------------------------------------------------------------------
109 def generateName(prefix = None):
111 int = whrandom.randint(1,1000);
113 return "Study" + str(int)
115 return prefix + str(int)
117 #--------------------------------------------------------------------------
119 def PersistentPresentation(theStudy, theSO, theWithID):
120 # put the sobject's content (with subchildren) to the string
122 attrs = theSO.GetAllAttributes()
125 for a in range(0,aLen):
127 if isinstance(attr,SALOMEDS._objref_AttributeTreeNode):
129 elif isinstance(attr,SALOMEDS._objref_AttributeTarget):
131 elif isinstance(attr,SALOMEDS._objref_AttributeReal) or \
132 isinstance(attr,SALOMEDS._objref_AttributeInteger) or \
133 isinstance(attr,SALOMEDS._objref_AttributeName) or \
134 isinstance(attr,SALOMEDS._objref_AttributeComment) or \
135 isinstance(attr,SALOMEDS._objref_AttributePersistentRef) or \
136 isinstance(attr,SALOMEDS._objref_AttributeLocalID) or \
137 isinstance(attr,SALOMEDS._objref_AttributeUserID):
138 aResult += " attribute value: " + str(attr.Value())
139 elif isinstance(attr,SALOMEDS._objref_AttributeIOR):
140 aResult += " attribute: IOR"
141 elif isinstance(attr,SALOMEDS._objref_AttributeSequenceOfReal) or \
142 isinstance(attr,SALOMEDS._objref_AttributeSequenceOfInteger):
143 aResult += " Sequence: " + str(attr.CorbaSequence())
144 elif isinstance(attr,SALOMEDS._objref_AttributeDrawable):
145 aResult += " Drawable: " + str(attr.IsDrawable())
146 elif isinstance(attr,SALOMEDS._objref_AttributeSelectable):
147 aResult += " Selectable: " + str(attr.IsSelectable())
148 elif isinstance(attr,SALOMEDS._objref_AttributeExpandable):
149 aResult += " Expandable: " + str(attr.IsExpandable())
150 elif isinstance(attr,SALOMEDS._objref_AttributeOpened):
151 aResult += " Opened: " + str(attr.IsOpened())
152 elif isinstance(attr,SALOMEDS._objref_AttributeTextColor):
153 aResult += " TextColor: " + str(attr.TextColor())
154 elif isinstance(attr,SALOMEDS._objref_AttributeTextHighlightColor):
155 aResult += " TextHighlightColor: " + str(attr.TextHighlightColor())
156 elif isinstance(attr,SALOMEDS._objref_AttributePixMap):
157 aResult += " PixMap: " + str(attr.GetPixMap())
158 elif isinstance(attr,SALOMEDS._objref_AttributeTableOfInteger) or \
159 isinstance(attr,SALOMEDS._objref_AttributeTableOfReal):
160 aResult += " Table with title: " + attr.GetTitle()
161 elif isinstance(attr,SALOMEDS._objref_AttributePythonObject):
162 aResult += " PythonObject: " + attr.GetObject()
165 aResult = "sobject: " + theSO.GetID() + " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
167 aResult = " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
168 anIter = theStudy.NewChildIterator(theSO)
170 aResult += PersistentPresentation(theStudy, anIter.Value(), theWithID)
174 #--------------------------------------------------------------------------
177 # returns the document list tree (as list)
178 aResult = [theSO.GetID()]
179 anIter = myStudy.NewChildIterator(theSO)
181 aResult += GetTree(anIter.Value())
185 #--------------------------------------------------------------------------
187 def CheckCopyPaste(theSO, theInfo ,theComponentPaste):
189 while aRoot.GetID() != "0:":
190 aRoot = aRoot.GetFather()
191 aTree = GetTree(aRoot)
192 aStudyPersist = PersistentPresentation(myStudy, aRoot, 1)
194 if not myStudyManager.CanCopy(theSO):
195 raise RuntimeError, "<CanCopy> for "+theInfo+" returns false"
197 if not myStudyManager.Copy(theSO):
198 raise RuntimeError, "<Copy> for "+theInfo+" returns false"
201 if not myStudyManager.CanPaste(theSO):
202 raise RuntimeError, "<CanPaste> for "+theInfo+" returns false"
204 # check: before paste study is not changed check
205 if aStudyPersist != PersistentPresentation(myStudy, aRoot, 1):
206 raise RuntimeError, "Study is changed before Paste calling for "+theInfo
209 if theComponentPaste:
210 aSObj = theSO.GetFatherComponent()
211 theInfo = theInfo + "(paste for component)"
212 if myStudyManager.Paste(aSObj) == None:
213 raise RuntimeError, "<Paste> for "+theInfo+" returns None object"
214 aNewTree = GetTree(aRoot)
216 for a in range(0,aLen):
217 if aTree[a] != aNewTree[a]:
218 return myStudy.FindObjectID(aNewTree[a])
220 if aLen < len(aNewTree):
221 return myStudy.FindObjectID(aNewTree[aLen])
223 raise RuntimeError, "After Copy calling the tree is not changed"
225 #--------------------------------------------------------------------------
227 def FindFileInDataDir(filename):
229 datadir = os.getenv("DATA_DIR")
230 if datadir is not None:
232 dirs = string.split(datadir, ":")
234 file = dir + "/" + filename
235 if os.path.exists(file):
237 datadir = os.getenv("KERNEL_ROOT_DIR") + "/examples/"
238 file = datadir + filename
239 if os.path.exists(file):
244 #--------------------------------------------------------------------------
248 def getActiveStudy(theStudyId=0):
249 global salome_study_ID
251 if verbose(): print "getActiveStudy"
252 if salome_study_ID == -1:
253 if salome_iapp.hasDesktop():
254 if verbose(): print "---in gui"
255 salome_study_ID = salome_iapp.sg.getActiveStudyId()
257 if verbose(): print "---outside gui"
259 aStudy=myStudyManager.GetStudyByID(theStudyId)
261 if verbose(): print "connection to existing study ", theStudyId
262 salome_study_ID = theStudyId
263 if salome_study_ID == -1:
264 salome_study_ID = createNewStudy()
265 if verbose(): print"--- Study Id ", salome_study_ID
266 return salome_study_ID
268 #--------------------------------------------------------------------------
270 def createNewStudy():
271 print "createNewStudy"
273 aStudyName = "noName"
275 listOfOpenStudies = myStudyManager.GetOpenStudies()
276 print listOfOpenStudies
277 while nameAlreadyInUse:
278 aStudyName = "extStudy_%d"%i
279 if aStudyName not in listOfOpenStudies:
284 theStudy = myStudyManager.NewStudy(aStudyName)
285 theStudyId = theStudy._get_StudyId()
286 print aStudyName, theStudyId
289 #--------------------------------------------------------------------------
291 salome_study_initial = 1
293 def salome_study_init(theStudyId=0):
295 Performs only once study creation or connection.
296 optional argument : theStudyId
297 When in embedded interpreter inside IAPP, theStudyId is not used
298 When used without GUI (external interpreter)
299 0 : create a new study (default).
300 n (>0) : try connection to study with Id = n, or create a new one
304 global salome_study_initial
305 global myStudyManager, myStudyId, myStudy, myStudyName
306 global orb, lcc, naming_service, cm
308 if salome_study_initial:
309 salome_study_initial = 0
311 orb, lcc, naming_service, cm = salome_kernel.salome_kernel_init()
313 # get Study Manager reference
314 if verbose(): print "looking for studyManager ..."
315 obj = naming_service.Resolve('myStudyManager')
316 myStudyManager = obj._narrow(SALOMEDS.StudyManager)
317 if verbose(): print "studyManager found"
319 # get active study Id, ref and name
320 myStudyId = getActiveStudy(theStudyId)
321 if verbose(): print "myStudyId",myStudyId
322 myStudy = myStudyManager.GetStudyByID(myStudyId)
323 myStudyName = myStudy._get_Name()
325 return myStudyManager, myStudyId, myStudy, myStudyName