Salome HOME
293f82ff8130fddc2f65b3e42a253a3412a9ab40
[modules/shaper.git] / src / ExchangePlugin / Test / TestExport.py
1 """
2       TestExport.py
3       Unit test of ExchangePlugin_TestExport class
4 """
5 #=========================================================================
6 # Initialization of the test
7 #=========================================================================
8 import os
9 import math
10
11 from ModelAPI import *
12 from GeomDataAPI import *
13 from GeomAlgoAPI import *
14 from GeomAPI import *
15
16 from TestImport import testImport
17
18 __updated__ = "2015-05-22"
19
20 aSession = ModelAPI_Session.get()
21 #=========================================================================
22 # Help functions
23 #=========================================================================
24 def removeFile(theFileName):
25     try: os.remove(theFileName)
26     except OSError: pass
27     assert not os.path.exists(theFileName), \
28             "Can not remove file {0}".format(theFileName)
29
30 #=========================================================================
31 # Common test function
32 #=========================================================================
33 def testExport(theType, theFormat, theFile, theVolume, theDelta):
34     # Import a reference part 
35     aSession.startOperation()
36     aPartFeature = aSession.moduleDocument().addFeature("Part")
37     aSession.finishOperation()
38     aPart = aSession.activeDocument()
39     
40     aSession.startOperation()
41     anImportFeature = aPart.addFeature("Import")
42     anImportFeature.string("import_file_selector").setValue("Data/screw.step")
43     anImportFeature.execute()
44     aSession.finishOperation()
45     
46     removeFile(theFile)
47     # Export a part
48     aSession.startOperation()
49     aFeatureKind = "Export"
50     anExportFeature = aPart.addFeature(aFeatureKind)
51     assert anExportFeature, "{0}: Can not create a feature {1}".format(theType, aFeatureKind)
52     
53     aFormatAttrName = "export_file_format"
54     aFormatAttr = anExportFeature.string(aFormatAttrName)
55     assert aFormatAttr, "{0}: Can not receive string field {1}".format(theType, aFormatAttrName)
56     aFormatAttr.setValue(theFormat)
57     
58     aFileAttrName = "export_file_selector"
59     aFileAttr = anExportFeature.string(aFileAttrName)
60     assert aFileAttr, "{0}: Can not receive string field {1}".format(theType, aFileAttrName)
61     aFileAttr.setValue(theFile)
62     
63     aSelectionListAttrName = "selection_list"
64     aSelectionListAttr = anExportFeature.selectionList(aSelectionListAttrName)
65     assert aSelectionListAttr, "{0}: Can not receive selection list field {1}".format(theType, aSelectionListAttrName)
66     aSelectionListAttr.setSelectionType("solids")
67     aSelectionListAttr.append(anImportFeature.firstResult(), anImportFeature.firstResult().shape())
68     
69     anExportFeature.execute()
70     aSession.finishOperation()
71     
72     assert os.path.exists(theFile), "{0}: Can not find exported file {1}".format(theType, theFile)
73     
74     # Test exported file by importing
75     testImport(theType, theFile, theVolume, theDelta)
76
77 if __name__ == '__main__':
78 #=========================================================================
79 # Export a shape into BREP
80 #=========================================================================
81     aRealVolume = 3.78827059338e-06
82     testExport("BREP", "BREP", os.path.join(os.getcwd(), "Data", "screw_export.brep"), aRealVolume, 10 ** -17)
83 #=========================================================================
84 # Export a shape into STEP
85 #=========================================================================
86     testExport("STEP", "STEP", os.path.join(os.getcwd(), "Data", "screw_export.step"), 3.7882546512e-06, 10 ** -17)
87     testExport("STP", "STEP", os.path.join(os.getcwd(), "Data", "screw_export.stp"), 3.7882546512e-06, 10 ** -17)
88 #=========================================================================
89 # Export a shape into IGES
90 #=========================================================================
91     testExport("IGES-5.1", "IGES-5.1", os.path.join(os.getcwd(), "Data", "screw_export-5.1.iges"), 1.98291079746e-06, 10 ** -17)
92     testExport("IGS-5.1", "IGES-5.1", os.path.join(os.getcwd(), "Data", "screw_export-5.1.igs"), 1.98291079746e-06, 10 ** -17)
93     testExport("IGES-5.3", "IGES-5.3", os.path.join(os.getcwd(), "Data", "screw_export-5.3.iges"), 3.78827060085e-06, 10 ** -17)
94     testExport("IGS-5.3", "IGES-5.3", os.path.join(os.getcwd(), "Data", "screw_export-5.3.igs"), 3.78827060085e-06, 10 ** -17)
95 #=========================================================================
96 # End of test
97 #=========================================================================