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