1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: GeomValidators_Different.cpp
4 // Created: 08 July 2015
5 // Author: Sergey POKHODENKO
7 #include <GeomValidators_Different.h>
9 #include <Events_InfoMessage.h>
11 #include <GeomDataAPI_Point2D.h>
12 #include <GeomAPI_Pnt2d.h>
18 const double tolerance = 1e-7;
20 //=================================================================================================
22 To extend GeomValidators_Different validator with new attribute types:
23 1. Modify function isEqualAttributes
24 2. Create new implementation of isEqual() for the new type
26 //=================================================================================================
28 bool isEqual(const AttributePoint2DPtr& theLeft, const AttributePoint2DPtr& theRight)
30 return theLeft->isInitialized() && theRight->isInitialized() &&
31 theLeft->pnt()->distance(theRight->pnt()) < tolerance;
34 bool isEqualAttributes(const AttributePtr& theLeft, const AttributePtr& theRight)
36 if (theLeft->attributeType() == GeomDataAPI_Point2D::typeId() &&
37 theRight->attributeType() == GeomDataAPI_Point2D::typeId())
38 return isEqual(std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theLeft),
39 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theRight));
45 * \brief Auxiliary class used in std::find_if
48 AttributePtr myAttribute;
51 IsEqual(const AttributePtr& theAttribute) : myAttribute(theAttribute) {}
52 /// \return true in case if AttributePtr is equal with myAttribute
53 bool operator()(const AttributePtr& theAttribute) {
54 return isEqualAttributes(myAttribute, theAttribute);
58 bool GeomValidators_Different::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
59 const std::list<std::string>& theArguments,
60 Events_InfoMessage& theError) const
62 std::map<std::string, std::list<AttributePtr> > anAttributesMap;
63 // For all attributes referred by theArguments
64 // sort it using attributeType() and store into anAttributesMap
65 std::list<std::string>::const_iterator anArgumentIt = theArguments.begin();
66 for (; anArgumentIt != theArguments.end(); ++anArgumentIt) {
67 AttributePtr anAttribute = theFeature->attribute(*anArgumentIt);
68 anAttributesMap[anAttribute->attributeType()].push_back(anAttribute);
71 // Search differences inside each attribute list
72 std::map<std::string, std::list<AttributePtr> >::const_iterator
73 anAttributesMapIt = anAttributesMap.begin();
74 for (; anAttributesMapIt != anAttributesMap.end(); ++anAttributesMapIt) {
75 const std::list<AttributePtr>& anAttributes = anAttributesMapIt->second;
76 // for the list of attributes check that all elements are unique
77 std::list<AttributePtr>::const_iterator anAttributeIt = anAttributes.begin();
78 if (anAttributeIt != anAttributes.end()) {
79 std::list<AttributePtr>::const_iterator aNextIt = anAttributeIt; ++aNextIt;
80 while (aNextIt != anAttributes.end()) {
81 // if equal attribute is found then all attributes are not different
82 std::list<AttributePtr>::const_iterator aFindIt =
83 std::find_if(aNextIt, anAttributes.end(), IsEqual(*anAttributeIt));
84 if (aFindIt != anAttributes.end()) {
85 theError = "Attributes " + (*anAttributeIt)->id() + " and " +
86 (*aFindIt)->id() + " are equal." ;
98 bool GeomValidators_Different::isNotObligatory(std::string theFeature, std::string theAttribute)