Salome HOME
Crash in sketch on external objects use. Scenario:
[modules/shaper.git] / src / PartSet / PartSet_Validators.cpp
index 99468bcf043962bd3b78a489d98eb62eb1579ef1..b446bab1642528b4922b8ce7e84956f677309445 100755 (executable)
@@ -7,15 +7,16 @@
 #include "PartSet_Validators.h"
 
 #include "PartSet_Tools.h"
+#include "PartSet_SketcherMgr.h"
 
 #include <TopoDS.hxx>
 #include <TopoDS_Edge.hxx>
 #include <BRep_Tool.hxx>
 #include <GeomAdaptor_Curve.hxx>
 #include <GeomAbs_CurveType.hxx>
-#include <GeomValidators_Tools.h>
 #include <ModuleBase_ISelection.h>
 #include <ModuleBase_WidgetShapeSelector.h>
+#include <ModuleBase_OperationFeature.h>
 
 #include <ModelAPI_AttributeRefAttr.h>
 #include <ModelAPI_AttributeSelection.h>
@@ -39,7 +40,7 @@
 int shapesNbPoints(const ModuleBase_ISelection* theSelection)
 {
   QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
-  ModuleBase_ISelection::filterPreselectionOnEqualPoints(aList);
+  ModuleBase_ISelection::filterSelectionOnEqualPoints(aList);
 
   int aCount = 0;
   foreach (ModuleBase_ViewerPrs aPrs, aList) {
@@ -72,128 +73,248 @@ int shapesNbLines(const ModuleBase_ISelection* theSelection)
   return aCount;
 }
 
-bool PartSet_DistanceSelection::isValid(const ModuleBase_ISelection* theSelection) const
+
+std::shared_ptr<GeomAPI_Pln> sketcherPlane(ModuleBase_Operation* theOperation)
 {
-  int aCount = shapesNbPoints(theSelection) + shapesNbLines(theSelection);
-  return (aCount > 0) && (aCount < 3);
+  std::shared_ptr<GeomAPI_Pln> aEmptyPln;
+  if (theOperation) {
+    ModuleBase_OperationFeature* aFeatureOp = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
+    if (aFeatureOp) {
+      CompositeFeaturePtr aFeature = 
+        std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeatureOp->feature());
+      if (aFeature && (aFeature->getKind() == SketchPlugin_Sketch::ID()))
+        return PartSet_Tools::sketchPlane(aFeature);
+    }
+  }
+  return aEmptyPln; 
 }
 
-bool PartSet_LengthSelection::isValid(const ModuleBase_ISelection* theSelection) const
+
+bool isEmptySelectionValid(ModuleBase_Operation* theOperation)
 {
-  int aCount = shapesNbLines(theSelection);
-  return (aCount == 1);
+  ModuleBase_OperationFeature* aFeatureOp = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
+  // during the create operation empty selection is always valid
+  if (!aFeatureOp->isEditOperation()) {
+    return true;
+  }
+  else {
+    if (PartSet_SketcherMgr::isSketchOperation(aFeatureOp)) {
+      std::shared_ptr<GeomAPI_Pln> aPlane = sketcherPlane(theOperation);
+      if (aPlane.get())
+        return true;
+      else 
+        return false;
+    }
+    else// in edit operation an empty selection is always valid, performed for re-entrant operrations
+      return true;
+  }
 }
 
-bool PartSet_PerpendicularSelection::isValid(const ModuleBase_ISelection* theSelection) const
+bool PartSet_DistanceSelection::isValid(const ModuleBase_ISelection* theSelection, ModuleBase_Operation* theOperation) const
 {
-  int aCount = shapesNbLines(theSelection);
-  return (aCount > 0) && (aCount < 3);
+  if (theSelection->getSelected(ModuleBase_ISelection::Viewer).size() == 0) {
+    return isEmptySelectionValid(theOperation);
+  } else {
+    int aCount = shapesNbPoints(theSelection) + shapesNbLines(theSelection);
+    return (aCount > 0) && (aCount < 3);
+  }
 }
 
