Salome HOME
Porting to SALOME 9.1.0.
[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):
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     assert os.path.exists(theFile)
80
81     # Test exported file by importing
82     testImport(theType, theFile, theVolume, theDelta)
83
84 def testExportXAO(theFile):
85     # Import a reference part
86     aSession.startOperation("Add part")
87     aPartFeature = aSession.moduleDocument().addFeature("Part")
88     aSession.finishOperation()
89     aPart = aSession.activeDocument()
90
91     aSession.startOperation("Import Box_1")
92     anImportFeature = aPart.addFeature("Import")
93     aShapePath = os.path.join(os.getenv("DATA_DIR"), "Shapes", "Brep", "box1.brep")
94     anImportFeature.string("file_path").setValue(aShapePath)
95     aSession.finishOperation()
96
97     # Create groups
98     aSession.startOperation("First group")
99     aGroupFeature = aSession.activeDocument().addFeature("Group")
100     aGroupFeature.data().setName("boite_1")
101     aSelectionListAttr = aGroupFeature.selectionList("group_list")
102     aSelectionListAttr.setSelectionType("solid")
103     aSelectionListAttr.append(anImportFeature.lastResult(), None)
104     aSession.finishOperation()
105
106     aSession.startOperation("Second Group")
107     aGroupFeature = aSession.activeDocument().addFeature("Group")
108     aGroupFeature.data().setName("")
109     aSelectionListAttr = aGroupFeature.selectionList("group_list")
110     aSelectionListAttr.setSelectionType("face")
111     aSelectionListAttr.append("box1_1/Shape1")
112     aSelectionListAttr.append("box1_1/Shape2")
113     aSession.finishOperation()
114
115     aSession.startOperation("Create a field")
116     aField = aSession.activeDocument().addFeature("Field")
117     aSelectionListAttr = aField.selectionList("selected")
118     aSelectionListAttr.setSelectionType("face")
119     aSelectionListAttr.append("box1_1/Shape1")
120     aSelectionListAttr.append("box1_1/Shape2")
121     aComponentNames = aField.stringArray("components_names")
122     aComponentNames.setSize(2) # two components
123     aComponentNames.setValue(0, "temperatue")
124     aComponentNames.setValue(1, "porosity")
125     aStamps = aField.intArray("stamps")
126     aStamps.setSize(1) # one step
127     aStamps.setValue(0, 10)
128     aTables = aField.tables("values")
129     aTables.setType(2) # double
130     aTables.setSize(1 + 2, 2, 1) # default row + number of selected, number of compoents, number of steps (tables)
131     aTables.setValue(1., 0, 0, 0) # value, index of selection, index of component, index of step
132     aTables.setValue(2., 1, 0, 0)
133     aTables.setValue(3., 2, 0, 0)
134     aTables.setValue(4., 0, 1, 0)
135     aTables.setValue(5., 1, 1, 0)
136     aTables.setValue(6., 2, 1, 0)
137     aSession.finishOperation()
138
139     # Export
140     aSession.startOperation("Export to XAO")
141     anExportFeature = aPart.addFeature("Export")
142     anExportFeature.string("xao_file_path").setValue(theFile)
143     anExportFeature.string("file_format").setValue("XAO")
144     anExportFeature.string("ExportType").setValue("XAO")
145     anExportFeature.string("xao_author").setValue("me")
146     anExportFeature.string("xao_geometry_name").setValue("mygeom")
147     aSession.finishOperation()
148
149     # Check exported file
150     aRefPath = os.path.join(os.getenv("DATA_DIR"), "Shapes", "Xao", "box2.xao")
151     import filecmp
152     assert filecmp.cmp(theFile, aRefPath)
153
154 if __name__ == '__main__':
155     with TemporaryDirectory() as tmp_dir:
156         #=========================================================================
157         # Export a shape into BREP
158         #=========================================================================
159         aRealVolume = 3.78827401738e-06
160         testExport("BREP", "BREP", os.path.join(tmp_dir, "screw_export.brep"), aRealVolume, 10 ** -17)
161         testExport("BRP", "BREP", os.path.join(tmp_dir, "screw_export.brp"), aRealVolume, 10 ** -17)
162         #=========================================================================
163         # Export a shape into STEP
164         #=========================================================================
165         testExport("STEP", "STEP", os.path.join(tmp_dir, "screw_export.step"), 3.78825807533e-06, 10 ** -17)
166         testExport("STP", "STEP", os.path.join(tmp_dir, "screw_export.stp"), 3.78825807533e-06, 10 ** -17)
167         #=========================================================================
168         # Export a shape into IGES
169         #=========================================================================
170         testExport("IGES-5.1", "IGES-5.1", os.path.join(tmp_dir, "screw_export-5.1.iges"), 3.78829613776e-06, 10 ** -17)
171         testExport("IGS-5.1", "IGES-5.1", os.path.join(tmp_dir, "screw_export-5.1.igs"), 3.78829613776e-06, 10 ** -17)
172         testExport("IGES-5.3", "IGES-5.3", os.path.join(tmp_dir, "screw_export-5.3.iges"), 3.78827401651e-06, 10 ** -17)
173         testExport("IGS-5.3", "IGES-5.3", os.path.join(tmp_dir, "screw_export-5.3.igs"), 3.78827401651e-06, 10 ** -17)
174         #=========================================================================
175         # Export a shape into XAO
176         #=========================================================================
177         testExportXAO(os.path.join(tmp_dir, "export.xao"))
178         #=========================================================================
179         # End of test
180         #=========================================================================
181
182         assert(model.checkPythonDump())