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