1 # Copyright (C) 2014-2022 CEA/DEN, EDF R&D
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.
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.
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
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 Unit test of ExchangePlugin_TestImport class
24 #=========================================================================
25 # Initialization of the test
26 #=========================================================================
27 from ModelAPI import *
28 from GeomAlgoAPI import *
33 from tempfile import TemporaryDirectory
35 __updated__ = "2015-05-22"
37 aSession = ModelAPI_Session.get()
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)
46 def testImport(theType, theFile, theVolume, theArea, 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()
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 if theType == "STP" or theType == "STEP":
58 aFieldName = "step_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 aFieldName = "step_scale_inter_units"
63 units = anImportFeature.boolean(aFieldName)
64 assert units, "{0}: Can not receive string field {1}".format(theType, aFieldName)
66 aFieldName = "file_path"
67 file = anImportFeature.string(aFieldName)
68 assert file, "{0}: Can not receive string field {1}".format(theType, aFieldName)
69 file.setValue(theFile)
70 aFieldName = "ImportType"
71 type = anImportFeature.string(aFieldName)
72 assert type, "{0}: Can not receive string field {1}".format(theType, aFieldName)
73 type.setValue(theType)
74 aSession.finishOperation()
77 assert anImportFeature.error() != ''
80 assert anImportFeature.error() == '', "{0}: The error after execution: {1}".format(theType, anImportFeature.error())
81 assert len(anImportFeature.results()) == 1, "{0}: Wrong number of results: expected = 1, real = {1}".format(theType, len(anImportFeature.results()))
82 aResultBody = modelAPI_ResultBody(anImportFeature.firstResult())
83 assert aResultBody, "{0}: The result is not a body".format(theType)
84 aShape = aResultBody.shape()
85 assert aShape, "{0}: The body does not have a shape".format(theType)
88 aRefVolume = theVolume
89 aResVolume = GeomAlgoAPI_ShapeTools.volume(aShape)
90 assert (math.fabs(aResVolume - aRefVolume) < theDelta), "{0}: The volume is wrong: expected = {1}, real = {2}".format(theType, aRefVolume, aResVolume)
93 aResArea = GeomAlgoAPI_ShapeTools.area(aShape)
94 assert (math.fabs(aResArea - aRefArea) < theDelta), "{0}: The area is wrong: expected = {1}, real = {2}".format(theType, aRefArea, aResArea)
97 # Create a part for import
98 aSession.startOperation("Create part for import")
99 aPartFeature = aSession.moduleDocument().addFeature("Part")
100 aSession.finishOperation()
101 aPart = aSession.activeDocument()
103 aSession.startOperation("Import XAO")
104 anImportFeature = aPart.addFeature("Import")
105 anImportFeature.string("file_path").setValue(getShapePath("Xao/box1.xao"))
106 aFieldName = "ImportType"
107 type = anImportFeature.string(aFieldName)
109 aSession.finishOperation()
112 assert anImportFeature.error() == ''
113 assert anImportFeature.name() == "mygeom"
114 assert len(anImportFeature.results()) == 1
115 assert modelAPI_ResultBody(anImportFeature.firstResult())
116 assert anImportFeature.firstResult().data().name() == "mygeom_1"
117 aCompositeFeature = featureToCompositeFeature(anImportFeature)
118 # Two groups and one field
119 assert aCompositeFeature.numberOfSubs(False) == 3
121 aFeature1 = aCompositeFeature.subFeature(0, False)
122 assert aFeature1.getKind() == "Group"
123 assert aFeature1.name() == "boite_1"
125 aSelectionList = aFeature1.selectionList("group_list")
126 assert aSelectionList.selectionType() == "solid"
127 assert aSelectionList.size() == 1
128 assert aSelectionList.value(0).namingName("") == "mygeom_1"
130 aFeature2 = aCompositeFeature.subFeature(1, False)
131 assert aFeature2.getKind() == "Group"
132 assert aFeature2.name() == "Group_2"
134 aSelectionList = aFeature2.selectionList("group_list")
135 assert aSelectionList.selectionType() == "face"
136 assert aSelectionList.size() == 2
137 assert aSelectionList.value(0).namingName("") == "mygeom_1/Shape_1"
138 assert aSelectionList.value(1).namingName("") == "mygeom_1/Shape_2"
140 aFeature3 = aCompositeFeature.subFeature(2, False)
141 assert aFeature3.getKind() == "Field"
142 assert aFeature3.name() == "color"
143 assert aFeature3.intArray("stamps").size() == 2
144 assert aFeature3.tables("values").rows() == 2
145 assert aFeature3.tables("values").columns() == 3
146 assert aFeature3.tables("values").tables() == 2
147 assert aFeature3.tables("values").type() == 1 # integer
148 assert aFeature3.selectionList("selected").size() == 1
150 if __name__ == '__main__':
151 with TemporaryDirectory() as tmp_dir:
152 #=========================================================================
153 # Create a shape imported from BREP
154 #=========================================================================
155 shape_path = getShapePath("Brep/solid.brep")
156 testImport("BREP", shape_path, 259982.297176, 39481.415022205365, 10 ** -5)
157 shape_path = shutil.copyfile(shape_path, os.path.join(tmp_dir, "solid.brp"))
158 testImport("BRP", shape_path, 259982.297176, 39481.415022205365, 10 ** -5)
159 #=========================================================================
160 # Create a shape imported from STEP
161 #=========================================================================
162 shape_path = getShapePath("Step/screw.step")
163 testImport("STEP", shape_path, 3.78827401738e-06, 0.0019293313778547085, 10 ** -17)
164 shape_path = shutil.copyfile(shape_path, os.path.join(tmp_dir, "screw.stp"))
165 testImport("STEP", shape_path, 3.78827401738e-06, 0.0019293313778547085, 10 ** -17)
166 #=========================================================================
167 # Create a shape imported from IGES
168 #=========================================================================
169 shape_path = getShapePath("Iges/bearing.igs")
170 testImport("IGS", shape_path, 0.0, 1.3407098545036494e-08, 10 ** -25)
171 shape_path = shutil.copyfile(shape_path, os.path.join(tmp_dir, "bearing.iges"))
172 testImport("IGES", shape_path, 0.0, 1.3407098545036494e-08, 10 ** -25)
174 #=========================================================================
175 # Create a shape imported from XAO
176 #=========================================================================
179 #=========================================================================
181 #=========================================================================
183 from salome.shaper import model
184 assert(model.checkPythonDump())
186 #=========================================================================
187 # Check import errors
188 #=========================================================================
189 testImport("BREP", "", 0, 0, 10 ** -25, True)
190 shape_path = getShapePath("Brep/solid.dwg")
191 testImport("BREP", shape_path, 0, 0, 10 ** -25, True)
192 shape_path = getShapePath("Xao/wrong_file.xao")
193 testImport("XAO", shape_path, 0, 0, 10 ** -25, True)