From f8c62679b948f4008d0189c8ce9d8109a71e3357 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me?= Date: Thu, 24 Sep 2020 10:16:11 +0200 Subject: [PATCH] Add unitary tests --- src/ConnectorAPI/Test/TestExportSTL.py | 159 +++++++++++++++++++++++++ src/ConnectorAPI/Test/tests.set | 1 + 2 files changed, 160 insertions(+) create mode 100644 src/ConnectorAPI/Test/TestExportSTL.py diff --git a/src/ConnectorAPI/Test/TestExportSTL.py b/src/ConnectorAPI/Test/TestExportSTL.py new file mode 100644 index 000000000..edc7eaf01 --- /dev/null +++ b/src/ConnectorAPI/Test/TestExportSTL.py @@ -0,0 +1,159 @@ +# Copyright (C) 2014-2020 CEA/DEN, EDF R&D +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +# + +""" + TestExport.py + Unit test of ExchangePlugin_TestExport class +""" +#========================================================================= +# Initialization of the test +#========================================================================= + +import salome + +import os +import math +from tempfile import TemporaryDirectory + +import GEOM + +from ModelAPI import * + +from salome.shaper import model + +from salome.geom import geomBuilder + +from GeomAlgoAPI import * + +__updated__ = "2015-05-22" + +salome.salome_init(1) +geompy = geomBuilder.New() + +#========================================================================= +# Help functions +#========================================================================= +def removeFile(theFileName): + try: os.remove(theFileName) + except OSError: pass + assert not os.path.exists(theFileName), \ + "Can not remove file {0}".format(theFileName) + +#========================================================================= +# test Export STL +#========================================================================= +def testExportSTL(theFile, theDelta, theErrorExpected = False): + + model.begin() + partSet = model.moduleDocument() + Part_1 = model.addPart(partSet) + Part_1_doc = Part_1.document() + Box_1 = model.addBox(Part_1_doc, 10, 10, 10) + Box_2 = model.addBox(Part_1_doc, 20, 20, 20) + model.do() + + # First export to GEOM + model.exportToGEOM(Part_1_doc) + model.end() + + theSurface = 600 + + print("theFile=",theFile) + + # deflection relative 0.0001 et Ascii + model.exportToSTL(Part_1_doc, theFile, model.selection("SOLID","Box_1_1"),0.0001, 0.5, True,False) + + #== assert os.path.exists(theFile) + + # Check results + test_stl_1 = geompy.ImportSTL(theFile) + Props = geompy.BasicProperties(test_stl_1) + print("\nBasic Properties:") + print(" Wires length: ", Props[0]) + print(" Surface area: ", Props[1]) + print(" Volume : ", Props[2]) + + aRefSurface = theSurface + aResSurface = Props[1] + assert (math.fabs(aResSurface - aRefSurface) < theDelta), "The volume is wrong: expected = {0}, real = {1}".format(aRefSurface, aResSurface) + + removeFile(theFile) + + theSurface = 600 + # deflection relative 0.0001 et binaire + model.exportToSTL(Part_1_doc, theFile, model.selection("SOLID", "Box_1_1" ),0.0001, 0.5, True,True) + + # Check results + test_stl_1 = geompy.ImportSTL(theFile) + Props = geompy.BasicProperties(test_stl_1) + print("\nBasic Properties:") + print(" Wires length: ", Props[0]) + print(" Surface area: ", Props[1]) + print(" Volume : ", Props[2]) + + aRefSurface = theSurface + aResSurface = Props[1] + assert (math.fabs(aResSurface - aRefSurface) < theDelta), "The volume is wrong: expected = {0}, real = {1}".format(aRefSurface, aResSurface) + + removeFile(theFile) + + theSurface = 600 + # deflection absolue et AScii + model.exportToSTL(Part_1_doc, theFile, model.selection("SOLID", "Box_1_1" ),0.0001, 0.5, False, False) + + # Check results + test_stl_1 = geompy.ImportSTL(theFile) + Props = geompy.BasicProperties(test_stl_1) + print("\nBasic Properties:") + print(" Wires length: ", Props[0]) + print(" Surface area: ", Props[1]) + print(" Volume : ", Props[2]) + + aRefSurface = theSurface + aResSurface = Props[1] + assert (math.fabs(aResSurface - aRefSurface) < theDelta), "The volume is wrong: expected = {0}, real = {1}".format(aRefSurface, aResSurface) + + theSurface = 600 + # deflection absolue et binaire + model.exportToSTL(Part_1_doc, theFile, model.selection("SOLID", "Box_1_1" ),0.0001, 0.5, False,True) + + # Check results + test_stl_1 = geompy.ImportSTL(theFile) + Props = geompy.BasicProperties(test_stl_1) + print("\nBasic Properties:") + print(" Wires length: ", Props[0]) + print(" Surface area: ", Props[1]) + print(" Volume : ", Props[2]) + + aRefSurface = theSurface + aResSurface = Props[1] + assert (math.fabs(aResSurface - aRefSurface) < theDelta), "The volume is wrong: expected = {0}, real = {1}".format(aRefSurface, aResSurface) + + model.end() + +if __name__ == '__main__': + with TemporaryDirectory() as tmp_dir: + aRealSurface = 0.00192878 + #========================================================================= + # Export a shape into STL + #========================================================================= + testExportSTL(os.path.join(tmp_dir, "export.stl"), 10 ** -5, True) + #========================================================================= + # End of test + #========================================================================= diff --git a/src/ConnectorAPI/Test/tests.set b/src/ConnectorAPI/Test/tests.set index 60e932add..31e182ca7 100644 --- a/src/ConnectorAPI/Test/tests.set +++ b/src/ConnectorAPI/Test/tests.set @@ -27,4 +27,5 @@ SET(TEST_NAMES Test17917 Test18887 Test3195 + TestExportSTL ) -- 2.39.2