Salome HOME
Merge branch 'BR_EDF_2018_Lot1'
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Symmetry.cpp
index d254eeef70bc3e9ff461c135abc20d7a2a1b06d0..d3d058ed3a9f35e61b8ead2df25535d601968ed6 100644 (file)
@@ -1,25 +1,42 @@
-// Copyright (C) 2014-2016 CEA/DEN, EDF R&D
-
-// File:        FeaturesPlugin_Symmetry.cpp
-// Created:     30 Nov 2016
-// Author:      Clarisse Genrault (CEA)
+// Copyright (C) 2014-2017  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<mailto:webmaster.salome@opencascade.com>
+//
 
 #include <FeaturesPlugin_Symmetry.h>
 
+#include <GeomAlgoAPI_CompoundBuilder.h>
 #include <GeomAlgoAPI_PointBuilder.h>
+#include <GeomAlgoAPI_FaceBuilder.h>
 
 #include <GeomAPI_Edge.h>
 #include <GeomAPI_Face.h>
 #include <GeomAPI_Lin.h>
 #include <GeomAPI_Pln.h>
+#include <GeomAPI_Trsf.h>
 
+#include <ModelAPI_AttributeBoolean.h>
 #include <ModelAPI_AttributeSelectionList.h>
 #include <ModelAPI_AttributeString.h>
 #include <ModelAPI_ResultBody.h>
 #include <ModelAPI_ResultPart.h>
 
-#include <iostream>
-#include <GeomAlgoAPI_FaceBuilder.h>
+#include <FeaturesPlugin_Tools.h>
 
 //=================================================================================================
 FeaturesPlugin_Symmetry::FeaturesPlugin_Symmetry()
@@ -44,6 +61,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());
 }
 
 //=================================================================================================
@@ -66,27 +86,36 @@ void FeaturesPlugin_Symmetry::execute()
 }
 
 //=================================================================================================
-void FeaturesPlugin_Symmetry::performSymmetryByPoint()
+bool FeaturesPlugin_Symmetry::collectSourceObjects(ListOfShape& theSourceShapes,
+                                                   std::list<ResultPtr>& theSourceResults)
 {
-  // Getting objects.
-  ListOfShape anObjects;
-  std::list<ResultPtr> aContextes;
   AttributeSelectionListPtr anObjectsSelList =
     selectionList(FeaturesPlugin_Symmetry::OBJECTS_LIST_ID());
   if (anObjectsSelList->size() == 0) {
-    return;
+    return false;
   }
-  for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
+  for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); 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
+    if (!anObject.get()) { // may be for not-activated parts
       eraseResults();
-      return;
+      return false;
     }
-    anObjects.push_back(anObject);
-    aContextes.push_back(anObjectAttr->context());
+    theSourceShapes.push_back(anObject);
+    theSourceResults.push_back(anObjectAttr->context());
   }
+  return true;
+}
+
+//=================================================================================================
+void FeaturesPlugin_Symmetry::performSymmetryByPoint()
+{
+  // Getting objects.
+  ListOfShape anObjects;
+  std::list<ResultPtr> aContextes;
+  if (!collectSourceObjects(anObjects, aContextes))
+    return;
 
   //Getting point.
   std::shared_ptr<GeomAPI_Pnt> aPoint;
@@ -115,10 +144,9 @@ void FeaturesPlugin_Symmetry::performSymmetryByPoint()
       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
       aTrsf->setSymmetry(aPoint);
       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
-      ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
-      aResultPart->setTrsf(*aContext, aTrsf);
-      setResult(aResultPart, aResultIndex);
-    } else {
+      buildResult(anOrigin, aTrsf, aResultIndex);
+    }
+    else {
       GeomAlgoAPI_Symmetry aSymmetryAlgo(aBaseShape, aPoint);
 
       if (!aSymmetryAlgo.check()) {
@@ -145,9 +173,7 @@ void FeaturesPlugin_Symmetry::performSymmetryByPoint()
         break;
       }
 
-      ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
-      loadNamingDS(aSymmetryAlgo, aResultBody, aBaseShape);
-      setResult(aResultBody, aResultIndex);
+      buildResult(aSymmetryAlgo, aBaseShape, aResultIndex);
     }
     aResultIndex++;
   }
@@ -162,22 +188,8 @@ void FeaturesPlugin_Symmetry::performSymmetryByAxis()
   // Getting objects.
   ListOfShape anObjects;
   std::list<ResultPtr> aContextes;
