1 # Copyright (C) 2010-2014 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
38 samples_dir = os.getenv("DATA_DIR")
41 if samples_dir is not None:
42 samples_dir = os.path.normpath(samples_dir)
43 datadir = samples_dir + "/MedFiles/"
44 tablesdir = samples_dir + "/Tables/"
46 # Graphics files extension
47 pictureext = os.getenv("PIC_EXT")
48 if pictureext == None:
53 class RepresentationType:
55 Types of representation.
65 _type2name = {OUTLINE: 'Outline',
67 WIREFRAME: 'Wireframe',
69 SURFACEEDGES: 'Surface With Edges',
71 POINTSPRITE: 'Point Sprite'}
74 def get_name(cls, type):
75 """Return paraview representation type by the primitive type."""
76 return cls._type2name[type]
79 class SalomeSession(object):
83 #sys.argv += ["--killall"]
84 #sys.argv += ["--portkill=" + port]
85 sys.argv += ["--show-desktop=1"]
86 sys.argv += ["--splash=0"]
87 sys.argv += ["--modules=MED,PARAVIS"]
88 sys.argv += ["--standalone=study"]
89 sys.argv += ["--embedded=SalomeAppEngine,cppContainer,registry,moduleCatalog"]
90 clt, d = runSalome.main()
95 #VTN: workaround for crash on CentOS.6.3.64
97 #os.system('killSalomeWithPort.py {0}'.format(self.port))
98 #os.system('killSalomeWithPort.py ' + self.port)
99 #import killSalomeWithPort
100 #killSalomeWithPort.killMyPort(self.port)
105 # Auxiliary functions
106 def test_values(value, et_value, check_error=0):
110 et_length = len(et_value)
111 if (length != et_length):
112 err_msg = "ERROR!!! There is different number of created " + str(length) + " and etalon " + str(et_length) + " values!!!"
116 for i in range(et_length):
117 if abs(et_value[i]) > 1:
118 max_val = abs(0.001 * et_value[i])
119 if abs(et_value[i] - value[i]) > max_val:
120 err_msg = "ERROR!!! Got value " + str(value[i]) + " is not equal to etalon value " + str(ret_value[i]) + "!!!"
125 if abs(et_value[i] - value[i]) > max_val:
126 err_msg = "ERROR!!! Got value " + value[i] + " is not equal to etalon value " + et_value[i] + "!!!"
128 if check_error and error > 0:
129 err_msg = ("There is(are) some error(s) was(were) found... "
130 "For more info see ERRORs above...")
131 raise RuntimeError(err_msg)
135 def get_picture_dir(subdir):
136 res_dir = os.getenv("PARAVIS_TEST_PICS")
138 # Add username and current date to unify the directory
139 cur_user = getpass.getuser()
140 cur_date = date.today().strftime("%Y%m%d")
141 res_dir = tempfile.gettempdir() + \
142 "/pic_" + cur_user + \
144 # Add subdirectory for the case to the directory path
145 res_dir += "/" + subdir
146 # Create the directory if doesn't exist
147 res_dir = os.path.normpath(res_dir)
148 if not os.path.exists(res_dir):
151 # Clean the directory
152 for root, dirs, files in os.walk(res_dir):
154 os.remove(os.path.join(root, f))
159 def call_and_check(prs, property_name, value, do_raise=1, compare_toler=-1.0):
160 """Utility function for 3D viewer test for common check of different
161 types of presentation parameters set"""
162 if property_name == 'Representation':
163 if value in prs.GetProperty('RepresentationTypesInfo'):
164 prs.SetPropertyWithName(property_name, value)
167 error_string = (str(value) + " value of " + property_name + " is not available for this type of presentations")
170 prs.SetPropertyWithName(property_name, value)
172 error_string = (str(value) + "value of " + property_name + " is not available for this type of presentations")
175 is_good = (error_string is None)
178 raise RuntimeError(error_string)
182 # compare just set value and the one got from presentation
183 really_set_value = prs.GetPropertyValue(property_name)
185 if compare_toler > 0:
186 is_equal = (fabs(really_set_value - value) < compare_toler)
188 is_equal = (really_set_value == value)
190 msg = str(really_set_value) + " has been set instead"
192 raise RuntimeError(msg)
200 def compare_lists(value, et_value, check_error=0, eps=1e-04):
202 Compare two lists: the same length and equality of corresponding items
203 param value - list to be compared
204 param et_value - etalon list
205 param check_error - flag to catch exception if errors>0
206 check_error=0 no exception, check_error !=0 catch exception
207 param eps - defines tolerance for comparison
208 return error - number of errors
213 et_length = len(et_value)
214 if length != et_length:
215 print "ERROR!!! There is different number of items in created ", length, " and etalon ", et_length, " lists!!!"
218 for i in range(et_length):
219 if abs(et_value[i]) > 1:
220 MAX = abs(eps*et_value[i])
223 if abs(et_value[i] - value[i])> MAX:
224 print "ERROR!!!", i, "-th item", value[i], " is not equal to etalon item", et_value[i], "!!!"
226 if check_error and error > 0:
227 raise RuntimeError, "There is(are) some error(s) was(were) found... For more info see ERRORs above..."
231 def setShaded(view, shading):
232 """Utility function to set shaded mode in view"""
234 view.LightDiffuseColor = [1, 1, 1]
236 view.LightDiffuseColor = [0, 0, 0]
239 def TimeStampId(proxy):
240 """Return tuple for the given MED proxy: (mesh_name, {field_name: [entity, timestamp_id]})
241 Originally defined in KERNEL_TEST/Tools/CommonFunctions file.
244 mesh_name = presentations.get_mesh_names(proxy).pop()
247 # get list of field names
248 fields_on_points = list(proxy.PointArrays)
249 fields_on_cells = list(proxy.CellArrays)
250 all_fields = fields_on_points + fields_on_cells
253 timestamps = proxy.TimestepValues.GetData()
254 timestamp_nb = len(timestamps)
256 for field in all_fields:
258 if fields_on_points.count(field) > 0:
259 entity = presentations.EntityType.NODE
260 elif fields_on_cells.count(field) > 0:
261 entity = presentations.EntityType.CELL
263 iterations[field] = [entity, timestamp_nb]
265 return mesh_name, iterations
268 def Import_Med_Field(paravis, file, field_names, check_errors=0, prs=[]):
269 """Builds presentations on the given fields of the MED file.
270 Originally defined in VISU_TEST/Grids/visu/ImportMedField/begin file.
273 paravis : PARAVIS instance
274 file_name : the full path to med file
275 field_names : the list of field names (for ex: ["pression","temperature","vitesse"])
276 prs : [[0,1,...], [], []]; empty list (sublist(s)) is ignored
282 5-VISU.TDEFORMEDSHAPE
286 9-VISU.TSCALARMAPONDEFORMEDSHAPE
294 # check the file accessibility
295 if not os.access(file, os.F_OK):
296 msg = "File " + file + " does not exist!!!"
297 raise RuntimeError, msg
300 paravis.ImportFile(file)
301 proxy = presentations.pvs.GetActiveSource()
303 raise RuntimeError, "ERROR!!! Can't import file!!!"
305 for i in range(len(field_names)):
306 print "Name of the field: ", field_names[i]
310 mesh_name, iterations = TimeStampId(proxy)
312 if iterations.has_key(field_names[i]):
313 entity = iterations[field_names[i]][0]
314 iteration = iterations[field_names[i]][1]
316 msg="There is no information about TimeStampId of the " + field_names[i] + " field!!!"
317 raise RuntimeError, msg
323 if presentations.GaussPointsOnField(proxy, entity, field_names[i], iteration) is None:
324 print "ERROR!!! Created GaussPoints presentation is None!!!"; nb_errors+=1
326 if presentations.ScalarMapOnField(proxy, entity, field_names[i], iteration) is None:
327 print "ERROR!!! Created ScalarMap presentation is None!!!"; nb_errors+=1
329 if presentations.IsoSurfacesOnField(proxy, entity, field_names[i], iteration) is None:
330 print "ERROR!!! Created IsoSurfaces presentation is None!!!"; nb_errors+=1
332 if presentations.CutPlanesOnField(proxy, entity, field_names[i], iteration) is None:
333 print "ERROR!!! Created CutPlanes presentation is None!!!"; nb_errors+=1
335 if presentations.CutLinesOnField(proxy, entity, field_names[i], iteration) is None:
336 print "ERROR!!! Created CutLines presentation is None!!!"; nb_errors+=1
338 if presentations.DeformedShapeOnField(proxy, entity, field_names[i], iteration) is None:
339 print "ERROR!!! Created DeformedShape presentation is None!!!"; nb_errors+=1
341 if presentations.VectorsOnField(proxy, entity, field_names[i], iteration) is None:
342 print "ERROR!!! Created Vectors presentation is None!!!"; nb_errors+=1
344 if presentations.StreamLinesOnField(proxy, entity, field_names[i], iteration) is None:
345 print "ERROR!!! Created StreamLines presentation is None!!!"; nb_errors+=1
347 if presentations.Plot3DOnField(proxy, entity, field_names[i], iteration) is None:
348 print "ERROR!!! Created Plot3D presentation is None!!!"; nb_errors+=1
350 if presentations.DeformedShapeAndScalarMapOnField(proxy, entity, field_names[i], iteration) is None:
351 print "ERROR!!! Created ScalarMapOnDeformedShape presentation is None!!!"; nb_errors+=1
353 # check if number of errors has increased
355 print "Presentation(s) creation...OK"
357 if nb_errors > 0 and check_errors:
358 raise RuntimeError, "There are some errors were occured!!! For more information see ERRORs above..."
362 def delete_with_inputs(obj):
363 """Deletes the given object with all its inputs"""
367 while obj_to_delete is not None:
368 tmp_obj = obj_to_delete
370 if hasattr(tmp_obj, 'Input'):
371 obj_to_delete = tmp_obj.Input
373 pvsimple.Delete(tmp_obj)
376 salome_session = SalomeSession()