]> SALOME platform Git repositories - modules/shaper.git/blob - src/ExchangePlugin/Test/TestExport.py
Salome HOME
Merge remote-tracking branch 'origin/Toolbars_Management'
[modules/shaper.git] / src / ExchangePlugin / Test / TestExport.py
1 ## Copyright (C) 2014-2017  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
18 ## email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 ##
20
21 """
22       TestExport.py
23       Unit test of ExchangePlugin_TestExport class
24 """
25 #=========================================================================
26 # Initialization of the test
27 #=========================================================================
28 import os
29 import math
30 from tempfile import TemporaryDirectory
31
32 from ModelAPI import *
33
34 from TestImport import testImport
35
36 from salome.shaper import model
37
38 __updated__ = "2015-05-22"
39
40 aSession = ModelAPI_Session.get()
41 #=========================================================================
42 # Help functions
43 #=========================================================================
44 def removeFile(theFileName):
45     try: os.remove(theFileName)
46     except OSError: pass
47     assert not os.path.exists(theFileName), \
48             "Can not remove file {0}".format(theFileName)
49
50 #=========================================================================
51 # Common test function
52 #=========================================================================
53 def testExport(theType, theFormat, theFile, theVolume, theDelta, theErrorExpected = False):
54     # Import a reference part
55     aSession.startOperation("Add part")
56     aPartFeature = aSession.moduleDocument().addFeature("Part")
57     aSession.finishOperation()
58     aPart = aSession.activeDocument()
59
60     aSession.startOperation("Import screw")
61     anImportFeature = aPart.addFeature("Import")
62     aShapePath = os.path.join(os.getenv("DATA_DIR"), "Shapes", "Step", "screw.step")
63     anImportFeature.string("file_path").setValue(aShapePath)
64     aSession.finishOperation()
65
66     removeFile(theFile)
67     # Export a part
68     aSession.startOperation("Export part")
69     anExportFeature = aPart.addFeature("Export")
70     anExportFeature.string("file_format").setValue(theFormat)
71     print("theFile=",theFile)
72     anExportFeature.string("file_path").setValue(theFile)
73     anExportFeature.string("ExportType").setValue("Regular")
74     aSelectionListAttr = anExportFeature.selectionList("selection_list")
75     aSelectionListAttr.setSelectionType("solids")
76     aSelectionListAttr.append(anImportFeature.firstResult(), anImportFeature.firstResult().shape())
77     aSession.finishOperation()
78
79     if theErrorExpected:
80         assert anExportFeature.error() != ""
81         aPart.removeFeature(anExportFeature)
82     else:
83         assert os.path.exists(theFile)
84
85         # Test exported file by importing
86         testImport(theType, theFile, theVolume, theDelta)
87
88 def testExportXAO(theFile, theEmptyFormat = False):
89     type = "XAO"
90     format = "XAO"
91     if theEmptyFormat:
92         type = "Regular"
93         format = ""
94
95     # Import a reference part
96     aSession.startOperation("Add part")
97     aPartFeature = aSession.moduleDocument().addFeature("Part")
98     aSession.finishOperation()
99     aPart = aSession.activeDocument()
100
101     aSession.startOperation("Import Box_1")
102     anImportFeature = aPart.addFeature("Import")
103     aShapePath = os.path.join(os.getenv("DATA_DIR"), "Shapes", "Brep", "box1.brep")
104     anImportFeature.string("file_path").setValue(aShapePath)
105     aSession.finishOperation()
106
107     # Create groups
108     aSession.startOperation("First group")
109     aGroupFeature = aSession.activeDocument().addFeature("Group")
110     aGroupFeature.data().setName("boite_1")
111     aSelectionListAttr = aGroupFeature.selectionList("group_list")
112     aSelectionListAttr.setSelectionType("solid")
113     aSelectionListAttr.append(anImportFeature.lastResult(), None)
114     aSession.finishOperation()
115
116     aSession.startOperation("Second Group")
117     aGroupFeature = aSession.activeDocument().addFeature("Group")
118     aGroupFeature.data().setName("")
119     aSelectionListAttr = aGroupFeature.selectionList("group_list")
120     aSelectionListAttr.setSelectionType("face")
121     aSelectionListAttr.append("box1_1/Shape_1")
122     aSelectionListAttr.append("box1_1/Shape_2")
123     aSession.finishOperation()
124
125     aSession.startOperation("Create a field")
126     aField = aSession.activeDocument().addFeature("Field")
127     aSelectionListAttr = aField.selectionList("selected")
128     aSelectionListAttr.setSelectionType("face")
129     aSelectionListAttr.append("box1_1/Shape_1")
130     aSelectionListAttr.append("box1_1/Shape_2")
131     aComponentNames = aField.stringArray("components_names")
132     aComponentNames.setSize(2) # two components
133     aComponentNames.setValue(0, "temperatue")
134     aComponentNames.setValue(1, "porosity")
135     aStamps = aField.intArray("stamps")
136     aStamps.setSize(1) # one step
137     aStamps.setValue(0, 10)
138     aTables = aField.tables("values")
139     aTables.setType(2) # double
140     aTables.setSize(1 + 2, 2, 1) # default row + number of selected, number of compoents, number of steps (tables)
141     aTables.setValue(1., 0, 0, 0) # value, index of selection, index of component, index of step
142     aTables.setValue(2., 1, 0, 0)
143     aTables.setValue(3., 2, 0, 0)
144     aTables.setValue(4., 0, 1, 0)
145     aTables.setValue(5., 1, 1, 0)
146     aTables.setValue(6., 2, 1, 0)
147     aSession.finishOperation()
148
149     # Export
150     aSession.startOperation("Export to XAO")
151     anExportFeature = aPart.addFeature("Export")
152     anExportFeature.string("xao_file_path").setValue(theFile)
153     anExportFeature.string("file_format").setValue(type)
154     anExportFeature.string("ExportType").setValue(type)
155     anExportFeature.string("xao_author").setValue("me")
156     anExportFeature.string("xao_geometry_name").setValue("mygeom")
157     aSession.finishOperation()
158
159     # Check exported file
160     aRefPath = os.path.join(os.getenv("DATA_DIR"), "Shapes", "Xao", "box2.xao")
161     import filecmp
162     assert filecmp.cmp(theFile, aRefPath)
163
164 if __name__ == '__main__':
165     with TemporaryDirectory() as tmp_dir:
166         #=========================================================================
167         # Export a shape into BREP
168         #=========================================================================
169         aRealVolume = 3.78827401738e-06
170         testExport("BREP", "BREP", os.path.join(tmp_dir, "screw_export.brep"), aRealVolume, 10 ** -17)
171         testExport("BRP", "BREP", os.path.join(tmp_dir, "screw_export.brp"), aRealVolume, 10 ** -17)
172         testExport("Regular", "", os.path.join(tmp_dir, "screw_export.brep"), aRealVolume, 10 ** -17)
173         #=========================================================================
174         # Export a shape into STEP
175         #=========================================================================
176         testExport("STEP", "STEP", os.path.join(tmp_dir, "screw_export.step"), 3.78825807533e-06, 10 ** -17)
177         testExport("STP", "STEP", os.path.join(tmp_dir, "screw_export.stp"), 3.78825807533e-06, 10 ** -17)
178         testExport("Regular", "", os.path.join(tmp_dir, "screw_export.step"), 3.78825807533e-06, 10 ** -17)
179         #=========================================================================
180         # Export a shape into IGES
181         #=========================================================================
182         testExport("IGES-5.1", "IGES-5.1", os.path.join(tmp_dir, "screw_export-5.1.iges"), 3.78829613776e-06, 10 ** -17)
183         testExport("IGS-5.1", "IGES-5.1", os.path.join(tmp_dir, "screw_export-5.1.igs"), 3.78829613776e-06, 10 ** -17)
184         testExport("Regular", "", os.path.join(tmp_dir, "screw_export-5.1.iges"), 3.78829613776e-06, 10 ** -17)
185         testExport("IGES-5.3", "IGES-5.3", os.path.join(tmp_dir, "screw_export-5.3.iges"), 3.78827401651e-06, 10 ** -17)
186         testExport("IGS-5.3", "IGES-5.3", os.path.join(tmp_dir, "screw_export-5.3.igs"), 3.78827401651e-06, 10 ** -17)
187         #=========================================================================
188         # Export a shape into XAO
189         #=========================================================================
190         testExportXAO(os.path.join(tmp_dir, "export.xao"))
191         testExportXAO(os.path.join(tmp_dir, "export.xao"), True)
192         #=========================================================================
193         # Check error when export to unsupported format
194         #=========================================================================
195         testExport("Regular", "", os.path.join(tmp_dir, "screw_export.dwg"), 3.78825807533e-06, 10 ** -17, True)
196         #=========================================================================
197         # End of test
198         #=========================================================================
199
200         assert(model.checkPythonDump())