Salome HOME
Issue #803: Put all the python modules in the same python package newgeom
[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
13 from TestImport import testImport
14
15 from salome.shaper import model
16
17 __updated__ = "2015-05-22"
18
19 aSession = ModelAPI_Session.get()
20 #=========================================================================
21 # Help functions
22 #=========================================================================
23 def removeFile(theFileName):
24     try: os.remove(theFileName)
25     except OSError: pass
26     assert not os.path.exists(theFileName), \
27             "Can not remove file {0}".format(theFileName)
28
29 #=========================================================================
30 # Common test function
31 #=========================================================================
32 def testExport(theType, theFormat, theFile, theVolume, theDelta):
33     # Import a reference part
34     aSession.startOperation("Add part")
35     aPartFeature = aSession.moduleDocument().addFeature("Part")
36     aSession.finishOperation()
37     aPart = aSession.activeDocument()
38
39     aSession.startOperation("Import screw")
40     anImportFeature = aPart.addFeature("Import")
41     anImportFeature.string("file_path").setValue("Data/screw.step")
42     aSession.finishOperation()
43
44     removeFile(theFile)
45     # Export a part
46     aSession.startOperation("Export part")
47     anExportFeature = aPart.addFeature("Export")
48     anExportFeature.string("file_format").setValue(theFormat)
49     print "theFile=",theFile
50     anExportFeature.string("file_path").setValue(theFile)
51     anExportFeature.string("ExportType").setValue("Regular")
52     aSelectionListAttr = anExportFeature.selectionList("selection_list")
53     aSelectionListAttr.setSelectionType("solids")
54     aSelectionListAttr.append(anImportFeature.firstResult(), anImportFeature.firstResult().shape())
55     aSession.finishOperation()
56
57     assert os.path.exists(theFile)
58
59     # Test exported file by importing
60     testImport(theType, theFile, theVolume, theDelta)
61
62 def testExportXAO():
63     # Import a reference part
64     aSession.startOperation("Add part")
65     aPartFeature = aSession.moduleDocument().addFeature("Part")
66     aSession.finishOperation()
67     aPart = aSession.activeDocument()
68
69     aSession.startOperation("Import Box_1")
70     anImportFeature = aPart.addFeature("Import")
71     anImportFeature.string("file_path").setValue("Data/Box_1.brep")
72     aSession.finishOperation()
73
74     # Create groups
75     aSession.startOperation("First group")
76     aGroupFeature = aSession.activeDocument().addFeature("Group")
77     aGroupFeature.data().setName("boite_1")
78     aSelectionListAttr = aGroupFeature.selectionList("group_list")
79     aSelectionListAttr.setSelectionType("solid")
80     aSelectionListAttr.append(anImportFeature.lastResult(), None)
81     aSession.finishOperation()
82
83     aSession.startOperation("Second Group")
84     aGroupFeature = aSession.activeDocument().addFeature("Group")
85     aGroupFeature.data().setName("")
86     aSelectionListAttr = aGroupFeature.selectionList("group_list")
87     aSelectionListAttr.setSelectionType("face")
88     aSelectionListAttr.append("Box_1_1/Shape1")
89     aSelectionListAttr.append("Box_1_1/Shape2")
90     aSession.finishOperation()
91
92     aSession.startOperation("Create a field")
93     aField = aSession.activeDocument().addFeature("Field")
94     aSelectionListAttr = aField.selectionList("selected")
95     aSelectionListAttr.setSelectionType("face")
96     aSelectionListAttr.append("Box_1_1/Shape1")
97     aSelectionListAttr.append("Box_1_1/Shape2")
98     aComponentNames = aField.stringArray("components_names")
99     aComponentNames.setSize(2) # two components
100     aComponentNames.setValue(0, "temperatue")
101     aComponentNames.setValue(1, "porosity")
102     aStamps = aField.intArray("stamps")
103     aStamps.setSize(1) # one step
104     aStamps.setValue(0, 10)
105     aTables = aField.tables("values")
106     aTables.setType(2) # double
107     aTables.setSize(1 + 2, 2, 1) # default row + number of selected, number of compoents, number of steps (tables)
108     aTables.setValue(1., 0, 0, 0) # value, index of selection, index of component, index of step
109     aTables.setValue(2., 1, 0, 0)
110     aTables.setValue(3., 2, 0, 0)
111     aTables.setValue(4., 0, 1, 0)
112     aTables.setValue(5., 1, 1, 0)
113     aTables.setValue(6., 2, 1, 0)
114     aSession.finishOperation()
115
116     # Export
117     aSession.startOperation("Export to XAO")
118     anExportFeature = aPart.addFeature("Export")
119     anExportFeature.string("xao_file_path").setValue("Data/export.xao")
120     anExportFeature.string("file_format").setValue("XAO")
121     anExportFeature.string("ExportType").setValue("XAO")
122     anExportFeature.string("xao_author").setValue("me")
123     anExportFeature.string("xao_geometry_name").setValue("mygeom")
124     aSession.finishOperation()
125
126     # Check exported file
127 #    import filecmp
128 #    assert filecmp.cmp("Data/export.xao", "Data/export_ref.xao")
129
130 if __name__ == '__main__':
131 #=========================================================================
132 # Export a shape into BREP
133 #=========================================================================
134     aRealVolume = 3.78827401738e-06
135     testExport("BREP", "BREP", os.path.join(os.getcwd(), "Data", "screw_export.brep"), aRealVolume, 10 ** -17)
136     testExport("BRP", "BREP", os.path.join(os.getcwd(), "Data", "screw_export.brp"), aRealVolume, 10 ** -17)
137 #=========================================================================
138 # Export a shape into STEP
139 #=========================================================================
140     testExport("STEP", "STEP", os.path.join(os.getcwd(), "Data", "screw_export.step"), 3.78825807533e-06, 10 ** -17)
141     testExport("STP", "STEP", os.path.join(os.getcwd(), "Data", "screw_export.stp"), 3.78825807533e-06, 10 ** -17)
142 #=========================================================================
143 # Export a shape into IGES
144 #=========================================================================
145     testExport("IGES-5.1", "IGES-5.1", os.path.join(os.getcwd(), "Data", "screw_export-5.1.iges"), 3.78829613776e-06, 10 ** -17)
146     testExport("IGS-5.1", "IGES-5.1", os.path.join(os.getcwd(), "Data", "screw_export-5.1.igs"), 3.78829613776e-06, 10 ** -17)
147     testExport("IGES-5.3", "IGES-5.3", os.path.join(os.getcwd(), "Data", "screw_export-5.3.iges"), 3.78827401651e-06, 10 ** -17)
148     testExport("IGS-5.3", "IGES-5.3", os.path.join(os.getcwd(), "Data", "screw_export-5.3.igs"), 3.78827401651e-06, 10 ** -17)
149 #=========================================================================
150 # Export a shape into XAO
151 #=========================================================================
152     testExportXAO()
153 #=========================================================================
154 # End of test
155 #=========================================================================
156
157 assert(model.checkPythonDump())