1 # Copyright (C) 2014-2021 CEA/DEN, EDF R&D
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.
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.
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
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 from GeomAlgoAPI import *
22 from GeomDataAPI import *
23 from ModelAPI import ModelAPI_Feature, ModelAPI_Session
24 from ModelHighAPI import *
26 from salome.shaper.model import sketcher
31 GeomAPI_Shape.SOLID: "GeomAPI_Shape.SOLID",
32 GeomAPI_Shape.FACE: "GeomAPI_Shape.FACE",
33 GeomAPI_Shape.EDGE: "GeomAPI_Shape.EDGE",
34 GeomAPI_Shape.VERTEX: "GeomAPI_Shape.VERTEX"}
37 def generateTests(theFeature, theFeatureName, theTestsList = []):
38 """ Generates tests for theFeature.
39 :param theFeature: feature to test. Should be ModelHighAPI_Interface.
40 :param theFeatureName: feature name to put in test commands.
41 :param theTestsList: list of test to be generated. If empty generates all tests.
43 if "testNbResults" in theTestsList or len(theTestsList) == 0:
44 aNbResults = len(theFeature.results())
45 print("model.testNbResults({}, {})".format(theFeatureName, aNbResults))
47 if "testNbSubResults" in theTestsList or len(theTestsList) == 0:
48 aNbResults = len(theFeature.results())
50 for anIndex in range(0, aNbResults):
51 aNbSubResults.append(theFeature.results()[anIndex].numberOfSubs())
52 print("model.testNbSubResults({}, {})".format(theFeatureName, aNbSubResults))
54 if "testNbSubShapes" in theTestsList or len(theTestsList) == 0:
55 aNbResults = len(theFeature.results())
56 for aShapeType in aShapeTypes:
58 for anIndex in range(0, aNbResults):
59 aShape = theFeature.results()[anIndex].resultSubShapePair()[0].shape()
60 aNbResultSubShapes = 0
61 aShapeExplorer = GeomAPI_ShapeExplorer(aShape, aShapeType)
62 while aShapeExplorer.more():
63 aNbResultSubShapes += 1
65 aNbSubShapes.append(aNbResultSubShapes)
66 print("model.testNbSubShapes({}, {}, {})".format(theFeatureName, aShapeTypes[aShapeType], aNbSubShapes))
68 if "testResultsVolumes" in theTestsList or len(theTestsList) == 0:
69 aNbResults = len(theFeature.results())
71 for anIndex in range(0, aNbResults):
72 aResultsVolumes.append(GeomAlgoAPI_ShapeTools_volume(theFeature.results()[anIndex].resultSubShapePair()[0].shape()))
73 print("model.testResultsVolumes({}, [{}])".format(theFeatureName, ", ".join("{:0.27f}".format(i) for i in aResultsVolumes)))
75 if "testResultsAreas" in theTestsList or len(theTestsList) == 0:
76 aNbResults = len(theFeature.results())
78 for anIndex in range(0, aNbResults):
79 aResultsAreas.append(GeomAlgoAPI_ShapeTools_area(theFeature.results()[anIndex].resultSubShapePair()[0].shape()))
80 print("model.testResultsAreas({}, [{}])".format(theFeatureName, ", ".join("{:0.27f}".format(i) for i in aResultsAreas)))
83 def testNbResults(theFeature, theExpectedNbResults):
84 """ Tests number of feature results.
85 :param theFeature: feature to test.
86 :param theExpectedNbResults: expected number of results.
88 aNbResults = len(theFeature.results())
89 assert (aNbResults == theExpectedNbResults), "Number of results: {}. Expected: {}.".format(aNbResults, theExpectedNbResults)
92 def testNbSubResults(theFeature, theExpectedNbSubResults):
93 """ Tests number of feature sub-results for each result.
94 :param theFeature: feature to test.
95 :param theExpectedNbSubResults: list of sub-results numbers. Size of list should be equal to len(theFeature.results()).
97 aNbResults = len(theFeature.results())
98 aListSize = len(theExpectedNbSubResults)
99 assert (aNbResults == aListSize), "Number of results: {} not equal to list size: {}.".format(aNbResults, aListSize)
100 for anIndex in range(0, aNbResults):
101 aNbSubResults = theFeature.results()[anIndex].numberOfSubs()
102 anExpectedNbSubResults = theExpectedNbSubResults[anIndex]
103 assert (aNbSubResults == anExpectedNbSubResults), "Number of sub-results for result[{}]: {}. Expected: {}.".format(anIndex, aNbSubResults, anExpectedNbSubResults)
106 def testNbSubShapes(theFeature, theShapeType, theExpectedNbSubShapes):
107 """ Tests number of feature sub-shapes of passed type for each result.
108 :param theFeature: feature to test.
109 :param theShapeType: shape type of sub-shapes to test.
110 :param theExpectedNbSubShapes: list of sub-shapes numbers. Size of list should be equal to len(theFeature.results()).
112 aNbResults = len(theFeature.results())
113 aListSize = len(theExpectedNbSubShapes)
114 assert (aNbResults == aListSize), "Number of results: {} not equal to list size: {}.".format(aNbResults, aListSize)
115 for anIndex in range(0, aNbResults):
116 aNbResultSubShapes = 0
117 anExpectedNbSubShapes = theExpectedNbSubShapes[anIndex]
118 aShape = theFeature.results()[anIndex].resultSubShapePair()[0].shape()
119 aShapeExplorer = GeomAPI_ShapeExplorer(aShape, theShapeType)
120 while aShapeExplorer.more():
121 aNbResultSubShapes += 1
122 aShapeExplorer.next()
123 assert (aNbResultSubShapes == anExpectedNbSubShapes), "Number of sub-shapes of type {} for result[{}]: {}. Expected: {}.".format(aShapeTypes[theShapeType], anIndex, aNbResultSubShapes, anExpectedNbSubShapes)
126 def testResultsVolumes(theFeature, theExpectedResultsVolumes, theNbSignificantDigits = 7):
127 """ Tests results volumes.
128 :param theFeature: feature to test.
129 :param theExpectedResultsVolumes: list of results volumes. Size of list should be equal to len(theFeature.results()).
131 aTolerance = 10**(-theNbSignificantDigits)
132 aNbResults = len(theFeature.results())
133 aListSize = len(theExpectedResultsVolumes)
134 assert (aNbResults == aListSize), "Number of results: {} not equal to list size: {}.".format(aNbResults, aListSize)
135 for anIndex in range(0, aNbResults):
136 aResultVolume = GeomAlgoAPI_ShapeTools_volume(theFeature.results()[anIndex].resultSubShapePair()[0].shape())
137 aResultVolumeStr = "{:0.27f}".format(aResultVolume).lstrip("0").lstrip(".").lstrip("0")
138 anExpectedResultVolume = theExpectedResultsVolumes[anIndex]
139 anExpectedResultVolumeStr = "{:0.27f}".format(anExpectedResultVolume).lstrip("0").lstrip(".").lstrip("0")
140 assert math.fabs(aResultVolume - anExpectedResultVolume) <= aTolerance * math.fabs(anExpectedResultVolume), "Volume of result[{}]: {:0.27f}. Expected: {:0.27f}. The first {} significant digits not equal.".format(anIndex, aResultVolume, anExpectedResultVolume, theNbSignificantDigits)
143 def testResultsAreas(theFeature, theExpectedResultsVolumes, theNbSignificantDigits = 7):
144 """ Tests results areas.
145 :param theFeature: feature to test.
146 :param theExpectedResultsAreas: list of results areas. Size of list should be equal to len(theFeature.results()).
148 aTolerance = 10**(-theNbSignificantDigits)
149 aNbResults = len(theFeature.results())
150 aListSize = len(theExpectedResultsVolumes)
151 assert (aNbResults == aListSize), "Number of results: {} not equal to list size: {}.".format(aNbResults, aListSize)
152 for anIndex in range(0, aNbResults):
153 aResultVolume = GeomAlgoAPI_ShapeTools_area(theFeature.results()[anIndex].resultSubShapePair()[0].shape())
154 aResultVolumeStr = "{:0.27f}".format(aResultVolume).lstrip("0").lstrip(".").lstrip("0")
155 anExpectedResultVolume = theExpectedResultsVolumes[anIndex]
156 anExpectedResultVolumeStr = "{:0.27f}".format(anExpectedResultVolume).lstrip("0").lstrip(".").lstrip("0")
157 assert math.fabs(aResultVolume - anExpectedResultVolume) <= aTolerance * math.fabs(anExpectedResultVolume), "Area of result[{}]: {:0.27f}. Expected: {:0.27f}. The first {} significant digits not equal.".format(anIndex, aResultVolume, anExpectedResultVolume, theNbSignificantDigits)
160 def testHaveNamingFaces(theFeature, theModel, thePartDoc) :
161 """ Tests if all faces of result have a name
162 :param theFeature: feature to test.
164 # open transaction since all the checking are performed in tests after model.end() call
166 # Get feature result/sub-result
167 aResult = theFeature.results()[0].resultSubShapePair()[0]
168 # Get result/sub-result shape
169 shape = aResult.shape()
170 # Create shape explorer with desired shape type
171 shapeExplorer = GeomAPI_ShapeExplorer(shape, GeomAPI_Shape.FACE)
172 # Create list, and store selections in it
174 while shapeExplorer.more():
175 selection = theModel.selection(aResult, shapeExplorer.current()) # First argument should be result/sub-result, second is sub-shape on this result/sub-result
176 selectionList.append(selection)
178 # Create group with this selection list
179 Group_1 = theModel.addGroup(thePartDoc, selectionList)
182 # Now you can check that all selected shapes in group have right shape type and name.
183 groupFeature = Group_1.feature()
184 groupSelectionList = groupFeature.selectionList("group_list")
185 assert(groupSelectionList.size() == len(selectionList))
186 for index in range(0, groupSelectionList.size()):
187 attrSelection = groupSelectionList.value(index)
188 shape = attrSelection.value()
189 name = attrSelection.namingName()
190 assert(shape.isFace())
191 assert(name != ""), "String empty"
193 def testHaveNamingEdges(theFeature, theModel, thePartDoc) :
194 """ Tests if all edges of result have a name
195 :param theFeature: feature to test.
197 # Get feature result/sub-result
198 aResult = theFeature.results()[0].resultSubShapePair()[0]
199 # Get result/sub-result shape
200 shape = aResult.shape()
201 # Create shape explorer with desired shape type
202 shapeExplorer = GeomAPI_ShapeExplorer(shape, GeomAPI_Shape.EDGE)
203 # Create list, and store selections in it
205 while shapeExplorer.more():
206 selection = theModel.selection(aResult, shapeExplorer.current()) # First argument should be result/sub-result, second is sub-shape on this result/sub-result
207 selectionList.append(selection)
209 # Create group with this selection list
210 Group_1 = theModel.addGroup(thePartDoc, selectionList)
214 # Now you can check that all selected shapes in group have right shape type and name.
215 groupFeature = Group_1.feature()
216 groupSelectionList = groupFeature.selectionList("group_list")
218 assert(groupSelectionList.size() == len(selectionList))
219 for index in range(0, groupSelectionList.size()):
220 attrSelection = groupSelectionList.value(index)
221 shape = attrSelection.value()
222 name = attrSelection.namingName()
223 assert(shape.isEdge())
224 assert(name != ""), "String empty"
226 def lowerLevelSubResults(theResult, theList):
227 """ Collects in a list all lover level sub-results (without children).
228 Auxiliary method for context correct definition.
230 nbSubs = theResult.numberOfSubs()
232 theList.append(theResult)
234 for sub in range(0, nbSubs):
235 lowerLevelSubResults(theResult.subResult(sub), theList)
237 def testHaveNamingByType(theFeature, theModel, thePartDoc, theSubshapeType) :
238 """ Tests if all sub-shapes of result have a unique name
239 :param theFeature: feature to test.
240 :param theSubshapeType: type of sub-shape
242 if not theFeature.results():
244 aFirstRes = theFeature.results()[0]
246 lowerLevelSubResults(aFirstRes, aResList)
249 shapesList = [] # to append only unique shapes (not isSame)
251 # Get feature result/sub-result
252 aResult = aR.resultSubShapePair()[0]
253 # Get result/sub-result shape
254 shape = aResult.shape()
255 # Create shape explorer with desired shape type
256 shapeExplorer = GeomAPI_ShapeExplorer(shape, theSubshapeType)
257 # Create list, and store selections in it
258 while shapeExplorer.more():
259 current = shapeExplorer.current()
260 if current.isEdge() and GeomAPI.GeomAPI_Edge(current).isDegenerated(): # skip degenerative edges because they are not selected
264 for alreadyThere in shapesList:
265 if alreadyThere.isSame(current):
270 shapesList.append(current)
271 selection = theModel.selection(aResult, current) # First argument should be result/sub-result, second is sub-shape on this result/sub-result
272 selectionList.append(selection)
274 # Create group with this selection list
275 # (do not create group if nothing is selected)
276 if (len(selectionList) == 0):
278 Group_1 = theModel.addGroup(thePartDoc, selectionList)
281 groupSelectionList = Group_1.feature().selectionList("group_list")
282 assert(groupSelectionList.size() == len(selectionList))
284 # Check that all selected shapes in group have right shape type and unique name.
285 checkGroup(Group_1, theSubshapeType)
287 def testHaveNamingSubshapes(theFeature, theModel, thePartDoc) :
288 """ Tests if all vertices/edges/faces of result have a unique name
289 :param theFeature: feature to test.
291 assert(len(theFeature.results()) > 0)
292 testHaveNamingByType(theFeature, theModel, thePartDoc, GeomAPI_Shape.VERTEX)
293 testHaveNamingByType(theFeature, theModel, thePartDoc, GeomAPI_Shape.EDGE)
294 testHaveNamingByType(theFeature, theModel, thePartDoc, GeomAPI_Shape.FACE)
296 def testNbSubFeatures(theComposite, theKindOfSub, theExpectedCount):
297 """ Tests number of sub-features of the given type
298 :param theComposite composite feature to check its subs
299 :param theKindOfSub kind of sub-feature to calculate count
300 :param theExpectedCount expected number of sub-features
303 for aSub in theComposite.features().list():
304 aFeature = ModelAPI_Feature.feature(aSub)
305 if aFeature is not None and aFeature.getKind() == theKindOfSub:
307 assert (count == theExpectedCount), "Number of sub-features of type {}: {}, expected {}".format(theKindOfSub, count, theExpectedCount)
309 def assertSketchArc(theArcFeature):
310 """ Tests whether the arc is correctly defined
312 aCenterPnt = geomDataAPI_Point2D(theArcFeature.attribute("center_point"))
313 aStartPnt = geomDataAPI_Point2D(theArcFeature.attribute("start_point"))
314 aEndPnt = geomDataAPI_Point2D(theArcFeature.attribute("end_point"))
315 aRadius = theArcFeature.real("radius")
316 aDistCS = sketcher.tools.distancePointPoint(aCenterPnt, aStartPnt)
317 aDistCE = sketcher.tools.distancePointPoint(aCenterPnt, aEndPnt)
318 assert math.fabs(aDistCS - aDistCE) < TOLERANCE, "Wrong arc: center-start distance {}, center-end distance {}".format(aDistCS, aDistCE)
319 assert math.fabs(aRadius.value() -aDistCS) < TOLERANCE, "Wrong arc: radius is {0}, expected {1}".format(aRadius.value(), aDistCS)
321 def checkResult(theFeature,theModel,NbRes,NbSubRes,NbSolid,NbFace,NbEdge,NbVertex):
322 """ Tests numbers of sub-shapes in results
324 theModel.testNbResults(theFeature, NbRes)
325 theModel.testNbSubResults(theFeature, NbSubRes)
326 theModel.testNbSubShapes(theFeature, GeomAPI_Shape.SOLID, NbSolid)
327 theModel.testNbSubShapes(theFeature, GeomAPI_Shape.FACE, NbFace)
328 theModel.testNbSubShapes(theFeature, GeomAPI_Shape.EDGE, NbEdge)
329 theModel.testNbSubShapes(theFeature, GeomAPI_Shape.VERTEX, NbVertex)
331 def checkGroup(theGroup, theShapeType):
332 """ Check that all selected shapes in group have correct shape type and unique name
334 groupFeature = theGroup.feature()
335 groupSelectionList = groupFeature.selectionList("group_list")
336 presented_names = set()
337 for index in range(0, groupSelectionList.size()):
338 attrSelection = groupSelectionList.value(index)
339 shape = attrSelection.value()
340 name = attrSelection.namingName()
341 if theShapeType == GeomAPI_Shape.VERTEX:
342 assert(shape.isVertex())
343 elif theShapeType == GeomAPI_Shape.EDGE:
344 assert(shape.isEdge())
345 elif theShapeType == GeomAPI_Shape.FACE:
346 assert(shape.isFace())
347 assert(name != ""), "String empty"
348 presented_names.add(name)
349 assert(len(presented_names) == groupSelectionList.size()), "Some names are not unique"
351 def createSubShape(thePartDoc, theModel, theSelection):
352 """ Create feature according to the type of the given subshape
354 if theSelection.shapeType() == "VERTEX":
355 return theModel.addVertex(thePartDoc, [theSelection])
356 elif theSelection.shapeType() == "EDGE":
357 return theModel.addEdge(thePartDoc, [theSelection])
358 elif theSelection.shapeType() == "FACE":
359 return theModel.addFace(thePartDoc, [theSelection])
361 def checkFilter(thePartDoc, theModel, theFilter, theShapesList):
362 """ Check filter's work on specified shape.
363 Shapes given as a dictionary of selection and expected result.
365 aFiltersFactory = ModelAPI_Session.get().filters()
366 for sel, res in theShapesList.items():
369 shapeType = "UNKNOWN"
370 if sel.variantType() == ModelHighAPI_Selection.VT_ResultSubShapePair:
371 parent = sel.resultSubShapePair()[0]
372 shape = sel.resultSubShapePair()[1]
374 shape = sel.resultSubShapePair()[0].shape()
375 shapeName = sel.name()
376 shapeType = shape.shapeTypeStr()
380 subShapeFeature = createSubShape(thePartDoc, theModel, sel)
382 parent = subShapeFeature.results()[0].resultSubShapePair()[0]
383 shape = subShapeFeature.results()[0].resultSubShapePair()[0].shape()
384 shapeType = sel.typeSubShapeNamePair()[0]
385 shapeName = sel.typeSubShapeNamePair()[1]
386 assert aFiltersFactory.isValid(theFilter.feature(), parent, shape) == res, "Filter result for {} \"{}\" incorrect. Expected {}.".format(shapeType, shapeName, res)