Salome HOME
ModuleBase_ViewerPrs is wrapped into shared_ptr: remove include of this class from...
[modules/shaper.git] / src / PartSet / PartSet_Validators.cpp
index 9b4aa471db355d9369f04e6c83e2a6526a7b91be..38ef8be3a061a371ceb08401c4dbbd656a46a3b3 100755 (executable)
@@ -17,6 +17,7 @@
 #include <ModuleBase_ISelection.h>
 #include <ModuleBase_WidgetShapeSelector.h>
 #include <ModuleBase_OperationFeature.h>
+#include <ModuleBase_ViewerPrs.h>
 
 #include <ModelAPI_AttributeRefAttr.h>
 #include <ModelAPI_AttributeSelection.h>
 
 int shapesNbPoints(const ModuleBase_ISelection* theSelection)
 {
-  QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
+  QList<ModuleBase_ViewerPrsPtr> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
   ModuleBase_ISelection::filterSelectionOnEqualPoints(aList);
 
   int aCount = 0;
-  foreach (ModuleBase_ViewerPrs aPrs, aList) {
-    const TopoDS_Shape& aShape = aPrs.shape();
-    if (!aShape.IsNull()) {
-      if (aShape.ShapeType() == TopAbs_VERTEX)
+  foreach (ModuleBase_ViewerPrsPtr aPrs, aList) {
+    const GeomShapePtr& aShape = aPrs->shape();
+    if (aShape.get() && !aShape->isNull()) {
+      if (aShape->shapeType() == GeomAPI_Shape::VERTEX)
         aCount++;
     }
   }
@@ -55,13 +56,14 @@ int shapesNbPoints(const ModuleBase_ISelection* theSelection)
 
 int shapesNbLines(const ModuleBase_ISelection* theSelection)
 {
-  QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
+  QList<ModuleBase_ViewerPrsPtr> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
   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);
+  foreach(ModuleBase_ViewerPrsPtr aPrs, aList) {
+    const GeomShapePtr& aShape = aPrs->shape();
+    if (aShape.get() && !aShape->isNull()) {
+      if (aShape->shapeType() == GeomAPI_Shape::EDGE) {
+        const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
+        TopoDS_Edge aEdge = TopoDS::Edge(aTDShape);
         Standard_Real aStart, aEnd;
         Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
         GeomAdaptor_Curve aAdaptor(aCurve);
@@ -155,14 +157,14 @@ bool PartSet_RadiusSelection::isValid(const ModuleBase_ISelection* theSelection,
   if (theSelection->getSelected(ModuleBase_ISelection::Viewer).size() == 0) {
     return isEmptySelectionValid(theOperation);
   } else {
-    QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
-    ModuleBase_ViewerPrs aPrs;
+    QList<ModuleBase_ViewerPrsPtr> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
     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);
+    foreach (ModuleBase_ViewerPrsPtr aPrs, aList) {
+      const GeomShapePtr& aShape = aPrs->shape();
+      if (aShape.get() && !aShape->isNull()) {
+        if (aShape->shapeType() == GeomAPI_Shape::EDGE) {
+          const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
+          TopoDS_Edge aEdge = TopoDS::Edge(aTDShape);
           Standard_Real aStart, aEnd;
           Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
           GeomAdaptor_Curve aAdaptor(aCurve);
@@ -180,7 +182,7 @@ bool PartSet_RigidSelection::isValid(const ModuleBase_ISelection* theSelection,
   if (theSelection->getSelected(ModuleBase_ISelection::Viewer).size() == 0) {
     return isEmptySelectionValid(theOperation);
   } else {
-    QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
+    QList<ModuleBase_ViewerPrsPtr> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
     return (aList.count() == 1);
   }
 }
@@ -213,8 +215,8 @@ bool PartSet_FilletSelection::isValid(const ModuleBase_ISelection* theSelection,
   if (theSelection->getSelected(ModuleBase_ISelection::Viewer).size() == 0) {
     return isEmptySelectionValid(theOperation);
   } else {
-    int aCount = shapesNbLines(theSelection);
-    return (aCount > 0) && (aCount < 3);
+    int aCount = shapesNbPoints(theSelection);
+    return aCount == 1;
   }
 }
 
@@ -223,36 +225,25 @@ bool PartSet_TangentSelection::isValid(const ModuleBase_ISelection* theSelection
   if (theSelection->getSelected(ModuleBase_ISelection::Viewer).size() == 0) {
     return isEmptySelectionValid(theOperation);
   } else {
-    QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
+    QList<ModuleBase_ViewerPrsPtr> 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)
+    ModuleBase_ViewerPrsPtr aPrs = aList.first();
+    const GeomShapePtr& aShape = aPrs->shape();
+    if (!aShape.get() || aShape->isNull() || aShape->shapeType() != GeomAPI_Shape::EDGE)
       return false;
-
-    std::shared_ptr<GeomAPI_Shape> aShapePtr(new GeomAPI_Shape);
-    aShapePtr->setImpl(new TopoDS_Shape(aShape));
-    GeomAPI_Edge aEdge1(aShapePtr);
+    GeomAPI_Edge aEdge1(aShape);
 
     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)
+        const GeomShapePtr& aShape2 = aPrs->shape();
+        if (!aShape2.get() || aShape2->isNull() || aShape2->shapeType() != GeomAPI_Shape::EDGE)
           return false;
+        GeomAPI_Edge aEdge2(aShape2);
 
-        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())
@@ -281,14 +272,12 @@ bool PartSet_EqualSelection::isValid(const ModuleBase_ISelection* theSelection,
   if (theSelection->getSelected(ModuleBase_ISelection::Viewer).size() == 0) {
     return isEmptySelectionValid(theOperation);
   } else {
-    QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
-    ModuleBase_ViewerPrs aPrs;
+    QList<ModuleBase_ViewerPrsPtr> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
     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()) {
+    foreach (ModuleBase_ViewerPrsPtr aPrs, aList) {
+      GeomShapePtr aShape = aPrs->shape();
+      if (aShape.get() && aShape->isEdge()) {
         aCount++;
         GeomAPI_Edge aEdge(aShape);
         if (aEdge.isLine()) {
@@ -314,6 +303,24 @@ bool PartSet_EqualSelection::isValid(const ModuleBase_ISelection* theSelection,
   }
 }
 
+bool PartSet_CollinearSelection::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_MiddlePointSelection::isValid(const ModuleBase_ISelection* theSelection, ModuleBase_Operation* theOperation) const
+{
+  if (theSelection->getSelected(ModuleBase_ISelection::Viewer).size() == 0)
+    return isEmptySelectionValid(theOperation);
+  else
+    return shapesNbLines(theSelection) == 1 && shapesNbPoints(theSelection) == 1;
+}
+
 
 std::string PartSet_DifferentObjectsValidator::errorMessage(
                          const PartSet_DifferentObjectsValidator::ErrorType& theType,
@@ -515,79 +522,6 @@ bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute
   return true;
 }
 
-bool PartSet_SketchEntityValidator::isValid(const AttributePtr& theAttribute,
-                                            const std::list<std::string>& theArguments,
-                                            std::string& theError) const
-{
-  bool isSketchEntities = true;
-  std::set<std::string> anEntityKinds;
-  std::string anEntityKindsStr;
-  std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
-  for (; anIt != aLast; anIt++) {
-    anEntityKinds.insert(*anIt);
-    if (!anEntityKindsStr.empty())
-      anEntityKindsStr += ", ";
-    anEntityKindsStr += *anIt;
-  }
-
-  std::string anAttributeType = theAttribute->attributeType();
-  if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
-    AttributeSelectionListPtr aSelectionListAttr = 
-                      std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
-    // all context objects should be sketch entities
-    for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize && isSketchEntities; i++) {
-      AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
-      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);
-    // all context objects should be sketch entities
-    for (int i = 0, aSize = aRefListAttr->size(); i < aSize && isSketchEntities; i++) {
-      ObjectPtr anObject = aRefListAttr->object(i);
-      // 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_AttributeRefAttr::typeId()) {
-    std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
-                     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
-    isSketchEntities = false;
-    if (aRef->isObject()) {
-      ObjectPtr anObject = aRef->object();
-      if (anObject.get() != NULL) {
-        FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
-        if (aFeature.get() != NULL)
-          isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
-      }
-    }
-  }
-  if (!isSketchEntities) {
-    theError = "It refers to feature, which kind is not in the list: " + anEntityKindsStr;
-  }
-
-  return isSketchEntities;
-}
-
 bool PartSet_CoincidentAttr::isValid(const AttributePtr& theAttribute, 
                                      const std::list<std::string>& theArguments,
                                      std::string& theError) const
@@ -605,6 +539,7 @@ bool PartSet_CoincidentAttr::isValid(const AttributePtr& theAttribute,
   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
   QList<FeaturePtr> aCoinsideLines;
+  QList<FeaturePtr> aCoins;
 
   bool isObject = aRefAttr->isObject();
   ObjectPtr anObject = aRefAttr->object();
@@ -630,9 +565,9 @@ bool PartSet_CoincidentAttr::isValid(const AttributePtr& theAttribute,
         AttributePtr aAR = aRAttr->attr();
         if (aAR->id() != SketchPlugin_Arc::CENTER_ID()) // ignore constraint to center of arc
           aCoinList.insert(aConstrFeature);
-          PartSet_Tools::findCoincidences(aConstrFeature, aCoinsideLines,
+          PartSet_Tools::findCoincidences(aConstrFeature, aCoinsideLines, aCoins,
                                           SketchPlugin_ConstraintCoincidence::ENTITY_A());
-          PartSet_Tools::findCoincidences(aConstrFeature, aCoinsideLines,
+          PartSet_Tools::findCoincidences(aConstrFeature, aCoinsideLines, aCoins,
                                           SketchPlugin_ConstraintCoincidence::ENTITY_B());
       }
     }