-bool PartSet_ParallelSelection::isValid(const ModuleBase_ISelection* theSelection) const
+bool PartSet_LengthSelection::isValid(const ModuleBase_ISelection* theSelection, ModuleBase_Operation* theOperation) const
 {
-  int aCount = shapesNbLines(theSelection);
-  return (aCount > 0) && (aCount < 3);
+  if (theSelection->getSelected(ModuleBase_ISelection::Viewer).size() == 0) {
+    return isEmptySelectionValid(theOperation);
+  } else {
+    int aCount = shapesNbLines(theSelection);
+    return (aCount == 1);
+  }
 }
 
-bool PartSet_RadiusSelection::isValid(const ModuleBase_ISelection* theSelection) const
+bool PartSet_PerpendicularSelection::isValid(const ModuleBase_ISelection* theSelection, ModuleBase_Operation* theOperation) const
 {
-  QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
-  ModuleBase_ViewerPrs aPrs;
-  int aCount = 0;
-  foreach (ModuleBase_ViewerPrs aPrs, aList) {
-    const TopoDS_Shape& aShape = aPrs.shape();
-    if (!aShape.IsNull()) {
-      if (aShape.ShapeType() == TopAbs_EDGE) {
-        TopoDS_Edge aEdge = TopoDS::Edge(aShape);
-        Standard_Real aStart, aEnd;
-        Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
-        GeomAdaptor_Curve aAdaptor(aCurve);
-        if (aAdaptor.GetType() == GeomAbs_Circle)
-          aCount++;
+  if (theSelection->getSelected(ModuleBase_ISelection::Viewer).size() == 0) {
+    return isEmptySelectionValid(theOperation);
+  } else {
+    int aCount = shapesNbLines(theSelection);
+    return (aCount > 0) && (aCount < 3);
+  }
+}
+
+bool PartSet_ParallelSelection::isValid(const ModuleBase_ISelection* theSelection, ModuleBase_Operation* theOperation) const
+{
+  if (theSelection->getSelected(ModuleBase_ISelection::Viewer).size() == 0) {
+    return isEmptySelectionValid(theOperation);
+  } else {
+    int aCount = shapesNbLines(theSelection);
+    return (aCount > 0) && (aCount < 3);
+  }
+}
+
+bool PartSet_RadiusSelection::isValid(const ModuleBase_ISelection* theSelection, ModuleBase_Operation* theOperation) const
+{
+  if (theSelection->getSelected(ModuleBase_ISelection::Viewer).size() == 0) {
+    return isEmptySelectionValid(theOperation);
+  } else {
+    QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
+    ModuleBase_ViewerPrs aPrs;
+    int aCount = 0;
+    foreach (ModuleBase_ViewerPrs aPrs, aList) {
+      const TopoDS_Shape& aShape = aPrs.shape();
+      if (!aShape.IsNull()) {
+        if (aShape.ShapeType() == TopAbs_EDGE) {
+          TopoDS_Edge aEdge = TopoDS::Edge(aShape);
+          Standard_Real aStart, aEnd;
+          Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
+          GeomAdaptor_Curve aAdaptor(aCurve);
+          if (aAdaptor.GetType() == GeomAbs_Circle)
+            aCount++;
+        }
       }
     }
+    return (aCount == 1);
   }
-  return (aCount == 1);
 }
 
-bool PartSet_RigidSelection::isValid(const ModuleBase_ISelection* theSelection) const
+bool PartSet_RigidSelection::isValid(const ModuleBase_ISelection* theSelection, ModuleBase_Operation* theOperation) const
 {
-  QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
-  return (aList.count() == 1);
+  if (theSelection->getSelected(ModuleBase_ISelection::Viewer).size() == 0) {
+    return isEmptySelectionValid(theOperation);
+  } else {
+    QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
+    return (aList.count() == 1);
+  }
 }
 
 
-bool PartSet_CoincidentSelection::isValid(const ModuleBase_ISelection* theSelection) const
+bool PartSet_CoincidentSelection::isValid(const ModuleBase_ISelection* theSelection, ModuleBase_Operation* theOperation) const
 {
-  // Coincident can be applied to points and to lines
-  int aCount = shapesNbPoints(theSelection);
-  aCount += shapesNbLines(theSelection);
-  return (aCount > 0) && (aCount < 3);
+  if (theSelection->getSelected(ModuleBase_ISelection::Viewer).size() == 0) {
+    return isEmptySelectionValid(theOperation);
+  } else {
+    // Coincident can be applied to points and to lines
+    int aCount = shapesNbPoints(theSelection);
+    aCount += shapesNbLines(theSelection);
+    return (aCount > 0) && (aCount < 3);
+  }
 }
 
-bool PartSet_HVDirSelection::isValid(const ModuleBase_ISelection* theSelection) const
+bool PartSet_HVDirSelection::isValid(const ModuleBase_ISelection* theSelection, ModuleBase_Operation* theOperation) const
 {
-  int aCount = shapesNbLines(theSelection);
-  return (aCount == 1);
+  if (theSelection->getSelected(ModuleBase_ISelection::Viewer).size() == 0) {
+    return isEmptySelectionValid(theOperation);
+  } else {
+    int aCount = shapesNbLines(theSelection);
+    return (aCount == 1);
+  }
 }
 
-bool PartSet_FilletSelection::isValid(const ModuleBase_ISelection* theSelection) const
+bool PartSet_FilletSelection::isValid(const ModuleBase_ISelection* theSelection, ModuleBase_Operation* theOperation) const
 {
-  int aCount = shapesNbLines(theSelection);
-  return (aCount > 0) && (aCount < 3);
+  if (theSelection->getSelected(ModuleBase_ISelection::Viewer).size() == 0) {
+    return isEmptySelectionValid(theOperation);
+  } else {
+    int aCount = shapesNbPoints(theSelection);
+    return aCount == 1;
+  }
 }
 
-bool PartSet_TangentSelection::isValid(const ModuleBase_ISelection* theSelection) const
+bool PartSet_TangentSelection::isValid(const ModuleBase_ISelection* theSelection, ModuleBase_Operation* theOperation) const
 {
-  QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
-  if ((aList.size() == 0) || (aList.size() > 2))
-    return false;
-
-  ModuleBase_ViewerPrs aPrs = aList.first();
-  const TopoDS_Shape& aShape = aPrs.shape();
-  if (aShape.IsNull())
-    return false;
-
-  if (aShape.ShapeType() != TopAbs_EDGE)
+  if (theSelection->getSelected(ModuleBase_ISelection::Viewer).size() == 0) {
+    return isEmptySelectionValid(theOperation);
+  } else {
+    QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
+    if ((aList.size() == 0) || (aList.size() > 2))
+      return false;
+
+    ModuleBase_ViewerPrs aPrs = aList.first();
+    const TopoDS_Shape& aShape = aPrs.shape();
+    if (aShape.IsNull())
+      return false;
+
+    if (aShape.ShapeType() != TopAbs_EDGE)
+      return false;
+
+    std::shared_ptr<GeomAPI_Shape> aShapePtr(new GeomAPI_Shape);
+    aShapePtr->setImpl(new TopoDS_Shape(aShape));
+    GeomAPI_Edge aEdge1(aShapePtr);
+
+    if (aEdge1.isLine() || aEdge1.isArc()) {
+      if (aList.size() == 2) {
+        // Check second selection
+        aPrs = aList.last();
+        const TopoDS_Shape& aShape2 = aPrs.shape();
+        if (aShape2.IsNull())
+          return false;
+
+        if (aShape2.ShapeType() != TopAbs_EDGE)
+          return false;
+
+        std::shared_ptr<GeomAPI_Shape> aShapePtr2(new GeomAPI_Shape);
+        aShapePtr2->setImpl(new TopoDS_Shape(aShape2));
+        GeomAPI_Edge aEdge2(aShapePtr2);
+        if (aEdge1.isLine() && aEdge2.isArc())
+          return true;
+        else if (aEdge1.isArc() && aEdge2.isLine())
+          return true;
+        else
+          return false;
+      } else
+        return true;
+    }
     return false;
+  }
+}
 
-  std::shared_ptr<GeomAPI_Shape> aShapePtr(new GeomAPI_Shape);
-  aShapePtr->setImpl(new TopoDS_Shape(aShape));
-  GeomAPI_Edge aEdge1(aShapePtr);
-
-  if (aEdge1.isLine() || aEdge1.isArc()) {
-    if (aList.size() == 2) {
-      // Check second selection
-      aPrs = aList.last();
-      const TopoDS_Shape& aShape2 = aPrs.shape();
-      if (aShape2.IsNull())
-        return false;
-
-      if (aShape2.ShapeType() != TopAbs_EDGE)
-        return false;
-
-      std::shared_ptr<GeomAPI_Shape> aShapePtr2(new GeomAPI_Shape);
-      aShapePtr2->setImpl(new TopoDS_Shape(aShape2));
-      GeomAPI_Edge aEdge2(aShapePtr2);
-      if (aEdge1.isLine() && aEdge2.isArc())
-        return true;
-      else if (aEdge1.isArc() && aEdge2.isLine())
-        return true;
-      else
-        return false;
-    } else
-      return true;
+bool PartSet_AngleSelection::isValid(const ModuleBase_ISelection* theSelection, ModuleBase_Operation* theOperation) const
+{
+  if (theSelection->getSelected(ModuleBase_ISelection::Viewer).size() == 0) {
+    return isEmptySelectionValid(theOperation);
+  } else {
+    int aCount = shapesNbLines(theSelection);
+    return (aCount > 0) && (aCount < 3);
   }
-  return false;
 }
 
-bool PartSet_AngleSelection::isValid(const ModuleBase_ISelection* theSelection) const
+bool PartSet_EqualSelection::isValid(const ModuleBase_ISelection* theSelection, ModuleBase_Operation* theOperation) const
 {
-  int aCount = shapesNbLines(theSelection);
-  return (aCount > 0) && (aCount < 3);
+  if (theSelection->getSelected(ModuleBase_ISelection::Viewer).size() == 0) {
+    return isEmptySelectionValid(theOperation);
+  } else {
+    QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
+    ModuleBase_ViewerPrs aPrs;
+    int aCount = 0;
+    int aType = 0;
+    foreach (ModuleBase_ViewerPrs aPrs, aList) {
+      std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
+      aShape->setImpl(new TopoDS_Shape(aPrs.shape()));
+      if (aShape->isEdge()) {
+        aCount++;
+        GeomAPI_Edge aEdge(aShape);
+        if (aEdge.isLine()) {
+          if (aCount == 1)
+            aType = 1;
+          else if (aType != 1)
+            return false;
+        } else if (aEdge.isCircle()) {
+          if (aCount == 1)
+            aType = 2;
+          else if (aType != 2)
+            return false;
+        } else if (aEdge.isArc()) {
+          if (aCount == 1)
+            aType = 3;
+          else if (aType != 3)
+            return false;
+        }
+      } else
+        return false;
+    }
+    return (aCount > 0) && (aCount < 3);
+  }
 }
 
+
 std::string PartSet_DifferentObjectsValidator::errorMessage(
                          const PartSet_DifferentObjectsValidator::ErrorType& theType,
                          const std::string& thEqualObject, const std::string& theFirstAttribute,
@@ -429,6 +550,21 @@ bool PartSet_SketchEntityValidator::isValid(const AttributePtr& theAttribute,
       }
     }
   }
+  if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
+    AttributeSelectionPtr aSelectAttr = 
+                      std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
+    ObjectPtr anObject = aSelectAttr->context();
+    // a context of the selection attribute is a feature result. It can be a case when the result
+    // of the feature is null, e.g. the feature is modified and has not been executed yet.
+    // The validator returns an invalid result here. The case is an extrusion built on a sketch
+    // feature. A new sketch element creation leads to an empty result.
+    if (!anObject.get())
+      isSketchEntities = false;
+    else {
+      FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
+      isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
+    }
+  }
   if (anAttributeType == ModelAPI_AttributeRefList::typeId()) {
     AttributeRefListPtr aRefListAttr =
       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);