-  AttributeSelectionListPtr anObjectsSelList =
-    selectionList(FeaturesPlugin_Symmetry::OBJECTS_LIST_ID());
-  if (anObjectsSelList->size() == 0) {
+  if (!collectSourceObjects(anObjects, aContextes))
     return;
-  }
-  for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); 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);
-    aContextes.push_back(anObjectAttr->context());
-  }
 
   //Getting axis.
   std::shared_ptr<GeomAPI_Ax1> anAxis;
@@ -208,10 +220,9 @@ void FeaturesPlugin_Symmetry::performSymmetryByAxis()
       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
       aTrsf->setSymmetry(anAxis);
       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
-      ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
-      aResultPart->setTrsf(*aContext, aTrsf);
-      setResult(aResultPart, aResultIndex);
-    } else {
+      buildResult(anOrigin, aTrsf, aResultIndex);
+    }
+    else {
       GeomAlgoAPI_Symmetry aSymmetryAlgo(aBaseShape, anAxis);
 
       if (!aSymmetryAlgo.check()) {
@@ -238,9 +249,7 @@ void FeaturesPlugin_Symmetry::performSymmetryByAxis()
         break;
       }
 
-      ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
-      loadNamingDS(aSymmetryAlgo, aResultBody, aBaseShape);
-      setResult(aResultBody, aResultIndex);
+      buildResult(aSymmetryAlgo, aBaseShape, aResultIndex);
     }
     aResultIndex++;
   }
@@ -255,22 +264,8 @@ void FeaturesPlugin_Symmetry::performSymmetryByPlane()
   // Getting objects.
   ListOfShape anObjects;
   std::list<ResultPtr> aContextes;
-  AttributeSelectionListPtr anObjectsSelList =
-    selectionList(FeaturesPlugin_Symmetry::OBJECTS_LIST_ID());
-  if (anObjectsSelList->size() == 0) {
+  if (!collectSourceObjects(anObjects, aContextes))
     return;
-  }
-  for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); 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);
-    aContextes.push_back(anObjectAttr->context());
-  }
 
   //Getting axis.
   std::shared_ptr<GeomAPI_Ax2> aPlane;
@@ -303,9 +298,7 @@ void FeaturesPlugin_Symmetry::performSymmetryByPlane()
       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
       aTrsf->setSymmetry(aPlane);
       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
-      ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
-      aResultPart->setTrsf(*aContext, aTrsf);
-      setResult(aResultPart, aResultIndex);
+      buildResult(anOrigin, aTrsf, aResultIndex);
     } else {
       GeomAlgoAPI_Symmetry aSymmetryAlgo(aBaseShape, aPlane);
 
@@ -333,9 +326,7 @@ void FeaturesPlugin_Symmetry::performSymmetryByPlane()
         break;
       }
 
-      ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
-      loadNamingDS(aSymmetryAlgo, aResultBody, aBaseShape);
-      setResult(aResultBody, aResultIndex);
+      buildResult(aSymmetryAlgo, aBaseShape, aResultIndex);
     }
     aResultIndex++;
   }
@@ -344,19 +335,55 @@ void FeaturesPlugin_Symmetry::performSymmetryByPlane()
   removeResults(aResultIndex);
 }
 
+//=================================================================================================
+void FeaturesPlugin_Symmetry::buildResult(GeomAlgoAPI_Symmetry& theSymmetryAlgo,
+                                          std::shared_ptr<GeomAPI_Shape> theBaseShape,
+                                          int theResultIndex)
+{
+  // Compose source shape and the result of symmetry.
+  GeomShapePtr aCompound;
+  if (boolean(KEEP_ORIGINAL_RESULT())->value()) {
+    ListOfShape aShapes;
+    aShapes.push_back(theBaseShape);
+    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);
+  setResult(aResultBody, theResultIndex);
+}
+
+//=================================================================================================
+void FeaturesPlugin_Symmetry::buildResult(ResultPartPtr theOriginal,
+                                          std::shared_ptr<GeomAPI_Trsf> theTrsf,
+                                          int& 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)
 {
-  // Store and name the result.
-  theResultBody->storeModified(theBaseShape, theSymmetryAlgo.shape());
-
   // Name the faces
   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theSymmetryAlgo.mapOfSubShapes();
-  int aReflectedTag = 1;
   std::string aReflectedName = "Symmetried";
-  theResultBody->loadAndOrientModifiedShapes(&theSymmetryAlgo,
-                                              theBaseShape, GeomAPI_Shape::FACE,
-                                              aReflectedTag, aReflectedName, *aSubShapes.get());
+  FeaturesPlugin_Tools::storeModifiedShapes(theSymmetryAlgo, theResultBody,
+                                            theBaseShape, 1, 2, 3, aReflectedName,
+                                            *aSubShapes.get());
 }