Salome HOME
enable make test
[modules/paravis.git] / test / VisuPrs / SWIG_scripts / A9.py
1 # Copyright (C) 2010-2015  CEA/DEN, EDF R&D
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 # This case corresponds to: /visu/SWIG_scripts/A9 case
21 # Import MED file; create Scalar Map, Cut Planes, Cut Lines,
22 # Cut Segment, Iso Surfaces, Animation; display curves.
23  
24 import math
25 import random
26 from time import sleep
27
28 from presentations import *
29
30
31 # Delay
32 DELAY = 0.25
33
34 # MED files and table directories
35 samples_dir = os.getenv("DATA_DIR")
36 datadir = None
37 tablesdir = None
38 if samples_dir is not None:
39     samples_dir = os.path.normpath(samples_dir)
40     datadir = samples_dir + "/MedFiles/"
41     tablesdir = samples_dir + "/Tables/"
42
43 # Get view
44 view = pvs.GetRenderView()
45 sleep(DELAY)
46
47 # Destroy the view
48 pvs.Delete(view)
49
50
51 # Create view and set background
52 view = pvs.CreateRenderView()
53 reset_view(view)
54
55 color = [0, 0.3, 1]
56 view.Background = color
57 pvs.Render()
58
59 # Load MED reader plugin
60 pv_root_dir = os.getenv("PARAVIS_ROOT_DIR")
61 pvs.LoadPlugin(pv_root_dir + "/lib/paraview/libMedReaderPlugin.so")
62
63 # Import MED file
64 med_file = datadir + "pointe.med"
65 field_name = "fieldnodedouble"
66 entity = EntityType.NODE
67 timestamp = 1
68
69 med_reader = pvs.MEDReader(FileName=med_file)
70 med_reader.UpdatePipeline()
71
72 # Create scalar map
73 scalarmap = ScalarMapOnField(med_reader, entity, field_name, timestamp)
74 pvs.Show(scalarmap.Input)
75
76 # Set view properties
77 print "view.CameraFocalPoint = [0, 0, 0]"
78 view.CameraFocalPoint = [0, 0, 0]
79 print "view.CameraParallelScale = 2"
80 view.CameraParallelScale = 2
81 print "pvs.ResetCamera(view)"
82 pvs.ResetCamera(view)
83
84 # Play with scalar bar
85 bar = get_bar()
86 lt = bar.LookupTable
87
88 range_min = lt.RGBPoints[0]
89 range_max = lt.RGBPoints[4]
90 delta = (range_max - range_min) / 2.0
91 nb_colors = lt.NumberOfTableValues
92 nb_colors = 64
93 lt.Discretize = 1
94 for i in xrange(2, nb_colors):
95     lt.NumberOfTableValues = nb_colors
96     x = range_min + delta * i / nb_colors
97     y = range_max - delta * i / nb_colors
98     lt.RGBPoints[0] = x
99     lt.RGBPoints[4] = y
100     pvs.Render(view)
101     sleep(DELAY / 4.0)
102
103 lt.RGBPoints[0] = range_min
104 lt.RGBPoints[4] = range_max
105
106 print "pvs.ResetCamera(view)"
107 pvs.ResetCamera(view)
108
109 # Create another view for cut planes
110 view = pvs.CreateRenderView()
111 reset_view(view)
112
113 color = [0, 0.7, 0]
114 view.Background = color
115 pvs.Render(view)
116
117 displacement = 0.5
118 orient = Orientation.YZ
119 cutplanes = CutPlanesOnField(med_reader, entity, field_name, timestamp,
120                              orientation=orient,
121                              displacement=displacement)
122 print "CutPlanesOnField(...)"
123
124 display_only(cutplanes, view)
125 print "display_only(cutplanes, view)"
126
127 cam_pos = view.CameraPosition
128 cam_pos[0] = cam_pos[0] + 10
129 print "Set view.CameraPosition"
130 cutplanes.Scale[0] = 3
131 cutplanes.Scale[1] = 10
132 pvs.Render(view)
133 sleep(DELAY)
134
135 pvs.ResetCamera(view)
136
137 slice_filter = cutplanes.Input
138 offset_vals = slice_filter.SliceOffsetValues
139 nb_planes = len(offset_vals)
140 nb_planes = 30
141 bounds = get_bounds(med_reader)
142 for i in xrange(nb_planes, 1, -1):
143     x = math.pi / 2.0 * (nb_planes - i) / nb_planes
144     y = math.pi / 2.0 * (nb_planes - i) / nb_planes
145     normal = get_normal_by_orientation(orient, x, y)
146     slice_filter.SliceType.Normal = normal
147     pos = get_positions(i, normal, bounds, displacement)
148     slice_filter.SliceOffsetValues = pos
149     pvs.Render(view)
150     sleep(DELAY)
151
152 nb_planes = 10
153 normal = [0, 1, 0]
154 slice_filter.SliceType.Normal = normal
155 for i in xrange(1, nb_planes):
156     pos = get_positions(i, normal, bounds, displacement)
157     slice_filter.SliceOffsetValues = pos
158     pvs.Render(view)
159     sleep(DELAY)
160
161 slice_filter.SliceType.Normal = [0, 0, 1]
162 slice_filter.UpdatePipeline()
163 print "pvs.ResetCamera(view)"
164 pvs.ResetCamera(view)
165
166 # Create one more view for iso surfaces
167 view = pvs.CreateRenderView()
168 reset_view(view)
169
170 color = [1, 0.7, 0]
171 view.Background = color
172 pvs.Render(view)
173 sleep(DELAY)
174
175 isosurf = IsoSurfacesOnField(med_reader, entity, field_name, timestamp)
176 display_only(isosurf, view)
177 pvs.ResetCamera(view)
178 print "display_only(isosurf, view)"
179 sleep(DELAY)
180
181 contour = isosurf.Input
182 nb_surfaces = len(contour.Isosurfaces)
183 nb_surfaces = 32
184 scalar_range = get_data_range(med_reader, entity, field_name, cut_off=True)
185 for i in xrange(2, nb_surfaces):
186     contours = get_contours(scalar_range, i)
187     contour.Isosurfaces = contours
188     pvs.Render(view)
189     sleep(DELAY)
190
191 contour.Isosurfaces = get_contours(scalar_range, 10)
192 contour.UpdatePipeline()
193 print "pvs.ResetCamera(view)"
194 pvs.ResetCamera(view)
195
196 # Create one more view for cut lines
197 view = pvs.CreateRenderView()
198 reset_view(view)
199
200 color = [0.7, 0.7, 0.7]
201 view.Background = color
202 pvs.Render(view)
203 sleep(DELAY)
204
205 cutlines, curves = CutLinesOnField(med_reader, entity, field_name, timestamp,
206                                    orientation1=Orientation.ZX,
207                                    orientation2=Orientation.YZ,
208                                    generate_curves=True)
209 print "CutLinesOnField(...)"
210 display_only(cutlines, view)
211 pvs.ResetCamera(view)
212 print "display_only(cutlines, view)"
213 sleep(DELAY)
214
215 xy_view = pvs.CreateXYPlotView()
216 print "pvs.CreateXYPlotView()"
217 index = 0
218 for curve in curves:
219     xyrep = pvs.Show(curve, xy_view)
220     xyrep.AttributeType = 'Point Data'
221     xyrep.UseIndexForXAxis = 0
222     xyrep.XArrayName = 'arc_length'
223     pvs.Render(xy_view)
224     set_visible_lines(xyrep, [field_name])
225     xyrep.SeriesLabel = [field_name, 'Y' + str(index)]
226     r = str(random.random())
227     g = str(random.random())
228     b = str(random.random())
229     xyrep.SeriesColor = [field_name, r, g, b]
230     index += 1
231
232 pvs.Render(xy_view)
233 sleep(DELAY)
234
235 # Create one more view for cut segment
236 view = pvs.CreateRenderView()
237 reset_view(view)
238
239 color = [0.0, 0.7, 0.3]
240 view.Background = color
241 pvs.Render(view)
242 sleep(DELAY)
243
244 point1 = [0.0, -1.0, 2.5]
245 point2 = [0.0, 1.0, 2.5]
246 cutsegment = CutSegmentOnField(med_reader, entity, field_name, timestamp,
247                                point1, point2)
248 print "CutSegmentOnField(...)"
249 display_only(cutsegment, view)
250 pvs.ResetCamera(view)
251 print "display_only(cutsegment, view)"
252 sleep(DELAY)
253
254 xy_view = pvs.CreateXYPlotView()
255 curve = pvs.Show(cutsegment.Input, xy_view)
256 curve.AttributeType = 'Point Data'
257 curve.UseIndexForXAxis = 0
258 curve.XArrayName = 'arc_length'
259 set_visible_lines(xyrep, [field_name])
260
261 pvs.Render(xy_view)
262 sleep(DELAY)
263
264
265 # Create one more view for animation
266 view = pvs.CreateRenderView()
267 reset_view(view)
268
269 med_file = datadir + "TimeStamps.med"
270 field_name = "vitesse"
271 entity = EntityType.NODE
272 timestamp = 2
273
274 med_reader = pvs.MEDReader(FileName=med_file)
275
276 isosurf = IsoSurfacesOnField(med_reader, entity, field_name, timestamp)
277 pvs.ResetCamera(view)
278
279 print "Start Animation"
280 pvs.AnimateReader(med_reader, view)