Salome HOME
Copyright update 2022
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Rotation.cpp
old mode 100755 (executable)
new mode 100644 (file)
index 9ba830b..cfbc56c
@@ -1,21 +1,47 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-// File:        FeaturesPlugin_Rotation.cpp
-// Created:     12 May 2015
-// Author:      Dmitry Bobylev
+// Copyright (C) 2014-2022  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_Rotation.h>
 
 #include <ModelAPI_AttributeDouble.h>
 #include <ModelAPI_AttributeSelectionList.h>
+#include <ModelAPI_AttributeString.h>
 #include <ModelAPI_ResultBody.h>
 #include <ModelAPI_ResultPart.h>
+#include <ModelAPI_Tools.h>
+
+#include <GeomAlgoAPI_MakeShapeList.h>
+#include <GeomAlgoAPI_PointBuilder.h>
+#include <GeomAlgoAPI_Rotation.h>
+#include <GeomAlgoAPI_Tools.h>
 
+#include <GeomAPI_Ax1.h>
 #include <GeomAPI_Edge.h>
 #include <GeomAPI_Lin.h>
+#include <GeomAPI_ShapeIterator.h>
+#include <GeomAPI_ShapeHierarchy.h>
+#include <GeomAPI_Trsf.h>
 
 #include <FeaturesPlugin_Tools.h>
 
+static const std::string ROTATION_VERSION_1("v9.5");
+
 //=================================================================================================
 FeaturesPlugin_Rotation::FeaturesPlugin_Rotation()
 {
@@ -24,6 +50,9 @@ FeaturesPlugin_Rotation::FeaturesPlugin_Rotation()
 //=================================================================================================
 void FeaturesPlugin_Rotation::initAttributes()
 {
+  data()->addAttribute(FeaturesPlugin_Rotation::CREATION_METHOD(),
+                       ModelAPI_AttributeString::typeId());
+
   AttributeSelectionListPtr aSelection =
     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
     FeaturesPlugin_Rotation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
@@ -31,108 +60,178 @@ void FeaturesPlugin_Rotation::initAttributes()
   data()->addAttribute(FeaturesPlugin_Rotation::AXIS_OBJECT_ID(),
                        ModelAPI_AttributeSelection::typeId());
   data()->addAttribute(FeaturesPlugin_Rotation::ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
+
+  data()->addAttribute(FeaturesPlugin_Rotation::CENTER_POINT_ID(),
+                       ModelAPI_AttributeSelection::typeId());
+  data()->addAttribute(FeaturesPlugin_Rotation::START_POINT_ID(),
+                       ModelAPI_AttributeSelection::typeId());
+  data()->addAttribute(FeaturesPlugin_Rotation::END_POINT_ID(),
+                       ModelAPI_AttributeSelection::typeId());
+
+  if (!aSelection->isInitialized()) {
+    // new feature, not read from file
+    data()->setVersion(ROTATION_VERSION_1);
+  }
 }
 
 //=================================================================================================
 void FeaturesPlugin_Rotation::execute()
 {
-  // Getting objects.
-  ListOfShape anObjects;
-  std::list<ResultPtr> aContextes;
-  AttributeSelectionListPtr anObjectsSelList =
-    selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
-  if (anObjectsSelList->size() == 0) {
-    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()) {
-      return;
+  AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Rotation::CREATION_METHOD());
+  std::string aMethodType = aMethodTypeAttr->value();
+
+  GeomTrsfPtr aTrsf;
+  if (aMethodType == CREATION_METHOD_BY_ANGLE())
+    aTrsf = rotationByAxisAndAngle();
+  else if (aMethodType == CREATION_METHOD_BY_THREE_POINTS())
+    aTrsf = rotationByThreePoints();
+
+  performRotation(aTrsf);
+}
+
+//=================================================================================================
+GeomTrsfPtr FeaturesPlugin_Rotation::rotationByAxisAndAngle()
+{
+  //Getting axis.
+  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();
     }
-    anObjects.push_back(anObject);
-    aContextes.push_back(anObjectAttr->context());
+  }
+  if (!aShape.get()) {
+    setError(aSelectionError);
+    return GeomTrsfPtr();
   }
 
-  //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()));
+  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 GeomTrsfPtr();
   }
 
-  // Getting angle.
+  std::shared_ptr<GeomAPI_Ax1> anAxis (new GeomAPI_Ax1(anEdge->line()->location(),
+                                                       anEdge->line()->direction()));
   double anAngle = real(FeaturesPlugin_Rotation::ANGLE_ID())->value();
 
