1 # Copyright (C) 2014-2022 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 testNbUniqueSubShapes(theFeature, theShapeType, theExpectedNbSubShapes):
127 """ Tests number of unique feature sub-shapes of passed type for each result.
128 :param theFeature: feature to test.
129 :param theShapeType: shape type of sub-shapes to test.
130 :param theExpectedNbSubShapes: list of sub-shapes numbers. Size of list should be equal to len(theFeature.results()).
132 aResults = theFeature.feature().results()
133 aNbResults = len(aResults)
134 aListSize = len(theExpectedNbSubShapes)
135 assert (aNbResults == aListSize), "Number of results: {} not equal to list size: {}.".format(aNbResults, aListSize)
136 for anIndex in range(0, aNbResults):
137 aNbResultSubShapes = 0
138 anExpectedNbSubShapes = theExpectedNbSubShapes[anIndex]
139 aNbResultSubShapes = aResults[anIndex].shape().subShapes(theShapeType, True).size()
140 assert (aNbResultSubShapes == anExpectedNbSubShapes), "Number of sub-shapes of type {} for result[{}]: {}. Expected: {}.".format(aShapeTypes[theShapeType], anIndex, aNbResultSubShapes, anExpectedNbSubShapes)
143 def testCompound(theFeature, NbSubRes, NbSolid, NbFace, NbEdge, NbVertex):
144 """ Tests number of unique sub-shapes in compound result
146 aResults = theFeature.feature().results()
147 aNbResults = len(aResults)
148 assert (aNbResults == 1), "Number of results: {} not equal to 1.".format(aNbResults)
149 assert aResults[0].shape().isCompound(), "Result shape type: {}. Expected: COMPOUND.".format(aResults[0].shape().shapeTypeStr())
150 testNbSubResults(theFeature, NbSubRes)
151 testNbUniqueSubShapes(theFeature, GeomAPI_Shape.SOLID, NbSolid)
152 testNbUniqueSubShapes(theFeature, GeomAPI_Shape.FACE, NbFace)
153 testNbUniqueSubShapes(theFeature, GeomAPI_Shape.EDGE, NbEdge)
154 testNbUniqueSubShapes(theFeature, GeomAPI_Shape.VERTEX, NbVertex)
157 def testResults(theFeature, NbRes, NbSubRes, NbShell, NbFace, NbEdge, NbVertex):
158 """ Tests numbers of unique sub-shapes in the results
160 aResults = theFeature.feature().results()
161 aNbResults = len(aResults)
162 assert (aNbResults == NbRes), "Number of results: {} not equal to {}}.".format(aNbResults, NbRes)
163 testNbSubResults(theFeature, NbSubRes)
164 testNbUniqueSubShapes(theFeature, GeomAPI_Shape.SHELL, NbShell)
165 testNbUniqueSubShapes(theFeature, GeomAPI_Shape.FACE, NbFace)
166 testNbUniqueSubShapes(theFeature, GeomAPI_Shape.EDGE, NbEdge)
167 testNbUniqueSubShapes(theFeature, GeomAPI_Shape.VERTEX, NbVertex)
170 def testResultsVolumes(theFeature, theExpectedResultsVolumes, theNbSignificantDigits = 7):
171 """ Tests results volumes.
172 :param theFeature: feature to test.
173 :param theExpectedResultsVolumes: list of results volumes. Size of list should be equal to len(theFeature.results()).
175 aTolerance = 10**(-theNbSignificantDigits)
176 aNbResults = len(theFeature.results())
177 aListSize = len(theExpectedResultsVolumes)
178 assert (aNbResults == aListSize), "Number of results: {} not equal to list size: {}.".format(aNbResults, aListSize)
179 for anIndex in range(0, aNbResults):
180 aResultVolume = GeomAlgoAPI_ShapeTools_volume(theFeature.results()[anIndex].resultSubShapePair()[0].shape())
181 aResultVolumeStr = "{:0.27f}".format(aResultVolume).lstrip("0").lstrip(".").lstrip("0")
182 anExpectedResultVolume = theExpectedResultsVolumes[anIndex]
183 anExpectedResultVolumeStr = "{:0.27f}".format(anExpectedResultVolume).lstrip("0").lstrip(".").lstrip("0")
184 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)
187 def testResultsAreas(theFeature, theExpectedResultsVolumes, theNbSignificantDigits = 7):
188 """ Tests results areas.
189 :param theFeature: feature to test.
190 :param theExpectedResultsAreas: list of results areas. Size of list should be equal to len(theFeature.results()).
192 aTolerance = 10**(-theNbSignificantDigits)
193 aNbResults = len(theFeature.results())
194 aListSize = len(theExpectedResultsVolumes)
195 assert (aNbResults == aListSize), "Number of results: {} not equal to list size: {}.".format(aNbResults, aListSize)
196 for anIndex in range(0, aNbResults):
197 aResultVolume = GeomAlgoAPI_ShapeTools_area(theFeature.results()[anIndex].resultSubShapePair()[0].shape())
198 aResultVolumeStr = "{:0.27f}".format(aResultVolume).lstrip("0").lstrip(".").lstrip("0")
199 anExpectedResultVolume = theExpectedResultsVolumes[anIndex]
200 anExpectedResultVolumeStr = "{:0.27f}".format(anExpectedResultVolume).lstrip("0").lstrip(".").lstrip("0")
201 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)
204 def testHaveNamingFaces(theFeature, theModel, thePartDoc) :
205 """ Tests if all faces of result have a name
206 :param theFeature: feature to test.
208 # open transaction since all the checking are performed in tests after model.end() call
210 # Get feature result/sub-result
211 aResult = theFeature.results()[0].resultSubShapePair()[0]
212 # Get result/sub-result shape
213 shape = aResult.shape()
214 # Create shape explorer with desired shape type
215 shapeExplorer = GeomAPI_ShapeExplorer(shape, GeomAPI_Shape.FACE)
216 # Create list, and store selections in it
218 while shapeExplorer.more():
219 selection = theModel.selection(aResult, shapeExplorer.current()) # First argument should be result/sub-result, second is sub-shape on this result/sub-result
220 selectionList.append(selection)
222 # Create group with this selection list
223 Group_1 = theModel.addGroup(thePartDoc, selectionList)
226 # Now you can check that all selected shapes in group have right shape type and name.
227 groupFeature = Group_1.feature()
228 groupSelectionList = groupFeature.selectionList("group_list")
229 assert(groupSelectionList.size() == len(selectionList))
230 for index in range(0, groupSelectionList.size()):
231 attrSelection = groupSelectionList.value(index)
232 shape = attrSelection.value()
233 name = attrSelection.namingName()
234 assert(shape.isFace())
235 assert(name != ""), "String empty"
237 def testHaveNamingEdges(theFeature, theModel, thePartDoc) :
238 """ Tests if all edges of result have a name
239 :param theFeature: feature to test.
241 # Get feature result/sub-result
242 aResult = theFeature.results()[0].resultSubShapePair()[0]
243 # Get result/sub-result shape
244 shape = aResult.shape()
245 # Create shape explorer with desired shape type
246 shapeExplorer = GeomAPI_ShapeExplorer(shape, GeomAPI_Shape.EDGE)
247 # Create list, and store selections in it
249 while shapeExplorer.more():
250 selection = theModel.selection(aResult, shapeExplorer.current()) # First argument should be result/sub-result, second is sub-shape on this result/sub-result
251 selectionList.append(selection)
253 # Create group with this selection list
254 Group_1 = theModel.addGroup(thePartDoc, selectionList)
258 # Now you can check that all selected shapes in group have right shape type and name.
259 groupFeature = Group_1.feature()
260 groupSelectionList = groupFeature.selectionList("group_list")
262 assert(groupSelectionList.size() == len(selectionList))
263 for index in range(0, groupSelectionList.size()):
264 attrSelection = groupSelectionList.value(index)
265 shape = attrSelection.value()
266 name = attrSelection.namingName()
267 assert(shape.isEdge())
268 assert(name != ""), "String empty"
270 def lowerLevelSubResults(theResult, theList):
271 """ Collects in a list all lover level sub-results (without children).
272 Auxiliary method for context correct definition.
274 nbSubs = theResult.numberOfSubs()
276 theList.append(theResult)
278 for sub in range(0, nbSubs):
279 lowerLevelSubResults(theResult.subResult(sub), theList)
281 def testHaveNamingByType(theFeature, theModel, thePartDoc, theSubshapeType) :
282 """ Tests if all sub-shapes of result have a unique name
283 :param theFeature: feature to test.
284 :param theSubshapeType: type of sub-shape
286 if not theFeature.results():
288 aFirstRes = theFeature.results()[0]
290 lowerLevelSubResults(aFirstRes, aResList)
293 shapesList = [] # to append only unique shapes (not isSame)
295 # Get feature result/sub-result
296 aResult = aR.resultSubShapePair()[0]
297 # Get result/sub-result shape
298 shape = aResult.shape()
299 # Create shape explorer with desired shape type
300 shapeExplorer = GeomAPI_ShapeExplorer(shape, theSubshapeType)
301 # Create list, and store selections in it
302 while shapeExplorer.more():
303 current = shapeExplorer.current()
304 if current.isEdge() and GeomAPI.GeomAPI_Edge(current).isDegenerated(): # skip degenerative edges because they are not selected
308 for alreadyThere in shapesList:
309 if alreadyThere.isSame(current):
314 shapesList.append(current)
315 selection = theModel.selection(aResult, current) # First argument should be result/sub-result, second is sub-shape on this result/sub-result
316 selectionList.append(selection)
318 # Create group with this selection list
319 # (do not create group if nothing is selected)
320 if (len(selectionList) == 0):
322 Group_1 = theModel.addGroup(thePartDoc, selectionList)
325 groupSelectionList = Group_1.feature().selectionList("group_list")
326 assert(groupSelectionList.size() == len(selectionList))
328 # Check that all selected shapes in group have right shape type and unique name.
329 checkGroup(Group_1, theSubshapeType)
331 def testHaveNamingSubshapes(theFeature, theModel, thePartDoc) :
332 """ Tests if all vertices/edges/faces of result have a unique name
333 :param theFeature: feature to test.
335 assert(len(theFeature.results()) > 0)
336 testHaveNamingByType(theFeature, theModel, thePartDoc, GeomAPI_Shape.VERTEX)
337 testHaveNamingByType(theFeature, theModel, thePartDoc, GeomAPI_Shape.EDGE)
338 testHaveNamingByType(theFeature, theModel, thePartDoc, GeomAPI_Shape.FACE)
340 def testNbSubFeatures(theComposite, theKindOfSub, theExpectedCount):
341 """ Tests number of sub-features of the given type
342 :param theComposite composite feature to check its subs
343 :param theKindOfSub kind of sub-feature to calculate count
344 :param theExpectedCount expected number of sub-features
347 for aSub in theComposite.features().list():
348 aFeature = ModelAPI_Feature.feature(aSub)
349 if aFeature is not None and aFeature.getKind() == theKindOfSub:
351 assert (count == theExpectedCount), "Number of sub-features of type {}: {}, expected {}".format(theKindOfSub, count, theExpectedCount)
353 def assertSketchArc(theArcFeature):
354 """ Tests whether the arc is correctly defined
356 aCenterPnt = geomDataAPI_Point2D(theArcFeature.attribute("center_point"))
357 aStartPnt = geomDataAPI_Point2D(theArcFeature.attribute("start_point"))
358 aEndPnt = geomDataAPI_Point2D(theArcFeature.attribute("end_point"))
359 aRadius = theArcFeature.real("radius")
360 aDistCS = sketcher.tools.distancePointPoint(aCenterPnt, aStartPnt)
361 aDistCE = sketcher.tools.distancePointPoint(aCenterPnt, aEndPnt)
362 assert math.fabs(aDistCS - aDistCE) < TOLERANCE, "Wrong arc: center-start distance {}, center-end distance {}".format(aDistCS, aDistCE)
363 assert math.fabs(aRadius.value() -aDistCS) < TOLERANCE, "Wrong arc: radius is {0}, expected {1}".format(aRadius.value(), aDistCS)
365 def checkResult(theFeature,theModel,NbRes,NbSubRes,NbSolid,NbFace,NbEdge,NbVertex):
366 """ Tests numbers of sub-shapes in results
368 theModel.testNbResults(theFeature, NbRes)
369 theModel.testNbSubResults(theFeature, NbSubRes)
370 theModel.testNbSubShapes(theFeature, GeomAPI_Shape.SOLID, NbSolid)
371 theModel.testNbSubShapes(theFeature, GeomAPI_Shape.FACE, NbFace)
372 theModel.testNbSubShapes(theFeature, GeomAPI_Shape.EDGE, NbEdge)
373 theModel.testNbSubShapes(theFeature, GeomAPI_Shape.VERTEX, NbVertex)
375 def checkGroup(theGroup, theShapeType):
376 """ Check that all selected shapes in group have correct shape type and unique name
378 groupFeature = theGroup.feature()
379 groupSelectionList = groupFeature.selectionList("group_list")
380 presented_names = set()
381 for index in range(0, groupSelectionList.size()):
382 attrSelection = groupSelectionList.value(index)
383 shape = attrSelection.value()
384 name = attrSelection.namingName()
385 if theShapeType == GeomAPI_Shape.VERTEX:
386 assert(shape.isVertex())
387 elif theShapeType == GeomAPI_Shape.EDGE:
388 assert(shape.isEdge())
389 elif theShapeType == GeomAPI_Shape.FACE:
390 assert(shape.isFace())
391 assert(name != ""), "String empty"
392 presented_names.add(name)
393 assert(len(presented_names) == groupSelectionList.size()), "Some names are not unique"
395 def createSubShape(thePartDoc, theModel, theSelection):
396 """ Create feature according to the type of the given subshape
398 if theSelection.shapeType() == "VERTEX":
399 return theModel.addVertex(thePartDoc, [theSelection])
400 elif theSelection.shapeType() == "EDGE":
401 return theModel.addEdge(thePartDoc, [theSelection])
402 elif theSelection.shapeType() == "FACE":
403 return theModel.addFace(thePartDoc, [theSelection])
405 def checkFilter(thePartDoc, theModel, theFilter, theShapesList):
406 """ Check filter's work on specified shape.
407 Shapes given as a dictionary of selection and expected result.
409 aFiltersFactory = ModelAPI_Session.get().filters()
410 for sel, res in theShapesList.items():
413 shapeType = "UNKNOWN"
414 if sel.variantType() == ModelHighAPI_Selection.VT_ResultSubShapePair:
415 parent = sel.resultSubShapePair()[0]
416 shape = sel.resultSubShapePair()[1]
418 shape = sel.resultSubShapePair()[0].shape()
419 shapeName = sel.name()
420 shapeType = shape.shapeTypeStr()
424 subShapeFeature = createSubShape(thePartDoc, theModel, sel)
426 parent = subShapeFeature.results()[0].resultSubShapePair()[0]
427 shape = subShapeFeature.results()[0].resultSubShapePair()[0].shape()
428 shapeType = sel.typeSubShapeNamePair()[0]
429 shapeName = sel.typeSubShapeNamePair()[1]
430 assert aFiltersFactory.isValid(theFilter.feature(), parent, shape) == res, "Filter result for {} \"{}\" incorrect. Expected {}.".format(shapeType, shapeName, res)