1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE
4 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 # 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
37 #--------------------------------------------------------------------------
39 def DumpComponent(SO, Builder,offset):
41 it = myStudy.NewChildIterator(SO)
44 a=offset*"--" + ">" + CSO.GetID()
45 find,AtName = Builder.FindAttribute(CSO, "AttributeName")
47 a=a+":"+AtName.Value()
48 find,AtIOR = Builder.FindAttribute(CSO, "AttributeIOR")
51 find,RefSO = CSO.ReferencedObject()
55 DumpComponent(CSO, Builder,offset+2)
58 #--------------------------------------------------------------------------
62 Dump a study, given the ior
65 itcomp = myStudy.NewComponentIterator()
66 Builder = myStudy.NewBuilder()
69 name = SC.ComponentDataType()
70 print("-> ComponentDataType is " + name)
71 DumpComponent(SC,Builder, 1)
74 #--------------------------------------------------------------------------
79 mySO = myStudy.FindObjectID(id);
81 ok, anAttr = mySO.FindAttribute("AttributeIOR")
83 AtIOR = anAttr._narrow(SALOMEDS.AttributeIOR)
84 if AtIOR.Value() != "":
85 myObj = orb.string_to_object(AtIOR.Value())
88 def ObjectToSObject(obj):
92 ior = orb.object_to_string(obj)
94 mySO = myStudy.FindObjectIOR(ior)
98 mySO = ObjectToSObject(obj)
105 mySO = myStudy.FindObjectID(id);
108 #--------------------------------------------------------------------------
110 def generateName(prefix = None):
112 int = random.randint(1,1000);
114 return "Study" + str(int)
116 return prefix + str(int)
118 #--------------------------------------------------------------------------
120 def PersistentPresentation(theSO, theWithID):
122 # put the sobject's content (with subchildren) to the string
124 attrs = theSO.GetAllAttributes()
127 for a in range(0,aLen):
129 if isinstance(attr,SALOMEDS._objref_AttributeTreeNode):
131 elif isinstance(attr,SALOMEDS._objref_AttributeTarget):
133 elif isinstance(attr,SALOMEDS._objref_AttributeReal) or \
134 isinstance(attr,SALOMEDS._objref_AttributeInteger) or \
135 isinstance(attr,SALOMEDS._objref_AttributeName) or \
136 isinstance(attr,SALOMEDS._objref_AttributeComment) or \
137 isinstance(attr,SALOMEDS._objref_AttributePersistentRef) or \
138 isinstance(attr,SALOMEDS._objref_AttributeLocalID) or \
139 isinstance(attr,SALOMEDS._objref_AttributeUserID):
140 aResult += " attribute value: " + str(attr.Value())
141 elif isinstance(attr,SALOMEDS._objref_AttributeIOR):
142 aResult += " attribute: IOR"
143 elif isinstance(attr,SALOMEDS._objref_AttributeSequenceOfReal) or \
144 isinstance(attr,SALOMEDS._objref_AttributeSequenceOfInteger):
145 aResult += " Sequence: " + str(attr.CorbaSequence())
146 elif isinstance(attr,SALOMEDS._objref_AttributeDrawable):
147 aResult += " Drawable: " + str(attr.IsDrawable())
148 elif isinstance(attr,SALOMEDS._objref_AttributeSelectable):
149 aResult += " Selectable: " + str(attr.IsSelectable())
150 elif isinstance(attr,SALOMEDS._objref_AttributeExpandable):
151 aResult += " Expandable: " + str(attr.IsExpandable())
152 elif isinstance(attr,SALOMEDS._objref_AttributeOpened):
153 aResult += " Opened: " + str(attr.IsOpened())
154 elif isinstance(attr,SALOMEDS._objref_AttributeTextColor):
155 aResult += " TextColor: " + str(attr.TextColor())
156 elif isinstance(attr,SALOMEDS._objref_AttributeTextHighlightColor):
157 aResult += " TextHighlightColor: " + str(attr.TextHighlightColor())
158 elif isinstance(attr,SALOMEDS._objref_AttributePixMap):
159 aResult += " PixMap: " + str(attr.GetPixMap())
160 elif isinstance(attr,SALOMEDS._objref_AttributeTableOfInteger) or \
161 isinstance(attr,SALOMEDS._objref_AttributeTableOfReal):
162 aResult += " Table with title: " + attr.GetTitle()
163 elif isinstance(attr,SALOMEDS._objref_AttributePythonObject):
164 aResult += " PythonObject: " + attr.GetObject()
167 aResult = "sobject: " + theSO.GetID() + " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
169 aResult = " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
170 anIter = myStudy.NewChildIterator(theSO)
172 aResult += PersistentPresentation(anIter.Value(), theWithID)
176 #--------------------------------------------------------------------------
179 # 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):
194 while aRoot.GetID() != "0:":
195 aRoot = aRoot.GetFather()
196 aTree = GetTree(aRoot)
197 aStudyPersist = PersistentPresentation(aRoot, 1)
199 if not myStudy.CanCopy(theSO):
200 raise RuntimeError("<CanCopy> for "+theInfo+" returns false")
202 if not myStudy.Copy(theSO):
203 raise RuntimeError("<Copy> for "+theInfo+" returns false")
206 if not myStudy.CanPaste(theSO):
207 raise RuntimeError("<CanPaste> for "+theInfo+" returns false")
209 # check: before paste study is not changed check
210 if aStudyPersist != PersistentPresentation(aRoot, 1):
211 raise RuntimeError("Study is changed before Paste calling for "+theInfo)
214 if theComponentPaste:
215 aSObj = theSO.GetFatherComponent()
216 theInfo = theInfo + "(paste for component)"
217 if myStudy.Paste(aSObj) == None:
218 raise RuntimeError("<Paste> for "+theInfo+" returns None object")
219 aNewTree = GetTree(aRoot)
221 for a in range(0,aLen):
222 if aTree[a] != aNewTree[a]:
223 return myStudy.FindObjectID(aNewTree[a])
225 if aLen < len(aNewTree):
226 return myStudy.FindObjectID(aNewTree[aLen])
228 raise RuntimeError("After Copy calling the tree is not changed")
230 #--------------------------------------------------------------------------
232 def GetComponentVersion(theComponent, all_versions = False):
233 # returns the document list tree (as list)
235 props = myStudy.GetProperties()
236 stored_components = props.GetStoredComponents()
237 version = "no component data" # vsr: better raise an exception in this case?
238 if theComponent in stored_components:
240 version = props.GetComponentVersions(theComponent)
241 for i in range(len(version)):
242 if not version[i]: version[i] = "unknown"
246 version = props.GetComponentVersion(theComponent)
247 if not version: version = "unknown"
252 #--------------------------------------------------------------------------
254 def FindFileInDataDir(filename):
256 datadir = os.getenv("DATA_DIR")
257 if datadir is not None:
259 dirs = string.split(datadir, ":")
261 file = dir + "/" + filename
262 if os.path.exists(file):
264 datadir = os.getenv("KERNEL_ROOT_DIR") + "/examples/"
265 file = datadir + filename
266 if os.path.exists(file):
271 #--------------------------------------------------------------------------
273 def openStudy(theStudyPath):
274 if verbose(): print("openStudy (begin)")
275 global myStudy, myStudyName
276 myStudy.Open(theStudyPath)
277 myStudyName = myStudy._get_Name()
278 if verbose(): print("openStudy (end):", theStudyPath, myStudy._get_Name())
280 #--------------------------------------------------------------------------
282 def salome_study_init_without_session(theStudyPath=None):
284 Performs only once study creation or connection.
285 optional argument : theStudyPath
286 None : attach to the currently active single study;
287 create new empty study if there is active study
288 <URL> (str) : open study with the given file name
290 global myStudy, myStudyName
291 global orb, lcc, naming_service, cm
293 if verbose(): print("theStudyPath:", theStudyPath)
296 orb = CORBA.ORB_init([''])
298 # get Study reference
299 if verbose(): print("looking for study...")
301 myStudy = KernelDS.myStudy()
302 if verbose(): print("Study found")
306 if theStudyPath and isinstance(theStudyPath, (str, bytes)):
307 if isinstance(theStudyPath, bytes):
308 theStudyPath = str(theStudyPath, 'UTF8')
309 openStudy(theStudyPath)
313 myStudyName = myStudy._get_Name()
315 return myStudy, myStudyName
317 #--------------------------------------------------------------------------
319 def salome_study_init(theStudyPath=None):
321 Performs only once study creation or connection.
322 optional argument : theStudyPath
323 None : attach to the currently active single study;
324 create new empty study if there is active study
325 <URL> (str) : open study with the given file name
327 global myStudy, myStudyName
328 global orb, lcc, naming_service, cm
330 if verbose(): print("theStudyPath:", theStudyPath)
332 orb, lcc, naming_service, cm, _, _, _ = salome_kernel.salome_kernel_init()
334 # get Study reference
335 if verbose(): print("looking for study...")
336 obj = naming_service.Resolve('/Study')
337 myStudy = obj._narrow(SALOMEDS.Study)
338 if verbose(): print("Study found")
342 if theStudyPath and isinstance(theStudyPath, (str, bytes)):
343 if isinstance(theStudyPath, bytes):
344 theStudyPath = str(theStudyPath, 'UTF8')
345 openStudy(theStudyPath)
349 myStudyName = myStudy._get_Name()
351 return myStudy, myStudyName
353 def salome_study_close():
354 global myStudy, myStudyName
355 myStudy, myStudyName = None, None