1 // Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
20 #include <GeomValidators_Different.h>
22 #include <Events_InfoMessage.h>
24 #include <GeomDataAPI_Point2D.h>
25 #include <GeomAPI_Pnt2d.h>
31 const double tolerance = 1e-7;
33 //=================================================================================================
35 To extend GeomValidators_Different validator with new attribute types:
36 1. Modify function isEqualAttributes
37 2. Create new implementation of isEqual() for the new type
39 //=================================================================================================
41 bool isEqual(const AttributePoint2DPtr& theLeft, const AttributePoint2DPtr& theRight)
43 return theLeft->isInitialized() && theRight->isInitialized() &&
44 theLeft->pnt()->distance(theRight->pnt()) < tolerance;
47 bool isEqualAttributes(const AttributePtr& theLeft, const AttributePtr& theRight)
49 if (theLeft->attributeType() == GeomDataAPI_Point2D::typeId() &&
50 theRight->attributeType() == GeomDataAPI_Point2D::typeId())
51 return isEqual(std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theLeft),
52 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theRight));
58 * \brief Auxiliary class used in std::find_if
61 AttributePtr myAttribute;
64 IsEqual(const AttributePtr& theAttribute) : myAttribute(theAttribute) {}
65 /// \return true in case if AttributePtr is equal with myAttribute
66 bool operator()(const AttributePtr& theAttribute) {
67 return isEqualAttributes(myAttribute, theAttribute);
71 bool GeomValidators_Different::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
72 const std::list<std::string>& theArguments,
73 Events_InfoMessage& theError) const
75 std::map<std::string, std::list<AttributePtr> > anAttributesMap;
76 // For all attributes referred by theArguments
77 // sort it using attributeType() and store into anAttributesMap
78 std::list<std::string>::const_iterator anArgumentIt = theArguments.begin();
79 for (; anArgumentIt != theArguments.end(); ++anArgumentIt) {
80 AttributePtr anAttribute = theFeature->attribute(*anArgumentIt);
81 anAttributesMap[anAttribute->attributeType()].push_back(anAttribute);
84 // Search differences inside each attribute list
85 std::map<std::string, std::list<AttributePtr> >::const_iterator
86 anAttributesMapIt = anAttributesMap.begin();
87 for (; anAttributesMapIt != anAttributesMap.end(); ++anAttributesMapIt) {
88 const std::list<AttributePtr>& anAttributes = anAttributesMapIt->second;
89 // for the list of attributes check that all elements are unique
90 std::list<AttributePtr>::const_iterator anAttributeIt = anAttributes.begin();
91 if (anAttributeIt != anAttributes.end()) {
92 std::list<AttributePtr>::const_iterator aNextIt = anAttributeIt; ++aNextIt;
93 while (aNextIt != anAttributes.end()) {
94 // if equal attribute is found then all attributes are not different
95 std::list<AttributePtr>::const_iterator aFindIt =
96 std::find_if(aNextIt, anAttributes.end(), IsEqual(*anAttributeIt));
97 if (aFindIt != anAttributes.end()) {
98 theError = "Attributes " + (*anAttributeIt)->id() + " and " +
99 (*aFindIt)->id() + " are equal." ;
112 bool GeomValidators_Different::isNotObligatory(std::string theFeature, std::string theAttribute)