Salome HOME
Meet the coding style (split too long lines)
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Translation.cpp
index 19ec3564ae804999dd42963d2851767990d137e9..eff54dc689f759480f280cca2cc47ef3a11f5a39 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+// 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
 //
 // 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
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-// See http://www.salome-platform.org/ or
-// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 #include <FeaturesPlugin_Translation.h>
 
 #include <GeomAPI_Edge.h>
 #include <GeomAPI_Lin.h>
+#include <GeomAPI_ShapeIterator.h>
 #include <GeomAPI_Trsf.h>
 
 #include <GeomAlgoAPI_PointBuilder.h>
+#include <GeomAlgoAPI_Tools.h>
 
 #include <FeaturesPlugin_Tools.h>
 
@@ -104,7 +105,6 @@ void FeaturesPlugin_Translation::performTranslationByAxisAndDistance()
       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);
@@ -112,25 +112,45 @@ void FeaturesPlugin_Translation::performTranslationByAxisAndDistance()
   }
 
   //Getting axis.
-  std::shared_ptr<GeomAPI_Ax1> anAxis;
-  std::shared_ptr<GeomAPI_Edge> anEdge;
-  std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
-    selection(FeaturesPlugin_Translation::AXIS_OBJECT_ID());
-  if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
-    anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
-  } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
-             anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
-    anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
+  static const std::string aSelectionError = "Error: The axis shape selection is bad.";
+  AttributeSelectionPtr anObjRef = selection(AXIS_OBJECT_ID());
+  GeomShapePtr aShape = anObjRef->value();
+  if (!aShape.get()) {
+    if (anObjRef->context().get()) {
+      aShape = anObjRef->context()->shape();
+    }
   }
-  if(anEdge) {
-    anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
-                                                          anEdge->line()->direction()));
+  if (!aShape.get()) {
+    setError(aSelectionError);
+    return;
   }
 
+  GeomEdgePtr anEdge;
+  if (aShape->isEdge())
+  {
+    anEdge = aShape->edge();
+  }
+  else if (aShape->isCompound())
+  {
+    GeomAPI_ShapeIterator anIt(aShape);
+    anEdge = anIt.current()->edge();
+  }
+
+  if (!anEdge.get())
+  {
+    setError(aSelectionError);
+    return;
+  }
+
+  std::shared_ptr<GeomAPI_Ax1> anAxis(new GeomAPI_Ax1(anEdge->line()->location(),
+                                                      anEdge->line()->direction()));
+
+
   // Getting distance.
   double aDistance = real(FeaturesPlugin_Translation::DISTANCE_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();
@@ -158,27 +178,20 @@ void FeaturesPlugin_Translation::performTranslationByAxisAndDistance()
       aTranslationAlgo->build();
 
       // Checking that the algorithm worked properly.
-      if(!aTranslationAlgo->isDone()) {
-        static const std::string aFeatureError = "Error: Translation algorithm failed.";
-        setError(aFeatureError);
-        break;
-      }
-      if(aTranslationAlgo->shape()->isNull()) {
-        static const std::string aShapeError = "Error: Resulting shape is Null.";
-        setError(aShapeError);
-        break;
-      }
-      if(!aTranslationAlgo->isValid()) {
-        std::string aFeatureError = "Error: Resulting shape is not valid.";
-        setError(aFeatureError);
+      if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTranslationAlgo, getKind(), anError)) {
+        setError(anError);
         break;
       }
 
       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
-      aResultBody->storeModified(aBaseShape, aTranslationAlgo->shape());
+
+      ListOfShape aShapes;
+      aShapes.push_back(aBaseShape);
       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
-                                               aBaseShape,
+                                               aShapes,
+                                               ListOfShape(),
                                                aTranslationAlgo,
+                                               aTranslationAlgo->shape(),
                                                "Translated");
       setResult(aResultBody, aResultIndex);
     }
@@ -205,7 +218,6 @@ void FeaturesPlugin_Translation::performTranslationByDimensions()
       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);
@@ -218,6 +230,7 @@ void FeaturesPlugin_Translation::performTranslationByDimensions()
   double aDZ = real(FeaturesPlugin_Translation::DZ_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();
@@ -245,27 +258,20 @@ void FeaturesPlugin_Translation::performTranslationByDimensions()
       aTranslationAlgo->build();
 
       // Checking that the algorithm worked properly.
-      if(!aTranslationAlgo->isDone()) {
-        static const std::string aFeatureError = "Error: Translation algorithm failed.";
-        setError(aFeatureError);
-        break;
-      }
-      if(aTranslationAlgo->shape()->isNull()) {
-        static const std::string aShapeError = "Error: Resulting shape is Null.";
-        setError(aShapeError);
-        break;
-      }
-      if(!aTranslationAlgo->isValid()) {
-        std::string aFeatureError = "Error: Resulting shape is not valid.";
-        setError(aFeatureError);
+      if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTranslationAlgo, getKind(), anError)) {
+        setError(anError);
         break;
       }
 
       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
-      aResultBody->storeModified(aBaseShape, aTranslationAlgo->shape());
+
+      ListOfShape aShapes;
+      aShapes.push_back(aBaseShape);
       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
-                                               aBaseShape,
+                                               aShapes,
+                                               ListOfShape(),
                                                aTranslationAlgo,
+                                               aTranslationAlgo->shape(),
                                                "Translated");
       setResult(aResultBody, aResultIndex);
     }
@@ -292,7 +298,6 @@ void FeaturesPlugin_Translation::performTranslationByTwoPoints()
       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);
@@ -318,6 +323,7 @@ void FeaturesPlugin_Translation::performTranslationByTwoPoints()
   }
 
   // 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();
@@ -345,27 +351,20 @@ void FeaturesPlugin_Translation::performTranslationByTwoPoints()
       aTranslationAlgo->build();
 
       // Checking that the algorithm worked properly.
-      if(!aTranslationAlgo->isDone()) {
-        static const std::string aFeatureError = "Error: Translation algorithm failed.";
-        setError(aFeatureError);
-        break;
-      }
-      if(aTranslationAlgo->shape()->isNull()) {
-        static const std::string aShapeError = "Error: Resulting shape is Null.";
-        setError(aShapeError);
-        break;
-      }
-      if(!aTranslationAlgo->isValid()) {
-        std::string aFeatureError = "Error: Resulting shape is not valid.";
-        setError(aFeatureError);
+      if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTranslationAlgo, getKind(), anError)) {
+        setError(anError);
         break;
       }
 
       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
-      aResultBody->storeModified(aBaseShape, aTranslationAlgo->shape());
+
+      ListOfShape aShapes;
+      aShapes.push_back(aBaseShape);
       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
-                                               aBaseShape,
+                                               aShapes,
+                                               ListOfShape(),
                                                aTranslationAlgo,
+                                               aTranslationAlgo->shape(),
                                                "Translated");
       setResult(aResultBody, aResultIndex);
     }