Back to index

salome-paravis  6.5.0
paravistest.py
Go to the documentation of this file.
00001 # Copyright (C) 2010-2012  CEA/DEN, EDF R&D
00002 #
00003 # This library is free software; you can redistribute it and/or
00004 # modify it under the terms of the GNU Lesser General Public
00005 # License as published by the Free Software Foundation; either
00006 # version 2.1 of the License.
00007 #
00008 # This library is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 # Lesser General Public License for more details.
00012 #
00013 # You should have received a copy of the GNU Lesser General Public
00014 # License along with this library; if not, write to the Free Software
00015 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 #
00017 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 #
00019 
00020 """
00021 This module provides auxiliary classes, functions and variables for testing.
00022 """
00023 
00024 #from __future__ import print_function
00025 
00026 from math import fabs
00027 import os
00028 from datetime import date
00029 
00030 import salome
00031 
00032 
00033 # Auxiliary variables
00034 
00035 # Data directory
00036 samples_dir = os.getenv("DATA_DIR")
00037 datadir = None
00038 tablesdir = None
00039 if samples_dir is not None:
00040     samples_dir = os.path.normpath(samples_dir)
00041     datadir = samples_dir + "/MedFiles/"
00042     tablesdir = samples_dir + "/Tables/"
00043 
00044 # Graphica files extension
00045 pictureext = os.getenv("PIC_EXT")
00046 if pictureext == None:
00047     pictureext = "png"
00048 
00049 
00050 # Auxiliary classes
00051 class RepresentationType:
00052     """
00053     Types of representation.
00054     """
00055     OUTLINE = 0
00056     POINTS = 1
00057     WIREFRAME = 2
00058     SURFACE = 3
00059     SURFACEEDGES = 4
00060     VOLUME = 5
00061     POINTSPRITE = 6
00062 
00063     _type2name = {OUTLINE: 'Outline',
00064                   POINTS: 'Points',
00065                   WIREFRAME: 'Wireframe',
00066                   SURFACE: 'Surface',
00067                   SURFACEEDGES: 'Surface With Edges',
00068                   VOLUME: 'Volume',
00069                   POINTSPRITE: 'Point Sprite'}
00070 
00071     @classmethod
00072     def get_name(cls, type):
00073         """Return paraview representation type by the primitive type."""
00074         return cls._type2name[type]
00075 
00076 
00077 class SalomeSession(object):
00078     def __init__(self):
00079         import runSalome
00080         import sys
00081         sys.argv += ["--killall"]
00082         sys.argv += ["--show-desktop=1"]
00083         sys.argv += ["--splash=0"]
00084         sys.argv += ["--modules=MED,VISU,PARAVIS"]
00085         clt, d = runSalome.main()
00086         port = d['port']
00087         self.port = port
00088         return
00089 
00090     def __del__(self):
00091         #os.system('killSalomeWithPort.py {0}'.format(self.port))
00092         os.system('killSalomeWithPort.py ' + self.port)
00093         return
00094     pass
00095 
00096 
00097 # Auxiliary functions
00098 def test_values(value, et_value, check_error=0):
00099     """Test values."""
00100     error = 0
00101     length = len(value)
00102     et_length = len(et_value)
00103     if (length != et_length):
00104         err_msg = "ERROR!!! There is different number of created " + str(length) + " and etalon " + str(et_length) + " values!!!"
00105         print err_msg
00106         error = error + 1
00107     else:
00108         for i in range(et_length):
00109             if abs(et_value[i]) > 1:
00110                 max_val = abs(0.001 * et_value[i])
00111                 if abs(et_value[i] - value[i]) > max_val:
00112                     err_msg = "ERROR!!! Got value " + str(value[i]) + " is not equal to etalon value " + str(ret_value[i]) + "!!!"
00113                     print err_msg
00114                     error = error + 1
00115             else:
00116                 max_val = 0.001
00117                 if abs(et_value[i] - value[i]) > max_val:
00118                     err_msg = "ERROR!!! Got value " + value[i] + " is not equal to etalon value " + et_value[i] + "!!!"
00119                     error = error + 1
00120     if check_error and error > 0:
00121         err_msg = ("There is(are) some error(s) was(were) found... "
00122                    "For more info see ERRORs above...")
00123         raise RuntimeError(err_msg)
00124     return error
00125 
00126 
00127 def get_picture_dir(pic_dir, subdir):
00128     res_dir = pic_dir
00129     if not res_dir:
00130         res_dir = "/tmp/pic"
00131 
00132     # Add current date and subdirectory for the case to the directory path
00133     cur_date = date.today().strftime("%d%m%Y")
00134     res_dir += "/test_" + cur_date + "/" + subdir
00135     # Create the directory if doesn't exist
00136     res_dir = os.path.normpath(res_dir)
00137     if not os.path.exists(res_dir):
00138         os.makedirs(res_dir)
00139     else:
00140         # Clean the directory
00141         for root, dirs, files in os.walk(res_dir):
00142             for f in files:
00143                 os.remove(os.path.join(root, f))
00144 
00145     return res_dir
00146 
00147 
00148 def call_and_check(prs, property_name, value, do_raise=1, compare_toler=-1.0):
00149     """Utility function for 3D viewer test for common check of different
00150     types of presentation parameters set"""
00151     try:
00152         prs.SetPropertyWithName(property_name, value)
00153     except ValueError:
00154         error_string = (str(value) + "value of " + property_name + " is not available for this type of presentations")
00155     else:
00156         error_string = None
00157     is_good = (error_string is None)
00158     if not is_good:
00159         if do_raise:
00160             raise RuntimeError(error_string)
00161         else:
00162             print error_string
00163     else:
00164         # compare just set value and the one got from presentation
00165         really_set_value = prs.GetPropertyValue(property_name)
00166         is_equal = 1
00167         if compare_toler > 0:
00168             is_equal = (fabs(really_set_value - value) < compare_toler)
00169         else:
00170             is_equal = (really_set_value == value)
00171         if not is_equal:
00172             msg = str(really_set_value) + " has been set instead"
00173             if do_raise:
00174                 raise RuntimeError(msg)
00175             else:
00176                 print msg
00177                 is_good = False
00178 
00179     return is_good
00180 
00181 
00182 def setShaded(view, shading):
00183     """Utility function to set shaded mode in view"""
00184     if shading == 0:
00185         view.LightDiffuseColor = [1, 1, 1]
00186     if shading == 1:
00187         view.LightDiffuseColor = [0, 0, 0]
00188 
00189 
00190 # Run Salome
00191 salome_session = SalomeSession()
00192 salome.salome_init()
00193 
00194 # Create new study
00195 print "Creating new study...",
00196 aStudy = salome.myStudyManager.NewStudy("Study1")
00197 if aStudy is None:
00198     raise RuntimeError("FAILED")
00199 else:
00200     print "OK"
00201 
00202 salome.myStudy = aStudy