Salome HOME
updated copyright message
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Scale.cpp
index d46e8fcac278460aa98ae66a607d07b5432b29a9..b7a9c29f804665fff51dbeecb5674242cf085059 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2023  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_Scale.h>
+#include <FeaturesPlugin_Tools.h>
+
+#include <GeomAPI_Trsf.h>
 
+#include <GeomAlgoAPI_MakeShapeList.h>
 #include <GeomAlgoAPI_PointBuilder.h>
+#include <GeomAlgoAPI_Scale.h>
 #include <GeomAlgoAPI_Tools.h>
 
 #include <ModelAPI_AttributeDouble.h>
 #include <ModelAPI_AttributeString.h>
 #include <ModelAPI_ResultBody.h>
 #include <ModelAPI_ResultPart.h>
+#include <ModelAPI_Tools.h>
+
+static const std::string SCALE_VERSION_1("v9.5");
 
-#include <FeaturesPlugin_Tools.h>
 
 //=================================================================================================
 FeaturesPlugin_Scale::FeaturesPlugin_Scale()
@@ -58,6 +64,11 @@ void FeaturesPlugin_Scale::initAttributes()
                        ModelAPI_AttributeDouble::typeId());
   data()->addAttribute(FeaturesPlugin_Scale::SCALE_FACTOR_Z_ID(),
                        ModelAPI_AttributeDouble::typeId());
+
+  if (!aSelection->isInitialized()) {
+    // new feature, not read from file
+    data()->setVersion(SCALE_VERSION_1);
+  }
 }
 
 //=================================================================================================
@@ -69,8 +80,7 @@ void FeaturesPlugin_Scale::execute()
   if (aMethodType == FeaturesPlugin_Scale::CREATION_METHOD_BY_FACTOR()) {
     performScaleByFactor();
   }
-
-  if (aMethodType == FeaturesPlugin_Scale::CREATION_METHOD_BY_DIMENSIONS()) {
+  else if (aMethodType == FeaturesPlugin_Scale::CREATION_METHOD_BY_DIMENSIONS()) {
     performScaleByDimensions();
   }
 }
@@ -78,24 +88,16 @@ void FeaturesPlugin_Scale::execute()
 //=================================================================================================
 void FeaturesPlugin_Scale::performScaleByFactor()
 {
+  bool isKeepSubShapes = data()->version() == SCALE_VERSION_1;
+
   // Getting objects.
-  ListOfShape anObjects;
-  std::list<ResultPtr> aContextes;
-  AttributeSelectionListPtr anObjectsSelList =
-    selectionList(FeaturesPlugin_Scale::OBJECTS_LIST_ID());
-  if (anObjectsSelList->size() == 0) {
+  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;
-  }
-  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
-      return;
-    }
-    anObjects.push_back(anObject);
-    aContextes.push_back(anObjectAttr->context());
-  }
 
   // Getting the center point
   std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
@@ -114,22 +116,14 @@ void FeaturesPlugin_Scale::performScaleByFactor()
   // Getting scale factor
   double aScaleFactor = real(FeaturesPlugin_Scale::SCALE_FACTOR_ID())->value();
 
-  // Moving each object.
+  // Collect transformation for 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<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_Scale> aScaleAlgo(
-      new GeomAlgoAPI_Scale(aBaseShape, aCenterPoint, aScaleFactor));
-
-    if (!aScaleAlgo->check()) {
-      setError(aScaleAlgo->getError());
-      return;
-    }
-
-    aScaleAlgo->build();
+        new GeomAlgoAPI_Scale(aBaseShape, aCenterPoint, aScaleFactor));
 
     // Checking that the algorithm worked properly.
     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aScaleAlgo, getKind(), anError)) {
@@ -137,18 +131,22 @@ void FeaturesPlugin_Scale::performScaleByFactor()
       break;
     }
 
-    ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
+    anObjects.markModified(aBaseShape, aScaleAlgo->shape());
+    aMakeShapeList->appendAlgo(aScaleAlgo);
+  }
 
-    ListOfShape aShapes;
-    aShapes.push_back(aBaseShape);
-    FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
-                                             aShapes,
-                                             ListOfShape(),
-                                             aScaleAlgo,
-                                             aScaleAlgo->shape(),
-                                             "Scaled");
-    setResult(aResultBody, aResultIndex);
-    aResultIndex++;
+  // Build results of the scaling
+  int aResultIndex = 0;
+  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, "Scaled");
+    // 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.
@@ -158,24 +156,16 @@ void FeaturesPlugin_Scale::performScaleByFactor()
 //=================================================================================================
 void FeaturesPlugin_Scale::performScaleByDimensions()
 {
+  bool isKeepSubShapes = data()->version() == SCALE_VERSION_1;
+
   // Getting objects.
-  ListOfShape anObjects;
-  std::list<ResultPtr> aContextes;
-  AttributeSelectionListPtr anObjectsSelList =
-    selectionList(FeaturesPlugin_Scale::OBJECTS_LIST_ID());
-  if (anObjectsSelList->size() == 0) {
+  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;
-  }
-  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
-      return;
-    }
-    anObjects.push_back(anObject);
-    aContextes.push_back(anObjectAttr->context());
-  }
 
   // Getting the center point
   std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
@@ -196,12 +186,11 @@ void FeaturesPlugin_Scale::performScaleByDimensions()
   double aScaleFactorY = real(FeaturesPlugin_Scale::SCALE_FACTOR_Y_ID())->value();
   double aScaleFactorZ = real(FeaturesPlugin_Scale::SCALE_FACTOR_Z_ID())->value();
 
-  // Moving each object.
+  // Collect transformation for 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<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_Scale> aScaleAlgo(new GeomAlgoAPI_Scale(aBaseShape,
                                                                         aCenterPoint,
@@ -209,31 +198,28 @@ void FeaturesPlugin_Scale::performScaleByDimensions()
                                                                         aScaleFactorY,
                                                                         aScaleFactorZ));
 
-    if (!aScaleAlgo->check()) {
-      setError(aScaleAlgo->getError());
-      return;
-    }
-
-    aScaleAlgo->build();
-
     // Checking that the algorithm worked properly.
     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aScaleAlgo, getKind(), anError)) {
       setError(anError);
       break;
     }
 
-    ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
+    anObjects.markModified(aBaseShape, aScaleAlgo->shape());
+    aMakeShapeList->appendAlgo(aScaleAlgo);
+  }
 
-    ListOfShape aShapes;
-    aShapes.push_back(aBaseShape);
-    FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
-                                             aShapes,
-                                             ListOfShape(),
-                                             aScaleAlgo,
-                                             aScaleAlgo->shape(),
-                                             "Scaled");
-    setResult(aResultBody, aResultIndex);
-    aResultIndex++;
+  // Build results of the scaling
+  int aResultIndex = 0;
+  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, "Scaled");
+    // 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.