]> SALOME platform Git repositories - modules/kernel.git/blob - src/KERNEL_PY/salome_study.py
Salome HOME
fc432a30c783ecebda036bacf661c1e59126d48a
[modules/kernel.git] / src / KERNEL_PY / salome_study.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
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.
11 #
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.
16 #
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
20 #
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23
24 #  File   : salome_study.py
25 #  Author : Paul RASCLE, EDF
26 #  Module : SALOME
27 #  $Header$
28 #
29 import salome_kernel
30 import SALOMEDS
31 import salome_iapp
32 from launchConfigureParser import verbose
33
34 myStudy = None
35 myStudyName = None
36
37 #--------------------------------------------------------------------------
38
39 def DumpComponent(Study, SO, Builder,offset):
40   it = Study.NewChildIterator(SO)
41   while it.More():
42     CSO = it.Value()
43     a=offset*"--" + ">" + CSO.GetID()
44     find,AtName = Builder.FindAttribute(CSO, "AttributeName")
45     if find:
46       a=a+":"+AtName.Value()
47     find,AtIOR = Builder.FindAttribute(CSO, "AttributeIOR")
48     if find:
49       a=a+":"+AtIOR.Value()
50     find,RefSO = CSO.ReferencedObject()
51     if find:
52       a=a+":"+RefSO.GetID()
53     print a
54     DumpComponent(Study, CSO, Builder,offset+2)
55     it.Next()
56
57 #--------------------------------------------------------------------------
58
59 def DumpStudy(Study):
60     """
61     Dump a study, given the ior
62     """
63     itcomp = Study.NewComponentIterator()
64     Builder = Study.NewBuilder()
65     while itcomp.More():
66       SC = itcomp.Value()
67       name = SC.ComponentDataType()
68       print "-> ComponentDataType is " + name
69       DumpComponent(Study, SC,Builder, 1)
70       itcomp.Next()
71
72 #--------------------------------------------------------------------------
73
74 def IDToObject(id):
75     global myStudy
76     myObj = None
77     mySO = myStudy.FindObjectID(id);
78     if mySO is not None:
79         ok, anAttr = mySO.FindAttribute("AttributeIOR")
80         if ok:
81             AtIOR = anAttr._narrow(SALOMEDS.AttributeIOR)
82             if AtIOR.Value() != "":
83                 myObj = orb.string_to_object(AtIOR.Value())
84     return myObj
85
86 def ObjectToSObject(obj):
87     mySO = None
88     if obj is not None:
89         ior =  orb.object_to_string(obj)
90         if ior != "":
91             mySO = myStudy.FindObjectIOR(ior)
92     return mySO
93
94 def ObjectToID(obj):
95     mySO = ObjectToSObject(obj)
96     if mySO:
97         return mySO.GetID()
98     return ""
99
100 def IDToSObject(id):
101     global myStudy
102     mySO = myStudy.FindObjectID(id);
103     return mySO
104
105     #--------------------------------------------------------------------------
106
107 def generateName(prefix = None):
108     import random;
109     int = random.randint(1,1000);
110     if prefix is None:
111         return "Study" + str(int)
112     else :
113         return prefix + str(int)
114
115     #--------------------------------------------------------------------------
116
117 def PersistentPresentation(theSO, theWithID):
118     # put the sobject's content (with subchildren) to the string
119     aResult = ""
120     attrs = theSO.GetAllAttributes()
121     aLen = len(attrs)
122     anUncopied = 0
123     for a in range(0,aLen):
124         attr = attrs[a]
125         if isinstance(attr,SALOMEDS._objref_AttributeTreeNode):
126             anUncopied += 1
127         elif isinstance(attr,SALOMEDS._objref_AttributeTarget):
128             anUncopied += 1
129         elif isinstance(attr,SALOMEDS._objref_AttributeReal) or \
130              isinstance(attr,SALOMEDS._objref_AttributeInteger) or \
131              isinstance(attr,SALOMEDS._objref_AttributeName) or \
132              isinstance(attr,SALOMEDS._objref_AttributeComment) or \
133              isinstance(attr,SALOMEDS._objref_AttributePersistentRef) or \
134              isinstance(attr,SALOMEDS._objref_AttributeLocalID) or \
135              isinstance(attr,SALOMEDS._objref_AttributeUserID):
136             aResult += " attribute value: " + str(attr.Value())
137         elif isinstance(attr,SALOMEDS._objref_AttributeIOR):
138             aResult += " attribute: IOR"
139         elif isinstance(attr,SALOMEDS._objref_AttributeSequenceOfReal) or \
140              isinstance(attr,SALOMEDS._objref_AttributeSequenceOfInteger):
141             aResult += " Sequence: " + str(attr.CorbaSequence())
142         elif isinstance(attr,SALOMEDS._objref_AttributeDrawable):
143             aResult += " Drawable: " + str(attr.IsDrawable())
144         elif isinstance(attr,SALOMEDS._objref_AttributeSelectable):
145             aResult += " Selectable: " + str(attr.IsSelectable())
146         elif isinstance(attr,SALOMEDS._objref_AttributeExpandable):
147             aResult += " Expandable: " + str(attr.IsExpandable())
148         elif isinstance(attr,SALOMEDS._objref_AttributeOpened):
149             aResult += " Opened: " + str(attr.IsOpened())
150         elif isinstance(attr,SALOMEDS._objref_AttributeTextColor):
151             aResult += " TextColor: " + str(attr.TextColor())
152         elif isinstance(attr,SALOMEDS._objref_AttributeTextHighlightColor):
153             aResult += " TextHighlightColor: " + str(attr.TextHighlightColor())
154         elif isinstance(attr,SALOMEDS._objref_AttributePixMap):
155             aResult += " PixMap: " + str(attr.GetPixMap())
156         elif isinstance(attr,SALOMEDS._objref_AttributeTableOfInteger) or \
157              isinstance(attr,SALOMEDS._objref_AttributeTableOfReal):
158             aResult += " Table with title: " + attr.GetTitle()
159         elif isinstance(attr,SALOMEDS._objref_AttributePythonObject):
160             aResult += " PythonObject: " + attr.GetObject()
161
162     if theWithID:
163         aResult = "sobject: " + theSO.GetID() + " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
164     else:
165         aResult = " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
166     anIter = myStudy.NewChildIterator(theSO)
167     while anIter.More():
168         aResult += PersistentPresentation(anIter.Value(), theWithID)
169         anIter.Next()
170     return aResult
171
172     #--------------------------------------------------------------------------
173
174 def GetTree(theSO):
175     # returns the document list tree (as list)
176     global myStudy
177     aResult = [theSO.GetID()]
178     anIter = myStudy.NewChildIterator(theSO)
179     while anIter.More():
180         aResult += GetTree(anIter.Value())
181         anIter.Next()
182     return aResult
183
184     #--------------------------------------------------------------------------
185
186 def CheckCopyPaste(theSO, theInfo ,theComponentPaste):
187     global myStudy
188
189     aRoot = theSO
190     while aRoot.GetID() != "0:":
191         aRoot = aRoot.GetFather()
192     aTree = GetTree(aRoot)
193     aStudyPersist = PersistentPresentation(aRoot, 1)
194
195     if not myStudy.CanCopy(theSO):
196         raise RuntimeError, "<CanCopy> for "+theInfo+" returns false"
197     
198     if not myStudy.Copy(theSO):
199         raise RuntimeError, "<Copy> for "+theInfo+" returns false"
200
201     
202     if not myStudy.CanPaste(theSO):
203         raise RuntimeError, "<CanPaste> for "+theInfo+" returns false"
204
205     # check: before paste study is not changed check
206     if aStudyPersist != PersistentPresentation(aRoot, 1):
207         raise RuntimeError, "Study is changed before Paste calling for "+theInfo
208     
209     aSObj = theSO
210     if theComponentPaste:
211         aSObj = theSO.GetFatherComponent()
212         theInfo = theInfo + "(paste for component)"
213     if myStudy.Paste(aSObj) == None:
214         raise RuntimeError, "<Paste> for "+theInfo+" returns None object"
215     aNewTree = GetTree(aRoot)
216     aLen = len(aTree)
217     for a in range(0,aLen):
218         if aTree[a] != aNewTree[a]:
219             return myStudy.FindObjectID(aNewTree[a])
220         
221     if aLen < len(aNewTree):
222         return myStudy.FindObjectID(aNewTree[aLen])
223     
224     raise RuntimeError, "After Copy calling the tree is not changed"
225     
226     #--------------------------------------------------------------------------
227
228 def GetComponentVersion(theComponent, all_versions = False):
229     # returns the document list tree (as list)
230     global myStudy
231     props = myStudy.GetProperties()
232     stored_components = props.GetStoredComponents()
233     version = "no component data" # vsr: better raise an exception in this case?
234     if theComponent in stored_components:
235       if all_versions:
236         version = props.GetComponentVersions(theComponent)
237         for i in range(len(version)):
238           if not version[i]: version[i] = "unknown"
239           pass
240         pass
241       else:
242         version = props.GetComponentVersion(theComponent)
243         if not version: version = "unknown"
244         pass
245       pass
246     return version
247     
248     #--------------------------------------------------------------------------
249
250 def FindFileInDataDir(filename):
251     import os
252     datadir = os.getenv("DATA_DIR")
253     if datadir is not None:
254         import string
255         dirs = string.split(datadir, ":")
256         for dir in dirs:
257             file = dir + "/" + filename
258             if os.path.exists(file):
259                 return file;
260     datadir = os.getenv("KERNEL_ROOT_DIR") + "/examples/"
261     file = datadir + filename
262     if os.path.exists(file):
263         return file;
264
265     return None
266
267     #--------------------------------------------------------------------------
268
269 def openStudy(theStudyPath):
270     global myStudy
271     print "openStudy"
272     theStudy = myStudy.Open(theStudyPath)
273     theStudyName = theStudy._get_Name()
274     print theStudyPath, theStudyName
275     return theStudyName
276
277     #--------------------------------------------------------------------------
278 def clearStudy():
279     global myStudy
280     print "clearStudy"
281     myStudy.Clear()
282     myStudy = None
283     pass
284
285     #--------------------------------------------------------------------------
286
287 def salome_study_init(theStudyPath=None):
288     """
289     Performs only once study creation or connection.
290     optional argument : theStudyPath
291         None        : attach to the currently active single study;
292                       create new empty study if there is active study
293         <URL> (str) : open study with the given file name
294     """
295     global myStudy, myStudyName
296     global orb, lcc, naming_service, cm
297
298     if verbose(): print "theStudyPath:", theStudyPath
299     if not myStudy:
300         orb, lcc, naming_service, cm = salome_kernel.salome_kernel_init()
301
302         # get Study reference
303         if verbose(): print "looking for study..."
304         obj = naming_service.Resolve('Study')
305         myStudy = obj._narrow(SALOMEDS.Study)
306         if verbose(): print "Study found"
307         pass
308
309     # get study name
310     myStudyName = myStudy._get_Name()
311
312     return myStudy, myStudyName
313
314 def salome_study_close():
315     global myStudy, myStudyName
316     myStudy, myStudyName = None, None
317     pass