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 #--------------------------------------------------------------------------
64 itcomp = Study.NewComponentIterator()
68 name = SC.ComponentDataType()
69 MESSAGE( "-> ComponentDataType is " + name )
70 DumpComponent(Study, SC, 1)
73 #--------------------------------------------------------------------------
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)
101 mySO = myStudy.FindObjectID(id);
104 #--------------------------------------------------------------------------
106 def generateName(prefix = None):
108 int = whrandom.randint(1,1000);
110 return "Study" + str(int)
112 return prefix + str(int)
114 #--------------------------------------------------------------------------
116 def PersistentPresentation(theStudy, theSO, theWithID):
117 # put the sobject's content (with subchildren) to the string
119 attrs = theSO.GetAllAttributes()
122 for a in range(0,aLen):
124 if isinstance(attr,SALOMEDS._objref_AttributeTreeNode):
126 elif isinstance(attr,SALOMEDS._objref_AttributeTarget):
128 elif isinstance(attr,SALOMEDS._objref_AttributeReal) or \
129 isinstance(attr,SALOMEDS._objref_AttributeInteger) or \
130 isinstance(attr,SALOMEDS._objref_AttributeName) or \
131 isinstance(attr,SALOMEDS._objref_AttributeComment) or \
132 isinstance(attr,SALOMEDS._objref_AttributePersistentRef) or \
133 isinstance(attr,SALOMEDS._objref_AttributeLocalID) or \
134 isinstance(attr,SALOMEDS._objref_AttributeUserID):
135 aResult += " attribute value: " + str(attr.Value())
136 elif isinstance(attr,SALOMEDS._objref_AttributeIOR):
137 aResult += " attribute: IOR"
138 elif isinstance(attr,SALOMEDS._objref_AttributeSequenceOfReal) or \
139 isinstance(attr,SALOMEDS._objref_AttributeSequenceOfInteger):
140 aResult += " Sequence: " + str(attr.CorbaSequence())
141 elif isinstance(attr,SALOMEDS._objref_AttributeDrawable):
142 aResult += " Drawable: " + str(attr.IsDrawable())
143 elif isinstance(attr,SALOMEDS._objref_AttributeSelectable):
144 aResult += " Selectable: " + str(attr.IsSelectable())
145 elif isinstance(attr,SALOMEDS._objref_AttributeExpandable):
146 aResult += " Expandable: " + str(attr.IsExpandable())
147 elif isinstance(attr,SALOMEDS._objref_AttributeOpened):
148 aResult += " Opened: " + str(attr.IsOpened())
149 elif isinstance(attr,SALOMEDS._objref_AttributeTextColor):
150 aResult += " TextColor: " + str(attr.TextColor())
151 elif isinstance(attr,SALOMEDS._objref_AttributeTextHighlightColor):
152 aResult += " TextHighlightColor: " + str(attr.TextHighlightColor())
153 elif isinstance(attr,SALOMEDS._objref_AttributePixMap):
154 aResult += " PixMap: " + str(attr.GetPixMap())
155 elif isinstance(attr,SALOMEDS._objref_AttributeTableOfInteger) or \
156 isinstance(attr,SALOMEDS._objref_AttributeTableOfReal):
157 aResult += " Table with title: " + attr.GetTitle()
158 elif isinstance(attr,SALOMEDS._objref_AttributePythonObject):
159 aResult += " PythonObject: " + attr.GetObject()
162 aResult = "sobject: " + theSO.GetID() + " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
164 aResult = " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
165 anIter = theStudy.NewChildIterator(theSO)
167 aResult += PersistentPresentation(theStudy, anIter.Value(), theWithID)
171 #--------------------------------------------------------------------------
174 # returns the document list tree (as list)
175 aResult = [theSO.GetID()]
176 anIter = myStudy.NewChildIterator(theSO)
178 aResult += GetTree(anIter.Value())
182 #--------------------------------------------------------------------------
184 def CheckCopyPaste(theSO, theInfo ,theComponentPaste):
186 while aRoot.GetID() != "0:":
187 aRoot = aRoot.GetFather()
188 aTree = GetTree(aRoot)
189 aStudyPersist = PersistentPresentation(myStudy, aRoot, 1)
191 if not myStudyManager.CanCopy(theSO):
192 raise RuntimeError, "<CanCopy> for "+theInfo+" returns false"
194 if not myStudyManager.Copy(theSO):
195 raise RuntimeError, "<Copy> for "+theInfo+" returns false"
198 if not myStudyManager.CanPaste(theSO):
199 raise RuntimeError, "<CanPaste> for "+theInfo+" returns false"
201 # check: before paste study is not changed check
202 if aStudyPersist != PersistentPresentation(myStudy, aRoot, 1):
203 raise RuntimeError, "Study is changed before Paste calling for "+theInfo
206 if theComponentPaste:
207 aSObj = theSO.GetFatherComponent()
208 theInfo = theInfo + "(paste for component)"
209 if myStudyManager.Paste(aSObj) == None:
210 raise RuntimeError, "<Paste> for "+theInfo+" returns None object"
211 aNewTree = GetTree(aRoot)
213 for a in range(0,aLen):
214 if aTree[a] != aNewTree[a]:
215 return myStudy.FindObjectID(aNewTree[a])
217 if aLen < len(aNewTree):
218 return myStudy.FindObjectID(aNewTree[aLen])
220 raise RuntimeError, "After Copy calling the tree is not changed"
222 #--------------------------------------------------------------------------
224 def FindFileInDataDir(filename):
226 datadir = os.getenv("DATA_DIR")
227 if datadir is not None:
229 dirs = string.split(datadir, ":")
231 file = dir + "/" + filename
232 if os.path.exists(file):
234 datadir = os.getenv("KERNEL_ROOT_DIR") + "/examples/"
235 file = datadir + filename
236 if os.path.exists(file):
241 #--------------------------------------------------------------------------
245 def getActiveStudy(theStudyId=0):
246 global salome_study_ID
248 if verbose(): print "getActiveStudy"
249 if salome_study_ID == -1:
250 if salome_iapp.hasDesktop():
251 if verbose(): print "---in gui"
252 salome_study_ID = salome_iapp.sg.getActiveStudyId()
254 if verbose(): print "---outside gui"
256 aStudy=myStudyManager.GetStudyByID(theStudyId)
258 if verbose(): print "connection to existing study ", theStudyId
259 salome_study_ID = theStudyId
260 if salome_study_ID == -1:
261 salome_study_ID = createNewStudy()
262 if verbose(): print"--- Study Id ", salome_study_ID
263 return salome_study_ID
265 #--------------------------------------------------------------------------
267 def createNewStudy():
268 print "createNewStudy"
270 aStudyName = "noName"
272 listOfOpenStudies = myStudyManager.GetOpenStudies()
273 print listOfOpenStudies
274 while nameAlreadyInUse:
275 aStudyName = "extStudy_%d"%i
276 if aStudyName not in listOfOpenStudies:
281 theStudy = myStudyManager.NewStudy(aStudyName)
282 theStudyId = theStudy._get_StudyId()
283 print aStudyName, theStudyId
286 #--------------------------------------------------------------------------
288 salome_study_initial = 1
290 def salome_study_init(theStudyId=0):
292 Performs only once study creation or connection.
293 optional argument : theStudyId
294 When in embedded interpreter inside IAPP, theStudyId is not used
295 When used without GUI (external interpreter)
296 0 : create a new study (default).
297 n (>0) : try connection to study with Id = n, or create a new one
301 global salome_study_initial
302 global myStudyManager, myStudyId, myStudy, myStudyName
303 global orb, lcc, naming_service, cm
305 if salome_study_initial:
306 salome_study_initial = 0
308 orb, lcc, naming_service, cm = salome_kernel.salome_kernel_init()
310 # get Study Manager reference
311 if verbose(): print "looking for studyManager ..."
312 obj = naming_service.Resolve('myStudyManager')
313 myStudyManager = obj._narrow(SALOMEDS.StudyManager)
314 if verbose(): print "studyManager found"
316 # get active study Id, ref and name
317 myStudyId = getActiveStudy(theStudyId)
318 if verbose(): print "myStudyId",myStudyId
319 myStudy = myStudyManager.GetStudyByID(myStudyId)
320 myStudyName = myStudy._get_Name()
322 return myStudyManager, myStudyId, myStudy, myStudyName