1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <GeomValidators_Different.h>
23 #include <Events_InfoMessage.h>
25 #include <GeomDataAPI_Point2D.h>
26 #include <GeomAPI_Pnt2d.h>
32 const double tolerance = 1e-7;
34 //=================================================================================================
36 To extend GeomValidators_Different validator with new attribute types:
37 1. Modify function isEqualAttributes
38 2. Create new implementation of isEqual() for the new type
40 //=================================================================================================
42 bool isEqual(const AttributePoint2DPtr& theLeft, const AttributePoint2DPtr& theRight)
44 return theLeft->isInitialized() && theRight->isInitialized() &&
45 theLeft->pnt()->distance(theRight->pnt()) < tolerance;
48 bool isEqualAttributes(const AttributePtr& theLeft, const AttributePtr& theRight)
50 if (theLeft->attributeType() == GeomDataAPI_Point2D::typeId() &&
51 theRight->attributeType() == GeomDataAPI_Point2D::typeId())
52 return isEqual(std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theLeft),
53 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theRight));
59 * \brief Auxiliary class used in std::find_if
62 AttributePtr myAttribute;
65 IsEqual(const AttributePtr& theAttribute) : myAttribute(theAttribute) {}
66 /// \return true in case if AttributePtr is equal with myAttribute
67 bool operator()(const AttributePtr& theAttribute) {
68 return isEqualAttributes(myAttribute, theAttribute);
72 bool GeomValidators_Different::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
73 const std::list<std::string>& theArguments,
74 Events_InfoMessage& theError) const
76 std::map<std::string, std::list<AttributePtr> > anAttributesMap;
77 // For all attributes referred by theArguments
78 // sort it using attributeType() and store into anAttributesMap
79 std::list<std::string>::const_iterator anArgumentIt = theArguments.begin();
80 for (; anArgumentIt != theArguments.end(); ++anArgumentIt) {
81 AttributePtr anAttribute = theFeature->attribute(*anArgumentIt);
82 anAttributesMap[anAttribute->attributeType()].push_back(anAttribute);
85 // Search differences inside each attribute list
86 std::map<std::string, std::list<AttributePtr> >::const_iterator
87 anAttributesMapIt = anAttributesMap.begin();
88 for (; anAttributesMapIt != anAttributesMap.end(); ++anAttributesMapIt) {
89 const std::list<AttributePtr>& anAttributes = anAttributesMapIt->second;
90 // for the list of attributes check that all elements are unique
91 std::list<AttributePtr>::const_iterator anAttributeIt = anAttributes.begin();
92 if (anAttributeIt != anAttributes.end()) {
93 std::list<AttributePtr>::const_iterator aNextIt = anAttributeIt; ++aNextIt;
94 while (aNextIt != anAttributes.end()) {
95 // if equal attribute is found then all attributes are not different
96 std::list<AttributePtr>::const_iterator aFindIt =
97 std::find_if(aNextIt, anAttributes.end(), IsEqual(*anAttributeIt));
98 if (aFindIt != anAttributes.end()) {
99 theError = "Attributes " + (*anAttributeIt)->id() + " and " +
100 (*aFindIt)->id() + " are equal." ;
113 bool GeomValidators_Different::isNotObligatory(std::string theFeature, std::string theAttribute)