Salome HOME
Issue #1343 Improvement of Extrusion and Revolution operations: extrusion cut.
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Extrusion.cpp
index 029fd2bdbe5ab33516a589326800d3e78fa3ba56..d388035926c7cb47f7189600ee823ebf670537e3 100644 (file)
@@ -4,26 +4,24 @@
 // Created:     30 May 2014
 // Author:      Vitaly SMETANNIKOV
 
-#include <FeaturesPlugin_Extrusion.h>
+#include "FeaturesPlugin_Extrusion.h"
 
-#include <ModelAPI_BodyBuilder.h>
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Validator.h>
-#include <ModelAPI_Document.h>
-#include <ModelAPI_Data.h>
 #include <ModelAPI_ResultConstruction.h>
-#include <ModelAPI_ResultCompSolid.h>
 #include <ModelAPI_ResultBody.h>
 #include <ModelAPI_AttributeDouble.h>
 #include <ModelAPI_AttributeSelection.h>
 #include <ModelAPI_AttributeSelectionList.h>
-#include <ModelAPI_AttributeBoolean.h>
 #include <ModelAPI_AttributeString.h>
 #include <ModelAPI_AttributeReference.h>
 
+#include <GeomAlgoAPI_CompoundBuilder.h>
 #include <GeomAlgoAPI_Prism.h>
 #include <GeomAlgoAPI_ShapeTools.h>
 
