Salome HOME
Task #3230: Sketcher: create a curve passing through selected points or vertices...
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Validators.cpp
index 9c96e9ad224fd8f5893b617358c214ddca4b9560..320315bd2e6064d3469bd4e407d9e2ddca3ac94d 100644 (file)
@@ -31,6 +31,7 @@
 #include "SketchPlugin_Ellipse.h"
 #include "SketchPlugin_EllipticArc.h"
 #include "SketchPlugin_Fillet.h"
+#include "SketchPlugin_CurveFitting.h"
 #include "SketchPlugin_Line.h"
 #include "SketchPlugin_MacroArc.h"
 #include "SketchPlugin_MacroCircle.h"
@@ -49,6 +50,7 @@
 #include <ModelAPI_AttributeDouble.h>
 #include <ModelAPI_AttributeInteger.h>
 #include <ModelAPI_AttributeRefAttr.h>
+#include <ModelAPI_AttributeRefAttrList.h>
 #include <ModelAPI_AttributeRefList.h>
 #include <ModelAPI_AttributeSelectionList.h>
 #include <ModelAPI_AttributeString.h>
@@ -414,6 +416,15 @@ bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute,
 
   for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
     ObjectPtr aSelObject = aSelAttr->object(anInd);
+
+    // B-splines are not supported in Mirror yet
+    FeaturePtr aSelFeature = ModelAPI_Feature::feature(aSelObject);
+    if (aSelFeature && (aSelFeature->getKind() == SketchPlugin_BSpline::ID() ||
+        aSelFeature->getKind() == SketchPlugin_BSplinePeriodic::ID())) {
+      theError = "Not supported";
+      return false;
+    }
+
     std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
     std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
     for (; aMirIter != aMirroredObjects.end(); aMirIter++)
@@ -521,6 +532,15 @@ bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute,
         break;
     if (anObjIter != anInitialObjects.end())
       continue;
+
+    // B-splines are not supported in Copying features
+    FeaturePtr aSelFeature = ModelAPI_Feature::feature(aSelObject);
+    if (aSelFeature && (aSelFeature->getKind() == SketchPlugin_BSpline::ID() ||
+        aSelFeature->getKind() == SketchPlugin_BSplinePeriodic::ID())) {
+      theError = "Not supported";
+      return false;
+    }
+
     anObjIter = aCopiedObjects.begin();
     for (; anObjIter != aCopiedObjects.end(); anObjIter++)
       if (aSelObject == *anObjIter) {
@@ -1782,31 +1802,41 @@ bool SketchPlugin_SketchFeatureValidator::isValid(const AttributePtr& theAttribu
                                                   const std::list<std::string>& theArguments,
                                                   Events_InfoMessage& theError) const
 {
-  if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
+  if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId() &&
+      theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
     theError = "The attribute with the %1 type is not processed";
     theError.arg(theAttribute->attributeType());
     return false;
   }
 
   // check the attribute refers to a sketch feature
+  bool isSketchFeature = false;
   AttributeRefAttrPtr aRefAttr =
       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
-  bool isSketchFeature = aRefAttr->isObject();
-  if (isSketchFeature) {
-    FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
-    isSketchFeature = aFeature.get() != NULL;
+  if (aRefAttr) {
+    isSketchFeature = aRefAttr->isObject();
     if (isSketchFeature) {
-      std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
-          std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
-      isSketchFeature = aSketchFeature.get() != NULL;
+      FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
+      isSketchFeature = aFeature.get() != NULL;
+      if (isSketchFeature) {
+        std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
+            std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
+        isSketchFeature = aSketchFeature.get() != NULL;
+      }
+    }
+  }
+  else {
+    AttributeReferencePtr aReference =
+      std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
+    if (aReference) {
+      FeaturePtr aFeature = ModelAPI_Feature::feature(aReference->value());
+      isSketchFeature = aFeature.get() && aFeature->getKind() == SketchPlugin_Sketch::ID();
     }
   }
 
-  if (isSketchFeature)
-    return true;
-
-  theError = "The object selected is not a sketch feature";
-  return false;
+  if (!isSketchFeature)
+    theError = "The object selected is not a sketch feature";
+  return isSketchFeature;
 }
 
 bool SketchPlugin_MultiRotationAngleValidator::isValid(const AttributePtr& theAttribute,
@@ -1866,3 +1896,22 @@ bool SketchPlugin_BSplineValidator::isValid(const AttributePtr& theAttribute,
 
   return true;
 }
+
+bool SketchPlugin_CurveFittingValidator::isValid(const FeaturePtr& theFeature,
+                                                 const std::list<std::string>& theArguments,
+                                                 Events_InfoMessage& theError) const
+{
+  AttributeRefAttrListPtr aRefAttrList =
+      theFeature->refattrlist(SketchPlugin_CurveFitting::POINTS_ID());
+  AttributeBooleanPtr aPeriodicAttr =
+      theFeature->boolean(SketchPlugin_CurveFitting::PERIODIC_ID());
+
+  // check number of selected entities
+  int aMinNbPoints = aPeriodicAttr->value() ? 3 : 2;
+  if (aRefAttrList->size() < aMinNbPoints) {
+    theError = "Not enough points selected. Need at least %1 points.";
+    theError.arg(aMinNbPoints);
+    return false;
+  }
+  return true;
+}