Salome HOME
Issue #1664 In the Sketcher, add the function Split a segment - correction for arc...
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Pipe.cpp
index 3e46113b1933d88f41c2f4756e86c42f9da1e089..787a37fcc07b4582c88c90e030877961a9621da0 100644 (file)
 
 #include <GeomAlgoAPI_CompoundBuilder.h>
 #include <GeomAlgoAPI_Pipe.h>
-#include <GeomAPI_ShapeExplorer.h>
 #include <GeomAlgoAPI_ShapeTools.h>
+#include <GeomAPI_PlanarEdges.h>
+#include <GeomAPI_ShapeExplorer.h>
 
+#include <map>
 #include <sstream>
 
-//=================================================================================================
+static void storeSubShape(ResultBodyPtr theResultBody,
+                          const GeomShapePtr theShape,
+                          const GeomAPI_Shape::ShapeType theType,
+                          const std::shared_ptr<GeomAPI_DataMapOfShapeShape> theMapOfSubShapes,
+                          const std::string theName,
+                          int& theShapeIndex,
+                          int& theTag);
+
+//==================================================================================================
 FeaturesPlugin_Pipe::FeaturesPlugin_Pipe()
 {
 }
 
-//=================================================================================================
+//==================================================================================================
 void FeaturesPlugin_Pipe::initAttributes()
 {
   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
@@ -39,7 +49,7 @@ void FeaturesPlugin_Pipe::initAttributes()
   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), LOCATIONS_ID());
 }
 
-//=================================================================================================
+//==================================================================================================
 void FeaturesPlugin_Pipe::execute()
 {
   // Getting creation method.
@@ -47,6 +57,7 @@ void FeaturesPlugin_Pipe::execute()
 
   // Getting base objects.
   ListOfShape aBaseShapesList, aBaseFacesList;
+  std::map<ResultConstructionPtr, ListOfShape> aSketchWiresMap;
   AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
   if(!aBaseObjectsSelectionList.get()) {
     setError("Error: Could not get base objects selection list.");
@@ -64,8 +75,17 @@ void FeaturesPlugin_Pipe::execute()
     }
     std::shared_ptr<GeomAPI_Shape> aBaseShape = aBaseObjectSelection->value();
     if(aBaseShape.get() && !aBaseShape->isNull()) {
-      aBaseShape->shapeType() == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
-                                                       aBaseShapesList.push_back(aBaseShape);
+      GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
+      ResultConstructionPtr aConstruction =
+        std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
+      if(aConstruction.get() && !aBaseShape->isEqual(aConstruction->shape()) && aST == GeomAPI_Shape::WIRE) {
+        // It is a wire on the sketch, store it to make face later.
+        aSketchWiresMap[aConstruction].push_back(aBaseShape);
+        continue;
+      } else {
+      aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
+                                   aBaseShapesList.push_back(aBaseShape);
+      }
     } else {
       // This may be the whole sketch result selected, check and get faces.
       ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
@@ -94,8 +114,22 @@ void FeaturesPlugin_Pipe::execute()
     }
   }
 
+  // Make faces from sketch wires.
+  for(std::map<ResultConstructionPtr, ListOfShape>::const_iterator anIt = aSketchWiresMap.cbegin();
+      anIt != aSketchWiresMap.cend(); ++anIt) {
+    const std::shared_ptr<GeomAPI_PlanarEdges> aSketchPlanarEdges =
+      std::dynamic_pointer_cast<GeomAPI_PlanarEdges>((*anIt).first->shape());
+    const ListOfShape& aWiresList = (*anIt).second;
+    ListOfShape aFaces;
+    GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aSketchPlanarEdges->origin(),
+                                               aSketchPlanarEdges->norm(),
+                                               aWiresList,
+                                               aFaces);
+    aBaseFacesList.insert(aBaseFacesList.end(), aFaces.begin(), aFaces.end());
+  }
+
   // Searching faces with common edges.
