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