Salome HOME
Merge Python 3 porting.
[modules/paravis.git] / test / VisuPrs / SWIG_scripts / A9.py
1 # Copyright (C) 2010-2016  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 import sys
27 from time import sleep
28
29 from presentations import *
30
31
32 # Delay
33 DELAY = 0.25
34
35 # MED files and table directories
36 samples_dir = os.getenv("DATA_DIR")
37 datadir = None
38 tablesdir = None
39 if samples_dir is not None:
40     samples_dir = os.path.normpath(samples_dir)
41     datadir = samples_dir + "/MedFiles/"
42     tablesdir = samples_dir + "/Tables/"
43
44 # Get view
45 view = pvs.GetRenderView()
46 sleep(DELAY)
47
48 # Destroy the view
49 delete_pv_object(view)
50
51
52 # Create view and set background
53 view = pvs.CreateRenderView()
54 reset_view(view)
55
56 color = [0, 0.3, 1]
57 view.Background = color
58 pvs.Render()
59
60 # Load MED reader plugin
61 pv_root_dir = os.getenv("PARAVIS_ROOT_DIR")
62 if sys.platform == "win32":
63     pvs.LoadPlugin(pv_root_dir + "/lib/paraview/MEDReaderPlugin.dll")
64 elif sys.platform == "darwin":
65     pvs.LoadPlugin(pv_root_dir + "/lib/paraview/libMEDReaderPlugin.dylib")
66 else:
67     pvs.LoadPlugin(pv_root_dir + "/lib/paraview/libMEDReaderPlugin.so")
68
69 # Import MED file
70 med_file = datadir + "pointe.med"
71 field_name = "fieldnodedouble"
72 entity = EntityType.NODE
73 timestamp = 1
74
75 med_reader = pvs.MEDReader(FileName=med_file)
76 med_reader.UpdatePipeline()
77
78 # Create scalar map
79 scalarmap = ScalarMapOnField(med_reader, entity, field_name, timestamp)
80 pvs.Show(scalarmap.Input)
81
82 # Set view properties
83 print("view.CameraFocalPoint = [0, 0, 0]")
84 view.CameraFocalPoint = [0, 0, 0]
85 print("view.CameraParallelScale = 2")
86 view.CameraParallelScale = 2
87 print("pvs.ResetCamera(view)")
88 pvs.ResetCamera(view)
89
90 # Play with scalar bar
91 bar = get_bar()
92 lt = bar.LookupTable
93
94 range_min = lt.RGBPoints[0]
95 range_max = lt.RGBPoints[4]
96 delta = (range_max - range_min) / 2.0
97 nb_colors = lt.NumberOfTableValues
98 nb_colors = 64
99 lt.Discretize = 1
100 for i in range(2, nb_colors):
101     lt.NumberOfTableValues = nb_colors
102     x = range_min + delta * i / nb_colors
103     y = range_max - delta * i / nb_colors
104     lt.RGBPoints[0] = x
105     lt.RGBPoints[4] = y
106     pvs.Render(view)
107     sleep(DELAY / 4.0)
108
109 lt.RGBPoints[0] = range_min
110 lt.RGBPoints[4] = range_max
111
112 print("pvs.ResetCamera(view)")
113 pvs.ResetCamera(view)
114
115 # Create another view for cut planes
116 view = pvs.CreateRenderView()
117 reset_view(view)
118
119 color = [0, 0.7, 0]
120 view.Background = color
121 pvs.Render(view)
122
123 displacement = 0.5
124 orient = Orientation.YZ
125 cutplanes = CutPlanesOnField(med_reader, entity, field_name, timestamp,
126                              orientation=orient,
127                              displacement=displacement)
128 print("CutPlanesOnField(...)")
129
130 display_only(cutplanes, view)
131 print("display_only(cutplanes, view)")
132
133 cam_pos = view.CameraPosition
134 cam_pos[0] = cam_pos[0] + 10
135 print("Set view.CameraPosition")
136 cutplanes.Scale[0] = 3
137 cutplanes.Scale[1] = 10
138 pvs.Render(view)
139 sleep(DELAY)
140
141 pvs.ResetCamera(view)
142
143 slice_filter = cutplanes.Input
144 offset_vals = slice_filter.SliceOffsetValues
145 nb_planes = len(offset_vals)
146 nb_planes = 30
147 bounds = get_bounds(med_reader)
148 for i in range(nb_planes, 1, -1):
149     x = math.pi / 2.0 * (nb_planes - i) / nb_planes
150     y = math.pi / 2.0 * (nb_planes - i) / nb_planes
151     normal = get_normal_by_orientation(orient, x, y)
152     slice_filter.SliceType.Normal = normal
153     pos = get_positions(i, normal, bounds, displacement)
154     slice_filter.SliceOffsetValues = pos
155     pvs.Render(view)
156     sleep(DELAY)
157
158 nb_planes = 10
159 normal = [0, 1, 0]
160 slice_filter.SliceType.Normal = normal
161 for i in range(1, nb_planes):
162     pos = get_positions(i, normal, bounds, displacement)
163     slice_filter.SliceOffsetValues = pos
164     pvs.Render(view)
165     sleep(DELAY)
166
167 slice_filter.SliceType.Normal = [0, 0, 1]
168 slice_filter.UpdatePipeline()
169 print("pvs.ResetCamera(view)")
170 pvs.ResetCamera(view)
171
172 # Create one more view for iso surfaces
173 view = pvs.CreateRenderView()
174 reset_view(view)
175
176 color = [1, 0.7, 0]
177 view.Background = color
178 pvs.Render(view)
179 sleep(DELAY)
180
181 isosurf = IsoSurfacesOnField(med_reader, entity, field_name, timestamp)
182 display_only(isosurf, view)
183 pvs.ResetCamera(view)
184 print("display_only(isosurf, view)")
185 sleep(DELAY)
186
187 contour = isosurf.Input
188 nb_surfaces = len(contour.Isosurfaces)
189 nb_surfaces = 32
190 scalar_range = get_data_range(med_reader, entity, field_name, cut_off=True)
191 for i in range(2, nb_surfaces):
192     contours = get_contours(scalar_range, i)
193     contour.Isosurfaces = contours
194     pvs.Render(view)
195     sleep(DELAY)
196
197 contour.Isosurfaces = get_contours(scalar_range, 10)
198 contour.UpdatePipeline()
199 print("pvs.ResetCamera(view)")
200 pvs.ResetCamera(view)
201
202 # Create one more view for cut lines
203 view = pvs.CreateRenderView()
204 reset_view(view)
205
206 color = [0.7, 0.7, 0.7]
207 view.Background = color
208 pvs.Render(view)
209 sleep(DELAY)
210
211 cutlines, curves = CutLinesOnField(med_reader, entity, field_name, timestamp,
212                                    orientation1=Orientation.ZX,
213                                    orientation2=Orientation.YZ,
214                                    generate_curves=True)
215 print("CutLinesOnField(...)")
216 display_only(cutlines, view)
217 pvs.ResetCamera(view)
218 print("display_only(cutlines, view)")
219 sleep(DELAY)
220
221 xy_view = pvs.CreateXYPlotView()
222 print("pvs.CreateXYPlotView()")
223 index = 0
224 for curve in curves:
225     xyrep = pvs.Show(curve, xy_view)
226     xyrep.AttributeType = 'Point Data'
227     xyrep.UseIndexForXAxis = 0
228     xyrep.XArrayName = 'arc_length'
229     pvs.Render(xy_view)
230     set_visible_lines(xyrep, [field_name])
231     xyrep.SeriesLabel = [field_name, 'Y' + str(index)]
232     r = str(random.random())
233     g = str(random.random())
234     b = str(random.random())
235     xyrep.SeriesColor = [field_name, r, g, b]
236     index += 1
237
238 pvs.Render(xy_view)
239 sleep(DELAY)
240
241 # Create one more view for cut segment
242 view = pvs.CreateRenderView()
243 reset_view(view)
244
245 color = [0.0, 0.7, 0.3]
246 view.Background = color
247 pvs.Render(view)
248 sleep(DELAY)
249
250 point1 = [0.0, -1.0, 2.5]
251 point2 = [0.0, 1.0, 2.5]
252 cutsegment = CutSegmentOnField(med_reader, entity, field_name, timestamp,
253                                point1, point2)
254 print("CutSegmentOnField(...)")
255 display_only(cutsegment, view)
256 pvs.ResetCamera(view)
257 print("display_only(cutsegment, view)")
258 sleep(DELAY)
259
260 xy_view = pvs.CreateXYPlotView()
261 curve = pvs.Show(cutsegment.Input, xy_view)
262 curve.AttributeType = 'Point Data'
263 curve.UseIndexForXAxis = 0
264 curve.XArrayName = 'arc_length'
265 set_visible_lines(xyrep, [field_name])
266
267 pvs.Render(xy_view)
268 sleep(DELAY)
269
270
271 # Create one more view for animation
272 view = pvs.CreateRenderView()
273 reset_view(view)
274
275 med_file = datadir + "TimeStamps.med"
276 field_name = "vitesse"
277 entity = EntityType.NODE
278 timestamp = 2
279
280 med_reader = pvs.MEDReader(FileName=med_file)
281
282 isosurf = IsoSurfacesOnField(med_reader, entity, field_name, timestamp)
283 pvs.ResetCamera(view)
284
285 print("Start Animation")
286 pvs.AnimateReader(med_reader, view)