Salome HOME
Issue #2962: Provide name of 'Change Sketch plane' operation in Undo/Redo lists
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Scale.cpp
index ee82f611d3b9fffcae52f1cdb978b49955db503e..bdb4cdd52fde517cc999053ac555b78368b7d3f9 100644 (file)
@@ -1,12 +1,26 @@
-// Copyright (C) 2014-201x CEA/DEN, EDF R&D
-
-// File:        FeaturesPlugin_Scale.cpp
-// Created:     13 Jan 2017
-// Author:      Clarisse Genrault (CEA)
+// Copyright (C) 2014-2019  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
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 #include <FeaturesPlugin_Scale.h>
 
 #include <GeomAlgoAPI_PointBuilder.h>
+#include <GeomAlgoAPI_Tools.h>
 
 #include <ModelAPI_AttributeDouble.h>
 #include <ModelAPI_AttributeSelectionList.h>
@@ -14,6 +28,8 @@
 #include <ModelAPI_ResultBody.h>
 #include <ModelAPI_ResultPart.h>
 
+#include <FeaturesPlugin_Tools.h>
+
 //=================================================================================================
 FeaturesPlugin_Scale::FeaturesPlugin_Scale()
 {
@@ -74,7 +90,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);
@@ -99,52 +114,39 @@ 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;
-    bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
-
-    // Setting result.
-    if (isPart) {
-      std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
-      //aTrsf->setSymmetry(anAxis);
-      ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
-      ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
-      aResultPart->setTrsf(*aContext, aTrsf);
-      setResult(aResultPart, aResultIndex);
-    } else {
-      GeomAlgoAPI_Scale aScaleAlgo(aBaseShape, aCenterPoint, aScaleFactor);
-
-      if (!aScaleAlgo.check()) {
-        setError(aScaleAlgo.getError());
-        return;
-      }
-
-      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);
-        break;
-      }
-
-      ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
-      loadNamingDS(aScaleAlgo, aResultBody, aBaseShape);
-      setResult(aResultBody, aResultIndex);
+    std::shared_ptr<GeomAlgoAPI_Scale> aScaleAlgo(
+      new GeomAlgoAPI_Scale(aBaseShape, aCenterPoint, aScaleFactor));
+
+    if (!aScaleAlgo->check()) {
+      setError(aScaleAlgo->getError());
+      return;
     }
+
+    aScaleAlgo->build();
+
+    // Checking that the algorithm worked properly.
+    if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aScaleAlgo, getKind(), anError)) {
+      setError(anError);
+      break;
+    }
+
+    ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
+
+    ListOfShape aShapes;
+    aShapes.push_back(aBaseShape);
+    FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
+                                             aShapes,
+                                             ListOfShape(),
+                                             aScaleAlgo,
+                                             aScaleAlgo->shape(),
+                                             "Scaled");
+    setResult(aResultBody, aResultIndex);
     aResultIndex++;
   }
 
@@ -168,7 +170,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);
@@ -195,73 +196,45 @@ 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;
-    bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
-
-    // Setting result.
-    if (isPart) {
-      std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
-      //aTrsf->setSymmetry(anAxis);
-      ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
-      ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
-      aResultPart->setTrsf(*aContext, aTrsf);
-      setResult(aResultPart, aResultIndex);
-    } else {
-      GeomAlgoAPI_Scale aScaleAlgo(aBaseShape, aCenterPoint,
-                                   aScaleFactorX, aScaleFactorY, aScaleFactorZ);
-
-      if (!aScaleAlgo.check()) {
-        setError(aScaleAlgo.getError());
-        return;
-      }
-
-      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);
-        break;
-      }
-
-      ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
-      loadNamingDS(aScaleAlgo, aResultBody, aBaseShape);
-      setResult(aResultBody, aResultIndex);
+    std::shared_ptr<GeomAlgoAPI_Scale> aScaleAlgo(new GeomAlgoAPI_Scale(aBaseShape,
+                                                                        aCenterPoint,
+                                                                        aScaleFactorX,
+                                                                        aScaleFactorY,
+                                                                        aScaleFactorZ));
+
+    if (!aScaleAlgo->check()) {
+      setError(aScaleAlgo->getError());
+      return;
+    }
+
+    aScaleAlgo->build();
+
+    // Checking that the algorithm worked properly.
+    if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aScaleAlgo, getKind(), anError)) {
+      setError(anError);
+      break;
     }
+
+    ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
+
+    ListOfShape aShapes;
+    aShapes.push_back(aBaseShape);
+    FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
+                                             aShapes,
+                                             ListOfShape(),
+                                             aScaleAlgo,
+                                             aScaleAlgo->shape(),
+                                             "Scaled");
+    setResult(aResultBody, aResultIndex);
     aResultIndex++;
   }
 
   // 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();
-  int aReflectedTag = 1;
-  std::string aReflectedName = "Scaled";
-  theResultBody->loadAndOrientModifiedShapes(&theScaleAlgo,
-                                              theBaseShape, GeomAPI_Shape::FACE,
-                                              aReflectedTag, aReflectedName, *aSubShapes.get());
-}
\ No newline at end of file