-  if(aCreationMethod == "simple") {
+  if(aCreationMethod == CREATION_METHOD_SIMPLE()) {
     ListOfShape aShells;
     ListOfShape aFreeFaces;
     std::shared_ptr<GeomAPI_Shape> aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aBaseFacesList);
@@ -124,7 +158,7 @@ void FeaturesPlugin_Pipe::execute()
 
   // Getting Bi-Normal
   std::shared_ptr<GeomAPI_Shape> aBiNormal;
-  if(aCreationMethod == "binormal") {
+  if(aCreationMethod == CREATION_METHOD_BINORMAL()) {
     AttributeSelectionPtr aBiNormalSelection = selection(BINORMAL_ID());
     if(!aBiNormalSelection.get()) {
       setError("Error: Bi-Normal selection is empty.");
@@ -143,7 +177,7 @@ void FeaturesPlugin_Pipe::execute()
 
   // Getting locations.
   ListOfShape aLocations;
-  if(aCreationMethod == "locations") {
+  if(aCreationMethod == CREATION_METHOD_LOCATIONS()) {
     AttributeSelectionListPtr aLocationsSelectionList = selectionList(LOCATIONS_ID());
     if(!aLocationsSelectionList.get()) {
       setError("Error: Could not get locations selection list.");
@@ -170,12 +204,13 @@ void FeaturesPlugin_Pipe::execute()
 
   // Generating result for each object.
   int aResultIndex = 0;
-  if(aCreationMethod == "simple" || aCreationMethod == "binormal") {
+  if(aCreationMethod == CREATION_METHOD_SIMPLE() || aCreationMethod == CREATION_METHOD_BINORMAL()) {
     for(ListOfShape::const_iterator anIter = aBaseShapesList.cbegin(); anIter != aBaseShapesList.cend(); anIter++) {
       std::shared_ptr<GeomAPI_Shape> aBaseShape = *anIter;
 
-      GeomAlgoAPI_Pipe aPipeAlgo = aCreationMethod == "simple" ? GeomAlgoAPI_Pipe(aBaseShape, aPathShape) :
-                                                                 GeomAlgoAPI_Pipe(aBaseShape, aPathShape, aBiNormal);
+      GeomAlgoAPI_Pipe aPipeAlgo = aCreationMethod ==
+        CREATION_METHOD_SIMPLE() ? GeomAlgoAPI_Pipe(aBaseShape, aPathShape) :
+                                   GeomAlgoAPI_Pipe(aBaseShape, aPathShape, aBiNormal);
 
       if(!aPipeAlgo.isDone()) {
         setError("Error: Pipe algorithm failed.");
@@ -197,7 +232,7 @@ void FeaturesPlugin_Pipe::execute()
 
       storeResult(aBaseShape, aPipeAlgo, aResultIndex++);
     }
-  } else if(aCreationMethod == "locations") {
+  } else if(aCreationMethod == CREATION_METHOD_LOCATIONS()) {
     GeomAlgoAPI_Pipe aPipeAlgo = GeomAlgoAPI_Pipe(aBaseShapesList, aLocations, aPathShape);
 
     if(!aPipeAlgo.isDone()) {
@@ -227,7 +262,7 @@ void FeaturesPlugin_Pipe::execute()
   removeResults(aResultIndex);
 }
 
-//=================================================================================================
+//==================================================================================================
 void FeaturesPlugin_Pipe::storeResult(const std::shared_ptr<GeomAPI_Shape> theBaseShape,
                                       GeomAlgoAPI_Pipe& thePipeAlgo,
                                       const int theResultIndex)
@@ -248,7 +283,6 @@ void FeaturesPlugin_Pipe::storeResult(const std::shared_ptr<GeomAPI_Shape> theBa
   switch(aBaseShapeType) {
     case GeomAPI_Shape::VERTEX: {
       aShapeTypeToExplode = GeomAPI_Shape::VERTEX;
-      aGenName += "Edge";
       break;
     }
     case GeomAPI_Shape::EDGE:
@@ -258,16 +292,32 @@ void FeaturesPlugin_Pipe::storeResult(const std::shared_ptr<GeomAPI_Shape> theBa
       ListOfShape aV1History, aV2History;
       thePipeAlgo.generated(aV1, aV1History);
       thePipeAlgo.generated(aV2, aV2History);
-      aResultBody->generated(aV1, aV1History.front(), aGenName + "Edge_1", aGenTag++);
-      aResultBody->generated(aV2, aV2History.front(), aGenName + "Edge_2", aGenTag++);
+      if(!aV1History.empty()) {
+        aResultBody->generated(aV1, aV1History.front(), aGenName + "Edge_1", aGenTag++);
+      }
+      if(!aV2History.empty()) {
+        aResultBody->generated(aV2, aV2History.front(), aGenName + "Edge_2", aGenTag++);
+      }
     }
     case GeomAPI_Shape::FACE:
     case GeomAPI_Shape::SHELL: {
       aShapeTypeToExplode = GeomAPI_Shape::EDGE;
-      aGenName += "Face";
       break;
     }
+    case GeomAPI_Shape::COMPOUND: {
+      aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
+    }
   }
+
+  if(aShapeTypeToExplode == GeomAPI_Shape::VERTEX || aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
+    aResultBody->loadAndOrientGeneratedShapes(&thePipeAlgo, theBaseShape, GeomAPI_Shape::VERTEX,
+                                              aGenTag++, aGenName + "Edge", *aMapOfSubShapes.get());
+  }
+  if(aShapeTypeToExplode == GeomAPI_Shape::EDGE || aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
+    aResultBody->loadAndOrientGeneratedShapes(&thePipeAlgo, theBaseShape, GeomAPI_Shape::EDGE,
+                                              aGenTag++, aGenName + "Face", *aMapOfSubShapes.get());
+  }
+
   aResultBody->loadAndOrientGeneratedShapes(&thePipeAlgo, theBaseShape, aShapeTypeToExplode, aGenTag++, aGenName, *aMapOfSubShapes.get());
 
   // Store from shapes.
@@ -281,7 +331,7 @@ void FeaturesPlugin_Pipe::storeResult(const std::shared_ptr<GeomAPI_Shape> theBa
   setResult(aResultBody, theResultIndex);
 }
 
-//=================================================================================================
+//==================================================================================================
 void FeaturesPlugin_Pipe::storeResult(const ListOfShape& theBaseShapes,
                                       GeomAlgoAPI_Pipe& thePipeAlgo,
                                       const int theResultIndex)
@@ -339,7 +389,7 @@ void FeaturesPlugin_Pipe::storeResult(const ListOfShape& theBaseShapes,
   setResult(aResultBody, theResultIndex);
 }
 
-//=================================================================================================
+//==================================================================================================
 void FeaturesPlugin_Pipe::storeShapes(ResultBodyPtr theResultBody,
                                       const GeomAPI_Shape::ShapeType theBaseShapeType,
                                       const std::shared_ptr<GeomAPI_DataMapOfShapeShape> theMapOfSubShapes,
@@ -367,21 +417,51 @@ void FeaturesPlugin_Pipe::storeShapes(ResultBodyPtr theResultBody,
       aShapeTypeStr = "Face";
       break;
     }
+    case GeomAPI_Shape::COMPOUND: {
+      aShapeTypeToExplore = GeomAPI_Shape::COMPOUND;
+      break;
+    }
   }
 
   // Store shapes.
   int aShapeIndex = 1;
-  std::string aName = theName + aShapeTypeStr;
+  int aFaceIndex = 1;
   for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
-    std::shared_ptr<GeomAPI_Shape> aShape = *anIt;
-    for(GeomAPI_ShapeExplorer anExp(aShape, aShapeTypeToExplore); anExp.more(); anExp.next()) {
-      std::shared_ptr<GeomAPI_Shape> aSubShape = anExp.current();
-      if(theMapOfSubShapes->isBound(aSubShape)) {
-        aSubShape = theMapOfSubShapes->find(aSubShape);
-      }
-      std::ostringstream aStr;
-      aStr << aName << "_" << aShapeIndex++;
-      theResultBody->generated(aSubShape, aStr.str(), theTag++);
+    GeomShapePtr aShape = *anIt;
+
+    if(aShapeTypeToExplore == GeomAPI_Shape::COMPOUND) {
+      std::string aName = theName + (aShape->shapeType() == GeomAPI_Shape::EDGE ? "Edge" : "Face");
+      storeSubShape(theResultBody,
+                    aShape,
+                    aShape->shapeType(),
+                    theMapOfSubShapes,
+                    aName,
+                    aShape->shapeType() == GeomAPI_Shape::EDGE ? aShapeIndex : aFaceIndex,
+                    theTag);
+    } else {
+      std::string aName = theName + aShapeTypeStr;
+      storeSubShape(theResultBody, aShape, aShapeTypeToExplore,
+                    theMapOfSubShapes, aName, aShapeIndex, theTag);
+    }
+  }
+}
+
+//==================================================================================================
+void storeSubShape(ResultBodyPtr theResultBody,
+                   const GeomShapePtr theShape,
+                   const GeomAPI_Shape::ShapeType theType,
+                   const std::shared_ptr<GeomAPI_DataMapOfShapeShape> theMapOfSubShapes,
+                   const std::string theName,
+                   int& theShapeIndex,
+                   int& theTag)
+{
+  for(GeomAPI_ShapeExplorer anExp(theShape, theType); anExp.more(); anExp.next()) {
+    GeomShapePtr aSubShape = anExp.current();
+    if(theMapOfSubShapes->isBound(aSubShape)) {
+      aSubShape = theMapOfSubShapes->find(aSubShape);
     }
+    std::ostringstream aStr;
+    aStr << theName << "_" << theShapeIndex++;
+    theResultBody->generated(aSubShape, aStr.str(), theTag++);
   }
 }