Salome HOME
Merge branch 'master' into cgt/devCEA
[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 GeomAlgoAPI import *
10 import math
11
12 __updated__ = "2015-05-22"
13
14 aSession = ModelAPI_Session.get()
15 #=========================================================================
16 # Common test function
17 #=========================================================================
18 def testImport(theType, theFile, theVolume, theDelta):
19     # Create a part for import
20     aSession.startOperation("Create part for import")
21     aPartFeature = aSession.moduleDocument().addFeature("Part")
22     aSession.finishOperation()
23     aPart = aSession.activeDocument()
24
25     aSession.startOperation("Import file")
26     aFeatureKind = "Import"
27     anImportFeature = aPart.addFeature(aFeatureKind)
28     assert anImportFeature, "{0}: Can not create a feature {1}".format(theType, aFeatureKind)
29     aFieldName = "file_path"
30     file = anImportFeature.string(aFieldName)
31     assert file, "{0}: Can not receive string field {1}".format(theType, aFieldName)
32     file.setValue(theFile)
33     aSession.finishOperation()
34
35     # Check results
36     assert anImportFeature.error() == '', "{0}: The error after execution: {1}".format(theType, anImportFeature.error())
37     assert len(anImportFeature.results()) == 1, "{0}: Wrong number of results: expected = 1, real = {1}".format(theType, len(anImportFeature.results()))
38     aResultBody = modelAPI_ResultBody(anImportFeature.firstResult())
39     assert aResultBody, "{0}: The result is not a body".format(theType)
40     aShape = aResultBody.shape()
41     assert aShape, "{0}: The body does not have a shape".format(theType)
42
43     # Check shape volume
44     aRefVolume = theVolume
45     aResVolume = GeomAlgoAPI_ShapeTools.volume(aShape)
46     assert (math.fabs(aResVolume - aRefVolume) < theDelta), "{0}: The volume is wrong: expected = {1}, real = {2}".format(theType, aRefVolume, aResVolume)
47
48 def testImportXAO():
49     # Create a part for import
50     aSession.startOperation("Create part for import")
51     aPartFeature = aSession.moduleDocument().addFeature("Part")
52     aSession.finishOperation()
53     aPart = aSession.activeDocument()
54
55     aSession.startOperation("Import XAO")
56     anImportFeature = aPart.addFeature("Import")
57     anImportFeature.string("file_path").setValue("Data/test.xao")
58     aSession.finishOperation()
59
60     # Check results
61     assert anImportFeature.error() == ''
62     assert anImportFeature.name() == "mygeom"
63     assert len(anImportFeature.results()) == 1
64     assert modelAPI_ResultBody(anImportFeature.firstResult())
65     assert anImportFeature.firstResult().data().name() == "mygeom_1"
66     aCompositeFeature = featureToCompositeFeature(anImportFeature)
67     # Two groups and one field
68     assert aCompositeFeature.numberOfSubs(False) == 3
69
70     aFeature1 = aCompositeFeature.subFeature(0, False)
71     assert aFeature1.getKind() == "Group"
72     assert aFeature1.name() == "boite_1"
73
74     aSelectionList = aFeature1.selectionList("group_list")
75     assert aSelectionList.selectionType() == "solid"
76     assert aSelectionList.size() == 1
77     assert aSelectionList.value(0).namingName("") == "mygeom_1"
78
79     aFeature2 = aCompositeFeature.subFeature(1, False)
80     assert aFeature2.getKind() == "Group"
81     assert aFeature2.name() == "Group_2"
82
83     aSelectionList = aFeature2.selectionList("group_list") 
84     assert aSelectionList.selectionType() == "face"
85     assert aSelectionList.size() == 2
86     assert aSelectionList.value(0).namingName("") == "mygeom_1/Shape1"
87     print aSelectionList.value(1).namingName("")
88     assert aSelectionList.value(1).namingName("") == "mygeom_1/Shape2"
89
90     aFeature3 = aCompositeFeature.subFeature(2, False)
91     assert aFeature3.getKind() == "Field"
92     assert aFeature3.name() == "color"
93     assert aFeature3.intArray("stamps").size() == 2
94     assert aFeature3.tables("values").rows() == 2
95     assert aFeature3.tables("values").columns() == 3
96     assert aFeature3.tables("values").tables() == 2
97     assert aFeature3.tables("values").type() == 1 # integer
98     assert aFeature3.selectionList("selected").size() == 1
99
100 if __name__ == '__main__':
101 #=========================================================================
102 # Create a shape imported from BREP
103 #=========================================================================
104     testImport("BREP", "Data/solid.brep", 259982.297176, 10 ** -5)
105     testImport("BRP", "Data/solid.brp", 259982.297176, 10 ** -5)
106 #=========================================================================
107 # Create a shape imported from STEP
108 #=========================================================================
109     testImport("STP", "Data/screw.stp", 3.78827401738e-06, 10 ** -17)
110     testImport("STEP", "Data/screw.step", 3.78827401738e-06, 10 ** -17)
111 #=========================================================================
112 # Create a shape imported from IGES
113 #=========================================================================
114     testImport("IGES", "Data/bearing.iges", 6.86970803067e-14, 10 ** -25)
115     testImport("IGS", "Data/bearing.igs", 6.86970803067e-14, 10 ** -25)
116 #=========================================================================
117 # Create a shape imported from XAO
118 #=========================================================================
119     testImportXAO()
120 #=========================================================================
121 # End of test
122 #=========================================================================
123
124 from salome.shaper import model
125 assert(model.checkPythonDump())