Salome HOME
Porting to SALOME 9.1.0.
[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):
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     # Check results
65     assert anImportFeature.error() == '', "{0}: The error after execution: {1}".format(theType, anImportFeature.error())
66     assert len(anImportFeature.results()) == 1, "{0}: Wrong number of results: expected = 1, real = {1}".format(theType, len(anImportFeature.results()))
67     aResultBody = modelAPI_ResultBody(anImportFeature.firstResult())
68     assert aResultBody, "{0}: The result is not a body".format(theType)
69     aShape = aResultBody.shape()
70     assert aShape, "{0}: The body does not have a shape".format(theType)
71
72     # Check shape volume
73     aRefVolume = theVolume
74     aResVolume = GeomAlgoAPI_ShapeTools.volume(aShape)
75     assert (math.fabs(aResVolume - aRefVolume) < theDelta), "{0}: The volume is wrong: expected = {1}, real = {2}".format(theType, aRefVolume, aResVolume)
76
77 def testImportXAO():
78     # Create a part for import
79     aSession.startOperation("Create part for import")
80     aPartFeature = aSession.moduleDocument().addFeature("Part")
81     aSession.finishOperation()
82     aPart = aSession.activeDocument()
83
84     aSession.startOperation("Import XAO")
85     anImportFeature = aPart.addFeature("Import")
86     anImportFeature.string("file_path").setValue(getShapePath("Xao/box1.xao"))
87     aSession.finishOperation()
88
89     # Check results
90     assert anImportFeature.error() == ''
91     assert anImportFeature.name() == "mygeom"
92     assert len(anImportFeature.results()) == 1
93     assert modelAPI_ResultBody(anImportFeature.firstResult())
94     assert anImportFeature.firstResult().data().name() == "mygeom_1"
95     aCompositeFeature = featureToCompositeFeature(anImportFeature)
96     # Two groups and one field
97     assert aCompositeFeature.numberOfSubs(False) == 3
98
99     aFeature1 = aCompositeFeature.subFeature(0, False)
100     assert aFeature1.getKind() == "Group"
101     assert aFeature1.name() == "boite_1"
102
103     aSelectionList = aFeature1.selectionList("group_list")
104     assert aSelectionList.selectionType() == "solid"
105     assert aSelectionList.size() == 1
106     assert aSelectionList.value(0).namingName("") == "mygeom_1"
107
108     aFeature2 = aCompositeFeature.subFeature(1, False)
109     assert aFeature2.getKind() == "Group"
110     assert aFeature2.name() == "Group_2"
111
112     aSelectionList = aFeature2.selectionList("group_list")
113     assert aSelectionList.selectionType() == "face"
114     assert aSelectionList.size() == 2
115     assert aSelectionList.value(0).namingName("") == "mygeom_1/Shape1"
116     print(aSelectionList.value(1).namingName(""))
117     assert aSelectionList.value(1).namingName("") == "mygeom_1/Shape2"
118
119     aFeature3 = aCompositeFeature.subFeature(2, False)
120     assert aFeature3.getKind() == "Field"
121     assert aFeature3.name() == "color"
122     assert aFeature3.intArray("stamps").size() == 2
123     assert aFeature3.tables("values").rows() == 2
124     assert aFeature3.tables("values").columns() == 3
125     assert aFeature3.tables("values").tables() == 2
126     assert aFeature3.tables("values").type() == 1 # integer
127     assert aFeature3.selectionList("selected").size() == 1
128
129 if __name__ == '__main__':
130     with TemporaryDirectory() as tmp_dir:
131         #=========================================================================
132         # Create a shape imported from BREP
133         #=========================================================================
134         shape_path = getShapePath("Brep/solid.brep")
135         testImport("BREP", shape_path, 259982.297176, 10 ** -5)
136         shape_path = shutil.copyfile(shape_path, os.path.join(tmp_dir, "solid.brp"))
137         testImport("BRP", shape_path, 259982.297176, 10 ** -5)
138         #=========================================================================
139         # Create a shape imported from STEP
140         #=========================================================================
141         shape_path = getShapePath("Step/screw.step")
142         testImport("STP", shape_path, 3.78827401738e-06, 10 ** -17)
143         shape_path = shutil.copyfile(shape_path, os.path.join(tmp_dir, "screw.stp"))
144         testImport("STEP", shape_path, 3.78827401738e-06, 10 ** -17)
145         #=========================================================================
146         # Create a shape imported from IGES
147         #=========================================================================
148         shape_path = getShapePath("Iges/bearing.igs")
149         testImport("IGES", shape_path, 6.86970803067e-14, 10 ** -25)
150         shape_path = shutil.copyfile(shape_path, os.path.join(tmp_dir, "bearing.iges"))
151         testImport("IGS", shape_path, 6.86970803067e-14, 10 ** -25)
152
153         #=========================================================================
154         # Create a shape imported from XAO
155         #=========================================================================
156         testImportXAO()
157
158         #=========================================================================
159         # End of test
160         #=========================================================================
161
162         from salome.shaper import model
163         assert(model.checkPythonDump())