Salome HOME
updated copyright message
[modules/shaper.git] / src / ExchangePlugin / Test / TestExport.py
1 # Copyright (C) 2014-2023  CEA, EDF
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, theArea, 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("step_file_path").setValue(aShapePath)
63     anImportFeature.string("ImportType").setValue("STEP")
64     anImportFeature.boolean("step_scale_inter_units").setValue(True)
65     aSession.finishOperation()
66
67     removeFile(theFile)
68     # Export a part
69     aSession.startOperation("Export part")
70     anExportFeature = aPart.addFeature("Export")
71     anExportFeature.string("file_format").setValue(theFormat)
72     print("theFile=",theFile)
73     anExportFeature.string("file_path").setValue(theFile)
74     anExportFeature.string("ExportType").setValue("Regular")
75     aSelectionListAttr = anExportFeature.selectionList("selection_list")
76     aSelectionListAttr.setSelectionType("solids")
77     aSelectionListAttr.append(anImportFeature.firstResult(), anImportFeature.firstResult().shape())
78     aSession.finishOperation()
79
80     if theErrorExpected:
81         assert anExportFeature.error() != ""
82         aPart.removeFeature(anExportFeature)
83     else:
84         assert os.path.exists(theFile)
85
86         # Test exported file by importing
87         testImport(theType, theFile, theVolume, theArea, theDelta)
88
89 def testExportXAO(theFile, theEmptyFormat = False):
90     type = "XAO"
91     format = "XAO"
92     if theEmptyFormat:
93         type = "Regular"
94         format = ""
95
96     # Import a reference part
97     aSession.startOperation("Add part")
98     aPartFeature = aSession.moduleDocument().addFeature("Part")
99     aSession.finishOperation()
100     aPart = aSession.activeDocument()
101
102     aSession.startOperation("Import Box_1")
103     anImportFeature = aPart.addFeature("Import")
104     aShapePath = os.path.join(os.getenv("DATA_DIR"), "Shapes", "Brep", "box1.brep")
105     anImportFeature.string("file_path").setValue(aShapePath)
106     anImportFeature.string("ImportType").setValue("BREP")
107     aSession.finishOperation()
108
109     # Create groups
110     aSession.startOperation("First group")
111     aGroupFeature = aSession.activeDocument().addFeature("Group")
112     aGroupFeature.data().setName("boite_1")
113     aSelectionListAttr = aGroupFeature.selectionList("group_list")
114     aSelectionListAttr.setSelectionType("solid")
115     aSelectionListAttr.append(anImportFeature.lastResult(), None)
116     aSession.finishOperation()
117
118     aSession.startOperation("Second Group")
119     aGroupFeature = aSession.activeDocument().addFeature("Group")
120     aGroupFeature.data().setName("")
121     aSelectionListAttr = aGroupFeature.selectionList("group_list")
122     aSelectionListAttr.setSelectionType("face")
123     aSelectionListAttr.append("box1_1/Shape_1")
124     aSelectionListAttr.append("box1_1/Shape_2")
125     aSession.finishOperation()
126
127     aSession.startOperation("Create a field")
128     aField = aSession.activeDocument().addFeature("Field")
129     aSelectionListAttr = aField.selectionList("selected")
130     aSelectionListAttr.setSelectionType("face")
131     aSelectionListAttr.append("box1_1/Shape_1")
132     aSelectionListAttr.append("box1_1/Shape_2")
133     aComponentNames = aField.stringArray("components_names")
134     aComponentNames.setSize(2) # two components
135     aComponentNames.setValue(0, "temperatue")
136     aComponentNames.setValue(1, "porosity")
137     aStamps = aField.intArray("stamps")
138     aStamps.setSize(1) # one step
139     aStamps.setValue(0, 10)
140     aTables = aField.tables("values")
141     aTables.setType(2) # double
142     aTables.setSize(1 + 2, 2, 1) # default row + number of selected, number of compoents, number of steps (tables)
143     aTables.setValue(1., 0, 0, 0) # value, index of selection, index of component, index of step
144     aTables.setValue(2., 1, 0, 0)
145     aTables.setValue(3., 2, 0, 0)
146     aTables.setValue(4., 0, 1, 0)
147     aTables.setValue(5., 1, 1, 0)
148     aTables.setValue(6., 2, 1, 0)
149     aSession.finishOperation()
150
151     # Export
152     aSession.startOperation("Export to XAO")
153     anExportFeature = aPart.addFeature("Export")
154     anExportFeature.string("xao_file_path").setValue(theFile)
155     anExportFeature.string("file_format").setValue(type)
156     anExportFeature.string("ExportType").setValue(type)
157     anExportFeature.string("xao_author").setValue("me")
158     anExportFeature.string("xao_geometry_name").setValue("mygeom")
159     aSession.finishOperation()
160
161     # Check exported file
162     aRefPath = os.path.join(os.getenv("DATA_DIR"), "Shapes", "Xao", "box2.xao")
163     # endlines may be different on different platforms, thus compare files line-by-line
164     areFilesEqual = True
165     with open(theFile, 'r') as file, open(aRefPath, 'r') as ref:
166         l1 = l2 = True
167         while l1 and l2 and areFilesEqual:
168             l1 = file.readline()
169             l2 = ref.readline()
170             areFilesEqual = l1 == l2
171     assert areFilesEqual
172
173 if __name__ == '__main__':
174     with TemporaryDirectory() as tmp_dir:
175         #=========================================================================
176         # Export a shape into BREP
177         #=========================================================================
178         aRealVolume = 3.78827401738e-06
179         testExport("BREP", "BREP", os.path.join(tmp_dir, "screw_export.brep"), aRealVolume, 0.0019293313778547098, 10 ** -17)
180         testExport("BRP", "BREP", os.path.join(tmp_dir, "screw_export.brp"), aRealVolume, 0.0019293313778547098, 10 ** -17)
181         testExport("BREP", "", os.path.join(tmp_dir, "screw_export.brep"), aRealVolume, 0.0019293313778547098, 10 ** -17)
182         #=========================================================================
183         # Export a shape into STEP
184         #=========================================================================
185         testExport("STEP", "STEP", os.path.join(tmp_dir, "screw_export.step"), 3.788258075329978e-06, 0.0019293304476337928, 10 ** -17)
186         testExport("STEP", "STEP", os.path.join(tmp_dir, "screw_export.stp"), 3.788258075329978e-06, 0.0019293304476337928, 10 ** -17)
187         testExport("STEP", "", os.path.join(tmp_dir, "screw_export.step"), 3.788258075329978e-06, 0.0019293304476337928, 10 ** -17)
188         #=========================================================================
189         # Export a shape into IGES
190         #=========================================================================
191         testExport("IGES", "IGES-5.1", os.path.join(tmp_dir, "screw_export-5.1.iges"), 0.0, 0.0019293313766693052, 10 ** -17)
192         testExport("IGS", "IGES-5.1", os.path.join(tmp_dir, "screw_export-5.1.igs"), 0.0, 0.0019293313766693052, 10 ** -17)
193         testExport("IGES", "", os.path.join(tmp_dir, "screw_export-5.1.iges"), 0.0, 0.0019293313766693052, 10 ** -17)
194         testExport("IGES", "IGES-5.3", os.path.join(tmp_dir, "screw_export-5.3.iges"), 3.78827401651e-06, 0.001929331377591282, 10 ** -17)
195         testExport("IGS", "IGES-5.3", os.path.join(tmp_dir, "screw_export-5.3.igs"), 3.78827401651e-06, 0.001929331377591282, 10 ** -17)
196         #=========================================================================
197         # Export a shape into XAO
198         #=========================================================================
199         testExportXAO(os.path.join(tmp_dir, "export.xao"))
200         testExportXAO(os.path.join(tmp_dir, "export.xao"), True)
201         #=========================================================================
202         # Check error when export to unsupported format
203         #=========================================================================
204         testExport("BREP", "", os.path.join(tmp_dir, "screw_export.dwg"), 3.78825807533e-06, 0.0019293313766693052, 10 ** -17, True)
205         #=========================================================================
206         # End of test
207         #=========================================================================
208
209         assert(model.checkPythonDump())