Salome HOME
Issue #1343 Improvement of Extrusion and Revolution operations: extrusion cut.
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Extrusion.cpp
index cebc0bb44cd024e123d796b13681fa978eba2570..d388035926c7cb47f7189600ee823ebf670537e3 100644 (file)
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
+
 // File:        FeaturesPlugin_Extrusion.cpp
 // Created:     30 May 2014
 // Author:      Vitaly SMETANNIKOV
 
 #include "FeaturesPlugin_Extrusion.h"
+
 #include <ModelAPI_Session.h>
-#include <ModelAPI_Document.h>
-#include <ModelAPI_Data.h>
+#include <ModelAPI_Validator.h>
 #include <ModelAPI_ResultConstruction.h>
 #include <ModelAPI_ResultBody.h>
 #include <ModelAPI_AttributeDouble.h>
 #include <ModelAPI_AttributeSelection.h>
-#include <ModelAPI_AttributeBoolean.h>
-#include <GeomAlgoAPI_Extrusion.h>
+#include <ModelAPI_AttributeSelectionList.h>
+#include <ModelAPI_AttributeString.h>
+#include <ModelAPI_AttributeReference.h>
 
-using namespace std;
-#define _LATERAL_TAG 1
-#define _FIRST_TAG 2
-#define _LAST_TAG 3
-#define EDGE 6
+#include <GeomAlgoAPI_CompoundBuilder.h>
+#include <GeomAlgoAPI_Prism.h>
+#include <GeomAlgoAPI_ShapeTools.h>
 
+#include <sstream>
+
+//=================================================================================================
 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
 {
 }
 
+//=================================================================================================
 void FeaturesPlugin_Extrusion::initAttributes()
 {
-  data()->addAttribute(FeaturesPlugin_Extrusion::FACE_ID(), ModelAPI_AttributeSelection::type());
-  data()->addAttribute(FeaturesPlugin_Extrusion::SIZE_ID(), ModelAPI_AttributeDouble::type());
-  data()->addAttribute(FeaturesPlugin_Extrusion::REVERSE_ID(), ModelAPI_AttributeBoolean::type());
+  AttributeSelectionListPtr aSelection = 
+    std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
+    LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
+  // extrusion works with faces always
+  aSelection->setSelectionType("FACE");
+
+  data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
+
+  data()->addAttribute(TO_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
+  data()->addAttribute(FROM_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
+
+  data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
+  data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
+
+  data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
+  data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
+
+  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()
 {
-  std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = std::dynamic_pointer_cast<
-    ModelAPI_AttributeSelection>(data()->attribute(FeaturesPlugin_Extrusion::FACE_ID()));
-  if (!aFaceRef)
-    return;
-
-  std::shared_ptr<GeomAPI_Shape> aFace = 
-    std::dynamic_pointer_cast<GeomAPI_Shape>(aFaceRef->value());
-  if (!aFace)
-    return;
-
-  std::shared_ptr<GeomAPI_Shape> aContext;
-  ResultPtr aContextRes = aFaceRef->context();
-  if (aContextRes) {
-    if (aContextRes->groupName() == ModelAPI_ResultBody::group())
-      aContext = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aContextRes)->shape();
-    else if (aContextRes->groupName() == ModelAPI_ResultConstruction::group())
-      aContext = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes)->shape();
+  /// 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();
   }
-  if (!aContext) {
-    static const std::string aContextError = "The selection context is bad";
-    setError(aContextError);
-    return;
+
+  // Getting faces.
+  ListOfShape aFacesList;
+  for(int anIndex = 0; anIndex < aFacesSelectionList->size(); anIndex++) {
+    AttributeSelectionPtr aFaceSel = aFacesSelectionList->value(anIndex);
+    std::shared_ptr<GeomAPI_Shape> aFaceShape = aFaceSel->value();
+    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);
+        return;
+      }
+      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);
+      }
+    }
   }
 
-  double aSize = data()->real(FeaturesPlugin_Extrusion::SIZE_ID())->value();
-  if (data()->boolean(FeaturesPlugin_Extrusion::REVERSE_ID())->value())
-    aSize = -aSize;
+  // Getting sizes.
+  double aToSize = 0.0;
+  double aFromSize = 0.0;
 
