-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslice.py
104 lines (76 loc) · 3.04 KB
/
slice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Export slice of cgns meshes
# Examples:
# pvbatch slice.py --field Pressure -x --loc 0.2 solution.cgns
# pvbatch slice.py --field Velocity -z solution.cgns
# pvbatch slice.py --help
#
# MIT license: Sebastien Lemaire
import os
import argparse
from paraview.simple import *
parser = argparse.ArgumentParser(description='Slice cgns files. To be used with pvbatch for example: pvbatch slice.py -y file.cgns')
parser.add_argument('path', type=str, nargs='?', help='cgns file path', default='.')
parser.add_argument('-x', action='store_true', default=False,
help='Slice in the x direction')
parser.add_argument('-y', action='store_true', default=False,
help='Slice in the y direction')
parser.add_argument('-z', action='store_true', default=False,
help='Slice in the z direction')
parser.add_argument('--field', type=str, default="Velocity",
help='Field to visualise')
parser.add_argument('--loc', type=float, default=0,
help='Slice location in the slice normal direction')
args = parser.parse_args()
if args.x:
slice_name = "x"
Normal = [1, 0, 0]
Origin = [args.loc, 0, 0]
elif args.y:
slice_name = "y"
Normal = [0, 1, 0]
Origin = [0, args.loc, 0]
else:
# if args.z:
slice_name = "z"
Normal = [0, 0, -1]
Origin = [0, 0, args.loc]
field = args.field
cgnsfile = args.path
export_filename = "{}_slice_{}_{}.png".format(os.path.splitext(cgnsfile)[0],
slice_name,
field)
solutionCgns = CGNSSeriesReader(registrationName='solution.cgns', FileNames=[cgnsfile])
solutionCgns.Bases = ['Base_Volume_elements']
solutionCgns.CellArrayStatus = []
solutionCgns.CellArrayStatus = [field]
renderView1 = GetActiveViewOrCreate('RenderView')
slice_obj = Slice(registrationName='slice_obj', Input=solutionCgns)
slice_obj.SliceType.Origin = Origin
slice_obj.SliceType.Normal = Normal
slice_objDisplay = Show(slice_obj, renderView1, 'GeometryRepresentation')
# update the view to ensure updated data information
renderView1.Update()
# set scalar coloring
if field == "Velocity":
ColorBy(slice_objDisplay, ('CELLS', field, 'Magnitude'))
else:
ColorBy(slice_objDisplay, ('CELLS', field))
# rescale color and/or opacity maps used to include current data range
slice_objDisplay.RescaleTransferFunctionToDataRange(True, False)
# show color bar/color legend
slice_objDisplay.SetScalarBarVisibility(renderView1, True)
ArrayRange = slice_obj.CellData.GetArray(field).GetRange(-1)
variableLUT = GetColorTransferFunction(field)
variableLUT.RescaleTransferFunction(ArrayRange[0], ArrayRange[1])
# get layout
layout1 = GetLayout()
layout1.SetSize(1920, 1080)
renderView1.InteractionMode = '2D'
renderView1.CameraPosition = [0, 0, 0]
renderView1.CameraFocalPoint = Normal
renderView1.ResetCamera()
# save screenshot
SaveScreenshot(export_filename, renderView1, ImageResolution=[1920, 1080])
print("{} saved".format(export_filename))