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
31 #--------------------------------------------------------------------------
33 def DumpComponent(Study, SO, offset):
34 it = Study.NewChildIterator(SO)
35 Builder = Study.NewBuilder()
39 anAttr = Builder.FindOrCreateAttribute(CSO, "AttributeName")
40 AtName = anAttr._narrow(SALOMEDS.AttributeName)
41 t_name = AtName.Value()
48 MESSAGE( a + ">" + str(CSO.GetID()) + " " + str(t_name[1]) )
49 t_RefSO = CSO.ReferencedObject()
57 MESSAGE( a + ">" + str(RefSO.GetID()) )
58 DumpComponent(Study, CSO, offset+2)
60 #--------------------------------------------------------------------------
63 itcomp = Study.NewComponentIterator()
67 name = SC.ComponentDataType()
68 MESSAGE( "-> ComponentDataType is " + name )
69 DumpComponent(Study, SC, 1)
72 #--------------------------------------------------------------------------
76 mySO = myStudy.FindObjectID(id);
78 ok, anAttr = mySO.FindAttribute("AttributeIOR")
80 AtIOR = anAttr._narrow(SALOMEDS.AttributeIOR)
81 if AtIOR.Value() != "":
82 myObj = orb.string_to_object(AtIOR.Value())
85 def ObjectToSObject(obj):
88 ior = orb.object_to_string(obj)
90 mySO = myStudy.FindObjectIOR(ior)
94 mySO = ObjectToSObject(obj)
100 mySO = myStudy.FindObjectID(id);
103 #--------------------------------------------------------------------------
105 def generateName(prefix = None):
107 int = whrandom.randint(1,1000);
109 return "Study" + str(int)
111 return prefix + str(int)
113 #--------------------------------------------------------------------------
115 def PersistentPresentation(theStudy, theSO, theWithID):
116 # put the sobject's content (with subchildren) to the string
118 attrs = theSO.GetAllAttributes()
121 for a in range(0,aLen):
123 if isinstance(attr,SALOMEDS._objref_AttributeTreeNode):
125 elif isinstance(attr,SALOMEDS._objref_AttributeTarget):
127 elif isinstance(attr,SALOMEDS._objref_AttributeReal) or \
128 isinstance(attr,SALOMEDS._objref_AttributeInteger) or \
129 isinstance(attr,SALOMEDS._objref_AttributeName) or \
130 isinstance(attr,SALOMEDS._objref_AttributeComment) or \
131 isinstance(attr,SALOMEDS._objref_AttributePersistentRef) or \
132 isinstance(attr,SALOMEDS._objref_AttributeLocalID) or \
133 isinstance(attr,SALOMEDS._objref_AttributeUserID):
134 aResult += " attribute value: " + str(attr.Value())
135 elif isinstance(attr,SALOMEDS._objref_AttributeIOR):
136 aResult += " attribute: IOR"
137 elif isinstance(attr,SALOMEDS._objref_AttributeSequenceOfReal) or \
138 isinstance(attr,SALOMEDS._objref_AttributeSequenceOfInteger):
139 aResult += " Sequence: " + str(attr.CorbaSequence())
140 elif isinstance(attr,SALOMEDS._objref_AttributeDrawable):
141 aResult += " Drawable: " + str(attr.IsDrawable())
142 elif isinstance(attr,SALOMEDS._objref_AttributeSelectable):
143 aResult += " Selectable: " + str(attr.IsSelectable())
144 elif isinstance(attr,SALOMEDS._objref_AttributeExpandable):
145 aResult += " Expandable: " + str(attr.IsExpandable())
146 elif isinstance(attr,SALOMEDS._objref_AttributeOpened):
147 aResult += " Opened: " + str(attr.IsOpened())
148 elif isinstance(attr,SALOMEDS._objref_AttributeTextColor):
149 aResult += " TextColor: " + str(attr.TextColor())
150 elif isinstance(attr,SALOMEDS._objref_AttributeTextHighlightColor):
151 aResult += " TextHighlightColor: " + str(attr.TextHighlightColor())
152 elif isinstance(attr,SALOMEDS._objref_AttributePixMap):
153 aResult += " PixMap: " + str(attr.GetPixMap())
154 elif isinstance(attr,SALOMEDS._objref_AttributeTableOfInteger) or \
155 isinstance(attr,SALOMEDS._objref_AttributeTableOfReal):
156 aResult += " Table with title: " + attr.GetTitle()
157 elif isinstance(attr,SALOMEDS._objref_AttributePythonObject):
158 aResult += " PythonObject: " + attr.GetObject()
161 aResult = "sobject: " + theSO.GetID() + " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
163 aResult = " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
164 anIter = theStudy.NewChildIterator(theSO)
166 aResult += PersistentPresentation(theStudy, anIter.Value(), theWithID)
170 #--------------------------------------------------------------------------
173 # returns the document list tree (as list)
174 aResult = [theSO.GetID()]
175 anIter = myStudy.NewChildIterator(theSO)
177 aResult += GetTree(anIter.Value())
181 #--------------------------------------------------------------------------
183 def CheckCopyPaste(theSO, theInfo ,theComponentPaste):
185 while aRoot.GetID() != "0:":
186 aRoot = aRoot.GetFather()
187 aTree = GetTree(aRoot)
188 aStudyPersist = PersistentPresentation(myStudy, aRoot, 1)
190 if not myStudyManager.CanCopy(theSO):
191 raise RuntimeError, "<CanCopy> for "+theInfo+" returns false"
193 if not myStudyManager.Copy(theSO):
194 raise RuntimeError, "<Copy> for "+theInfo+" returns false"
197 if not myStudyManager.CanPaste(theSO):
198 raise RuntimeError, "<CanPaste> for "+theInfo+" returns false"
200 # check: before paste study is not changed check
201 if aStudyPersist != PersistentPresentation(myStudy, aRoot, 1):
202 raise RuntimeError, "Study is changed before Paste calling for "+theInfo
205 if theComponentPaste:
206 aSObj = theSO.GetFatherComponent()
207 theInfo = theInfo + "(paste for component)"
208 if myStudyManager.Paste(aSObj) == None:
209 raise RuntimeError, "<Paste> for "+theInfo+" returns None object"
210 aNewTree = GetTree(aRoot)
212 for a in range(0,aLen):
213 if aTree[a] != aNewTree[a]:
214 return myStudy.FindObjectID(aNewTree[a])
216 if aLen < len(aNewTree):
217 return myStudy.FindObjectID(aNewTree[aLen])
219 raise RuntimeError, "After Copy calling the tree is not changed"
221 #--------------------------------------------------------------------------
223 def FindFileInDataDir(filename):
225 datadir = os.getenv("DATA_DIR")
226 if datadir is not None:
228 dirs = string.split(datadir, ":")
230 file = dir + "/" + filename
231 if os.path.exists(file):
233 datadir = os.getenv("KERNEL_ROOT_DIR") + "/examples/"
234 file = datadir + filename
235 if os.path.exists(file):
240 #--------------------------------------------------------------------------
244 def getActiveStudy(theStudyId=0):
245 global salome_study_ID
247 print "getActiveStudy"
248 if salome_study_ID == -1:
249 if salome_iapp.hasDesktop():
251 salome_study_ID = salome_iapp.sg.getActiveStudyId()
253 print "---outside gui"
255 aStudy=myStudyManager.GetStudyByID(theStudyId)
257 print "connection to existing study ", theStudyId
258 salome_study_ID = theStudyId
259 if salome_study_ID == -1:
260 salome_study_ID = createNewStudy()
261 print"--- Study Id ", salome_study_ID
262 return salome_study_ID
264 #--------------------------------------------------------------------------
266 def createNewStudy():
267 print "createNewStudy"
269 aStudyName = "noName"
271 listOfOpenStudies = myStudyManager.GetOpenStudies()
272 print listOfOpenStudies
273 while nameAlreadyInUse:
274 aStudyName = "extStudy_%d"%i
275 if aStudyName not in listOfOpenStudies:
280 theStudy = myStudyManager.NewStudy(aStudyName)
281 theStudyId = theStudy._get_StudyId()
282 print aStudyName, theStudyId
285 #--------------------------------------------------------------------------
287 salome_study_initial = 1
289 def salome_study_init(theStudyId=0):
291 Performs only once study creation or connection.
292 optional argument : theStudyId
293 When in embedded interpreter inside IAPP, theStudyId is not used
294 When used without GUI (external interpreter)
295 0 : create a new study (default).
296 n (>0) : try connection to study with Id = n, or create a new one
300 global salome_study_initial
301 global myStudyManager, myStudyId, myStudy, myStudyName
302 global orb, lcc, naming_service, cm
304 if salome_study_initial:
305 salome_study_initial = 0
307 orb, lcc, naming_service, cm = salome_kernel.salome_kernel_init()
309 # get Study Manager reference
310 print "looking for studyManager ..."
311 obj = naming_service.Resolve('myStudyManager')
312 myStudyManager = obj._narrow(SALOMEDS.StudyManager)
313 print "studyManager found"
315 # get active study Id, ref and name
316 myStudyId = getActiveStudy(theStudyId)
317 print "myStudyId",myStudyId
318 myStudy = myStudyManager.GetStudyByID(myStudyId)
319 myStudyName = myStudy._get_Name()
321 return myStudyManager, myStudyId, myStudy, myStudyName