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