Salome HOME
Merge remote-tracking branch 'origin/Toolbars_Management'
[modules/shaper.git] / src / ExchangePlugin / Test / TestImport.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       TestImport.py
23       Unit test of ExchangePlugin_TestImport class
24 """
25 #=========================================================================
26 # Initialization of the test
27 #=========================================================================
28 from ModelAPI import *
29 from GeomAlgoAPI import *
30
31 import os
32 import math
33 import shutil
34 from tempfile import TemporaryDirectory
35
36 __updated__ = "2015-05-22"
37
38 aSession = ModelAPI_Session.get()
39
40 #=========================================================================
41 # Common test function
42 #=========================================================================
43 def getShapePath(path):
44     shapes_dir = os.path.join(os.getenv("DATA_DIR"), "Shapes")
45     return os.path.join(shapes_dir, path)
46
47 def testImport(theType, theFile, theVolume, theDelta, theErrorExpected = False):
48     # Create a part for import
49     aSession.startOperation("Create part for import")
50     aPartFeature = aSession.moduleDocument().addFeature("Part")
51     aSession.finishOperation()
52     aPart = aSession.activeDocument()
53
54     aSession.startOperation("Import file")
55     aFeatureKind = "Import"
56     anImportFeature = aPart.addFeature(aFeatureKind)
57     assert anImportFeature, "{0}: Can not create a feature {1}".format(theType, aFeatureKind)
58     aFieldName = "file_path"
59     file = anImportFeature.string(aFieldName)
60     assert file, "{0}: Can not receive string field {1}".format(theType, aFieldName)
61     file.setValue(theFile)
62     aSession.finishOperation()
63
64     if theErrorExpected:
65         assert anImportFeature.error() != ''
66     else:
67         # Check results
68         assert anImportFeature.error() == '', "{0}: The error after execution: {1}".format(theType, anImportFeature.error())
69         assert len(anImportFeature.results()) == 1, "{0}: Wrong number of results: expected = 1, real = {1}".format(theType, len(anImportFeature.results()))
70         aResultBody = modelAPI_ResultBody(anImportFeature.firstResult())
71         assert aResultBody, "{0}: The result is not a body".format(theType)
72         aShape = aResultBody.shape()
73         assert aShape, "{0}: The body does not have a shape".format(theType)
74
75         # Check shape volume
76         aRefVolume = theVolume
77         aResVolume = GeomAlgoAPI_ShapeTools.volume(aShape)
78         assert (math.fabs(aResVolume - aRefVolume) < theDelta), "{0}: The volume is wrong: expected = {1}, real = {2}".format(theType, aRefVolume, aResVolume)
79
80 def testImportXAO():
81     # Create a part for import
82     aSession.startOperation("Create part for import")
83     aPartFeature = aSession.moduleDocument().addFeature("Part")
84     aSession.finishOperation()
85     aPart = aSession.activeDocument()
86
87     aSession.startOperation("Import XAO")
88     anImportFeature = aPart.addFeature("Import")
89     anImportFeature.string("file_path").setValue(getShapePath("Xao/box1.xao"))
90     aSession.finishOperation()
91
92     # Check results
93     assert anImportFeature.error() == ''
94     assert anImportFeature.name() == "mygeom"
95     assert len(anImportFeature.results()) == 1
96     assert modelAPI_ResultBody(anImportFeature.firstResult())
97     assert anImportFeature.firstResult().data().name() == "mygeom_1"
98     aCompositeFeature = featureToCompositeFeature(anImportFeature)
99     # Two groups and one field
100     assert aCompositeFeature.numberOfSubs(False) == 3
101
102     aFeature1 = aCompositeFeature.subFeature(0, False)
103     assert aFeature1.getKind() == "Group"
104     assert aFeature1.name() == "boite_1"
105
106     aSelectionList = aFeature1.selectionList("group_list")
107     assert aSelectionList.selectionType() == "solid"
108     assert aSelectionList.size() == 1
109     assert aSelectionList.value(0).namingName("") == "mygeom_1"
110
111     aFeature2 = aCompositeFeature.subFeature(1, False)
112     assert aFeature2.getKind() == "Group"
113     assert aFeature2.name() == "Group_2"
114
115     aSelectionList = aFeature2.selectionList("group_list")
116     assert aSelectionList.selectionType() == "face"
117     assert aSelectionList.size() == 2
118     assert aSelectionList.value(0).namingName("") == "mygeom_1/Shape_1"
119     assert aSelectionList.value(1).namingName("") == "mygeom_1/Shape_2"
120
121     aFeature3 = aCompositeFeature.subFeature(2, False)
122     assert aFeature3.getKind() == "Field"
123     assert aFeature3.name() == "color"
124     assert aFeature3.intArray("stamps").size() == 2
125     assert aFeature3.tables("values").rows() == 2
126     assert aFeature3.tables("values").columns() == 3
127     assert aFeature3.tables("values").tables() == 2
128     assert aFeature3.tables("values").type() == 1 # integer
129     assert aFeature3.selectionList("selected").size() == 1
130
131 if __name__ == '__main__':
132     with TemporaryDirectory() as tmp_dir:
133         #=========================================================================
134         # Create a shape imported from BREP
135         #=========================================================================
136         shape_path = getShapePath("Brep/solid.brep")
137         testImport("BREP", shape_path, 259982.297176, 10 ** -5)
138         shape_path = shutil.copyfile(shape_path, os.path.join(tmp_dir, "solid.brp"))
139         testImport("BRP", shape_path, 259982.297176, 10 ** -5)
140         #=========================================================================
141         # Create a shape imported from STEP
142         #=========================================================================
143         shape_path = getShapePath("Step/screw.step")
144         testImport("STP", shape_path, 3.78827401738e-06, 10 ** -17)
145         shape_path = shutil.copyfile(shape_path, os.path.join(tmp_dir, "screw.stp"))
146         testImport("STEP", shape_path, 3.78827401738e-06, 10 ** -17)
147         #=========================================================================
148         # Create a shape imported from IGES
149         #=========================================================================
150         shape_path = getShapePath("Iges/bearing.igs")
151         testImport("IGES", shape_path, 6.86970803067e-14, 10 ** -25)
152         shape_path = shutil.copyfile(shape_path, os.path.join(tmp_dir, "bearing.iges"))
153         testImport("IGS", shape_path, 6.86970803067e-14, 10 ** -25)
154
155         #=========================================================================
156         # Create a shape imported from XAO
157         #=========================================================================
158         testImportXAO()
159
160         #=========================================================================
161         # Check import errors
162         #=========================================================================
163         testImport("BREP", "", 0, 10 ** -25, True)
164         shape_path = getShapePath("Brep/solid.dwg")
165         testImport("BREP", shape_path, 0, 10 ** -25, True)
166         shape_path = getShapePath("Xao/wrong_file.xao")
167         testImport("XAO", shape_path, 0, 10 ** -25, True)
168
169         #=========================================================================
170         # End of test
171         #=========================================================================
172
173         from salome.shaper import model
174         assert(model.checkPythonDump())