Salome HOME
Fix crashes in unit tests (produced by stack of transactions)
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Placement.cpp
index 627d10666b9819996d38dd6a4479a6aad52e8be7..0acf3a84e07c22da2d7d80bc2c0757fde884230a 100644 (file)
@@ -1,8 +1,21 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
-
-// File:        FeaturesPlugin_Placement.cpp
-// Created:     2 Dec 2014
-// Author:      Artem ZHIDKOV
+// 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_Placement.h"
 
 #include <GeomAPI_Edge.h>
 #include <GeomAPI_Face.h>
 #include <GeomAPI_Pln.h>
+#include <GeomAPI_ShapeIterator.h>
 #include <GeomAlgoAPI_Placement.h>
 #include <GeomAlgoAPI_Transform.h>
+#include <GeomAlgoAPI_Tools.h>
+
+#include <FeaturesPlugin_Tools.h>
 
 FeaturesPlugin_Placement::FeaturesPlugin_Placement()
 {
@@ -27,7 +44,7 @@ FeaturesPlugin_Placement::FeaturesPlugin_Placement()
 void FeaturesPlugin_Placement::initAttributes()
 {
 
-  AttributeSelectionListPtr aSelection = 
+  AttributeSelectionListPtr aSelection =
     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
     OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
 
@@ -47,10 +64,10 @@ void FeaturesPlugin_Placement::execute()
     return;
   }
   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
-    std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
+    std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
+      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);
@@ -104,21 +121,8 @@ void FeaturesPlugin_Placement::execute()
   // Verify planarity of faces and linearity of edges
   std::shared_ptr<GeomAPI_Shape> aShapes[2] = {aStartShape, anEndShape};
   for (int i = 0; i < 2; i++) {
-    if (aShapes[i]->isFace()) {
-      std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShapes[i]));
-      if (!aFace->isPlanar()) {
-        static const std::string aPlanarityError = "Error: One of selected faces is not planar.";
-        setError(aPlanarityError);
-        return;
-      }
-    }
-    else if (aShapes[i]->isEdge()) {
-      std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShapes[i]));
-      if (!anEdge->isLine()) {
-        static const std::string aLinearityError = "Error: One of selected endges is not linear.";
-        setError(aLinearityError);
-        return;
-      }
+    if (!isShapeValid(aShapes[i])) {
+      return;
     }
   }
 
@@ -138,39 +142,35 @@ void FeaturesPlugin_Placement::execute()
 
   // Applying transformation to each object.
   int aResultIndex = 0;
+  std::string anError;
   std::list<ResultPtr>::iterator aContext = aContextes.begin();
   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
       anObjectsIt++, aContext++) {
 
-    if ((*aContext)->groupName() == ModelAPI_ResultPart::group()) { // for part results just set transformation
+    // for part results just set transformation
+    if (aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group()) {
       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
       aResultPart->setTrsf(aContextRes, aTrsf);
       setResult(aResultPart, aResultIndex);
     } else {
       std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
-      GeomAlgoAPI_Transform aTransformAlgo(aBaseShape, aTrsf);
+      std::shared_ptr<GeomAlgoAPI_Transform> aTransformAlgo(new GeomAlgoAPI_Transform(aBaseShape,
+                                                                                      aTrsf));
 
       // Checking that the algorithm worked properly.
-      if(!aTransformAlgo.isDone()) {
-        static const std::string aFeatureError = "Error: Transform algorithm failed.";
-        setError(aFeatureError);
-        break;
-      }
-      if(aTransformAlgo.shape()->isNull()) {
-        static const std::string aShapeError = "Error: Resulting shape is Null.";
-        setError(aShapeError);
-        break;
-      }
-      if(!aTransformAlgo.isValid()) {
-        std::string aFeatureError = "Error: Resulting shape is not valid.";
-        setError(aFeatureError);
+      if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTransformAlgo, getKind(), anError)) {
+        setError(anError);
         break;
       }
 
       //LoadNamingDS
-      std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
-      loadNamingDS(aTransformAlgo, aResultBody, aBaseShape);
+      ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
+
+      ListOfShape aShapes;
+      aShapes.push_back(aBaseShape);
+      FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aShapes, ListOfShape(),
+                                               aTransformAlgo, aTransformAlgo->shape(), "Placed");
       setResult(aResultBody, aResultIndex);
     }
     aResultIndex++;
@@ -180,36 +180,44 @@ void FeaturesPlugin_Placement::execute()
   removeResults(aResultIndex);
 }
 
-//============================================================================
-void FeaturesPlugin_Placement::loadNamingDS(GeomAlgoAPI_Transform& theTransformAlgo,
-                                            std::shared_ptr<ModelAPI_ResultBody> theResultBody,
-                                            std::shared_ptr<GeomAPI_Shape> theBaseShape)
+//==================================================================================================
+bool FeaturesPlugin_Placement::isShapeValid(GeomShapePtr theShape)
 {
-  //load result
-  theResultBody->storeModified(theBaseShape, theTransformAlgo.shape());
-
-  std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTransformAlgo.mapOfSubShapes();
-
-    // put modifed faces in DF
-  int aPlacedTag = 1;
-  std::string aPlacedName = "Placed";
-
-  switch(theBaseShape->shapeType()) {
-    case GeomAPI_Shape::COMPOUND:
-    case GeomAPI_Shape::COMPSOLID:
-    case GeomAPI_Shape::SOLID:
-    case GeomAPI_Shape::SHELL:
-      theResultBody->loadAndOrientModifiedShapes(&theTransformAlgo,
-                                                 theBaseShape, GeomAPI_Shape::FACE,
-                                                 aPlacedTag, aPlacedName + "_Face", *aSubShapes.get());
-    case GeomAPI_Shape::FACE:
-    case GeomAPI_Shape::WIRE:
-      theResultBody->loadAndOrientModifiedShapes(&theTransformAlgo,
-                                                 theBaseShape, GeomAPI_Shape::EDGE,
-                                                 ++aPlacedTag, aPlacedName + "_Edge", *aSubShapes.get());
-    case GeomAPI_Shape::EDGE:
-      theResultBody->loadAndOrientModifiedShapes(&theTransformAlgo,
-                                                 theBaseShape, GeomAPI_Shape::VERTEX,
-                                                 ++aPlacedTag, aPlacedName + "_Vertex", *aSubShapes.get());
+  if (theShape->isCompound()) {
+    GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
+    for (GeomAPI_ShapeIterator anIt(theShape); anIt.more(); anIt.next()) {
+      GeomShapePtr aCurrentShape = anIt.current();
+      if (aShapeType == GeomAPI_Shape::SHAPE) {
+        aShapeType = aCurrentShape->shapeType();
+      }
+      else if (aShapeType != aCurrentShape->shapeType()) {
+        static const std::string aLinearityError =
+          "Error: Selected compound contains shapes with different types.";
+        setError(aLinearityError);
+        return false;
+      }
+
+      if (!isShapeValid(aCurrentShape)) {
+        return false;
+      }
+    }
   }
+  else if (theShape->isFace()) {
+    std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(theShape));
+    if (!aFace->isPlanar()) {
+      static const std::string aPlanarityError = "Error: One of selected faces is not planar.";
+      setError(aPlanarityError);
+      return false;
+    }
+  }
+  else if (theShape->isEdge()) {
+    std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
+    if (!anEdge->isLine()) {
+      static const std::string aLinearityError = "Error: One of selected edges is not linear.";
+      setError(aLinearityError);
+      return false;
+    }
+  }
+
+  return true;
 }