Salome HOME
Update copyrights
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Symmetry.cpp
index 418913246cdd883fae29ab0f9c868cdcd0f7e8ef..a243e55f30b0137cf373080a386e9d27a515da81 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_Symmetry.h>
 #include <GeomAlgoAPI_CompoundBuilder.h>
 #include <GeomAlgoAPI_PointBuilder.h>
 #include <GeomAlgoAPI_FaceBuilder.h>
+#include <GeomAlgoAPI_Copy.h>
+#include <GeomAlgoAPI_MakeShapeList.h>
+#include <GeomAlgoAPI_Tools.h>
 
 #include <GeomAPI_Edge.h>
 #include <GeomAPI_Face.h>
 #include <GeomAPI_Lin.h>
 #include <GeomAPI_Pln.h>
+#include <GeomAPI_ShapeIterator.h>
 #include <GeomAPI_Trsf.h>
 
+#include <ModelAPI_AttributeBoolean.h>
 #include <ModelAPI_AttributeSelectionList.h>
 #include <ModelAPI_AttributeString.h>
 #include <ModelAPI_ResultBody.h>
@@ -60,6 +64,9 @@ void FeaturesPlugin_Symmetry::initAttributes()
 
   data()->addAttribute(FeaturesPlugin_Symmetry::PLANE_OBJECT_ID(),
                        ModelAPI_AttributeSelection::typeId());
+
+  data()->addAttribute(FeaturesPlugin_Symmetry::KEEP_ORIGINAL_RESULT(),
+                       ModelAPI_AttributeBoolean::typeId());
 }
 
 //=================================================================================================
@@ -95,7 +102,6 @@ bool FeaturesPlugin_Symmetry::collectSourceObjects(ListOfShape& theSourceShapes,
       anObjectsSelList->value(anObjectsIndex);
     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
     if (!anObject.get()) { // may be for not-activated parts
-      eraseResults();
       return false;
     }
     theSourceShapes.push_back(anObject);
@@ -128,12 +134,13 @@ void FeaturesPlugin_Symmetry::performSymmetryByPoint()
   }
 
   // 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) {
@@ -143,29 +150,19 @@ void FeaturesPlugin_Symmetry::performSymmetryByPoint()
       buildResult(anOrigin, aTrsf, aResultIndex);
     }
     else {
-      GeomAlgoAPI_Symmetry aSymmetryAlgo(aBaseShape, aPoint);
+      std::shared_ptr<GeomAlgoAPI_Symmetry> aSymmetryAlgo(
+        new GeomAlgoAPI_Symmetry(aBaseShape, aPoint));
 
-      if (!aSymmetryAlgo.check()) {
-        setError(aSymmetryAlgo.getError());
+      if (!aSymmetryAlgo->check()) {
+        setError(aSymmetryAlgo->getError());
         return;
       }
 
-      aSymmetryAlgo.build();
+      aSymmetryAlgo->build();
 
       // Checking that the algorithm worked properly.
-      if(!aSymmetryAlgo.isDone()) {
-        static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
-        setError(aFeatureError);
-        break;
-      }
-      if(aSymmetryAlgo.shape()->isNull()) {
-        static const std::string aShapeError = "Error: Resulting shape is Null.";
-        setError(aShapeError);
-        break;
-      }
-      if(!aSymmetryAlgo.isValid()) {
-        std::string aFeatureError = "Error: Resulting shape is not valid.";
-        setError(aFeatureError);
+      if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aSymmetryAlgo, getKind(), anError)) {
+        setError(anError);
         break;
       }
 
@@ -188,28 +185,48 @@ void FeaturesPlugin_Symmetry::performSymmetryByAxis()
     return;
 
   //Getting axis.
-  std::shared_ptr<GeomAPI_Ax1> anAxis;
-  std::shared_ptr<GeomAPI_Edge> anEdge;
-  std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
-    selection(FeaturesPlugin_Symmetry::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()));
+
+
   // 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) {
