1 # Copyright (C) 2010-2016 CEA/DEN, EDF R&D
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.
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.
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
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 This module provides auxiliary classes, functions and variables for testing.
24 #from __future__ import print_function
30 from datetime import date
35 samples_dir = os.getenv("DATA_DIR")
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 texturesdir = samples_dir + "/Textures/"
45 # Graphics files extension
46 pictureext = os.getenv("PIC_EXT")
47 if pictureext == None:
52 class RepresentationType:
54 Types of representation.
64 _type2name = {OUTLINE: 'Outline',
66 WIREFRAME: 'Wireframe',
68 SURFACEEDGES: 'Surface With Edges',
70 POINTSPRITE: 'Point Sprite'}
73 def get_name(cls, type):
74 """Return paraview representation type by the primitive type."""
75 return cls._type2name[type]
79 def test_values(value, et_value, check_error=0):
83 et_length = len(et_value)
84 if (length != et_length):
85 err_msg = "ERROR!!! There is different number of created " + str(length) + " and etalon " + str(et_length) + " values!!!"
89 for i in range(et_length):
90 if abs(et_value[i]) > 1:
91 max_val = abs(0.001 * et_value[i])
92 if abs(et_value[i] - value[i]) > max_val:
93 err_msg = "ERROR!!! Got value " + str(value[i]) + " is not equal to etalon value " + str(ret_value[i]) + "!!!"
98 if abs(et_value[i] - value[i]) > max_val:
99 err_msg = "ERROR!!! Got value " + value[i] + " is not equal to etalon value " + et_value[i] + "!!!"
101 if check_error and error > 0:
102 err_msg = ("There is(are) some error(s) was(were) found... "
103 "For more info see ERRORs above...")
104 raise RuntimeError(err_msg)
108 def get_picture_dir(subdir):
109 res_dir = os.getenv("PARAVIS_TEST_PICS")
111 # Add username and current date to unify the directory
112 cur_user = getpass.getuser()
113 cur_date = date.today().strftime("%Y%m%d")
114 res_dir = tempfile.gettempdir() + \
115 "/pic_" + cur_user + \
117 # Add subdirectory for the case to the directory path
118 res_dir += "/" + subdir
119 # Create the directory if doesn't exist
120 res_dir = os.path.normpath(res_dir)
121 if not os.path.exists(res_dir):
124 # Clean the directory
125 for root, dirs, files in os.walk(res_dir):
127 os.remove(os.path.join(root, f))
132 def call_and_check(prs, property_name, value, do_raise=1, compare_toler=-1.0):
133 """Utility function for 3D viewer test for common check of different
134 types of presentation parameters set"""
135 if property_name == 'Representation':
136 if value in prs.GetProperty('RepresentationTypesInfo'):
137 prs.SetPropertyWithName(property_name, value)
140 error_string = (str(value) + " value of " + property_name + " is not available for this type of presentations")
143 prs.SetPropertyWithName(property_name, value)
145 error_string = (str(value) + "value of " + property_name + " is not available for this type of presentations")
148 is_good = (error_string is None)
151 raise RuntimeError(error_string)
155 # compare just set value and the one got from presentation
156 really_set_value = prs.GetPropertyValue(property_name)
158 if compare_toler > 0:
159 is_equal = (fabs(really_set_value - value) < compare_toler)
161 is_equal = (really_set_value == value)
163 msg = str(really_set_value) + " has been set instead"
165 raise RuntimeError(msg)
173 def compare_lists(value, et_value, check_error=0, eps=1e-04):
175 Compare two lists: the same length and equality of corresponding items
176 param value - list to be compared
177 param et_value - etalon list
178 param check_error - flag to catch exception if errors>0
179 check_error=0 no exception, check_error !=0 catch exception
180 param eps - defines tolerance for comparison
181 return error - number of errors
186 et_length = len(et_value)
187 if length != et_length:
188 print "ERROR!!! There is different number of items in created ", length, " and etalon ", et_length, " lists!!!"
191 for i in range(et_length):
192 if abs(et_value[i]) > 1:
193 MAX = abs(eps*et_value[i])
196 if abs(et_value[i] - value[i])> MAX:
197 print "ERROR!!!", i, "-th item", value[i], " is not equal to etalon item", et_value[i], "!!!"
199 if check_error and error > 0:
200 raise RuntimeError, "There is(are) some error(s) was(were) found... For more info see ERRORs above..."
204 def setShaded(view, shading):
205 """Utility function to set shaded mode in view"""
207 view.LightDiffuseColor = [1, 1, 1]
209 view.LightDiffuseColor = [0, 0, 0]
212 def TimeStampId(proxy):
213 """Return tuple for the given MED proxy: (mesh_name, {field_name: [entity, timestamp_id]})
214 Originally defined in KERNEL_TEST/Tools/CommonFunctions file.
217 mesh_name = presentations.get_mesh_full_names(proxy).pop()
220 # get list of field names
221 all_fields = proxy.GetProperty("FieldsTreeInfo")[::2]
224 timestamps = proxy.TimestepValues.GetData()
225 timestamp_nb = len(timestamps)
227 for field in all_fields:
228 entity = presentations.get_field_entity(field)
229 field_short_name = presentations.get_field_short_name(field)
231 iterations[field_short_name] = [entity, timestamp_nb]
233 return mesh_name, iterations
236 def Import_Med_Field(filename, field_names, check_errors=0, prs=[]):
237 """Builds presentations on the given fields of the MED file.
238 Originally defined in VISU_TEST/Grids/visu/ImportMedField/begin file.
241 filename : the full path to med file
242 field_names : the list of field names (for ex: ["pression","temperature","vitesse"])
243 prs : [[0,1,...], [], []]; empty list (sublist(s)) is ignored
249 5-VISU.TDEFORMEDSHAPE
253 9-VISU.TSCALARMAPONDEFORMEDSHAPE
259 print "File: ", filename
261 # check the file accessibility
262 if not os.access(filename, os.F_OK):
263 msg = "File " + filename + " does not exist!!!"
264 raise RuntimeError, msg
268 pvsimple.OpenDataFile(filename)
269 proxy = presentations.pvs.GetActiveSource()
271 raise RuntimeError, "ERROR!!! Can't import file!!!"
273 for i in range(len(field_names)):
274 print "Name of the field: ", field_names[i]
278 mesh_name, iterations = TimeStampId(proxy)
280 if iterations.has_key(field_names[i]):
281 entity = iterations[field_names[i]][0]
282 iteration = iterations[field_names[i]][1]
284 msg="There is no information about TimeStampId of the " + field_names[i] + " field!!!"
285 raise RuntimeError, msg
292 if presentations.GaussPointsOnField(proxy, entity, field_names[i], iteration) is None:
293 print "ERROR!!! Created GaussPoints presentation is None!!!"; nb_errors+=1
295 if presentations.ScalarMapOnField(proxy, entity, field_names[i], iteration) is None:
296 print "ERROR!!! Created ScalarMap presentation is None!!!"; nb_errors+=1
298 if presentations.IsoSurfacesOnField(proxy, entity, field_names[i], iteration) is None:
299 print "ERROR!!! Created IsoSurfaces presentation is None!!!"; nb_errors+=1
301 if presentations.CutPlanesOnField(proxy, entity, field_names[i], iteration) is None:
302 print "ERROR!!! Created CutPlanes presentation is None!!!"; nb_errors+=1
304 if presentations.CutLinesOnField(proxy, entity, field_names[i], iteration) is None:
305 print "ERROR!!! Created CutLines presentation is None!!!"; nb_errors+=1
307 if presentations.DeformedShapeOnField(proxy, entity, field_names[i], iteration) is None:
308 print "ERROR!!! Created DeformedShape presentation is None!!!"; nb_errors+=1
310 if presentations.VectorsOnField(proxy, entity, field_names[i], iteration) is None:
311 print "ERROR!!! Created Vectors presentation is None!!!"; nb_errors+=1
313 if presentations.StreamLinesOnField(proxy, entity, field_names[i], iteration) is None:
314 print "ERROR!!! Created StreamLines presentation is None!!!"; nb_errors+=1
316 if presentations.Plot3DOnField(proxy, entity, field_names[i], iteration) is None:
317 print "ERROR!!! Created Plot3D presentation is None!!!"; nb_errors+=1
319 if presentations.DeformedShapeAndScalarMapOnField(proxy, entity, field_names[i], iteration) is None:
320 print "ERROR!!! Created ScalarMapOnDeformedShape presentation is None!!!"; nb_errors+=1
322 """ This exception comes from get_nb_components(...) function.
323 The reason of exception is an implementation of MEDReader
324 activating the first leaf when reading MED file (refer to
325 MEDFileFieldRepresentationTree::activateTheFirst() and
326 MEDFileFieldRepresentationTree::getTheSingleActivated(...) methods).
328 print "ValueError exception is catched"
331 # check if number of errors has increased
333 print "Presentation(s) creation...OK"
335 if nb_errors > 0 and check_errors:
336 raise RuntimeError, "Errors occured!!! For more information see ERRORs above..."
340 def delete_with_inputs(obj):
341 """Deletes the given object with all its inputs"""
345 while obj_to_delete is not None:
346 tmp_obj = obj_to_delete
348 if hasattr(tmp_obj, 'Input'):
349 obj_to_delete = tmp_obj.Input
351 pvsimple.Delete(tmp_obj)