Salome HOME
Merge branch 'master' into V9_3_BR
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_Validators.cpp
index e0268ef42900091d5a5a60d3fbc02b004d1ec41e..4c7c8a4fc8afbbceb1fb1f1b6a1820c95730cc1f 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2019  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 "BuildPlugin_Validators.h"
@@ -26,6 +25,7 @@
 #include <GeomAPI_PlanarEdges.h>
 #include <GeomAPI_Pln.h>
 #include <GeomAPI_ShapeExplorer.h>
+#include <GeomAPI_ShapeIterator.h>
 
 #include <GeomAlgoAPI_CompoundBuilder.h>
 #include <GeomAlgoAPI_PaveFiller.h>
@@ -33,6 +33,8 @@
 #include <GeomAlgoAPI_ShapeTools.h>
 #include <GeomAlgoAPI_SketchBuilder.h>
 #include <GeomAlgoAPI_WireBuilder.h>
+#include <GeomAlgoAPI_MakeVolume.h>
+#include <GeomAlgoAPI_Tools.h>
 
 #include <GeomValidators_FeatureKind.h>
 #include <GeomValidators_ShapeType.h>
@@ -112,12 +114,6 @@ bool BuildPlugin_ValidatorBaseForBuild::isValid(const AttributePtr& theAttribute
         continue;
       }
     }
-
-    if(!aShape->isEqual(aContextShape)) {
-      // Local selection on body does not allowed.
-      theError = "Selected shape is in the local selection. Only global selection is allowed.";
-      return false;
-    }
   }
 
   return true;
@@ -166,13 +162,6 @@ bool BuildPlugin_ValidatorBaseForWire::isValid(const std::shared_ptr<ModelAPI_Fe
   return true;
 }
 
-//=================================================================================================
-bool BuildPlugin_ValidatorBaseForWire::isNotObligatory(std::string theFeature,
-                                                       std::string theAttribute)
-{
-  return false;
-}
-
 //=================================================================================================
 bool BuildPlugin_ValidatorBaseForFace::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
                                                const std::list<std::string>& theArguments,
@@ -192,6 +181,9 @@ bool BuildPlugin_ValidatorBaseForFace::isValid(const std::shared_ptr<ModelAPI_Fe
     return false;
   }
 
+  bool hasEdgesOrWires = false;
+  bool hasFaces = false;
+
   // Collect base shapes.
   ListOfShape anEdges;
   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
@@ -204,13 +196,23 @@ bool BuildPlugin_ValidatorBaseForFace::isValid(const std::shared_ptr<ModelAPI_Fe
       }
       aShape = aSelection->context()->shape();
     }
+    if (aShape->shapeType() == GeomAPI_Shape::FACE) {
+      // skip faces exploding
+      hasFaces = true;
+      continue;
+    }
+
     for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
+      hasEdgesOrWires = true;
       GeomShapePtr anEdge = anExp.current();
       anEdges.push_back(anEdge);
     }
   }
 
-  if(anEdges.empty()) {
+  if (hasFaces && hasEdgesOrWires) {
+    theError = "Faces and edges/wires should be selected together.";
+    return false;
+  } else if (hasEdgesOrWires && anEdges.empty()) {
     theError = "Objects are not selected.";
     return false;
   }
@@ -235,32 +237,106 @@ bool BuildPlugin_ValidatorBaseForFace::isValid(const std::shared_ptr<ModelAPI_Fe
     }
   }
 
