Salome HOME
[Code coverage]: Move checking the algorithm's result into separate function
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Scale.cpp
index 4e854c22a5f491e742f68ad57121d9b4c916382b..a2c6105c8bce9258524498c03f1714a582f61f9f 100644 (file)
@@ -21,6 +21,7 @@
 #include <FeaturesPlugin_Scale.h>
 
 #include <GeomAlgoAPI_PointBuilder.h>
+#include <GeomAlgoAPI_Tools.h>
 
 #include <ModelAPI_AttributeDouble.h>
 #include <ModelAPI_AttributeSelectionList.h>
@@ -90,7 +91,6 @@ void FeaturesPlugin_Scale::performScaleByFactor()
       anObjectsSelList->value(anObjectsIndex);
     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
     if(!anObject.get()) { // may be for not-activated parts
-      eraseResults();
       return;
     }
     anObjects.push_back(anObject);
@@ -115,39 +115,31 @@ void FeaturesPlugin_Scale::performScaleByFactor()
   double aScaleFactor = real(FeaturesPlugin_Scale::SCALE_FACTOR_ID())->value();
 
   // Moving each object.
+  std::string anError;
   int aResultIndex = 0;
   std::list<ResultPtr>::iterator aContext = aContextes.begin();
   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
         anObjectsIt++, aContext++) {
     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
-    GeomAlgoAPI_Scale aScaleAlgo(aBaseShape, aCenterPoint, aScaleFactor);
+    std::shared_ptr<GeomAlgoAPI_Scale> aScaleAlgo(
+      new GeomAlgoAPI_Scale(aBaseShape, aCenterPoint, aScaleFactor));
 
-    if (!aScaleAlgo.check()) {
-      setError(aScaleAlgo.getError());
+    if (!aScaleAlgo->check()) {
+      setError(aScaleAlgo->getError());
       return;
     }
 
-    aScaleAlgo.build();
+    aScaleAlgo->build();
 
     // Checking that the algorithm worked properly.
-    if(!aScaleAlgo.isDone()) {
-      static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
-      setError(aFeatureError);
-      break;
-    }
-    if(aScaleAlgo.shape()->isNull()) {
-      static const std::string aShapeError = "Error: Resulting shape is Null.";
-      setError(aShapeError);
-      break;
-    }
-    if(!aScaleAlgo.isValid()) {
-      std::string aFeatureError = "Error: Resulting shape is not valid.";
-      setError(aFeatureError);
+    if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aScaleAlgo, getKind(), anError)) {
+      setError(anError);
       break;
     }
 
     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
-    loadNamingDS(aScaleAlgo, aResultBody, aBaseShape);
+    aResultBody->storeModified(aBaseShape, aScaleAlgo->shape());
+    FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShape, aScaleAlgo, "Scaled");
     setResult(aResultBody, aResultIndex);
     aResultIndex++;
   }
@@ -172,7 +164,6 @@ void FeaturesPlugin_Scale::performScaleByDimensions()
       anObjectsSelList->value(anObjectsIndex);
     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
     if(!anObject.get()) { // may be for not-activated parts
-      eraseResults();
       return;
     }
     anObjects.push_back(anObject);
@@ -199,40 +190,34 @@ void FeaturesPlugin_Scale::performScaleByDimensions()
   double aScaleFactorZ = real(FeaturesPlugin_Scale::SCALE_FACTOR_Z_ID())->value();
 
   // Moving each object.
+  std::string anError;
   int aResultIndex = 0;
   std::list<ResultPtr>::iterator aContext = aContextes.begin();
   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
         anObjectsIt++, aContext++) {
     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
-    GeomAlgoAPI_Scale aScaleAlgo(aBaseShape, aCenterPoint,
-                                 aScaleFactorX, aScaleFactorY, aScaleFactorZ);
-
-    if (!aScaleAlgo.check()) {
-      setError(aScaleAlgo.getError());
+    std::shared_ptr<GeomAlgoAPI_Scale> aScaleAlgo(new GeomAlgoAPI_Scale(aBaseShape,
+                                                                        aCenterPoint,
+                                                                        aScaleFactorX,
+                                                                        aScaleFactorY,
+                                                                        aScaleFactorZ));
+
+    if (!aScaleAlgo->check()) {
+      setError(aScaleAlgo->getError());
       return;
     }
 
-    aScaleAlgo.build();
+    aScaleAlgo->build();
 
     // Checking that the algorithm worked properly.
-    if(!aScaleAlgo.isDone()) {
-      static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
-      setError(aFeatureError);
-      break;
-    }
-    if(aScaleAlgo.shape()->isNull()) {
-      static const std::string aShapeError = "Error: Resulting shape is Null.";
-      setError(aShapeError);
-      break;
-    }
-    if(!aScaleAlgo.isValid()) {
-      std::string aFeatureError = "Error: Resulting shape is not valid.";
-      setError(aFeatureError);
+    if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aScaleAlgo, getKind(), anError)) {
+      setError(anError);
       break;
     }
 
     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
-    loadNamingDS(aScaleAlgo, aResultBody, aBaseShape);
+    aResultBody->storeModified(aBaseShape, aScaleAlgo->shape());
+    FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShape, aScaleAlgo, "Scaled");
     setResult(aResultBody, aResultIndex);
     aResultIndex++;
   }
@@ -240,19 +225,3 @@ void FeaturesPlugin_Scale::performScaleByDimensions()
   // Remove the rest results if there were produced in the previous pass.
   removeResults(aResultIndex);
 }
-
-//=================================================================================================
-void FeaturesPlugin_Scale::loadNamingDS(GeomAlgoAPI_Scale& theScaleAlgo,
-                                        std::shared_ptr<ModelAPI_ResultBody> theResultBody,
-                                        std::shared_ptr<GeomAPI_Shape> theBaseShape)
-{
-  // Store and name the result.
-  theResultBody->storeModified(theBaseShape, theScaleAlgo.shape());
-
-  // Name the faces
-  std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theScaleAlgo.mapOfSubShapes();
-  std::string aScaledName = "Scaled";
-  FeaturesPlugin_Tools::storeModifiedShapes(theScaleAlgo, theResultBody,
-                                            theBaseShape, 1, 2, 3, aScaledName,
-                                            *aSubShapes.get());
-}