Salome HOME
updated copyright message
[modules/shaper.git] / src / ExchangePlugin / Test / TestExport.py
old mode 100644 (file)
new mode 100755 (executable)
index d3f88a7..9319d6e
@@ -1,22 +1,21 @@
-## Copyright (C) 2014-2017  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<mailto:webmaster.salome@opencascade.com>
-##
+# Copyright (C) 2014-2023  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
@@ -27,6 +26,7 @@
 #=========================================================================
 import os
 import math
+from tempfile import TemporaryDirectory
 
 from ModelAPI import *
 
@@ -49,7 +49,7 @@ def removeFile(theFileName):
 #=========================================================================
 # Common test function
 #=========================================================================
-def testExport(theType, theFormat, theFile, theVolume, theDelta):
+def testExport(theType, theFormat, theFile, theVolume, theArea, theDelta, theErrorExpected = False):
     # Import a reference part
     aSession.startOperation("Add part")
     aPartFeature = aSession.moduleDocument().addFeature("Part")
@@ -58,7 +58,10 @@ def testExport(theType, theFormat, theFile, theVolume, theDelta):
 
     aSession.startOperation("Import screw")
     anImportFeature = aPart.addFeature("Import")
-    anImportFeature.string("file_path").setValue("Data/screw.step")
+    aShapePath = os.path.join(os.getenv("DATA_DIR"), "Shapes", "Step", "screw.step")
+    anImportFeature.string("step_file_path").setValue(aShapePath)
+    anImportFeature.string("ImportType").setValue("STEP")
+    anImportFeature.boolean("step_scale_inter_units").setValue(True)
     aSession.finishOperation()
 
     removeFile(theFile)
@@ -66,7 +69,7 @@ def testExport(theType, theFormat, theFile, theVolume, theDelta):
     aSession.startOperation("Export part")
     anExportFeature = aPart.addFeature("Export")
     anExportFeature.string("file_format").setValue(theFormat)
-    print "theFile=",theFile
+    print("theFile=",theFile)
     anExportFeature.string("file_path").setValue(theFile)
     anExportFeature.string("ExportType").setValue("Regular")
     aSelectionListAttr = anExportFeature.selectionList("selection_list")
@@ -74,12 +77,22 @@ def testExport(theType, theFormat, theFile, theVolume, theDelta):
     aSelectionListAttr.append(anImportFeature.firstResult(), anImportFeature.firstResult().shape())
     aSession.finishOperation()
 
-    assert os.path.exists(theFile)
+    if theErrorExpected:
+        assert anExportFeature.error() != ""
+        aPart.removeFeature(anExportFeature)
+    else:
+        assert os.path.exists(theFile)
 
-    # Test exported file by importing
-    testImport(theType, theFile, theVolume, theDelta)
+        # Test exported file by importing
+        testImport(theType, theFile, theVolume, theArea, theDelta)
+
+def testExportXAO(theFile, theEmptyFormat = False):
+    type = "XAO"
+    format = "XAO"
+    if theEmptyFormat:
+        type = "Regular"
+        format = ""
 
-def testExportXAO():
     # Import a reference part
     aSession.startOperation("Add part")
     aPartFeature = aSession.moduleDocument().addFeature("Part")
@@ -88,7 +101,9 @@ def testExportXAO():
 
     aSession.startOperation("Import Box_1")
     anImportFeature = aPart.addFeature("Import")
-    anImportFeature.string("file_path").setValue("Data/Box_1.brep")
+    aShapePath = os.path.join(os.getenv("DATA_DIR"), "Shapes", "Brep", "box1.brep")
+    anImportFeature.string("file_path").setValue(aShapePath)
+    anImportFeature.string("ImportType").setValue("BREP")
     aSession.finishOperation()
 
     # Create groups
@@ -105,16 +120,16 @@ def testExportXAO():
     aGroupFeature.data().setName("")
     aSelectionListAttr = aGroupFeature.selectionList("group_list")
     aSelectionListAttr.setSelectionType("face")
-    aSelectionListAttr.append("Box_1_1/Shape1")
-    aSelectionListAttr.append("Box_1_1/Shape2")
+    aSelectionListAttr.append("box1_1/Shape_1")
+    aSelectionListAttr.append("box1_1/Shape_2")
     aSession.finishOperation()
 
     aSession.startOperation("Create a field")
     aField = aSession.activeDocument().addFeature("Field")
     aSelectionListAttr = aField.selectionList("selected")
     aSelectionListAttr.setSelectionType("face")
-    aSelectionListAttr.append("Box_1_1/Shape1")
-    aSelectionListAttr.append("Box_1_1/Shape2")
+    aSelectionListAttr.append("box1_1/Shape_1")
+    aSelectionListAttr.append("box1_1/Shape_2")
     aComponentNames = aField.stringArray("components_names")
     aComponentNames.setSize(2) # two components
     aComponentNames.setValue(0, "temperatue")
@@ -136,42 +151,59 @@ def testExportXAO():
     # Export
     aSession.startOperation("Export to XAO")
     anExportFeature = aPart.addFeature("Export")
-    anExportFeature.string("xao_file_path").setValue("Data/export.xao")
-    anExportFeature.string("file_format").setValue("XAO")
-    anExportFeature.string("ExportType").setValue("XAO")
+    anExportFeature.string("xao_file_path").setValue(theFile)
+    anExportFeature.string("file_format").setValue(type)
+    anExportFeature.string("ExportType").setValue(type)
     anExportFeature.string("xao_author").setValue("me")
     anExportFeature.string("xao_geometry_name").setValue("mygeom")
     aSession.finishOperation()
 
     # Check exported file
