Salome HOME
This commit was generated by cvs2git to create tag 'V1_3_0'.
[modules/kernel.git] / src / SALOME_SWIG / batchmode_salome.py
1 #  Copyright (C) 2003  CEA/DEN, EDF R&D
2 #
3 #
4 #
5 #  File   : batchmode_salome.py
6 #  Module : SALOME
7
8 import salome_shared_modules
9
10 from omniORB import CORBA
11 from LifeCycleCORBA import *
12 import SALOMEDS
13 from SALOME_NamingServicePy import *
14
15 #--------------------------------------------------------------------------
16
17 def ImportComponentGUI(ComponentName):
18     libName = "lib" + ComponentName + "_Swig"
19     command = "from " + libName + " import *"
20     exec ( command )
21     constructor = ComponentName + "GUI_Swig()"
22     command = "gui = " + constructor
23     exec ( command )
24     return gui
25
26     #--------------------------------------------------------------------------
27
28 def SalomeGUIgetAllSelected(self):
29     selNumber = self.SelectedCount()
30     listSelected = []
31     for i in range(selNumber):
32         listSelected.append(self.getSelected(i))
33     return listSelected
34     #--------------------------------------------------------------------------
35
36 def generateName(prefix = None):
37     import whrandom;
38     int = whrandom.randint(1,1000);
39     if prefix is None:
40         return "Study" + str(int)
41     else :
42         return prefix + str(int)
43
44 from libSALOME_Swig import *
45 ###from SalomePyQt import *
46 class SalomeGUI(SALOMEGUI_Swig):
47     getAllSelected = SalomeGUIgetAllSelected
48    
49     def getDesktop(self) :
50 #       return SalomePyQt.getDesktop()
51         return None
52
53     def getSelection(self) :
54 #       return SalomePyQt.getSelection()
55         return None
56
57     #--------------------------------------------------------------------------
58
59 def IDToObject(id):
60     mySO = myStudy.FindObjectID(id);
61     print id
62     Builder = myStudy.NewBuilder()
63     anAttr = Builder.FindOrCreateAttribute(mySO, "AttributeIOR")
64     AtIOR = anAttr._narrow(SALOMEDS.AttributeIOR)
65     myObj = orb.string_to_object(AtIOR.Value())
66     return myObj
67
68 def IDToSObject(id):
69     mySO = myStudy.FindObjectID(id);
70     return mySO
71
72     #--------------------------------------------------------------------------
73
74 def PersistentPresentation(theStudy, theSO, theWithID):
75     # put the sobject's content (with subchildren) to the string
76     aResult = ""
77     attrs = theSO.GetAllAttributes()
78     aLen = len(attrs)
79     anUncopied = 0
80     for a in range(0,aLen):
81         attr = attrs[a]
82         if isinstance(attr,SALOMEDS._objref_AttributeTreeNode):
83             anUncopied += 1
84         elif isinstance(attr,SALOMEDS._objref_AttributeTarget):
85             anUncopied += 1
86         elif isinstance(attr,SALOMEDS._objref_AttributeReal) or \
87              isinstance(attr,SALOMEDS._objref_AttributeInteger) or \
88              isinstance(attr,SALOMEDS._objref_AttributeName) or \
89              isinstance(attr,SALOMEDS._objref_AttributeComment) or \
90              isinstance(attr,SALOMEDS._objref_AttributePersistentRef) or \
91              isinstance(attr,SALOMEDS._objref_AttributeLocalID) or \
92              isinstance(attr,SALOMEDS._objref_AttributeUserID):
93             aResult += " attribute value: " + str(attr.Value())
94         elif isinstance(attr,SALOMEDS._objref_AttributeIOR):
95             aResult += " attribute: IOR"
96         elif isinstance(attr,SALOMEDS._objref_AttributeSequenceOfReal) or \
97              isinstance(attr,SALOMEDS._objref_AttributeSequenceOfInteger):
98             aResult += " Sequence: " + str(attr.CorbaSequence())
99         elif isinstance(attr,SALOMEDS._objref_AttributeDrawable):
100             aResult += " Drawable: " + str(attr.IsDrawable())
101         elif isinstance(attr,SALOMEDS._objref_AttributeSelectable):
102             aResult += " Selectable: " + str(attr.IsSelectable())
103         elif isinstance(attr,SALOMEDS._objref_AttributeExpandable):
104             aResult += " Expandable: " + str(attr.IsExpandable())
105         elif isinstance(attr,SALOMEDS._objref_AttributeOpened):
106             aResult += " Opened: " + str(attr.IsOpened())
107         elif isinstance(attr,SALOMEDS._objref_AttributeTextColor):
108             aResult += " TextColor: " + str(attr.TextColor())
109         elif isinstance(attr,SALOMEDS._objref_AttributeTextHighlightColor):
110             aResult += " TextHighlightColor: " + str(attr.TextHighlightColor())
111         elif isinstance(attr,SALOMEDS._objref_AttributePixMap):
112             aResult += " PixMap: " + str(attr.GetPixMap())
113         elif isinstance(attr,SALOMEDS._objref_AttributeTableOfInteger) or \
114              isinstance(attr,SALOMEDS._objref_AttributeTableOfReal):
115             aResult += " Table with title: " + attr.GetTitle()
116         elif isinstance(attr,SALOMEDS._objref_AttributePythonObject):
117             aResult += " PythonObject: " + attr.GetObject()
118
119     if theWithID:
120         aResult = "sobject: " + theSO.GetID() + " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
121     else:
122         aResult = " nbattrs: " + str(aLen - anUncopied) + aResult + '\n'
123     anIter = theStudy.NewChildIterator(theSO)
124     while anIter.More():
125         aResult += PersistentPresentation(theStudy, anIter.Value(), theWithID)
126         anIter.Next()
127     return aResult
128
129     #--------------------------------------------------------------------------
130
131 def GetTree(theSO):
132     # returns the document list tree (as list)
133     aResult = [theSO.GetID()]
134     anIter = myStudy.NewChildIterator(theSO)
135     while anIter.More():
136         aResult += GetTree(anIter.Value())
137         anIter.Next()
138     return aResult
139
140     #--------------------------------------------------------------------------
141
142 def CheckCopyPaste(theSO, theInfo ,theComponentPaste):
143     aRoot = theSO
144     while aRoot.GetID() != "0:":
145         aRoot = aRoot.GetFather()
146     aTree = GetTree(aRoot)
147     aStudyPersist = PersistentPresentation(myStudy, aRoot, 1)
148
149     if not myStudyManager.CanCopy(theSO):
150         raise RuntimeError, "<CanCopy> for "+theInfo+" returns false"
151     
152     if not myStudyManager.Copy(theSO):
153         raise RuntimeError, "<Copy> for "+theInfo+" returns false"
154
155     
156     if not myStudyManager.CanPaste(theSO):
157         raise RuntimeError, "<CanPaste> for "+theInfo+" returns false"
158
159     # check: before paste study is not changed check
160     if aStudyPersist != PersistentPresentation(myStudy, aRoot, 1):
161         raise RuntimeError, "Study is changed before Paste calling for "+theInfo
162     
163     aSObj = theSO
164     if theComponentPaste:
165         aSObj = theSO.GetFatherComponent()
166         theInfo = theInfo + "(paste for component)"
167     if not myStudyManager.Paste(aSObj):
168         raise RuntimeError, "<Paste> for "+theInfo+" returns false"
169     aNewTree = GetTree(aRoot)
170     aLen = len(aTree)
171     for a in range(0,aLen):
172         if aTree[a] != aNewTree[a]:
173             return myStudy.FindObjectID(aNewTree[a])
174         
175     if aLen < len(aNewTree):
176         return myStudy.FindObjectID(aNewTree[aLen])
177     
178     raise RuntimeError, "After Copy calling the tree is not changed"
179     
180     #--------------------------------------------------------------------------
181 def FindFileInDataDir(filename):
182     import os
183     datadir = os.getenv("DATA_DIR")
184     if datadir is not None:
185         import string
186         dirs = string.split(datadir, ":")
187         for dir in dirs:
188             file = dir + "/" + filename
189             if os.path.exists(file):
190                 return file;
191     datadir = os.getenv("KERNEL_ROOT_DIR") + "/examples/"
192     file = datadir + filename
193     if os.path.exists(file):
194         return file;
195
196     return None
197         
198 #--------------------------------------------------------------------------
199 # initialise the ORB
200 orb = CORBA.ORB_init([''], CORBA.ORB_ID)
201
202 # create an LifeCycleCORBA instance
203 lcc = LifeCycleCORBA(orb)
204
205 #create a naming service instance
206 naming_service = SALOME_NamingServicePy_i(orb)
207
208 # get Study Manager reference
209 obj = naming_service.Resolve('myStudyManager')
210 myStudyManager = obj._narrow(SALOMEDS.StudyManager)
211
212 # create new study
213 myStudy = myStudyManager.NewStudy("Study1")
214
215 myStudyName = myStudy._get_Name()
216
217 myStudyId = myStudy._get_StudyId()
218 print myStudyId
219