From b565d89ef94af8a24009fa3c6fa91eb2c2d2490b Mon Sep 17 00:00:00 2001 From: Artem Zhidkov Date: Tue, 9 Feb 2021 17:47:18 +0300 Subject: [PATCH] Improve GeomAlgoAPI_ShapeTools::volume() method to return the volume of the shape only, and forbid to return an area. For the calculation of area GeomAlgoAPI_ShapeTools::area() should be used. --- .../Test/TestFace_WholeSketch_1.py | 2 +- .../Test/TestFace_WholeSketch_2.py | 2 +- .../Test/TestShell_WholeSketch_1.py | 2 +- .../Test/TestShell_WholeSketch_2.py | 2 +- .../FeaturesPlugin_BooleanFuse.cpp | 2 +- .../FeaturesPlugin_BooleanSmash.cpp | 4 +-- .../FeaturesPlugin_CompositeBoolean.cpp | 6 ++--- src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.cpp | 19 +++++--------- .../ModelHighAPI_FeatureStore.cpp | 10 ++++++++ src/PythonAPI/model/tests/tests.py | 25 +++++++++++++++++++ 10 files changed, 51 insertions(+), 23 deletions(-) diff --git a/src/BuildPlugin/Test/TestFace_WholeSketch_1.py b/src/BuildPlugin/Test/TestFace_WholeSketch_1.py index cc6020f42..af3b48ae5 100644 --- a/src/BuildPlugin/Test/TestFace_WholeSketch_1.py +++ b/src/BuildPlugin/Test/TestFace_WholeSketch_1.py @@ -44,6 +44,6 @@ model.testNbResults(Face_1, 1) model.testNbSubShapes(Face_1, GeomAPI_Shape.FACE, [1]) model.testNbSubShapes(Face_1, GeomAPI_Shape.EDGE, [3]) model.testNbSubShapes(Face_1, GeomAPI_Shape.VERTEX, [6]) -model.testResultsVolumes(Face_1, [441.0539215686274]) +model.testResultsAreas(Face_1, [441.0539215686274]) assert(model.checkPythonDump()) diff --git a/src/BuildPlugin/Test/TestFace_WholeSketch_2.py b/src/BuildPlugin/Test/TestFace_WholeSketch_2.py index 3bd558fec..a6b249a9a 100644 --- a/src/BuildPlugin/Test/TestFace_WholeSketch_2.py +++ b/src/BuildPlugin/Test/TestFace_WholeSketch_2.py @@ -44,6 +44,6 @@ model.testNbResults(Face_1, 3) model.testNbSubShapes(Face_1, GeomAPI_Shape.FACE, [1, 1, 1]) model.testNbSubShapes(Face_1, GeomAPI_Shape.EDGE, [3, 2, 2]) model.testNbSubShapes(Face_1, GeomAPI_Shape.VERTEX, [6, 4, 4]) -model.testResultsVolumes(Face_1, [441.0539215686274, 628.318530717958, 628.318530717958]) +model.testResultsAreas(Face_1, [441.0539215686274, 628.318530717958, 628.318530717958]) assert(model.checkPythonDump()) diff --git a/src/BuildPlugin/Test/TestShell_WholeSketch_1.py b/src/BuildPlugin/Test/TestShell_WholeSketch_1.py index 552fcb8ce..79badd494 100644 --- a/src/BuildPlugin/Test/TestShell_WholeSketch_1.py +++ b/src/BuildPlugin/Test/TestShell_WholeSketch_1.py @@ -44,6 +44,6 @@ model.testNbResults(Shell_1, 1) model.testNbSubShapes(Shell_1, GeomAPI_Shape.FACE, [1]) model.testNbSubShapes(Shell_1, GeomAPI_Shape.EDGE, [3]) model.testNbSubShapes(Shell_1, GeomAPI_Shape.VERTEX, [6]) -model.testResultsVolumes(Shell_1, [441.0539215686274]) +model.testResultsAreas(Shell_1, [441.0539215686274]) assert(model.checkPythonDump()) diff --git a/src/BuildPlugin/Test/TestShell_WholeSketch_2.py b/src/BuildPlugin/Test/TestShell_WholeSketch_2.py index 147ba773d..553bc8666 100644 --- a/src/BuildPlugin/Test/TestShell_WholeSketch_2.py +++ b/src/BuildPlugin/Test/TestShell_WholeSketch_2.py @@ -44,6 +44,6 @@ model.testNbResults(Shell_1, 2) model.testNbSubShapes(Shell_1, GeomAPI_Shape.FACE, [1, 2]) model.testNbSubShapes(Shell_1, GeomAPI_Shape.EDGE, [3, 4]) model.testNbSubShapes(Shell_1, GeomAPI_Shape.VERTEX, [6, 8]) -model.testResultsVolumes(Shell_1, [441.0539215686274, 1256.637061435917]) +model.testResultsAreas(Shell_1, [441.0539215686274, 1256.637061435917]) assert(model.checkPythonDump()) diff --git a/src/FeaturesPlugin/FeaturesPlugin_BooleanFuse.cpp b/src/FeaturesPlugin/FeaturesPlugin_BooleanFuse.cpp index 3ab6d589c..58af29113 100644 --- a/src/FeaturesPlugin/FeaturesPlugin_BooleanFuse.cpp +++ b/src/FeaturesPlugin/FeaturesPlugin_BooleanFuse.cpp @@ -185,7 +185,7 @@ void FeaturesPlugin_BooleanFuse::execute() std::shared_ptr aCutAlgo( new GeomAlgoAPI_Boolean(aOneObjectList, aShapesToAdd, GeomAlgoAPI_Tools::BOOL_CUT)); - if (GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) { + if (GeomAlgoAPI_ShapeTools::area(aCutAlgo->shape()) > 1.e-27) { aSolidsToFuse.push_back(aCutAlgo->shape()); aMakeShapeList->appendAlgo(aCutAlgo); } diff --git a/src/FeaturesPlugin/FeaturesPlugin_BooleanSmash.cpp b/src/FeaturesPlugin/FeaturesPlugin_BooleanSmash.cpp index 0c7f3bab1..c38e8c5ee 100644 --- a/src/FeaturesPlugin/FeaturesPlugin_BooleanSmash.cpp +++ b/src/FeaturesPlugin/FeaturesPlugin_BooleanSmash.cpp @@ -110,7 +110,7 @@ void FeaturesPlugin_BooleanSmash::execute() aShapesToAdd, GeomAlgoAPI_Tools::BOOL_CUT)); - if (GeomAlgoAPI_ShapeTools::volume(anObjectsCutAlgo->shape()) > 1.e-27) { + if (GeomAlgoAPI_ShapeTools::area(anObjectsCutAlgo->shape()) > 1.e-27) { aShapesToSmash.clear(); aShapesToSmash.push_back(anObjectsCutAlgo->shape()); aMakeShapeList->appendAlgo(anObjectsCutAlgo); @@ -122,7 +122,7 @@ void FeaturesPlugin_BooleanSmash::execute() aShapesToAdd, GeomAlgoAPI_Tools::BOOL_CUT)); - if (GeomAlgoAPI_ShapeTools::volume(aToolsCutAlgo->shape()) > 1.e-27) { + if (GeomAlgoAPI_ShapeTools::area(aToolsCutAlgo->shape()) > 1.e-27) { aTools.clear(); aTools.push_back(aToolsCutAlgo->shape()); aMakeShapeList->appendAlgo(aToolsCutAlgo); diff --git a/src/FeaturesPlugin/FeaturesPlugin_CompositeBoolean.cpp b/src/FeaturesPlugin/FeaturesPlugin_CompositeBoolean.cpp index cf8706cac..db4917048 100644 --- a/src/FeaturesPlugin/FeaturesPlugin_CompositeBoolean.cpp +++ b/src/FeaturesPlugin/FeaturesPlugin_CompositeBoolean.cpp @@ -181,7 +181,7 @@ bool FeaturesPlugin_CompositeBoolean::makeBoolean(const ListOfShape& theTools, return false; } - if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo->shape()) > 1.e-27) { + if(GeomAlgoAPI_ShapeTools::area(aBoolAlgo->shape()) > 1.e-27) { theObjects.push_back(anObject); theMakeShapes.push_back(aBoolAlgo); } @@ -238,7 +238,7 @@ bool FeaturesPlugin_CompositeBoolean::makeBoolean(const ListOfShape& theTools, aMakeShapeList->appendAlgo(aFillerAlgo); } - if(GeomAlgoAPI_ShapeTools::volume(aBoolRes) > 1.e-27) { + if(GeomAlgoAPI_ShapeTools::area(aBoolRes) > 1.e-27) { theObjects.push_back(aCompSolid); theMakeShapes.push_back(aMakeShapeList); } @@ -323,7 +323,7 @@ bool FeaturesPlugin_CompositeBoolean::makeBoolean(const ListOfShape& theTools, std::shared_ptr aCutAlgo(new GeomAlgoAPI_Boolean(aSolidsToFuse, aShapesToAdd, GeomAlgoAPI_Tools::BOOL_CUT)); - if(aCutAlgo->isDone() && GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) { + if(aCutAlgo->isDone() && GeomAlgoAPI_ShapeTools::area(aCutAlgo->shape()) > 1.e-27) { aSolidsToFuse.clear(); aSolidsToFuse.push_back(aCutAlgo->shape()); aMakeShapeList->appendAlgo(aCutAlgo); diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.cpp index c7dc30cf9..db8ad226b 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.cpp +++ b/src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.cpp @@ -135,20 +135,13 @@ double GeomAlgoAPI_ShapeTools::volume(const std::shared_ptr theSh return 0.0; } const Standard_Real anEps = 1.e-6; - TopExp_Explorer anExp(aShape, TopAbs_SOLID); - if (anExp.More()) { // return volume if there is at least one solid - double aVolume = 0.0; - for (; anExp.More(); anExp.Next()) { - GProp_GProps aGProps; - BRepGProp::VolumeProperties(anExp.Current(), aGProps, anEps); - aVolume += aGProps.Mass(); - } - return aVolume; + double aVolume = 0.0; + for (TopExp_Explorer anExp(aShape, TopAbs_SOLID); anExp.More(); anExp.Next()) { + GProp_GProps aGProps; + BRepGProp::VolumeProperties(anExp.Current(), aGProps, anEps); + aVolume += aGProps.Mass(); } - // return surfaces area - GProp_GProps aGProps; - BRepGProp::SurfaceProperties(aShape, aGProps, anEps); - return aGProps.Mass(); + return aVolume; } //================================================================================================== diff --git a/src/ModelHighAPI/ModelHighAPI_FeatureStore.cpp b/src/ModelHighAPI/ModelHighAPI_FeatureStore.cpp index 58e32136f..ebfbfce05 100644 --- a/src/ModelHighAPI/ModelHighAPI_FeatureStore.cpp +++ b/src/ModelHighAPI/ModelHighAPI_FeatureStore.cpp @@ -423,6 +423,16 @@ std::string ModelHighAPI_FeatureStore::dumpShape(std::shared_ptr& aResult< 1.e-5) { + aResult << "Area: "; + // volumes of too huge shapes write in the scientific format + if (anArea >= 1.e5) + aResult << std::scientific << std::setprecision(7); + else + aResult << std::fixed << std::setprecision(3); + aResult << anArea << std::endl; + } std::shared_ptr aCenter = GeomAlgoAPI_ShapeTools::centreOfMass(theShape); aResult<<"Center of mass: "; double aCenterVals[3] = {aCenter->x(), aCenter->y(), aCenter->z()}; diff --git a/src/PythonAPI/model/tests/tests.py b/src/PythonAPI/model/tests/tests.py index 90fe18fa6..a2ea86bca 100644 --- a/src/PythonAPI/model/tests/tests.py +++ b/src/PythonAPI/model/tests/tests.py @@ -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()) + aResultsVolumes = [] + 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 aResultsVolumes))) + def testNbResults(theFeature, theExpectedNbResults): """ Tests number of feature results. @@ -132,6 +139,24 @@ def testResultsVolumes(theFeature, theExpectedResultsVolumes, theNbSignificantDi 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) + +def testResultsAreas(theFeature, theExpectedResultsAreas, theNbSignificantDigits = 7): + """ Tests results areas. + :param theFeature: feature to test. + :param theExpectedResultsAreas: list of results volumes. 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 :param theFeature: feature to test. -- 2.39.2