-#    import filecmp
-#    assert filecmp.cmp("Data/export.xao", "Data/export_ref.xao")
+    aRefPath = os.path.join(os.getenv("DATA_DIR"), "Shapes", "Xao", "box2.xao")
+    # endlines may be different on different platforms, thus compare files line-by-line
+    areFilesEqual = True
+    with open(theFile, 'r') as file, open(aRefPath, 'r') as ref:
+        l1 = l2 = True
+        while l1 and l2 and areFilesEqual:
+            l1 = file.readline()
+            l2 = ref.readline()
+            areFilesEqual = l1 == l2
+    assert areFilesEqual
 
 if __name__ == '__main__':
-#=========================================================================
-# Export a shape into BREP
-#=========================================================================
-    aRealVolume = 3.78827401738e-06
-    testExport("BREP", "BREP", os.path.join(os.getcwd(), "Data", "screw_export.brep"), aRealVolume, 10 ** -17)
-    testExport("BRP", "BREP", os.path.join(os.getcwd(), "Data", "screw_export.brp"), aRealVolume, 10 ** -17)
-#=========================================================================
-# Export a shape into STEP
-#=========================================================================
-    testExport("STEP", "STEP", os.path.join(os.getcwd(), "Data", "screw_export.step"), 3.78825807533e-06, 10 ** -17)
-    testExport("STP", "STEP", os.path.join(os.getcwd(), "Data", "screw_export.stp"), 3.78825807533e-06, 10 ** -17)
-#=========================================================================
-# Export a shape into IGES
-#=========================================================================
-    testExport("IGES-5.1", "IGES-5.1", os.path.join(os.getcwd(), "Data", "screw_export-5.1.iges"), 3.78829613776e-06, 10 ** -17)
-    testExport("IGS-5.1", "IGES-5.1", os.path.join(os.getcwd(), "Data", "screw_export-5.1.igs"), 3.78829613776e-06, 10 ** -17)
-    testExport("IGES-5.3", "IGES-5.3", os.path.join(os.getcwd(), "Data", "screw_export-5.3.iges"), 3.78827401651e-06, 10 ** -17)
-    testExport("IGS-5.3", "IGES-5.3", os.path.join(os.getcwd(), "Data", "screw_export-5.3.igs"), 3.78827401651e-06, 10 ** -17)
-#=========================================================================
-# Export a shape into XAO
-#=========================================================================
-    testExportXAO()
-#=========================================================================
-# End of test
-#=========================================================================
-
-assert(model.checkPythonDump())
+    with TemporaryDirectory() as tmp_dir:
+        #=========================================================================
+        # Export a shape into BREP
+        #=========================================================================
+        aRealVolume = 3.78827401738e-06
+        testExport("BREP", "BREP", os.path.join(tmp_dir, "screw_export.brep"), aRealVolume, 0.0019293313778547098, 10 ** -17)
+        testExport("BRP", "BREP", os.path.join(tmp_dir, "screw_export.brp"), aRealVolume, 0.0019293313778547098, 10 ** -17)
+        testExport("BREP", "", os.path.join(tmp_dir, "screw_export.brep"), aRealVolume, 0.0019293313778547098, 10 ** -17)
+        #=========================================================================
+        # Export a shape into STEP
+        #=========================================================================
+        testExport("STEP", "STEP", os.path.join(tmp_dir, "screw_export.step"), 3.788258075329978e-06, 0.0019293304476337928, 10 ** -17)
+        testExport("STEP", "STEP", os.path.join(tmp_dir, "screw_export.stp"), 3.788258075329978e-06, 0.0019293304476337928, 10 ** -17)
+        testExport("STEP", "", os.path.join(tmp_dir, "screw_export.step"), 3.788258075329978e-06, 0.0019293304476337928, 10 ** -17)
+        #=========================================================================
+        # Export a shape into IGES
+        #=========================================================================
+        testExport("IGES", "IGES-5.1", os.path.join(tmp_dir, "screw_export-5.1.iges"), 0.0, 0.0019293313766693052, 10 ** -17)
+        testExport("IGS", "IGES-5.1", os.path.join(tmp_dir, "screw_export-5.1.igs"), 0.0, 0.0019293313766693052, 10 ** -17)
+        testExport("IGES", "", os.path.join(tmp_dir, "screw_export-5.1.iges"), 0.0, 0.0019293313766693052, 10 ** -17)
+        testExport("IGES", "IGES-5.3", os.path.join(tmp_dir, "screw_export-5.3.iges"), 3.78827401651e-06, 0.001929331377591282, 10 ** -17)
+        testExport("IGS", "IGES-5.3", os.path.join(tmp_dir, "screw_export-5.3.igs"), 3.78827401651e-06, 0.001929331377591282, 10 ** -17)
+        #=========================================================================
+        # Export a shape into XAO
+        #=========================================================================
+        testExportXAO(os.path.join(tmp_dir, "export.xao"))
+        testExportXAO(os.path.join(tmp_dir, "export.xao"), True)
+        #=========================================================================
+        # Check error when export to unsupported format
+        #=========================================================================
+        testExport("BREP", "", os.path.join(tmp_dir, "screw_export.dwg"), 3.78825807533e-06, 0.0019293313766693052, 10 ** -17, True)
+        #=========================================================================
+        # End of test
+        #=========================================================================
+
+        assert(model.checkPythonDump())