@@ -219,29 +236,19 @@ void FeaturesPlugin_Symmetry::performSymmetryByAxis()
       buildResult(anOrigin, aTrsf, aResultIndex);
     }
     else {
-      GeomAlgoAPI_Symmetry aSymmetryAlgo(aBaseShape, anAxis);
+      std::shared_ptr<GeomAlgoAPI_Symmetry> aSymmetryAlgo(
+        new GeomAlgoAPI_Symmetry(aBaseShape, anAxis));
 
-      if (!aSymmetryAlgo.check()) {
-        setError(aSymmetryAlgo.getError());
+      if (!aSymmetryAlgo->check()) {
+        setError(aSymmetryAlgo->getError());
         return;
       }
 
-      aSymmetryAlgo.build();
+      aSymmetryAlgo->build();
 
       // Checking that the algorithm worked properly.
-      if(!aSymmetryAlgo.isDone()) {
-        static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
-        setError(aFeatureError);
-        break;
-      }
-      if(aSymmetryAlgo.shape()->isNull()) {
-        static const std::string aShapeError = "Error: Resulting shape is Null.";
-        setError(aShapeError);
-        break;
-      }
-      if(!aSymmetryAlgo.isValid()) {
-        std::string aFeatureError = "Error: Resulting shape is not valid.";
-        setError(aFeatureError);
+      if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aSymmetryAlgo, getKind(), anError)) {
+        setError(anError);
         break;
       }
 
@@ -263,31 +270,49 @@ void FeaturesPlugin_Symmetry::performSymmetryByPlane()
   if (!collectSourceObjects(anObjects, aContextes))
     return;
 
-  //Getting axis.
-  std::shared_ptr<GeomAPI_Ax2> aPlane;
-  std::shared_ptr<GeomAPI_Pln> aPln;
-  std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
-    selection(FeaturesPlugin_Symmetry::PLANE_OBJECT_ID());
-  if (anObjRef && anObjRef->value() && anObjRef->value()->isFace()) {
-    aPln = std::shared_ptr<GeomAPI_Face>(new GeomAPI_Face(anObjRef->value()))->getPlane();
+  //Getting plane.
+  static const std::string aSelectionError = "Error: The plane shape selection is bad.";
+  AttributeSelectionPtr anObjRef = selection(PLANE_OBJECT_ID());
+  GeomShapePtr aShape = anObjRef->value();
+  if (!aShape.get()) {
+    if (anObjRef->context().get()) {
+      aShape = anObjRef->context()->shape();
+    }
+  }
+  if (!aShape.get()) {
+    setError(aSelectionError);
+    return;
+  }
+
+  GeomFacePtr aFace;
+  if (aShape->isFace())
+  {
+    aFace = aShape->face();
   }
-  else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
-             anObjRef->context()->shape() && anObjRef->context()->shape()->isFace()) {
-    aPln =
-      std::shared_ptr<GeomAPI_Face>(new GeomAPI_Face(anObjRef->context()->shape()))->getPlane();
+  else if (aShape->isCompound())
+  {
+    GeomAPI_ShapeIterator anIt(aShape);
+    aFace = anIt.current()->face();
   }
-  if (aPln) {
-    aPlane = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aPln->location(),
-                                                          aPln->direction()));
+
+  if (!aFace.get())
+  {
+    setError(aSelectionError);
+    return;
   }
 
+  std::shared_ptr<GeomAPI_Ax2> aPlane(new GeomAPI_Ax2(aFace->getPlane()->location(),
+                                                      aFace->getPlane()->direction()));
+
+
   // 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) {
@@ -296,29 +321,19 @@ void FeaturesPlugin_Symmetry::performSymmetryByPlane()
       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
       buildResult(anOrigin, aTrsf, aResultIndex);
     } else {
-      GeomAlgoAPI_Symmetry aSymmetryAlgo(aBaseShape, aPlane);
+      std::shared_ptr<GeomAlgoAPI_Symmetry> aSymmetryAlgo(
+        new GeomAlgoAPI_Symmetry(aBaseShape, aPlane));
 
-      if (!aSymmetryAlgo.check()) {
-        setError(aSymmetryAlgo.getError());
+      if (!aSymmetryAlgo->check()) {
+        setError(aSymmetryAlgo->getError());
         return;
       }
 
-      aSymmetryAlgo.build();
+      aSymmetryAlgo->build();
 
       // Checking that the algorithm worked properly.
-      if(!aSymmetryAlgo.isDone()) {
-        static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
-        setError(aFeatureError);
-        break;
-      }
-      if(aSymmetryAlgo.shape()->isNull()) {
-        static const std::string aShapeError = "Error: Resulting shape is Null.";
-        setError(aShapeError);
-        break;
-      }
-      if(!aSymmetryAlgo.isValid()) {
-        std::string aFeatureError = "Error: Resulting shape is not valid.";
-        setError(aFeatureError);
+      if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aSymmetryAlgo, getKind(), anError)) {
+        setError(anError);
         break;
       }
 
@@ -332,20 +347,30 @@ void FeaturesPlugin_Symmetry::performSymmetryByPlane()
 }
 
 //=================================================================================================
