Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/visu.git] / src / VISU_SWIG / VISU_Example_02.py
1 #  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 #  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 #
6 #  This library is free software; you can redistribute it and/or
7 #  modify it under the terms of the GNU Lesser General Public
8 #  License as published by the Free Software Foundation; either
9 #  version 2.1 of the License.
10 #
11 #  This library is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 #  Lesser General Public License for more details.
15 #
16 #  You should have received a copy of the GNU Lesser General Public
17 #  License along with this library; if not, write to the Free Software
18 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 #
20 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #
22 # T 2.24: Save/retrieve view parameters.
23 # Uses MED file fra.med from ${DATA_DIR}/MedFiles directory.
24 # This script is equivalent to non-regression test script 003/A3
25 #
26 import salome
27 import visu_gui
28 import SALOMEDS
29 import VISU
30 import os
31 import sys
32
33 medFile = os.getenv("DATA_DIR") + "/MedFiles/fra.med"
34
35 #%====================Stage1: Creating a new study====================%
36
37 print "**** Stage1: Creating a new study "
38
39 print "Creating a new study..................", 
40 myVisu = visu_gui.myVisu
41 myVisu.SetCurrentStudy(salome.myStudy)
42 myViewManager = myVisu.GetViewManager()
43 if myViewManager is None : print "Error"
44 else : print "OK"
45
46 #%====================Stage2: Saving view parameters before import====================%
47
48 print "**** Stage2: Saving view parameters before import "
49
50 myView = myViewManager.Create3DView()
51
52 print "Zooming trihedron.....................",
53 aScale = myView.GetParallelScale()
54 myView.SetParallelScale(aScale*4)
55 print "OK"
56
57 print "Rotating trihedron....................",
58 aPoint = (100,100,100)
59 myView.SetPointOfView(aPoint) 
60 print "OK"
61
62 print "Saving view parameters................",
63 aViewParamsName1 = 'ViewParams:1'
64 aSaveRes = myView.SaveViewParams(aViewParamsName1)
65 if aSaveRes != 1 : print "Error"
66 else : print "OK"
67
68 #%====================Stage3: Import MED file====================%
69
70 print "**** Stage3: Import MED file"
71
72 print 'Importing "fra.med"...................',
73 myResult = myVisu.ImportFile(medFile)
74 if myResult is None : print "Error"
75 else : print "OK"
76
77 print "Creating mesh.........................",
78 myMeshName = 'LE VOLUME'
79 myCellEntity = VISU.CELL
80 mesh = myVisu.MeshOnEntity(myResult,myMeshName,myCellEntity);
81 if mesh is None : print "Error"
82 else : print "OK"
83
84 myView.Maximize()
85 myView.Display(mesh)
86 myView.FitAll()
87
88 #%====================Stage4: Saving view parameters after import====================%
89
90 print "**** Stage4:  Saving view parameters after import"
91
92 print "Creating Scalar Map...................",
93 myFieldName = 'TAUX_DE_VIDE'
94 myNodeEntity = VISU.NODE
95 scalarmap = myVisu.ScalarMapOnField(myResult,myMeshName,myNodeEntity,myFieldName,1);
96 if scalarmap is None : print "Error"
97 else : print "OK"
98 myView.DisplayOnly(scalarmap)
99
100 print "Zooming 3D view.......................",
101 aScale = myView.GetParallelScale()
102 myView.SetParallelScale(aScale*2)
103 print "OK"
104
105 print "Setting view point(LEFT)..............",
106 myView.SetView((VISU.View3D.LEFT))
107 print "OK"
108
109 print "Saving view parameters................",
110 aViewParamsName2 = 'ViewParams:2'
111 aSaveRes = myView.SaveViewParams(aViewParamsName2)
112 if aSaveRes != 1 : print "Error"
113 else : print "OK"
114
115 print "Creating Cut Planes...................",
116 cutplanes = myVisu.CutPlanesOnField(myResult,myMeshName,myNodeEntity,myFieldName,1);
117 if cutplanes is None : print "Error"
118 else : print "OK"
119 cutplanes.SetNbPlanes(4)
120 myView.DisplayOnly(cutplanes)
121
122 print "Setting scaling.......................",
123
124 #Define parameters of scaling:
125 myXParam = 5
126 myYParam = 5
127 myZParam = 1
128
129 myXAxis = VISU.View3D.XAxis
130 myYAxis = VISU.View3D.YAxis
131 myZAxis = VISU.View3D.ZAxis
132
133 myView.ScaleView(myXAxis,myXParam)
134 myView.ScaleView(myYAxis,myYParam)
135 myView.ScaleView(myZAxis,myZParam)
136 print "OK"
137
138 print "Rotating 3d view......................",
139 aPoint = (100,40,0)
140 myView.SetPointOfView(aPoint) 
141 print "OK"
142
143 print "Fit All...............................",
144 myView.FitAll()
145 print "OK"
146
147 print "Saving view parameters................",
148 aViewParamsName3 = 'ViewParams:3'
149 aSaveRes = myView.SaveViewParams(aViewParamsName3)
150 if aSaveRes != 1 : print "Error"
151 else : print "OK"
152
153 #%====================Stage5: Restoring view parameters====================%
154
155 print "**** Stage5: Restoring view parameters"
156
157 print "Restoring first view parameters.......",
158 aRestoreRes = myView.RestoreViewParams(aViewParamsName1)
159 if aRestoreRes != 1 : print "Error"
160 else : print "OK"
161
162 print "Restoring second view parameters......",
163 aRestoreRes = myView.RestoreViewParams(aViewParamsName2)
164 if aRestoreRes != 1 : print "Error"
165 else : print "OK"
166
167 print "Displaing only Scalar Map.............",
168 myView.DisplayOnly(scalarmap)
169 print "OK"
170
171 print "Displaing only Cut Planes.............",
172 myView.DisplayOnly(cutplanes)
173 print "OK"
174
175 print "Restoring third view parameters.......",
176 aRestoreRes = myView.RestoreViewParams(aViewParamsName3)
177 if aRestoreRes != 1 : print "Error"
178 else : print "OK"
179
180 print "Displaing only Mesh...................",
181 myView.DisplayOnly(mesh)
182 print "OK"
183
184 print "Displaing only Scalar Map.............",
185 myView.DisplayOnly(scalarmap)
186 print "OK"
187
188 print "Displaing only Cut Planes.............",
189 myView.DisplayOnly(cutplanes)
190 print "OK"
191
192 #%====================Stage6: Changing of view parameters====================%
193
194 print "**** Stage6: Changing of view parameters"
195
196 print "Remove scaling........................",
197 myView.RemoveScale()
198 print "OK"
199
200 print "Fit All...............................",
201 myView.FitAll()
202 print "OK"
203
204 print "Rotating 3d view......................",
205 aPoint = (0,60,150)
206 myView.SetPointOfView(aPoint) 
207 print "OK"
208
209 print "Resaving first view parameters........",
210 aSaveRes = myView.SaveViewParams(aViewParamsName1)
211 if aSaveRes != 1 : print "Error"
212 else : print "OK"
213
214 print "Restoring third view parameters.......",
215 aRestoreRes = myView.RestoreViewParams(aViewParamsName3)
216 if aRestoreRes != 1 : print "Error"
217 else : print "OK"
218
219 print "Restoring first view parameters.......",
220 aRestoreRes = myView.RestoreViewParams(aViewParamsName1)
221 if aRestoreRes != 1 : print "Error"
222 else : print "OK"
223
224 print "Displaying only Mesh..................",
225 myView.DisplayOnly(mesh)
226 print "OK"
227
228 print "Displaying only Scalar Map............",
229 myView.DisplayOnly(scalarmap)
230 print "OK"
231
232 #%====================Stage7: Saving of created view parameters====================%
233
234 print "**** Stage7: Saving of created view parameters"
235
236 print "Saving study..........................",
237
238 str = os.getenv("TmpDir")
239 if str == None:
240         str = "/tmp"
241
242 file = str+'/VISU_005.hdf'
243
244 salome.myStudyManager.SaveAs(file, salome.myStudy, 0)
245 study_id = salome.myStudy._get_StudyId()
246 print "OK"
247
248 file_new = str+'/VISU_005_new.hdf'
249
250 if not sys.platform == "win32":
251   command = "mv " + file + " " + file_new
252 else:
253   command = "move /Y " + file + " " + file_new
254   
255 os.system(command)
256 file = file_new
257
258 # Opening a study tempoparily commented due to a problem
259 # with python initialization. To be restored after bug 10000 fixing.
260
261 #print "Opening just saved study..............",
262 #
263 #openedStudy = salome.myStudyManager.Open(file)]
264 openedStudy = salome.myStudy # temporarily
265 #myVisu.SetCurrentStudy(openedStudy)
266 myViewManager = myVisu.GetViewManager()
267 myView1 = myViewManager.Create3DView()
268 if myView1 is None : print "Error"
269 else : print "OK"
270
271 import time
272 time.sleep(1)
273
274 print "Restoring first view parameters.......",
275 aRestoreRes = myView1.RestoreViewParams(aViewParamsName1)
276 if aRestoreRes != 1 : print "Error"
277 else : print "OK"
278
279 Builder = openedStudy.NewBuilder()
280 #SCom = openedStudy.FindComponent("VISU")
281 #Builder.LoadWith(SCom ,myVisu)
282
283 print "Displaying Scalar Map.................",
284 SObj = openedStudy.FindObject('ScalarMap')
285 scalarmap1 = visu_gui.visu.SObjectToObject(SObj)
286 if scalarmap1 is None : print "Error"
287 else : print "OK"
288 myView1.DisplayOnly(scalarmap1)
289
290 print "Displaying Cut Planes.................",
291 SObj = openedStudy.FindObject('CutPlanes')
292 cutplanes1 = visu_gui.visu.SObjectToObject(SObj)
293 if cutplanes1 is None : print "Error"
294 else : print "OK"
295 myView1.DisplayOnly(cutplanes1)
296
297 print "Restoring second view parameters......",
298 aRestoreRes = myView1.RestoreViewParams(aViewParamsName2)
299 if aRestoreRes != 1 : print "Error"
300 else : print "OK"
301
302 print "Displaying Scalar Map.................",
303 myView1.DisplayOnly(scalarmap1)
304 print "OK"
305
306 print "Restoring third view parameters.......",
307 aRestoreRes = myView1.RestoreViewParams(aViewParamsName3)
308 if aRestoreRes != 1 : print "Error"
309 else : print "OK"
310
311 print "Displaying Cut Planes.................",
312 myView1.DisplayOnly(cutplanes1)
313 print "OK"
314
315 #%====================Stage8: Changing of saved session====================%
316
317 print "**** Stage8: Changing of saved session"
318
319 print "Deleting ViewParams:3,1,2.............",
320 SObj = openedStudy.FindObject(aViewParamsName3)
321 Builder.RemoveObject(SObj)
322 SObj = openedStudy.FindObject(aViewParamsName1)
323 Builder.RemoveObject(SObj)
324 SObj = openedStudy.FindObject(aViewParamsName2)
325 Builder.RemoveObject(SObj)
326 print "OK"
327
328 SObjList2 = openedStudy.FindObjectByName(aViewParamsName1, "VISU")
329 print "FindObjectByName(aViewParamsName1, VISU) returned ", len(SObjList2), " objects"
330
331 print "Zooming trihedron.....................",
332 aScale = myView.GetParallelScale()
333 myView1.SetParallelScale(aScale*2)
334 myView1.Update()
335 print "OK"
336
337 print "Rotating trihedron....................",
338 aPoint = (200,40,-40)
339 myView1.SetPointOfView(aPoint) 
340 print "OK"
341
342 print "Saving view parameters................",
343 aSaveRes = myView1.SaveViewParams(aViewParamsName1)
344 if aSaveRes != 1 : print "Error"
345 else : print "OK"
346
347 print "Renaming ViewParams:1.................",
348 SObj = openedStudy.FindObject(aViewParamsName1)
349 SObj.Name = "New_view"
350 print "OK"
351
352 print "Setting scaling.......................",
353 #Define parameters of scaling:
354 myXParam = 4
355 myYParam = 4
356 myZParam = 8
357
358 myXAxis = VISU.View3D.XAxis
359 myYAxis = VISU.View3D.YAxis
360 myZAxis = VISU.View3D.ZAxis
361
362 myView1.ScaleView(myXAxis,myXParam)
363 myView1.ScaleView(myYAxis,myYParam)
364 myView1.ScaleView(myZAxis,myZParam)
365 print "OK"
366
367 myView1.FitAll()
368
369 print "Saving view parameters................",
370 aSaveRes = myView1.SaveViewParams(aViewParamsName2)
371 if aSaveRes != 1 : print "Error"
372 else : print "OK"
373
374 print "Restoring first view parameters.......",
375 aRestoreRes = myView1.RestoreViewParams(aViewParamsName1)
376 if aSaveRes != 1 : print "Error"
377 else : print "OK"
378
379 print "Restoring second view parameters......",
380 aRestoreRes = myView1.RestoreViewParams(aViewParamsName2)
381 if aRestoreRes != 1 : print "Error"
382 else : print "OK"
383
384 # Remove the study file
385 if not sys.platform == "win32":
386   command = "rm -r " + file
387 else:
388   command = "del /F " + file
389 os.system(command)