+#include <sstream>
+
 //=================================================================================================
 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
 {
@@ -51,43 +49,48 @@ void FeaturesPlugin_Extrusion::initAttributes()
 
   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
+
+  // Composite Sketch attribute
+  data()->addAttribute(FeaturesPlugin_CompositeSketch::SKETCH_OBJECT_ID(),
+                       ModelAPI_AttributeReference::typeId());
+  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
+                       FeaturesPlugin_CompositeSketch::SKETCH_OBJECT_ID());
 }
 
 //=================================================================================================
 void FeaturesPlugin_Extrusion::execute()
 {
+  /// feature extrusion does not have the next attribute
+  AttributeSelectionListPtr aFacesSelectionList = selectionList(LIST_ID());
+  if (aFacesSelectionList.get() && !aFacesSelectionList->isInitialized()) {
+    AttributeReferencePtr aSketchAttr = reference(SKETCH_OBJECT_ID());
+    if (aSketchAttr.get() && aSketchAttr->isInitialized())
+      setSketchObjectToList();
+  }
+
   // Getting faces.
   ListOfShape aFacesList;
-  AttributeSelectionListPtr aFacesSelectionList = selectionList(LIST_ID());
   for(int anIndex = 0; anIndex < aFacesSelectionList->size(); anIndex++) {
-    std::shared_ptr<ModelAPI_AttributeSelection> aFaceSel = aFacesSelectionList->value(anIndex);
-    ResultPtr aContext = aFaceSel->context();
-    std::shared_ptr<GeomAPI_Shape> aContextShape = aContext->shape();
-    if(!aContextShape.get()) {
-      static const std::string aContextError = "The selection context is bad";
-      setError(aContextError);
-      break;
-    }
-
+    AttributeSelectionPtr aFaceSel = aFacesSelectionList->value(anIndex);
     std::shared_ptr<GeomAPI_Shape> aFaceShape = aFaceSel->value();
-    int aFacesNum = -1; // this mean that "aFace" is used
-    ResultConstructionPtr aConstruction = 
-      std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
-    if(!aFaceShape.get()) { // this may be the whole sketch result selected, check and get faces
-      if (aConstruction.get()) {
-        aFacesNum = aConstruction->facesNum();
-      } else {
-        static const std::string aFaceError = "Can not find basis for extrusion";
+    if(aFaceShape.get() && !aFaceShape->isNull()) { // Getting face.
+      aFacesList.push_back(aFaceShape);
+    } else { // This may be the whole sketch result selected, check and get faces.
+      ResultPtr aContext = aFaceSel->context();
+      std::shared_ptr<GeomAPI_Shape> aContextShape = aContext->shape();
+      if(!aContextShape.get()) {
+        static const std::string aContextError = "Error: The selection context is bad.";
+        setError(aContextError);
+        return;
+      }
+      ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
+      if(!aConstruction.get()) {
+        static const std::string aFaceError = "Error: Can not find basis for extrusion.";
         setError(aFaceError);
-        break;
+        return;
       }
-    }
-    for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
-      std::shared_ptr<GeomAPI_Shape> aBaseShape;
-      if (aFacesNum == -1) {
-        aFacesList.push_back(aFaceShape);
-        break;
-      } else {
+      int aFacesNum = aConstruction->facesNum();
+      for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
         aFaceShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
         aFacesList.push_back(aFaceShape);
       }
@@ -130,12 +133,9 @@ void FeaturesPlugin_Extrusion::execute()
   // Searching faces with common edges.
   ListOfShape aShells;
   ListOfShape aFreeFaces;
-  GeomAlgoAPI_ShapeTools::combineFacesToShells(aFacesList, aShells, aFreeFaces);
-  if(aShells.empty()) {
-    aShells = aFreeFaces;
-  } else {
-    aShells.merge(aFreeFaces);
-  }
+  std::shared_ptr<GeomAPI_Shape> aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aFacesList);
+  GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, aShells, aFreeFaces);
+  aShells.insert(aShells.end(), aFreeFaces.begin(), aFreeFaces.end());
 
   // Generating result for each shell and face.
   int aResultIndex = 0;
@@ -144,7 +144,7 @@ void FeaturesPlugin_Extrusion::execute()
 
     GeomAlgoAPI_Prism aPrismAlgo(aBaseShape, aToShape, aToSize, aFromShape, aFromSize);
     if(!aPrismAlgo.isDone()) {
-      static const std::string aPrismAlgoError = "Extrusion algorithm failed";
+      static const std::string aPrismAlgoError = "Error: Extrusion algorithm failed.";
       setError(aPrismAlgoError);
       aResultIndex = 0;
       break;
@@ -152,13 +152,13 @@ void FeaturesPlugin_Extrusion::execute()
 
     // Check if shape is valid
     if(!aPrismAlgo.shape().get() || aPrismAlgo.shape()->isNull()) {
-      static const std::string aShapeError = "Resulting shape is Null";
+      static const std::string aShapeError = "Error: Resulting shape is Null.";
       setError(aShapeError);
       aResultIndex = 0;
       break;
     }
     if(!aPrismAlgo.isValid()) {
-      std::string aPrismAlgoError = "Warning: resulting shape is not valid";
+      std::string aPrismAlgoError = "Error: Resulting shape is not valid.";
       setError(aPrismAlgoError);
       aResultIndex = 0;
       break;
@@ -173,6 +173,14 @@ void FeaturesPlugin_Extrusion::execute()
   removeResults(aResultIndex);
 }
 
+//=================================================================================================
+void FeaturesPlugin_Extrusion::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
+{
+  AttributeSelectionListPtr aFacesSelectionList = selectionList(LIST_ID());
+  if (aFacesSelectionList.get() && aFacesSelectionList->size() > 0)
+    aFacesSelectionList->clear();
+}
+
 //=================================================================================================
 void FeaturesPlugin_Extrusion::loadNamingDS(GeomAlgoAPI_Prism& thePrismAlgo,
                                             std::shared_ptr<ModelAPI_ResultBody> theResultBody,
@@ -181,34 +189,58 @@ void FeaturesPlugin_Extrusion::loadNamingDS(GeomAlgoAPI_Prism& thePrismAlgo,
   //load result
   theResultBody->storeGenerated(theBasis, thePrismAlgo.shape());
 
-  std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = thePrismAlgo.mapOfShapes();
+  std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = thePrismAlgo.mapOfSubShapes();
 
   //Insert lateral face : Face from Edge
   const std::string aLatName = "LateralFace";
   const int aLatTag = 1;
-  theResultBody->loadAndOrientGeneratedShapes(thePrismAlgo.makeShape().get(), theBasis, GeomAPI_Shape::EDGE, aLatTag, aLatName, *aSubShapes);
+  theResultBody->loadAndOrientGeneratedShapes(&thePrismAlgo, theBasis, GeomAPI_Shape::EDGE, aLatTag, aLatName, *aSubShapes);
 
   //Insert to faces
+  int aToFaceIndex = 1;
   const std::string aToName = "ToFace";
-  const int aToTag = 2;
-  const ListOfShape& aToFaces = thePrismAlgo.toFaces();
+  int aToTag = 2;
+  const ListOfShape& aToFaces = thePrismAlgo.toShapes();
   for(ListOfShape::const_iterator anIt = aToFaces.cbegin(); anIt != aToFaces.cend(); anIt++) {
     std::shared_ptr<GeomAPI_Shape> aToFace = *anIt;
     if(aSubShapes->isBound(aToFace)) {
       aToFace = aSubShapes->find(aToFace);
     }
-    theResultBody->generated(aToFace, aToName, aToTag);
+    std::ostringstream aStr;
+    aStr << aToName << "_" << aToFaceIndex++;
+    theResultBody->generated(aToFace, aStr.str(), aToTag++);
   }
 
   //Insert from faces
+  int aFromFaceIndex = 1;
   const std::string aFromName = "FromFace";
-  const int aFromTag = 3;
-  const ListOfShape& aFromFaces = thePrismAlgo.fromFaces();
+  int aFromTag = aToTag > 10000 ? aToTag : 10000;
+  const ListOfShape& aFromFaces = thePrismAlgo.fromShapes();
   for(ListOfShape::const_iterator anIt = aFromFaces.cbegin(); anIt != aFromFaces.cend(); anIt++) {
     std::shared_ptr<GeomAPI_Shape> aFromFace = *anIt;
     if(aSubShapes->isBound(aFromFace)) {
       aFromFace = aSubShapes->find(aFromFace);
     }
-    theResultBody->generated(aFromFace, aFromName, aFromTag);
+    std::ostringstream aStr;
+    aStr << aFromName << "_" << aFromFaceIndex++;
+    theResultBody->generated(aFromFace, aStr.str(), aFromTag++);
   }
 }
+
+//=================================================================================================
+void FeaturesPlugin_Extrusion::setSketchObjectToList()
+{
+  std::shared_ptr<ModelAPI_Feature> aSketchFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(
+                                                       reference(SKETCH_OBJECT_ID())->value());
+
+  if(aSketchFeature.get() && !aSketchFeature->results().empty()) {
+    ResultPtr aSketchRes = aSketchFeature->results().front();
+    ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
+    if(aConstruction.get()) {
+      AttributeSelectionListPtr aFacesSelectionList = selectionList(LIST_ID());
+      if (aFacesSelectionList.get() && aFacesSelectionList->size() == 0)
+        aFacesSelectionList->append(aSketchRes, std::shared_ptr<GeomAPI_Shape>());
+    }
+  }
+}
+