Salome HOME
Validators return InfoMessage instead of string as an error
[modules/shaper.git] / src / GeomValidators / GeomValidators_Different.cpp
index dcad93164ad65ecc2a69534b01aaf1b44aef0fd8..617161f9c1624faad28e75b0ceaab38191e0c956 100644 (file)
@@ -6,12 +6,17 @@
 
 #include <GeomValidators_Different.h>
 
+#include <Events_InfoMessage.h>
+
 #include <GeomDataAPI_Point2D.h>
+#include <GeomAPI_Pnt2d.h>
 
 #include <algorithm>
 #include <map>
 #include <list>
 
+const double tolerance = 1e-7;
+
 //=================================================================================================
 /* Help
 To extend GeomValidators_Different validator with new attribute types:
@@ -22,8 +27,7 @@ To extend GeomValidators_Different validator with new attribute types:
 
 bool isEqual(const AttributePoint2DPtr& theLeft, const AttributePoint2DPtr& theRight)
 {
-  return theLeft->x() == theRight->x() &&
-         theLeft->y() == theRight->y();
+  return theLeft->pnt()->distance(theRight->pnt()) < tolerance;
 }
 
 bool isEqualAttributes(const AttributePtr& theLeft, const AttributePtr& theRight)
@@ -35,10 +39,16 @@ bool isEqualAttributes(const AttributePtr& theLeft, const AttributePtr& theRight
   return false;
 }
 
+/** \class IsEqual
+ *  \ingroup Validators
+ *  \brief Auxiliary class used in std::find_if
+ */
 class IsEqual {
   AttributePtr myAttribute;
 public:
+  /// Constructor
   IsEqual(const AttributePtr& theAttribute) : myAttribute(theAttribute) {}
+  /// \return true in case if AttributePtr is equal with myAttribute
   bool operator()(const AttributePtr& theAttribute) {
     return isEqualAttributes(myAttribute, theAttribute);
   }
@@ -46,7 +56,7 @@ public:
 
 bool GeomValidators_Different::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
                                        const std::list<std::string>& theArguments,
-                                       std::string& theError) const
+                                       Events_InfoMessage& theError) const
 {
   std::map<std::string, std::list<AttributePtr> > anAttributesMap;
   // For all attributes referred by theArguments 
@@ -67,8 +77,12 @@ bool GeomValidators_Different::isValid(const std::shared_ptr<ModelAPI_Feature>&
       std::list<AttributePtr>::const_iterator aNextIt = anAttributeIt; ++aNextIt;
       while (aNextIt != anAttributes.end()) {
         // if equal attribute is found then all attributes are not different
-        if (std::find_if(aNextIt, anAttributes.end(), IsEqual(*anAttributeIt)) != anAttributes.end()) 
+        std::list<AttributePtr>::const_iterator aFindIt =
+            std::find_if(aNextIt, anAttributes.end(), IsEqual(*anAttributeIt));
+        if (aFindIt != anAttributes.end()) {
+          theError = "Attributes " + (*anAttributeIt)->id() + " and " + (*aFindIt)->id() + " are equal." ;
           return false;
+        }
         ++anAttributeIt;
         ++aNextIt; 
       }