-  // Rotating each object.
-  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();
-
-    // Setting result.
-    if (isPart) {
-      std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
-      aTrsf->setRotation(anAxis, anAngle);
-      ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
-      ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
-      aResultPart->setTrsf(*aContext, aTrsf);
-      setResult(aResultPart, aResultIndex);
-    } else {
-      GeomAlgoAPI_Rotation aRotationAlgo(aBaseShape, anAxis, anAngle);
-
-      // 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);
-        break;
-      }
-
-      ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
-      loadNamingDS(aRotationAlgo, aResultBody, aBaseShape);
-      setResult(aResultBody, aResultIndex);
+  GeomTrsfPtr aTrsf(new GeomAPI_Trsf());
+  aTrsf->setRotation(anAxis, anAngle);
+  return aTrsf;
+}
+
+//=================================================================================================
+GeomTrsfPtr FeaturesPlugin_Rotation::rotationByThreePoints()
+{
+  // Getting the center point and two points (start and end)
+  std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
+  std::shared_ptr<GeomAPI_Pnt> aStartPoint;
+  std::shared_ptr<GeomAPI_Pnt> anEndPoint;
+  std::shared_ptr<ModelAPI_AttributeSelection> aCenterRef =
+    selection(FeaturesPlugin_Rotation::CENTER_POINT_ID());
+  std::shared_ptr<ModelAPI_AttributeSelection> aStartPointRef =
+    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)) {
+    GeomShapePtr aCenterShape = aCenterRef->value();
+    if (!aCenterShape.get() && aCenterRef->context().get())
+      aCenterShape = aCenterRef->context()->shape();
+    GeomShapePtr aStartShape = aStartPointRef->value();
+    if (!aStartShape.get() && aStartPointRef->context().get())
+      aStartShape = aStartPointRef->context()->shape();
+    GeomShapePtr anEndShape = anEndPointRef->value();
+    if (!anEndShape.get() && anEndPointRef->context().get())
+      anEndShape = anEndPointRef->context()->shape();
+    if (aStartShape && anEndShape && aCenterShape) {
+      aCenterPoint = GeomAlgoAPI_PointBuilder::point(aCenterShape);
+      aStartPoint = GeomAlgoAPI_PointBuilder::point(aStartShape);
+      anEndPoint = GeomAlgoAPI_PointBuilder::point(anEndShape);
     }
-    aResultIndex++;
+
+    if (!aCenterPoint || !aStartPoint || !anEndPoint)
+      return GeomTrsfPtr();
   }
 
-  // Remove the rest results if there were produced in the previous pass.
-  removeResults(aResultIndex);
+  GeomTrsfPtr aTrsf(new GeomAPI_Trsf());
+  aTrsf->setRotation(aCenterPoint, aStartPoint, anEndPoint);
+  return aTrsf;
 }
 
-void FeaturesPlugin_Rotation::loadNamingDS(GeomAlgoAPI_Rotation& theRotaionAlgo,
-                                           std::shared_ptr<ModelAPI_ResultBody> theResultBody,
-                                           std::shared_ptr<GeomAPI_Shape> theBaseShape)
+//=================================================================================================
+void FeaturesPlugin_Rotation::performRotation(const GeomTrsfPtr& theTrsf)
 {
-  // Store result.
-  theResultBody->storeModified(theBaseShape, theRotaionAlgo.shape());
+  if (!theTrsf) {
+    setError("Invalid transformation.");
+    return;
+  }
+
+  bool isKeepSubShapes = data()->version() == ROTATION_VERSION_1;
+
+  // Getting objects.
+  GeomAPI_ShapeHierarchy anObjects;
+  std::list<ResultPtr> aParts;
+  ResultPtr aTextureSource;
+  AttributeSelectionListPtr anObjSelList = selectionList(OBJECTS_LIST_ID());
+  if (!FeaturesPlugin_Tools::shapesFromSelectionList(
+       anObjSelList, isKeepSubShapes, anObjects, aParts, aTextureSource))
+    return;
+
+  std::string anError;
+  int aResultIndex = 0;
+  // Rotating each part.
+  for (std::list<ResultPtr>::iterator aPRes = aParts.begin(); aPRes != aParts.end(); ++aPRes) {
+    ResultPartPtr anOriginal = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aPRes);
+    ResultPartPtr aResultPart = document()->copyPart(anOriginal, data(), aResultIndex);
+    aResultPart->setTrsf(anOriginal, theTrsf);
+    setResult(aResultPart, aResultIndex++);
+  }
 
-  int aRotatedTag = 1;
-  std::string aRotatedName = "Rotated";
-  std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theRotaionAlgo.mapOfSubShapes();
+  // Collect transformations for each object in a part.
+  std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
+  for(GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
+      anObjectsIt != anObjects.end(); ++anObjectsIt) {
+    std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
+    std::shared_ptr<GeomAlgoAPI_Transform> aRotationAlgo(
+        new GeomAlgoAPI_Transform(aBaseShape, theTrsf));
+
+    // Checking that the algorithm worked properly.
+    if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationAlgo, getKind(), anError)) {
+      setError(anError);
+      break;
+    }
 
-  FeaturesPlugin_Tools::storeModifiedShapes(theRotaionAlgo, theResultBody,
-                                            theBaseShape, aRotatedTag, aRotatedName,
-                                            *aSubShapes.get());
+    anObjects.markModified(aBaseShape, aRotationAlgo->shape());
+    aMakeShapeList->appendAlgo(aRotationAlgo);
+  }
+
+  // Build results of the rotation.
+  const ListOfShape& anOriginalShapes = anObjects.objects();
+  ListOfShape aTopLevel;
+  anObjects.topLevelObjects(aTopLevel);
+  for (ListOfShape::iterator anIt = aTopLevel.begin(); anIt != aTopLevel.end(); ++anIt) {
+    ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
+    ModelAPI_Tools::loadModifiedShapes(aResultBody, anOriginalShapes, ListOfShape(),
+                                       aMakeShapeList, *anIt, "Rotated");
+    // Copy image data, if any
+    ModelAPI_Tools::copyImageAttribute(aTextureSource, aResultBody);
+    setResult(aResultBody, aResultIndex++);
+  }
+
+  // Remove the rest results if there were produced in the previous pass.
+  removeResults(aResultIndex);
 }