Salome HOME
1665c2f208b0d45003f18312b311e3d5cd69166b
[modules/paravis.git] / test / VisuPrs / Util / paravistest.py
1 # Copyright (C) 2010-2013  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.
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 """
21 This module provides auxiliary classes, functions and variables for testing.
22 """
23
24 #from __future__ import print_function
25
26 from math import fabs
27 import os
28 import tempfile
29 import getpass
30 from datetime import date
31
32 import salome
33
34 # Auxiliary variables
35
36 # Data directory
37 samples_dir = os.getenv("DATA_DIR")
38 datadir = None
39 tablesdir = None
40 if samples_dir is not None:
41     samples_dir = os.path.normpath(samples_dir)
42     datadir = samples_dir + "/MedFiles/"
43     tablesdir = samples_dir + "/Tables/"
44
45 # Graphica files extension
46 pictureext = os.getenv("PIC_EXT")
47 if pictureext == None:
48     pictureext = "png"
49
50
51 # Auxiliary classes
52 class RepresentationType:
53     """
54     Types of representation.
55     """
56     OUTLINE = 0
57     POINTS = 1
58     WIREFRAME = 2
59     SURFACE = 3
60     SURFACEEDGES = 4
61     VOLUME = 5
62     POINTSPRITE = 6
63
64     _type2name = {OUTLINE: 'Outline',
65                   POINTS: 'Points',
66                   WIREFRAME: 'Wireframe',
67                   SURFACE: 'Surface',
68                   SURFACEEDGES: 'Surface With Edges',
69                   VOLUME: 'Volume',
70                   POINTSPRITE: 'Point Sprite'}
71
72     @classmethod
73     def get_name(cls, type):
74         """Return paraview representation type by the primitive type."""
75         return cls._type2name[type]
76
77
78 class SalomeSession(object):
79     def __init__(self):
80         import runSalome
81         import sys
82         #sys.argv += ["--killall"]
83         #sys.argv += ["--portkill=" + port]
84         sys.argv += ["--show-desktop=1"]
85         sys.argv += ["--splash=0"]
86         sys.argv += ["--modules=MED,PARAVIS"]
87         sys.argv += ["--standalone=study"]
88         sys.argv += ["--embedded=SalomeAppEngine,cppContainer,registry,moduleCatalog"]
89         clt, d = runSalome.main()
90         port = d['port']
91         self.port = port
92         return
93
94     #VTN: workaround for crash on CentOS.6.3.64
95     #def __del__(self):
96         #os.system('killSalomeWithPort.py {0}'.format(self.port))
97         #os.system('killSalomeWithPort.py ' + self.port)
98         #import killSalomeWithPort
99         #killSalomeWithPort.killMyPort(self.port)
100         #return
101     pass
102
103
104 # Auxiliary functions
105 def test_values(value, et_value, check_error=0):
106     """Test values."""
107     error = 0
108     length = len(value)
109     et_length = len(et_value)
110     if (length != et_length):
111         err_msg = "ERROR!!! There is different number of created " + str(length) + " and etalon " + str(et_length) + " values!!!"
112         print err_msg
113         error = error + 1
114     else:
115         for i in range(et_length):
116             if abs(et_value[i]) > 1:
117                 max_val = abs(0.001 * et_value[i])
118                 if abs(et_value[i] - value[i]) > max_val:
119                     err_msg = "ERROR!!! Got value " + str(value[i]) + " is not equal to etalon value " + str(ret_value[i]) + "!!!"
120                     print err_msg
121                     error = error + 1
122             else:
123                 max_val = 0.001
124                 if abs(et_value[i] - value[i]) > max_val:
125                     err_msg = "ERROR!!! Got value " + value[i] + " is not equal to etalon value " + et_value[i] + "!!!"
126                     error = error + 1
127     if check_error and error > 0:
128         err_msg = ("There is(are) some error(s) was(were) found... "
129                    "For more info see ERRORs above...")
130         raise RuntimeError(err_msg)
131     return error
132
133
134 def get_picture_dir(subdir):
135     res_dir = os.getenv("PARAVIS_TEST_PICS")
136     if not res_dir:
137         # Add username and current date to unify the directory
138         cur_user = getpass.getuser()
139         cur_date = date.today().strftime("%Y%m%d")
140         res_dir = tempfile.gettempdir() + \
141             "/pic_" + cur_user + \
142             "/test_" + cur_date
143     # Add subdirectory for the case to the directory path
144     res_dir += "/" + subdir
145     # Create the directory if doesn't exist
146     res_dir = os.path.normpath(res_dir)
147     if not os.path.exists(res_dir):
148         os.makedirs(res_dir)
149     else:
150         # Clean the directory
151         for root, dirs, files in os.walk(res_dir):
152             for f in files:
153                 os.remove(os.path.join(root, f))
154
155     return res_dir
156
157
158 def call_and_check(prs, property_name, value, do_raise=1, compare_toler=-1.0):
159     """Utility function for 3D viewer test for common check of different
160     types of presentation parameters set"""
161     try:
162         prs.SetPropertyWithName(property_name, value)
163     except ValueError:
164         error_string = (str(value) + "value of " + property_name + " is not available for this type of presentations")
165     else:
166         error_string = None
167     is_good = (error_string is None)
168     if not is_good:
169         if do_raise:
170             raise RuntimeError(error_string)
171         else:
172             print error_string
173     else:
174         # compare just set value and the one got from presentation
175         really_set_value = prs.GetPropertyValue(property_name)
176         is_equal = 1
177         if compare_toler > 0:
178             is_equal = (fabs(really_set_value - value) < compare_toler)
179         else:
180             is_equal = (really_set_value == value)
181         if not is_equal:
182             msg = str(really_set_value) + " has been set instead"
183             if do_raise:
184                 raise RuntimeError(msg)
185             else:
186                 print msg
187                 is_good = False
188
189     return is_good
190
191
192 def setShaded(view, shading):
193     """Utility function to set shaded mode in view"""
194     if shading == 0:
195         view.LightDiffuseColor = [1, 1, 1]
196     if shading == 1:
197         view.LightDiffuseColor = [0, 0, 0]
198
199
200 # Run Salome
201 salome_session = SalomeSession()
202 salome.salome_init()