Salome HOME
Issue #3122: Move Measurement item to Inspection menu
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Translation.cpp
index 1f8804a4f3d725f104b3a75486b67fae5ab0be0c..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
@@ -12,9 +12,9 @@
 //
 // 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>
 
@@ -103,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);
@@ -111,31 +112,51 @@ 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 (!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) {
-    anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
-                                                          anEdge->line()->direction()));
+
+  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();
         anObjectsIt++, aContext++) {
     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
-    bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
+    bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
 
     // Setting result.
     if (isPart) {
@@ -146,34 +167,32 @@ void FeaturesPlugin_Translation::performTranslationByAxisAndDistance()
       aResultPart->setTrsf(*aContext, aTrsf);
       setResult(aResultPart, aResultIndex);
     } else {
-      GeomAlgoAPI_Translation aTranslationAlgo(aBaseShape, anAxis, aDistance);
+      std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
+        new GeomAlgoAPI_Translation(aBaseShape, anAxis, aDistance));
 
-      if (!aTranslationAlgo.check()) {
-        setError(aTranslationAlgo.getError());
+      if (!aTranslationAlgo->check()) {
+        setError(aTranslationAlgo->getError());
         return;
       }
 
-      aTranslationAlgo.build();
+      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);
-      loadNamingDS(aTranslationAlgo, aResultBody, aBaseShape);
+
+      ListOfShape aShapes;
+      aShapes.push_back(aBaseShape);
+      FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
+                                               aShapes,
+                                               ListOfShape(),
+                                               aTranslationAlgo,
+                                               aTranslationAlgo->shape(),
+                                               "Translated");
       setResult(aResultBody, aResultIndex);
     }
     aResultIndex++;
@@ -199,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);
@@ -212,12 +230,13 @@ 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();
         anObjectsIt++, aContext++) {
     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
-    bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
+    bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
 
     // Setting result.
     if (isPart) {
@@ -228,34 +247,32 @@ void FeaturesPlugin_Translation::performTranslationByDimensions()
       aResultPart->setTrsf(*aContext, aTrsf);
       setResult(aResultPart, aResultIndex);
     } else {
-      GeomAlgoAPI_Translation aTranslationAlgo(aBaseShape, aDX, aDY, aDZ);
+      std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
+        new GeomAlgoAPI_Translation(aBaseShape, aDX, aDY, aDZ));
 
-      if (!aTranslationAlgo.check()) {
-        setError(aTranslationAlgo.getError());
+      if (!aTranslationAlgo->check()) {
+        setError(aTranslationAlgo->getError());
         return;
       }
 
-      aTranslationAlgo.build();
+      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);
-      loadNamingDS(aTranslationAlgo, aResultBody, aBaseShape);
+
+      ListOfShape aShapes;
+      aShapes.push_back(aBaseShape);
+      FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
+                                               aShapes,
+                                               ListOfShape(),
+                                               aTranslationAlgo,
+                                               aTranslationAlgo->shape(),
+                                               "Translated");
       setResult(aResultBody, aResultIndex);
     }
     aResultIndex++;
@@ -281,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);
@@ -307,12 +323,13 @@ 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();
         anObjectsIt++, aContext++) {
     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
-    bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
+    bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
 
     // Setting result.
     if (isPart) {
@@ -323,34 +340,32 @@ void FeaturesPlugin_Translation::performTranslationByTwoPoints()
       aResultPart->setTrsf(*aContext, aTrsf);
       setResult(aResultPart, aResultIndex);
     } else {
-      GeomAlgoAPI_Translation aTranslationAlgo(aBaseShape, aFirstPoint, aSecondPoint);
+      std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
+        new GeomAlgoAPI_Translation(aBaseShape, aFirstPoint, aSecondPoint));
 
-      if (!aTranslationAlgo.check()) {
-        setError(aTranslationAlgo.getError());
+      if (!aTranslationAlgo->check()) {
+        setError(aTranslationAlgo->getError());
         return;
       }
 
-      aTranslationAlgo.build();
+      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);
-      loadNamingDS(aTranslationAlgo, aResultBody, aBaseShape);
+
+      ListOfShape aShapes;
+      aShapes.push_back(aBaseShape);
+      FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
+                                               aShapes,
+                                               ListOfShape(),
+                                               aTranslationAlgo,
+                                               aTranslationAlgo->shape(),
+                                               "Translated");
       setResult(aResultBody, aResultIndex);
     }
     aResultIndex++;
@@ -359,19 +374,3 @@ void FeaturesPlugin_Translation::performTranslationByTwoPoints()
   // Remove the rest results if there were produced in the previous pass.
   removeResults(aResultIndex);
 }
-
-//=================================================================================================
-void FeaturesPlugin_Translation::loadNamingDS(GeomAlgoAPI_Translation& theTranslationAlgo,
-                                              std::shared_ptr<ModelAPI_ResultBody> theResultBody,
-                                              std::shared_ptr<GeomAPI_Shape> theBaseShape)
-{
-  // Store result.
-  theResultBody->storeModified(theBaseShape, theTranslationAlgo.shape());
-
-  std::string aTranslatedName = "Translated";
-  std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTranslationAlgo.mapOfSubShapes();
-
-  FeaturesPlugin_Tools::storeModifiedShapes(theTranslationAlgo, theResultBody,
-                                            theBaseShape, 1, 2, 3, aTranslatedName,
-                                            *aSubShapes.get());
-}