Salome HOME
Copyright update 2022
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Intersection.cpp
index e557498945a3add650f18bbbd6c4fa805341670b..4d8705ab5905627a98b93ec6f8a17744a4f7d161 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+// 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
 //
 // 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_Intersection.h"
 #include <ModelAPI_BodyBuilder.h>
 #include <ModelAPI_ResultBody.h>
 #include <ModelAPI_AttributeSelectionList.h>
+#include <ModelAPI_Tools.h>
 
 #include <GeomAlgoAPI_Intersection.h>
+#include <GeomAlgoAPI_MakeShapeList.h>
+#include <GeomAlgoAPI_Tools.h>
 #include <GeomAPI_ShapeExplorer.h>
 
 #include <sstream>
 
+static const std::string INTERSECTION_VERSION_1("v9.5");
+
 //=================================================================================================
 FeaturesPlugin_Intersection::FeaturesPlugin_Intersection()
 {
@@ -39,164 +43,62 @@ FeaturesPlugin_Intersection::FeaturesPlugin_Intersection()
 //=================================================================================================
 void FeaturesPlugin_Intersection::initAttributes()
 {
-  data()->addAttribute(FeaturesPlugin_Intersection::OBJECT_LIST_ID(),
-                       ModelAPI_AttributeSelectionList::typeId());
-  data()->addAttribute(FeaturesPlugin_Intersection::TOOL_LIST_ID(),
+  AttributePtr anObjectsAttr = data()->addAttribute(OBJECT_LIST_ID(),
                        ModelAPI_AttributeSelectionList::typeId());
+
+  initVersion(INTERSECTION_VERSION_1, anObjectsAttr, AttributePtr());
 }
 
 //=================================================================================================
 void FeaturesPlugin_Intersection::execute()
 {
-  ListOfShape anObjects, aTools;
-
+  GeomAPI_ShapeHierarchy anObjectsHierarchy;
+  ListOfShape aPlanes;
   // Getting objects.
-  AttributeSelectionListPtr anObjectsSelList =
-    selectionList(FeaturesPlugin_Intersection::OBJECT_LIST_ID());
-  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;
-    }
-    anObjects.push_back(anObject);
-  }
-
-  // Getting tools.
-  AttributeSelectionListPtr aToolsSelList =
-    selectionList(FeaturesPlugin_Intersection::TOOL_LIST_ID());
-  for (int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
-    std::shared_ptr<ModelAPI_AttributeSelection> aToolAttr = aToolsSelList->value(aToolsIndex);
-    std::shared_ptr<GeomAPI_Shape> aTool = aToolAttr->value();
-    if (!aTool.get()) {
-      return;
-    }
-    aTools.push_back(aTool);
-  }
-
-  if(anObjects.empty() || aTools.empty()) {
+  if (!processAttribute(OBJECT_LIST_ID(), anObjectsHierarchy, aPlanes)) {
     setError("Error: Objects or tools are empty.");
     return;
   }
 
   int aResultIndex = 0;
 
-  // Create result for each object.
-  for (ListOfShape::iterator
-       anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
-    std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
-    ListOfShape aListWithObject; aListWithObject.push_back(anObject);
-    GeomAlgoAPI_Intersection anIntersectionAlgo(aListWithObject, aTools);
-
-    // Checking that the algorithm worked properly.
-    if (!anIntersectionAlgo.isDone()) {
-      static const std::string aFeatureError = "Error: Intersection algorithm failed.";
-      setError(aFeatureError);
-      return;
-    }
-    if (anIntersectionAlgo.shape()->isNull()) {
-      static const std::string aShapeError = "Error: Resulting shape is Null.";
-      setError(aShapeError);
-      return;
-    }
-    if (!anIntersectionAlgo.isValid()) {
-      std::string aFeatureError = "Error: Resulting shape is not valid.";
-      setError(aFeatureError);
-      return;
-    }
-
-    std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
-    loadNamingDS(aResultBody, anObject, aTools, anIntersectionAlgo);
-    setResult(aResultBody, aResultIndex);
-    aResultIndex++;
+  // Create result.
+  const ListOfShape& anObjects = anObjectsHierarchy.objects();
+  GeomMakeShapePtr anIntersectionAlgo(new GeomAlgoAPI_Intersection(anObjects));
+
+  // Checking that the algorithm worked properly.
+  std::string anError;
+  if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(anIntersectionAlgo, getKind(), anError)) {
+    setError(anError);
+    return;
   }
 
-  // remove the rest results if there were produced in the previous pass
-  removeResults(aResultIndex);
-}
+  std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
+  aMakeShapeList->appendAlgo(anIntersectionAlgo);
 
-//=================================================================================================
-void FeaturesPlugin_Intersection::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
-                                               const std::shared_ptr<GeomAPI_Shape> theBaseShape,
-                                               const ListOfShape& theTools,
-                                               GeomAlgoAPI_MakeShape& theMakeShape)
-{
-  std::shared_ptr<GeomAPI_Shape> aResultShape = theMakeShape.shape();
-  theResultBody->storeModified(theBaseShape, aResultShape);
-
-  const int aDeletedVertexTag = 1;
-  const int aDeletedEdgeTag   = 2;
-  const int aDeletedFaceTag   = 3;
-
-  theResultBody->loadDeletedShapes(&theMakeShape,
-                                   theBaseShape,
-                                   GeomAPI_Shape::VERTEX,
-                                   aDeletedVertexTag);
-  theResultBody->loadDeletedShapes(&theMakeShape,
-                                   theBaseShape,
-                                   GeomAPI_Shape::EDGE,
-                                   aDeletedEdgeTag);
-  theResultBody->loadDeletedShapes(&theMakeShape,
-                                   theBaseShape,
-                                   GeomAPI_Shape::FACE,
-                                   aDeletedFaceTag);
-
-  ListOfShape aShapes = theTools;
-  aShapes.push_back(theBaseShape);
-  GeomAPI_DataMapOfShapeShape aShapesMap; // Map to store {result_shape, original_shape}
-  const int aShapeTypesNb = 2;
-  const GeomAPI_Shape::ShapeType aShapeTypes[aShapeTypesNb] =
-    {GeomAPI_Shape::VERTEX, GeomAPI_Shape::EDGE};
-  for(ListOfShape::const_iterator anIt = aShapes.cbegin(); anIt != aShapes.cend(); ++anIt) {
-    const GeomShapePtr aShape = *anIt;
-    for(int anIndex = 0; anIndex < aShapeTypesNb; ++anIndex) {
-      for(GeomAPI_ShapeExplorer anOrigShapeExp(aShape, aShapeTypes[anIndex]);
-          anOrigShapeExp.more();
-          anOrigShapeExp.next()) {
-        ListOfShape aHistory;
-        const GeomShapePtr aSubShape = anOrigShapeExp.current();
-        theMakeShape.modified(aSubShape, aHistory);
-        for(ListOfShape::const_iterator aHistoryIt = aHistory.cbegin();
-            aHistoryIt != aHistory.cend();
-            ++aHistoryIt) {
-          aShapesMap.bind(*aHistoryIt, aSubShape);
-        }
-      }
-    }
+  GeomShapePtr aResShape = anIntersectionAlgo->shape();
+  if (data()->version() == INTERSECTION_VERSION_1) {
+    // merge hierarchies of compounds containing objects and tools
+    // and append the result of the FUSE operation
+    aResShape = keepUnusedSubsOfCompound(aResShape, anObjectsHierarchy,
+                                         GeomAPI_ShapeHierarchy(), aMakeShapeList);
   }
 
-  int aModifiedVertexIndex(1),
-      aGeneratedVertexIndex(1),
-      aModifiedEdgeIndex(1),
-      aGeneratedEdgeIndex(1);
-  int aTag = 4;
-  GeomAPI_DataMapOfShapeShape aStoredShapes;
-  for(int anIndex = 0; anIndex < aShapeTypesNb; ++anIndex) {
-    for(GeomAPI_ShapeExplorer aShapeExp(aResultShape, aShapeTypes[anIndex]);
-        aShapeExp.more();
-        aShapeExp.next()) {
-      const GeomShapePtr aSubShape = aShapeExp.current();
-      if(aStoredShapes.isBound(aSubShape)) {
-        continue;
-      }
-      if(aShapesMap.isBound(aSubShape)) {
-        theResultBody->modified(aShapesMap.find(aSubShape),
-          aSubShape,
-          std::string("Modified_")
-            + (anIndex == 0 ? "Vertex_" : "Edge_")
-            + std::to_string((long long)(anIndex == 0 ? aModifiedVertexIndex++
-                                                      : aModifiedEdgeIndex++)),
-          aTag++);
-      } else {
-        theResultBody->generated(aSubShape,
-          std::string("Generated_")
-            + (anIndex == 0 ? "Vertex_" : "Edge_")
-            + std::to_string((long long)(anIndex == 0 ? aGeneratedVertexIndex++
-                                                      : aGeneratedEdgeIndex++)),
-          aTag++);
-      }
-      aStoredShapes.bind(aSubShape, aSubShape);
-    }
-  }
+  std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
+  ModelAPI_Tools::loadModifiedShapes(aResultBody,
+                                     anObjects,
+                                     ListOfShape(),
+                                     aMakeShapeList,
+                                     aResShape);
+  setResult(aResultBody, aResultIndex);
+  aResultIndex++;
+
+  ModelAPI_Tools::loadDeletedShapes(aResultBody,
+                                    GeomShapePtr(),
+                                    anObjects,
+                                    aMakeShapeList,
+                                    aResShape);
+
+  // remove the rest results if there were produced in the previous pass
+  removeResults(aResultIndex);
 }