1 from GeomAlgoAPI import *
3 from GeomDataAPI import *
4 from ModelAPI import ModelAPI_Feature
8 GeomAPI_Shape.SOLID: "GeomAPI_Shape.SOLID",
9 GeomAPI_Shape.FACE: "GeomAPI_Shape.FACE",
10 GeomAPI_Shape.EDGE: "GeomAPI_Shape.EDGE",
11 GeomAPI_Shape.VERTEX: "GeomAPI_Shape.VERTEX"}
14 def generateTests(theFeature, theFeatureName, theTestsList = []):
15 """ Generates tests for theFeature.
16 :param theFeature: feature to test. Should be ModelHighAPI_Interface.
17 :param theFeatureName: feature name to put in test commands.
18 :param theTestsList: list of test to be generated. If empty generates all tests.
20 if "testNbResults" in theTestsList or len(theTestsList) == 0:
21 aNbResults = len(theFeature.results())
22 print "model.testNbResults({}, {})".format(theFeatureName, aNbResults)
24 if "testNbSubResults" in theTestsList or len(theTestsList) == 0:
25 aNbResults = len(theFeature.results())
27 for anIndex in range(0, aNbResults):
28 aNbSubResults.append(theFeature.results()[anIndex].numberOfSubs())
29 print "model.testNbSubResults({}, {})".format(theFeatureName, aNbSubResults)
31 if "testNbSubShapes" in theTestsList or len(theTestsList) == 0:
32 aNbResults = len(theFeature.results())
33 for aShapeType in aShapeTypes:
35 for anIndex in range(0, aNbResults):
36 aShape = theFeature.results()[anIndex].resultSubShapePair()[0].shape()
37 aNbResultSubShapes = 0
38 aShapeExplorer = GeomAPI_ShapeExplorer(aShape, aShapeType)
39 while aShapeExplorer.more():
40 aNbResultSubShapes += 1
41 aShapeExplorer.next();
42 aNbSubShapes.append(aNbResultSubShapes)
43 print "model.testNbSubShapes({}, {}, {})".format(theFeatureName, aShapeTypes[aShapeType], aNbSubShapes)
45 if "testResultsVolumes" in theTestsList or len(theTestsList) == 0:
46 aNbResults = len(theFeature.results())
48 for anIndex in range(0, aNbResults):
49 aResultsVolumes.append(GeomAlgoAPI_ShapeTools_volume(theFeature.results()[anIndex].resultSubShapePair()[0].shape()))
50 print "model.testResultsVolumes({}, [{}])".format(theFeatureName, ", ".join("{:0.27f}".format(i) for i in aResultsVolumes))
53 def testNbResults(theFeature, theExpectedNbResults):
54 """ Tests number of feature results.
55 :param theFeature: feature to test.
56 :param theExpectedNbResults: expected number of results.
58 aNbResults = len(theFeature.results())
59 assert (aNbResults == theExpectedNbResults), "Number of results: {}. Expected: {}.".format(aNbResults, theExpectedNbResults)
62 def testNbSubResults(theFeature, theExpectedNbSubResults):
63 """ Tests number of feature sub-results for each result.
64 :param theFeature: feature to test.
65 :param theExpectedNbSubResults: list of sub-results numbers. Size of list should be equal to len(theFeature.results()).
67 aNbResults = len(theFeature.results())
68 aListSize = len(theExpectedNbSubResults)
69 assert (aNbResults == aListSize), "Number of results: {} not equal to list size: {}.".format(aNbResults, aListSize)
70 for anIndex in range(0, aNbResults):
71 aNbSubResults = theFeature.results()[anIndex].numberOfSubs()
72 anExpectedNbSubResults = theExpectedNbSubResults[anIndex]
73 assert (aNbSubResults == anExpectedNbSubResults), "Number of sub-results for result[{}]: {}. Expected: {}.".format(anIndex, aNbSubResults, anExpectedNbSubResults)
76 def testNbSubShapes(theFeature, theShapeType, theExpectedNbSubShapes):
77 """ Tests number of feature sub-shapes of passed type for each result.
78 :param theFeature: feature to test.
79 :param theShapeType: shape type of sub-shapes to test.
80 :param theExpectedNbSubShapes: list of sub-shapes numbers. Size of list should be equal to len(theFeature.results()).
82 aNbResults = len(theFeature.results())
83 aListSize = len(theExpectedNbSubShapes)
84 assert (aNbResults == aListSize), "Number of results: {} not equal to list size: {}.".format(aNbResults, aListSize)
85 for anIndex in range(0, aNbResults):
86 aNbResultSubShapes = 0
87 anExpectedNbSubShapes = theExpectedNbSubShapes[anIndex]
88 aShape = theFeature.results()[anIndex].resultSubShapePair()[0].shape()
89 aShapeExplorer = GeomAPI_ShapeExplorer(aShape, theShapeType)
90 while aShapeExplorer.more():
91 aNbResultSubShapes += 1
92 aShapeExplorer.next();
93 assert (aNbResultSubShapes == anExpectedNbSubShapes), "Number of sub-shapes of type {} for result[{}]: {}. Expected: {}.".format(aShapeTypes[theShapeType], anIndex, aNbResultSubShapes, anExpectedNbSubShapes)
96 def testResultsVolumes(theFeature, theExpectedResultsVolumes, theNbSignificantDigits = 7):
97 """ Tests results volumes.
98 :param theFeature: feature to test.
99 :param theExpectedResultsVolumes: list of results volumes. Size of list should be equal to len(theFeature.results()).
101 aTolerance = 10**(-theNbSignificantDigits)
102 aNbResults = len(theFeature.results())
103 aListSize = len(theExpectedResultsVolumes)
104 assert (aNbResults == aListSize), "Number of results: {} not equal to list size: {}.".format(aNbResults, aListSize)
105 for anIndex in range(0, aNbResults):
106 aResultVolume = GeomAlgoAPI_ShapeTools_volume(theFeature.results()[anIndex].resultSubShapePair()[0].shape())
107 aResultVolumeStr = "{:0.27f}".format(aResultVolume).lstrip("0").lstrip(".").lstrip("0")
108 anExpectedResultVolume = theExpectedResultsVolumes[anIndex]
109 anExpectedResultVolumeStr = "{:0.27f}".format(anExpectedResultVolume).lstrip("0").lstrip(".").lstrip("0")
110 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)
112 def testHaveNamingFaces(theFeature, theModel, thePartDoc) :
113 """ Tests if all faces of result have a name
114 :param theFeature: feature to test.
116 # Get feature result/sub-result
117 aResult = theFeature.results()[0].resultSubShapePair()[0]
118 # Get result/sub-result shape
119 shape = aResult.shape()
120 # Create shape explorer with desired shape type
121 shapeExplorer = GeomAPI_ShapeExplorer(shape, GeomAPI_Shape.FACE)
122 # Create list, and store selections in it
124 while shapeExplorer.more():
125 selection = theModel.selection(aResult, shapeExplorer.current()) # First argument should be result/sub-result, second is sub-shape on this result/sub-result
126 selectionList.append(selection)
128 # Create group with this selection list
129 Group_1 = theModel.addGroup(thePartDoc, selectionList)
133 # Now you can check that all selected shapes in group have right shape type and name.
134 groupFeature = Group_1.feature()
135 groupSelectionList = groupFeature.selectionList("group_list")
137 assert(groupSelectionList.size() == len(selectionList))
138 for index in range(0, groupSelectionList.size()):
139 attrSelection = groupSelectionList.value(index)
140 shape = attrSelection.value()
141 name = attrSelection.namingName()
142 assert(shape.isFace())
143 assert(name != ""), "String empty"
145 def testNbSubFeatures(theComposite, theKindOfSub, theExpectedCount):
146 """ Tests number of sub-features of the given type
147 :param theComposite composite feature to check its subs
148 :param theKindOfSub kind of sub-feature to calculate count
149 :param theExpectedCount expected number of sub-features
152 for aSub in theComposite.features().list():
153 aFeature = ModelAPI_Feature.feature(aSub)
154 if aFeature is not None and aFeature.getKind() == theKindOfSub:
156 assert (count == theExpectedCount), "Number of sub-features of type {}: {}, expected {}".format(theKindOfSub, count, theExpectedCount)