Salome HOME
Copyright update 2022
[modules/shaper.git] / src / GeomValidators / GeomValidators_Different.cpp
index 65b834d3af46624a5e0da5791b1e705581659e56..a2d625543df58c88f084e76ce480dba997a5bf45 100644 (file)
@@ -1,17 +1,35 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-// File:        GeomValidators_Different.cpp
-// Created:     08 July 2015
-// Author:      Sergey POKHODENKO
+// Copyright (C) 2014-2022  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
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// 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
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 #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,34 +40,41 @@ 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->isInitialized() && theRight->isInitialized() &&
+    theLeft->pnt()->distance(theRight->pnt()) < tolerance;
 }
 
 bool isEqualAttributes(const AttributePtr& theLeft, const AttributePtr& theRight)
 {
-  if (theLeft->attributeType() == GeomDataAPI_Point2D::typeId() && 
+  if (theLeft->attributeType() == GeomDataAPI_Point2D::typeId() &&
       theRight->attributeType() == GeomDataAPI_Point2D::typeId())
     return isEqual(std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theLeft),
                    std::dynamic_pointer_cast<GeomDataAPI_Point2D>(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);
   }
 };
 
 bool GeomValidators_Different::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
-                                       const std::list<std::string>& theArguments) const
+                                       const std::list<std::string>& theArguments,
+                                       Events_InfoMessage& theError) const
 {
   std::map<std::string, std::list<AttributePtr> > anAttributesMap;
-  // For all attributes referred by theArguments 
-  // sort it using attributeType() and store into anAttributesMap 
+  // For all attributes referred by theArguments
+  // sort it using attributeType() and store into anAttributesMap
   std::list<std::string>::const_iterator anArgumentIt = theArguments.begin();
   for (; anArgumentIt != theArguments.end(); ++anArgumentIt) {
     AttributePtr anAttribute = theFeature->attribute(*anArgumentIt);
@@ -57,7 +82,8 @@ bool GeomValidators_Different::isValid(const std::shared_ptr<ModelAPI_Feature>&
   }
 
   // Search differences inside each attribute list
-  std::map<std::string, std::list<AttributePtr> >::const_iterator anAttributesMapIt = anAttributesMap.begin();
+  std::map<std::string, std::list<AttributePtr> >::const_iterator
+    anAttributesMapIt = anAttributesMap.begin();
   for (; anAttributesMapIt != anAttributesMap.end(); ++anAttributesMapIt) {
     const std::list<AttributePtr>& anAttributes = anAttributesMapIt->second;
     // for the list of attributes check that all elements are unique
@@ -66,10 +92,15 @@ 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; 
+        ++aNextIt;
       }
     }
   }
@@ -77,7 +108,9 @@ bool GeomValidators_Different::isValid(const std::shared_ptr<ModelAPI_Feature>&
   return true;
 }
 
+// LCOV_EXCL_START
 bool GeomValidators_Different::isNotObligatory(std::string theFeature, std::string theAttribute)
 {
   return true;
 }
+// LCOV_EXCL_STOP