Salome HOME
2b21b5fe49beb74d407dfddca33f1f37f2ba27bb
[modules/shaper.git] / src / ExchangePlugin / Test / TestImport.py
1 """
2       TestImport.py
3       Unit test of ExchangePlugin_TestImport class
4 """
5 #=========================================================================
6 # Initialization of the test
7 #=========================================================================
8 from ModelAPI import *
9 from GeomDataAPI import *
10 from GeomAlgoAPI import *
11 from GeomAPI import *
12 import os
13 import math
14
15 __updated__ = "2015-05-22"
16
17 aSession = ModelAPI_Session.get()
18 #=========================================================================
19 # Common test function
20 #=========================================================================
21 def testImport(theType, theFile, theVolume, theDelta):
22     # Create a part for import
23     aSession.startOperation()
24     aPartFeature = aSession.moduleDocument().addFeature("Part")
25     aSession.finishOperation()
26     aPart = aSession.activeDocument()
27     
28     aSession.startOperation()
29     aFeatureKind = "Import"
30     anImportFeature = aPart.addFeature(aFeatureKind)
31     assert anImportFeature, "{0}: Can not create a feature {1}".format(theType, aFeatureKind)
32     aFieldName = "import_file_selector"
33     file = anImportFeature.string(aFieldName)
34     assert file, "{0}: Can not receive string field {1}".format(theType, aFieldName)
35     file.setValue(theFile)
36     anImportFeature.execute()
37     aSession.finishOperation()
38     
39     # Check results
40     assert anImportFeature.error() == '', "{0}: The error after execution: {1}".format(theType, anImportFeature.error())
41     assert len(anImportFeature.results()) == 1, "{0}: Wrong number of results: expected = 1, real = {1}".format(theType, len(anImportFeature.results()))
42     aResultBody = modelAPI_ResultBody(anImportFeature.firstResult())
43     assert aResultBody, "{0}: The result is not a body".format(theType)
44     aShape = aResultBody.shape()
45     assert aShape, "{0}: The body does not have a shape".format(theType)
46     
47     # Check shape volume
48     aRefVolume = theVolume
49     aResVolume = GeomAlgoAPI_ShapeProps.volume(aShape)
50     assert (math.fabs(aResVolume - aRefVolume) < theDelta), "{0}: The volume is wrong: expected = {1}, real = {2}".format(theType, aRefVolume, aResVolume)
51
52 if __name__ == '__main__':
53 #=========================================================================
54 # Create a shape imported from BREP
55 #=========================================================================
56     testImport("BREP", "Data/solid.brep", 259982.29715, 10 ** -5)
57     testImport("BRP", "Data/solid.brp", 259982.29715, 10 ** -5)
58 #=========================================================================
59 # Create a shape imported from STEP
60 #=========================================================================
61     testImport("STP", "Data/screw.stp", 3.78827059338e-06, 10 ** -17)
62     testImport("STEP", "Data/screw.step", 3.78827059338e-06, 10 ** -17)
63 #=========================================================================
64 # Create a shape imported from IGES
65 #=========================================================================
66     testImport("IGES", "Data/bearing.iges", 6.86980756235e-14, 10 ** -25)
67     testImport("IGS", "Data/bearing.igs", 6.86980756235e-14, 10 ** -25)
68 #=========================================================================
69 # End of test
70 #=========================================================================