-  std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
-  GeomAlgoAPI_Extrusion aFeature(aFace, aSize);
-  if(!aFeature.isDone()) {
-    static const std::string aFeatureError = "Extrusion algorithm failed";  
-    setError(aFeatureError);
-    return;
+  if(string(CREATION_METHOD())->value() == "BySizes") {
+    aToSize = real(TO_SIZE_ID())->value();
+    aFromSize =  real(FROM_SIZE_ID())->value();
+  } else {
+    aToSize = real(TO_OFFSET_ID())->value();
+    aFromSize =  real(FROM_OFFSET_ID())->value();
   }
 
-  // Check if shape is valid
-  if (aFeature.shape()->isNull()) {
-    static const std::string aShapeError = "Resulting shape is Null";     
-    setError(aShapeError);
-    return;
+  // Getting bounding planes.
+  std::shared_ptr<GeomAPI_Shape> aToShape;
+  std::shared_ptr<GeomAPI_Shape> aFromShape;
+
+  if(string(CREATION_METHOD())->value() == "ByPlanesAndOffsets") {
+    std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(TO_OBJECT_ID());
+    if(anObjRef.get() != NULL) {
+      aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
+      if(aToShape.get() == NULL && anObjRef->context().get() != NULL) {
+        aToShape =  anObjRef->context()->shape();
+      }
+    }
+    anObjRef = selection(FROM_OBJECT_ID());
+    if(anObjRef.get() != NULL) {
+      aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
+      if(aFromShape.get() == NULL && anObjRef->context().get() != NULL) {
+        aFromShape = anObjRef->context()->shape();
+      }
+    }
   }
-  if(!aFeature.isValid()) {
-    std::string aFeatureError = "Warning: resulting shape is not valid";  
-    setError(aFeatureError);
-    return;
-  }  
-  //LoadNamingDS
-  LoadNamingDS(aFeature, aResultBody, aFace, aContext);
-
-  setResult(aResultBody);
-}
 
-//============================================================================
-void FeaturesPlugin_Extrusion::LoadNamingDS(GeomAlgoAPI_Extrusion& theFeature, 
-  std::shared_ptr<ModelAPI_ResultBody> theResultBody, 
-  std::shared_ptr<GeomAPI_Shape> theBasis,
-  std::shared_ptr<GeomAPI_Shape> theContext)
-{  
+  // Searching faces with common edges.
+  ListOfShape aShells;
+  ListOfShape 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;
+  for(ListOfShape::const_iterator anIter = aShells.cbegin(); anIter != aShells.cend(); anIter++) {
+    std::shared_ptr<GeomAPI_Shape> aBaseShape = *anIter;
+
+    GeomAlgoAPI_Prism aPrismAlgo(aBaseShape, aToShape, aToSize, aFromShape, aFromSize);
+    if(!aPrismAlgo.isDone()) {
+      static const std::string aPrismAlgoError = "Error: Extrusion algorithm failed.";
+      setError(aPrismAlgoError);
+      aResultIndex = 0;
+      break;
+    }
 
+    // Check if shape is valid
+    if(!aPrismAlgo.shape().get() || aPrismAlgo.shape()->isNull()) {
+      static const std::string aShapeError = "Error: Resulting shape is Null.";
+      setError(aShapeError);
+      aResultIndex = 0;
+      break;
+    }
+    if(!aPrismAlgo.isValid()) {
+      std::string aPrismAlgoError = "Error: Resulting shape is not valid.";
+      setError(aPrismAlgoError);
+      aResultIndex = 0;
+      break;
+    }
 
-  //load result
-  if(theBasis->isEqual(theContext))
-    theResultBody->store(theFeature.shape());
-  else
-    theResultBody->storeGenerated(theContext, theFeature.shape()); 
-
-  GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
-  theFeature.mapOfShapes(*aSubShapes);
-
-    //Insert lateral face : Face from Edge
-  theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape(), theBasis, EDGE,_LATERAL_TAG, *aSubShapes);
-
-  //Insert bottom face
-  std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();  
-  if (!aBottomFace->isNull()) {
-       if (aSubShapes->isBound(aBottomFace)) {  
-               aBottomFace = aSubShapes->find(aBottomFace);            
-    }    
-    theResultBody->generated(aBottomFace, _FIRST_TAG);
+    ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
+    loadNamingDS(aPrismAlgo, aResultBody, aBaseShape);
+    setResult(aResultBody, aResultIndex);
+    aResultIndex++;
   }
 
+  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();
+}
 
-  //Insert top face
-  std::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
-  if (!aTopFace->isNull()) {
-    if (aSubShapes->isBound(aTopFace)) {        
-      aTopFace = aSubShapes->find(aTopFace);   
+//=================================================================================================
+void FeaturesPlugin_Extrusion::loadNamingDS(GeomAlgoAPI_Prism& thePrismAlgo,
+                                            std::shared_ptr<ModelAPI_ResultBody> theResultBody,
+                                            std::shared_ptr<GeomAPI_Shape> theBasis)
+{
+  //load result
+  theResultBody->storeGenerated(theBasis, thePrismAlgo.shape());
+
+  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, theBasis, GeomAPI_Shape::EDGE, aLatTag, aLatName, *aSubShapes);
+
+  //Insert to faces
+  int aToFaceIndex = 1;
+  const std::string aToName = "ToFace";
+  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(aTopFace, _LAST_TAG);
+    std::ostringstream aStr;
+    aStr << aToName << "_" << aToFaceIndex++;
+    theResultBody->generated(aToFace, aStr.str(), aToTag++);
   }
 
-  
+  //Insert from faces
+  int aFromFaceIndex = 1;
+  const std::string aFromName = "FromFace";
+  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);
+    }
+    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>());
+    }
+  }
+}
+