Salome HOME
Test case for issue #2918.
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Rotation.cpp
old mode 100755 (executable)
new mode 100644 (file)
index d87346d..7ef1d86
@@ -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_Rotation.h>
 #include <ModelAPI_ResultPart.h>
 
 #include <GeomAlgoAPI_PointBuilder.h>
+#include <GeomAlgoAPI_Tools.h>
 
 #include <GeomAPI_Edge.h>
 #include <GeomAPI_Lin.h>
+#include <GeomAPI_ShapeIterator.h>
 #include <GeomAPI_Trsf.h>
 
 #include <FeaturesPlugin_Tools.h>
@@ -99,25 +100,44 @@ void FeaturesPlugin_Rotation::performTranslationByAxisAndAngle()
   }
 
   //Getting axis.
-  std::shared_ptr<GeomAPI_Ax1> anAxis;
-  std::shared_ptr<GeomAPI_Edge> anEdge;
-  std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
-    selection(FeaturesPlugin_Rotation::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();
   }
-  if(anEdge) {
-    anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
-                                                          anEdge->line()->direction()));
+  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 angle.
   double anAngle = real(FeaturesPlugin_Rotation::ANGLE_ID())->value();
 
   // Rotating each object.
+  std::string anError;
   int aResultIndex = 0;
   std::list<ResultPtr>::iterator aContext = aContextes.begin();
   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
@@ -146,24 +166,21 @@ void FeaturesPlugin_Rotation::performTranslationByAxisAndAngle()
       aRotationAlgo->build();
 
       // Checking that the algorithm worked properly.
-      if(!aRotationAlgo->isDone()) {
-        static const std::string aFeatureError = "Error: Rotation algorithm failed.";
-        setError(aFeatureError);
-        break;
-      }
-      if(aRotationAlgo->shape()->isNull()) {
-        static const std::string aShapeError = "Error: Resulting shape is Null.";
-        setError(aShapeError);
-        break;
-      }
-      if(!aRotationAlgo->isValid()) {
-        std::string aFeatureError = "Error: Resulting shape is not valid.";
-        setError(aFeatureError);
+      if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationAlgo, getKind(), anError)) {
+        setError(anError);
         break;
       }
 
       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
-      FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShape, aRotationAlgo, "Rotated");
+
+      ListOfShape aShapes;
+      aShapes.push_back(aBaseShape);
+      FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
+                                               aShapes,
+                                               ListOfShape(),
+                                               aRotationAlgo,
+                                               aRotationAlgo->shape(),
+                                               "Rotated");
       setResult(aResultBody, aResultIndex);
     }
     aResultIndex++;
@@ -205,16 +222,17 @@ void FeaturesPlugin_Rotation::performTranslationByThreePoints()
     selection(FeaturesPlugin_Rotation::START_POINT_ID());
   std::shared_ptr<ModelAPI_AttributeSelection> anEndPointRef =
     selection(FeaturesPlugin_Rotation::END_POINT_ID());
-  if ((aCenterRef.get() != NULL) && (aStartPointRef.get() != NULL)
-      && (anEndPointRef.get() != NULL)) {
+  if ((aCenterRef.get() != NULL) &&
+      (aStartPointRef.get() != NULL) &&
+      (anEndPointRef.get() != NULL)) {
     GeomShapePtr aCenterShape = aCenterRef->value();
-    if (!aCenterShape.get())
+    if (!aCenterShape.get() && aCenterRef->context().get())
       aCenterShape = aCenterRef->context()->shape();
     GeomShapePtr aStartShape = aStartPointRef->value();
-    if (!aStartShape.get())
+    if (!aStartShape.get() && aStartPointRef->context().get())
       aStartShape = aStartPointRef->context()->shape();
-      GeomShapePtr anEndShape = anEndPointRef->value();
-    if (!anEndShape.get())
+    GeomShapePtr anEndShape = anEndPointRef->value();
+    if (!anEndShape.get() && anEndPointRef->context().get())
       anEndShape = anEndPointRef->context()->shape();
     if (aStartShape && anEndShape && aCenterShape) {
       aCenterPoint = GeomAlgoAPI_PointBuilder::point(aCenterShape);
@@ -224,6 +242,7 @@ void FeaturesPlugin_Rotation::performTranslationByThreePoints()
   }
 
   // Rotating each object.
+  std::string anError;
   int aResultIndex = 0;
   std::list<ResultPtr>::iterator aContext = aContextes.begin();
   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
@@ -253,25 +272,21 @@ void FeaturesPlugin_Rotation::performTranslationByThreePoints()
       aRotationAlgo->build();
 
       // Checking that the algorithm worked properly.
-      if(!aRotationAlgo->isDone()) {
-        static const std::string aFeatureError = "Error: Rotation algorithm failed.";
-        setError(aFeatureError);
-        break;
-      }
-      if(aRotationAlgo->shape()->isNull()) {
-        static const std::string aShapeError = "Error : Resulting shape is Null.";
-        setError(aShapeError);
-        break;
-      }
-      if(!aRotationAlgo->isValid()) {
-        std::string aFeatureError = "Error: Resulting shape is not valid.";
-        setError(aFeatureError);
+      if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationAlgo, getKind(), anError)) {
+        setError(anError);
         break;
       }
 
       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
-      aResultBody->storeModified(aBaseShape, aRotationAlgo->shape());
-      FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShape, aRotationAlgo, "Rotated");
+
+      ListOfShape aShapes;
+      aShapes.push_back(aBaseShape);
+      FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
+                                               aShapes,
+                                               ListOfShape(),
+                                               aRotationAlgo,
+                                               aRotationAlgo->shape(),
+                                               "Rotated");
       setResult(aResultBody, aResultIndex);
     }
     aResultIndex++;