-void FeaturesPlugin_Symmetry::buildResult(GeomAlgoAPI_Symmetry& theSymmetryAlgo,
-                                          std::shared_ptr<GeomAPI_Shape> theBaseShape,
-                                          int theResultIndex)
+void FeaturesPlugin_Symmetry::buildResult(
+  std::shared_ptr<GeomAlgoAPI_Symmetry>& theSymmetryAlgo,
+  std::shared_ptr<GeomAPI_Shape> theBaseShape, int theResultIndex)
 {
+  std::shared_ptr<GeomAlgoAPI_MakeShapeList> anAlgoList(new GeomAlgoAPI_MakeShapeList());
+  anAlgoList->appendAlgo(theSymmetryAlgo);
   // Compose source shape and the result of symmetry.
-  ListOfShape aShapes;
-  aShapes.push_back(theBaseShape);
-  aShapes.push_back(theSymmetryAlgo.shape());
-  std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
+  GeomShapePtr aCompound;
+  if (boolean(KEEP_ORIGINAL_RESULT())->value()) {
+    ListOfShape aShapes;
+    // add a copy of a base shape otherwise selection of this base shape is bad (2592)
+    std::shared_ptr<GeomAlgoAPI_Copy> aCopyAlgo(new GeomAlgoAPI_Copy(theBaseShape));
+    aShapes.push_back(aCopyAlgo->shape());
+    anAlgoList->appendAlgo(aCopyAlgo);
+
+    aShapes.push_back(theSymmetryAlgo->shape());
+    aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
+  } else
+    aCompound = theSymmetryAlgo->shape();
 
   // Store and name the result.
   ResultBodyPtr aResultBody = document()->createBody(data(), theResultIndex);
   aResultBody->storeModified(theBaseShape, aCompound);
-  loadNamingDS(theSymmetryAlgo, aResultBody, theBaseShape);
+  FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, theBaseShape, anAlgoList, "Symmetried");
   setResult(aResultBody, theResultIndex);
 }
 
@@ -354,27 +379,15 @@ void FeaturesPlugin_Symmetry::buildResult(ResultPartPtr theOriginal,
                                           std::shared_ptr<GeomAPI_Trsf> theTrsf,
                                           int& theResultIndex)
 {
-  std::shared_ptr<GeomAPI_Trsf> anIdentity(new GeomAPI_Trsf());
-  ResultPartPtr aCopy = document()->copyPart(theOriginal, data(), theResultIndex);
-  aCopy->setTrsf(theOriginal, anIdentity);
-  setResult(aCopy, theResultIndex);
-
-  ++theResultIndex;
+  if (boolean(KEEP_ORIGINAL_RESULT())->value()) {
+    std::shared_ptr<GeomAPI_Trsf> anIdentity(new GeomAPI_Trsf());
+    ResultPartPtr aCopy = document()->copyPart(theOriginal, data(), theResultIndex);
+    aCopy->setTrsf(theOriginal, anIdentity);
+    setResult(aCopy, theResultIndex);
+    ++theResultIndex;
+  }
 
   ResultPartPtr aResultPart = document()->copyPart(theOriginal, data(), theResultIndex);
   aResultPart->setTrsf(theOriginal, theTrsf);
   setResult(aResultPart, theResultIndex);
 }
-
-//=================================================================================================
-void FeaturesPlugin_Symmetry::loadNamingDS(GeomAlgoAPI_Symmetry& theSymmetryAlgo,
-                                           std::shared_ptr<ModelAPI_ResultBody> theResultBody,
-                                           std::shared_ptr<GeomAPI_Shape> theBaseShape)
-{
-  // Name the faces
-  std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theSymmetryAlgo.mapOfSubShapes();
-  std::string aReflectedName = "Symmetried";
-  FeaturesPlugin_Tools::storeModifiedShapes(theSymmetryAlgo, theResultBody,
-                                            theBaseShape, 1, 2, 3, aReflectedName,
-                                            *aSubShapes.get());
-}