Salome HOME
Copyright update 2022
[modules/shaper.git] / src / PythonAPI / model / tests / tests.py
index 687d2e1bb0c256440fbf26ab2d73c0791ad61cf4..79ac48d13ebedfa9aa899c33c5dcabcff3ffcf5f 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2014-2019  CEA/DEN, EDF R&D
+# Copyright (C) 2014-2022  CEA/DEN, EDF R&D
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -72,6 +72,13 @@ def generateTests(theFeature, theFeatureName, theTestsList = []):
       aResultsVolumes.append(GeomAlgoAPI_ShapeTools_volume(theFeature.results()[anIndex].resultSubShapePair()[0].shape()))
     print("model.testResultsVolumes({}, [{}])".format(theFeatureName, ", ".join("{:0.27f}".format(i) for i in aResultsVolumes)))
 
+  if "testResultsAreas" in theTestsList or len(theTestsList) == 0:
+    aNbResults = len(theFeature.results())
+    aResultsAreas = []
+    for anIndex in range(0, aNbResults):
+      aResultsAreas.append(GeomAlgoAPI_ShapeTools_area(theFeature.results()[anIndex].resultSubShapePair()[0].shape()))
+    print("model.testResultsAreas({}, [{}])".format(theFeatureName, ", ".join("{:0.27f}".format(i) for i in aResultsAreas)))
+
 
 def testNbResults(theFeature, theExpectedNbResults):
   """ Tests number of feature results.
@@ -130,7 +137,25 @@ def testResultsVolumes(theFeature, theExpectedResultsVolumes, theNbSignificantDi
     aResultVolumeStr = "{:0.27f}".format(aResultVolume).lstrip("0").lstrip(".").lstrip("0")
     anExpectedResultVolume = theExpectedResultsVolumes[anIndex]
     anExpectedResultVolumeStr = "{:0.27f}".format(anExpectedResultVolume).lstrip("0").lstrip(".").lstrip("0")
-    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)
+    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)
+
+
+def testResultsAreas(theFeature, theExpectedResultsVolumes, theNbSignificantDigits = 7):
+  """ Tests results areas.
+  :param theFeature: feature to test.
+  :param theExpectedResultsAreas: list of results areas. Size of list should be equal to len(theFeature.results()).
+  """
+  aTolerance = 10**(-theNbSignificantDigits)
+  aNbResults = len(theFeature.results())
+  aListSize = len(theExpectedResultsVolumes)
+  assert (aNbResults == aListSize), "Number of results: {} not equal to list size: {}.".format(aNbResults, aListSize)
+  for anIndex in range(0, aNbResults):
+    aResultVolume = GeomAlgoAPI_ShapeTools_area(theFeature.results()[anIndex].resultSubShapePair()[0].shape())
+    aResultVolumeStr = "{:0.27f}".format(aResultVolume).lstrip("0").lstrip(".").lstrip("0")
+    anExpectedResultVolume = theExpectedResultsVolumes[anIndex]
+    anExpectedResultVolumeStr = "{:0.27f}".format(anExpectedResultVolume).lstrip("0").lstrip(".").lstrip("0")
+    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)
+
 
 def testHaveNamingFaces(theFeature, theModel, thePartDoc) :
   """ Tests if all faces of result have a name
@@ -343,19 +368,21 @@ def checkFilter(thePartDoc, theModel, theFilter, theShapesList):
     shapeName = ""
     shapeType = "UNKNOWN"
     if sel.variantType() == ModelHighAPI_Selection.VT_ResultSubShapePair:
+      parent = sel.resultSubShapePair()[0]
       shape = sel.resultSubShapePair()[1]
       if shape.isNull():
         shape = sel.resultSubShapePair()[0].shape()
-        shapeName = sel.name()
-        shapeType = shape.shapeTypeStr()
+      shapeName = sel.name()
+      shapeType = shape.shapeTypeStr()
     else:
       needUndo = True
       theModel.begin()
       subShapeFeature = createSubShape(thePartDoc, theModel, sel)
       theModel.end()
+      parent = subShapeFeature.results()[0].resultSubShapePair()[0]
       shape = subShapeFeature.results()[0].resultSubShapePair()[0].shape()
       shapeType = sel.typeSubShapeNamePair()[0]
       shapeName = sel.typeSubShapeNamePair()[1]
-    assert aFiltersFactory.isValid(theFilter.feature(), shape) == res, "Filter result for {} \"{}\" incorrect. Expected {}.".format(shapeType, shapeName, res)
+    assert aFiltersFactory.isValid(theFilter.feature(), parent, shape) == res, "Filter result for {} \"{}\" incorrect. Expected {}.".format(shapeType, shapeName, res)
     if needUndo:
       theModel.undo()