Salome HOME
Update copyrights
[modules/shaper.git] / src / ExchangePlugin / Test / TestExport.py
1 # Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 """
21       TestExport.py
22       Unit test of ExchangePlugin_TestExport class
23 """
24 #=========================================================================
25 # Initialization of the test
26 #=========================================================================
27 import os
28 import math
29 from tempfile import TemporaryDirectory
30
31 from ModelAPI import *
32
33 from TestImport import testImport
34
35 from salome.shaper import model
36
37 __updated__ = "2015-05-22"
38
39 aSession = ModelAPI_Session.get()
40 #=========================================================================
41 # Help functions
42 #=========================================================================
43 def removeFile(theFileName):
44     try: os.remove(theFileName)
45     except OSError: pass
46     assert not os.path.exists(theFileName), \
47             "Can not remove file {0}".format(theFileName)
48
49 #=========================================================================
50 # Common test function
51 #=========================================================================
52 def testExport(theType, theFormat, theFile, theVolume, theDelta, theErrorExpected = False):
53     # Import a reference part
54     aSession.startOperation("Add part")
55     aPartFeature = aSession.moduleDocument().addFeature("Part")
56     aSession.finishOperation()
57     aPart = aSession.activeDocument()
58
59     aSession.startOperation("Import screw")
60     anImportFeature = aPart.addFeature("Import")
61     aShapePath = os.path.join(os.getenv("DATA_DIR"), "Shapes", "Step", "screw.step")
62     anImportFeature.string("file_path").setValue(aShapePath)
63     aSession.finishOperation()
64
65     removeFile(theFile)
66     # Export a part
67     aSession.startOperation("Export part")
68     anExportFeature = aPart.addFeature("Export")
69     anExportFeature.string("file_format").setValue(theFormat)
70     print("theFile=",theFile)
71     anExportFeature.string("file_path").setValue(theFile)
72     anExportFeature.string("ExportType").setValue("Regular")
73     aSelectionListAttr = anExportFeature.selectionList("selection_list")
74     aSelectionListAttr.setSelectionType("solids")
75     aSelectionListAttr.append(anImportFeature.firstResult(), anImportFeature.firstResult().shape())
76     aSession.finishOperation()
77
78     if theErrorExpected:
79         assert anExportFeature.error() != ""
80         aPart.removeFeature(anExportFeature)
81     else:
82         assert os.path.exists(theFile)
83
84         # Test exported file by importing
85         testImport(theType, theFile, theVolume, theDelta)
86
87 def testExportXAO(theFile, theEmptyFormat = False):
88     type = "XAO"
89     format = "XAO"
90     if theEmptyFormat:
91         type = "Regular"
92         format = ""
93
94     # Import a reference part
95     aSession.startOperation("Add part")
96     aPartFeature = aSession.moduleDocument().addFeature("Part")
97     aSession.finishOperation()
98     aPart = aSession.activeDocument()
99
100     aSession.startOperation("Import Box_1")
101     anImportFeature = aPart.addFeature("Import")
102     aShapePath = os.path.join(os.getenv("DATA_DIR"), "Shapes", "Brep", "box1.brep")
103     anImportFeature.string("file_path").setValue(aShapePath)
104     aSession.finishOperation()
105
106     # Create groups
107     aSession.startOperation("First group")
108     aGroupFeature = aSession.activeDocument().addFeature("Group")
109     aGroupFeature.data().setName("boite_1")
110     aSelectionListAttr = aGroupFeature.selectionList("group_list")
111     aSelectionListAttr.setSelectionType("solid")
112     aSelectionListAttr.append(anImportFeature.lastResult(), None)
113     aSession.finishOperation()
114
115     aSession.startOperation("Second Group")
116     aGroupFeature = aSession.activeDocument().addFeature("Group")
117     aGroupFeature.data().setName("")
118     aSelectionListAttr = aGroupFeature.selectionList("group_list")
119     aSelectionListAttr.setSelectionType("face")
120     aSelectionListAttr.append("box1_1/Shape_1")
121     aSelectionListAttr.append("box1_1/Shape_2")
122     aSession.finishOperation()
123
124     aSession.startOperation("Create a field")
125     aField = aSession.activeDocument().addFeature("Field")
126     aSelectionListAttr = aField.selectionList("selected")
127     aSelectionListAttr.setSelectionType("face")
128     aSelectionListAttr.append("box1_1/Shape_1")
129     aSelectionListAttr.append("box1_1/Shape_2")
130     aComponentNames = aField.stringArray("components_names")
131     aComponentNames.setSize(2) # two components
132     aComponentNames.setValue(0, "temperatue")
133     aComponentNames.setValue(1, "porosity")
134     aStamps = aField.intArray("stamps")
135     aStamps.setSize(1) # one step
136     aStamps.setValue(0, 10)
137     aTables = aField.tables("values")
138     aTables.setType(2) # double
139     aTables.setSize(1 + 2, 2, 1) # default row + number of selected, number of compoents, number of steps (tables)
140     aTables.setValue(1., 0, 0, 0) # value, index of selection, index of component, index of step
141     aTables.setValue(2., 1, 0, 0)
142     aTables.setValue(3., 2, 0, 0)
143     aTables.setValue(4., 0, 1, 0)
144     aTables.setValue(5., 1, 1, 0)
145     aTables.setValue(6., 2, 1, 0)
146     aSession.finishOperation()
147
148     # Export
149     aSession.startOperation("Export to XAO")
150     anExportFeature = aPart.addFeature("Export")
151     anExportFeature.string("xao_file_path").setValue(theFile)
152     anExportFeature.string("file_format").setValue(type)
153     anExportFeature.string("ExportType").setValue(type)
154     anExportFeature.string("xao_author").setValue("me")
155     anExportFeature.string("xao_geometry_name").setValue("mygeom")
156     aSession.finishOperation()
157
158     # Check exported file
159     aRefPath = os.path.join(os.getenv("DATA_DIR"), "Shapes", "Xao", "box2.xao")
160     import filecmp
161     assert filecmp.cmp(theFile, aRefPath)
162
163 if __name__ == '__main__':
164     with TemporaryDirectory() as tmp_dir:
165         #=========================================================================
166         # Export a shape into BREP
167         #=========================================================================
168         aRealVolume = 3.78827401738e-06
169         testExport("BREP", "BREP", os.path.join(tmp_dir, "screw_export.brep"), aRealVolume, 10 ** -17)
170         testExport("BRP", "BREP", os.path.join(tmp_dir, "screw_export.brp"), aRealVolume, 10 ** -17)
171         testExport("Regular", "", os.path.join(tmp_dir, "screw_export.brep"), aRealVolume, 10 ** -17)
172         #=========================================================================
173         # Export a shape into STEP
174         #=========================================================================
175         testExport("STEP", "STEP", os.path.join(tmp_dir, "screw_export.step"), 3.78825807533e-06, 10 ** -17)
176         testExport("STP", "STEP", os.path.join(tmp_dir, "screw_export.stp"), 3.78825807533e-06, 10 ** -17)
177         testExport("Regular", "", os.path.join(tmp_dir, "screw_export.step"), 3.78825807533e-06, 10 ** -17)
178         #=========================================================================
179         # Export a shape into IGES
180         #=========================================================================
181         testExport("IGES-5.1", "IGES-5.1", os.path.join(tmp_dir, "screw_export-5.1.iges"), 3.78829613776e-06, 10 ** -17)
182         testExport("IGS-5.1", "IGES-5.1", os.path.join(tmp_dir, "screw_export-5.1.igs"), 3.78829613776e-06, 10 ** -17)
183         testExport("Regular", "", os.path.join(tmp_dir, "screw_export-5.1.iges"), 3.78829613776e-06, 10 ** -17)
184         testExport("IGES-5.3", "IGES-5.3", os.path.join(tmp_dir, "screw_export-5.3.iges"), 3.78827401651e-06, 10 ** -17)
185         testExport("IGS-5.3", "IGES-5.3", os.path.join(tmp_dir, "screw_export-5.3.igs"), 3.78827401651e-06, 10 ** -17)
186         #=========================================================================
187         # Export a shape into XAO
188         #=========================================================================
189         testExportXAO(os.path.join(tmp_dir, "export.xao"))
190         testExportXAO(os.path.join(tmp_dir, "export.xao"), True)
191         #=========================================================================
192         # Check error when export to unsupported format
193         #=========================================================================
194         testExport("Regular", "", os.path.join(tmp_dir, "screw_export.dwg"), 3.78825807533e-06, 10 ** -17, True)
195         #=========================================================================
196         # End of test
197         #=========================================================================
198
199         assert(model.checkPythonDump())