-  // Check that they are planar.
-  std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
-  if(!aPln.get()) {
-    theError = "Selected object(s) should belong to only one plane.";
-    return false;
-  }
+  if (!anEdges.empty()) {
+    // Check that they are planar.
+    std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
+    if(!aPln.get()) {
+      theError = "Selected object(s) should belong to only one plane.";
+      return false;
+    }
 
-  // Check that selected objects have closed contours.
-  ListOfShape aFaces;
-  GeomAlgoAPI_SketchBuilder::createFaces(aPln->location(), aPln->xDirection(),
-                                         aPln->direction(), anEdges, aFaces);
-  if(aFaces.empty()) {
-    theError = "Selected objects do not generate closed contour.";
-    return false;
+    // Check that selected objects have closed contours.
+    GeomAlgoAPI_SketchBuilder aBuilder(aPln, anEdges);
+    const ListOfShape& aFaces = aBuilder.faces();
+    if(aFaces.empty()) {
+      theError = "Selected objects do not generate closed contour.";
+      return false;
+    }
   }
 
   return true;
 }
 
 //=================================================================================================
-bool BuildPlugin_ValidatorBaseForFace::isNotObligatory(std::string theFeature,
-                                                       std::string theAttribute)
+bool BuildPlugin_ValidatorBaseForSolids::isValid(
+  const std::shared_ptr<ModelAPI_Feature>& theFeature, const std::list<std::string>& theArguments,
+  Events_InfoMessage& theError) const
 {
-  return false;
+  // Get base objects list.
+  AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
+  if (!aSelectionList.get()) {
+    theError = "Could not get selection list.";
+    return false;
+  }
+  if (aSelectionList->size() == 0) {
+    theError = "Empty selection list.";
+    return false;
+  }
+
+  // Collect base shapes.
+  ListOfShape anOriginalShapes;
+  for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
+    AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
+    if (!aSelection->context().get()) {
+      theError = "Invalid selection.";
+      return false;
+    }
+    GeomShapePtr aShape = aSelection->value();
+    if (!aShape.get())
+      aShape = aSelection->context()->shape();
+    anOriginalShapes.push_back(aShape);
+  }
+
+  std::shared_ptr<GeomAlgoAPI_MakeVolume> anAlgorithm(
+    new GeomAlgoAPI_MakeVolume(anOriginalShapes, false));
+
+  std::string anErr;
+  if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(anAlgorithm, "MakeVolume", anErr)) {
+    theError = anErr;
+    return false;
+  }
+
+  // set of allowed types of results
+  std::set<GeomAPI_Shape::ShapeType> aResultType;
+  std::string aType = theArguments.back();
+  if (aType == "solid")
+    aResultType.insert(GeomAPI_Shape::SOLID);
+  else if (aType == "compsolid") {
+    aResultType.insert(GeomAPI_Shape::COMPSOLID);
+    aResultType.insert(GeomAPI_Shape::SOLID);
+  }
+
+  GeomShapePtr aCompound = anAlgorithm->shape();
+  if (aCompound->shapeType() == GeomAPI_Shape::COMPOUND) {
+    GeomAPI_ShapeIterator anIt(aCompound);
+    GeomShapePtr aFoundSub;
+    for (; anIt.more() && !aFoundSub; anIt.next()) {
+      aFoundSub = anIt.current();
+      if (aResultType.count(aFoundSub->shapeType()) == 0) {
+        theError = "Unable to build a solid";
+        return false;
+      }
+    }
+    if (anIt.more() || !aFoundSub.get()) {
+      theError = "Unable to build a solid";
+      return false;
+    }
+  } else if (aResultType.count(aCompound->shapeType()) == 0) {
+    theError = "Unable to build a solid";
+    return false;
+  }
+  // check the internal faces presence
+  for(GeomAPI_ShapeExplorer aFaces(aCompound, GeomAPI_Shape::FACE); aFaces.more(); aFaces.next()) {
+    if (aFaces.current()->orientation() == GeomAPI_Shape::INTERNAL) {
+      theError = "Internal faces are not allowed in the resulting solid";
+      return false;
+    }
+  }
+
+  return true;
 }
 
+
 //=================================================================================================
 bool BuildPlugin_ValidatorSubShapesSelection::isValid(const AttributePtr& theAttribute,
                                                       const std::list<std::string>& theArguments,