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