Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / PythonAPI / model / tests / tests.py
1 from GeomAlgoAPI import *
2 from GeomAPI import *
3 from GeomDataAPI import *
4 from ModelAPI import ModelAPI_Feature
5 import math
6 from salome.shaper.model import sketcher
7
8 TOLERANCE = 1.e-7
9
10 aShapeTypes = {
11   GeomAPI_Shape.SOLID:  "GeomAPI_Shape.SOLID",
12   GeomAPI_Shape.FACE:   "GeomAPI_Shape.FACE",
13   GeomAPI_Shape.EDGE:   "GeomAPI_Shape.EDGE",
14   GeomAPI_Shape.VERTEX: "GeomAPI_Shape.VERTEX"}
15
16
17 def generateTests(theFeature, theFeatureName, theTestsList = []):
18   """ Generates tests for theFeature.
19   :param theFeature: feature to test. Should be ModelHighAPI_Interface.
20   :param theFeatureName: feature name to put in test commands.
21   :param theTestsList: list of test to be generated. If empty generates all tests.
22   """
23   if "testNbResults" in theTestsList or len(theTestsList) == 0:
24     aNbResults = len(theFeature.results())
25     print "model.testNbResults({}, {})".format(theFeatureName, aNbResults)
26
27   if "testNbSubResults" in theTestsList or len(theTestsList) == 0:
28     aNbResults = len(theFeature.results())
29     aNbSubResults = []
30     for anIndex in range(0, aNbResults):
31       aNbSubResults.append(theFeature.results()[anIndex].numberOfSubs())
32     print "model.testNbSubResults({}, {})".format(theFeatureName, aNbSubResults)
33
34   if "testNbSubShapes" in theTestsList or len(theTestsList) == 0:
35     aNbResults = len(theFeature.results())
36     for aShapeType in aShapeTypes:
37       aNbSubShapes = []
38       for anIndex in range(0, aNbResults):
39         aShape = theFeature.results()[anIndex].resultSubShapePair()[0].shape()
40         aNbResultSubShapes = 0
41         aShapeExplorer = GeomAPI_ShapeExplorer(aShape, aShapeType)
42         while aShapeExplorer.more():
43           aNbResultSubShapes += 1
44           aShapeExplorer.next();
45         aNbSubShapes.append(aNbResultSubShapes)
46       print "model.testNbSubShapes({}, {}, {})".format(theFeatureName, aShapeTypes[aShapeType], aNbSubShapes)
47
48   if "testResultsVolumes" in theTestsList or len(theTestsList) == 0:
49     aNbResults = len(theFeature.results())
50     aResultsVolumes = []
51     for anIndex in range(0, aNbResults):
52       aResultsVolumes.append(GeomAlgoAPI_ShapeTools_volume(theFeature.results()[anIndex].resultSubShapePair()[0].shape()))
53     print "model.testResultsVolumes({}, [{}])".format(theFeatureName, ", ".join("{:0.27f}".format(i) for i in aResultsVolumes))
54
55
56 def testNbResults(theFeature, theExpectedNbResults):
57   """ Tests number of feature results.
58   :param theFeature: feature to test.
59   :param theExpectedNbResults: expected number of results.
60   """
61   aNbResults = len(theFeature.results())
62   assert (aNbResults == theExpectedNbResults), "Number of results: {}. Expected: {}.".format(aNbResults, theExpectedNbResults)
63
64
65 def testNbSubResults(theFeature, theExpectedNbSubResults):
66   """ Tests number of feature sub-results for each result.
67   :param theFeature: feature to test.
68   :param theExpectedNbSubResults: list of sub-results numbers. Size of list should be equal to len(theFeature.results()).
69   """
70   aNbResults = len(theFeature.results())
71   aListSize = len(theExpectedNbSubResults)
72   assert (aNbResults == aListSize), "Number of results: {} not equal to list size: {}.".format(aNbResults, aListSize)
73   for anIndex in range(0, aNbResults):
74     aNbSubResults = theFeature.results()[anIndex].numberOfSubs()
75     anExpectedNbSubResults = theExpectedNbSubResults[anIndex]
76     assert (aNbSubResults == anExpectedNbSubResults), "Number of sub-results for result[{}]: {}. Expected: {}.".format(anIndex, aNbSubResults, anExpectedNbSubResults)
77
78
79 def testNbSubShapes(theFeature, theShapeType, theExpectedNbSubShapes):
80   """ Tests number of feature sub-shapes of passed type for each result.
81   :param theFeature: feature to test.
82   :param theShapeType: shape type of sub-shapes to test.
83   :param theExpectedNbSubShapes: list of sub-shapes numbers. Size of list should be equal to len(theFeature.results()).
84   """
85   aNbResults = len(theFeature.results())
86   aListSize = len(theExpectedNbSubShapes)
87   assert (aNbResults == aListSize), "Number of results: {} not equal to list size: {}.".format(aNbResults, aListSize)
88   for anIndex in range(0, aNbResults):
89     aNbResultSubShapes = 0
90     anExpectedNbSubShapes = theExpectedNbSubShapes[anIndex]
91     aShape = theFeature.results()[anIndex].resultSubShapePair()[0].shape()
92     aShapeExplorer = GeomAPI_ShapeExplorer(aShape, theShapeType)
93     while aShapeExplorer.more():
94       aNbResultSubShapes += 1
95       aShapeExplorer.next();
96     assert (aNbResultSubShapes == anExpectedNbSubShapes), "Number of sub-shapes of type {} for result[{}]: {}. Expected: {}.".format(aShapeTypes[theShapeType], anIndex, aNbResultSubShapes, anExpectedNbSubShapes)
97
98
99 def testResultsVolumes(theFeature, theExpectedResultsVolumes, theNbSignificantDigits = 7):
100   """ Tests results volumes.
101   :param theFeature: feature to test.
102   :param theExpectedResultsVolumes: list of results volumes. Size of list should be equal to len(theFeature.results()).
103   """
104   aTolerance = 10**(-theNbSignificantDigits)
105   aNbResults = len(theFeature.results())
106   aListSize = len(theExpectedResultsVolumes)
107   assert (aNbResults == aListSize), "Number of results: {} not equal to list size: {}.".format(aNbResults, aListSize)
108   for anIndex in range(0, aNbResults):
109     aResultVolume = GeomAlgoAPI_ShapeTools_volume(theFeature.results()[anIndex].resultSubShapePair()[0].shape())
110     aResultVolumeStr = "{:0.27f}".format(aResultVolume).lstrip("0").lstrip(".").lstrip("0")
111     anExpectedResultVolume = theExpectedResultsVolumes[anIndex]
112     anExpectedResultVolumeStr = "{:0.27f}".format(anExpectedResultVolume).lstrip("0").lstrip(".").lstrip("0")
113     assert math.fabs(aResultVolume - anExpectedResultVolume) < aTolerance * anExpectedResultVolume, "Volume of result[{}]: {:0.27f}. Expected: {:0.27f}. The first {} significant digits not equal.".format(anIndex, aResultVolume, anExpectedResultVolume, theNbSignificantDigits)
114
115 def testHaveNamingFaces(theFeature, theModel, thePartDoc) :
116   """ Tests if all faces of result have a name
117   :param theFeature: feature to test.
118   """
119   # Get feature result/sub-result
120   aResult = theFeature.results()[0].resultSubShapePair()[0]
121   # Get result/sub-result shape
122   shape = aResult.shape()
123   # Create shape explorer with desired shape type
124   shapeExplorer = GeomAPI_ShapeExplorer(shape, GeomAPI_Shape.FACE)
125   # Create list, and store selections in it
126   selectionList = []
127   while shapeExplorer.more():
128     selection = theModel.selection(aResult, shapeExplorer.current()) # First argument should be result/sub-result, second is sub-shape on this result/sub-result
129     selectionList.append(selection)
130     shapeExplorer.next()
131   # Create group with this selection list
132   Group_1 = theModel.addGroup(thePartDoc, selectionList)
133   theModel.do()
134   theModel.end()
135
136   # Now you can check that all selected shapes in group have right shape type and name.
137   groupFeature = Group_1.feature()
138   groupSelectionList = groupFeature.selectionList("group_list")
139   theModel.end()
140   assert(groupSelectionList.size() == len(selectionList))
141   for index in range(0, groupSelectionList.size()):
142     attrSelection = groupSelectionList.value(index)
143     shape = attrSelection.value()
144     name = attrSelection.namingName()
145     assert(shape.isFace())
146     assert(name != ""), "String empty"
147
148 def testNbSubFeatures(theComposite, theKindOfSub, theExpectedCount):
149   """ Tests number of sub-features of the given type
150   :param theComposite     composite feature to check its subs
151   :param theKindOfSub     kind of sub-feature to calculate count
152   :param theExpectedCount expected number of sub-features
153   """
154   count = 0
155   for aSub in theComposite.features().list():
156     aFeature = ModelAPI_Feature.feature(aSub)
157     if aFeature is not None and aFeature.getKind() == theKindOfSub:
158        count += 1
159   assert (count == theExpectedCount), "Number of sub-features of type {}: {}, expected {}".format(theKindOfSub, count, theExpectedCount)
160
161 def assertSketchArc(theArcFeature):
162   """ Tests whether the arc is correctly defined
163   """
164   aCenterPnt = geomDataAPI_Point2D(theArcFeature.attribute("center_point"))
165   aStartPnt = geomDataAPI_Point2D(theArcFeature.attribute("start_point"))
166   aEndPnt = geomDataAPI_Point2D(theArcFeature.attribute("end_point"))
167   aRadius = theArcFeature.real("radius")
168   aDistCS = sketcher.tools.distancePointPoint(aCenterPnt, aStartPnt)
169   aDistCE = sketcher.tools.distancePointPoint(aCenterPnt, aEndPnt)
170   assert math.fabs(aDistCS - aDistCE) < TOLERANCE, "Wrong arc: center-start distance {}, center-end distance {}".format(aDistCS, aDistCE)
171   assert math.fabs(aRadius.value() -aDistCS) < TOLERANCE, "Wrong arc: radius is {0}, expected {1}".format(aRadius.value(), aDistCS)