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->pnt()->distance(theRight->pnt()) < tolerance;
33 bool isEqualAttributes(const AttributePtr& theLeft, const AttributePtr& theRight)
35 if (theLeft->attributeType() == GeomDataAPI_Point2D::typeId() &&
36 theRight->attributeType() == GeomDataAPI_Point2D::typeId())
37 return isEqual(std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theLeft),
38 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theRight));
44 * \brief Auxiliary class used in std::find_if
47 AttributePtr myAttribute;
50 IsEqual(const AttributePtr& theAttribute) : myAttribute(theAttribute) {}
51 /// \return true in case if AttributePtr is equal with myAttribute
52 bool operator()(const AttributePtr& theAttribute) {
53 return isEqualAttributes(myAttribute, theAttribute);
57 bool GeomValidators_Different::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
58 const std::list<std::string>& theArguments,
59 Events_InfoMessage& theError) const
61 std::map<std::string, std::list<AttributePtr> > anAttributesMap;
62 // For all attributes referred by theArguments
63 // sort it using attributeType() and store into anAttributesMap
64 std::list<std::string>::const_iterator anArgumentIt = theArguments.begin();
65 for (; anArgumentIt != theArguments.end(); ++anArgumentIt) {
66 AttributePtr anAttribute = theFeature->attribute(*anArgumentIt);
67 anAttributesMap[anAttribute->attributeType()].push_back(anAttribute);
70 // Search differences inside each attribute list
71 std::map<std::string, std::list<AttributePtr> >::const_iterator anAttributesMapIt = anAttributesMap.begin();
72 for (; anAttributesMapIt != anAttributesMap.end(); ++anAttributesMapIt) {
73 const std::list<AttributePtr>& anAttributes = anAttributesMapIt->second;
74 // for the list of attributes check that all elements are unique
75 std::list<AttributePtr>::const_iterator anAttributeIt = anAttributes.begin();
76 if (anAttributeIt != anAttributes.end()) {
77 std::list<AttributePtr>::const_iterator aNextIt = anAttributeIt; ++aNextIt;
78 while (aNextIt != anAttributes.end()) {
79 // if equal attribute is found then all attributes are not different
80 std::list<AttributePtr>::const_iterator aFindIt =
81 std::find_if(aNextIt, anAttributes.end(), IsEqual(*anAttributeIt));
82 if (aFindIt != anAttributes.end()) {
83 theError = "Attributes " + (*anAttributeIt)->id() + " and " + (*aFindIt)->id() + " are equal." ;
95 bool GeomValidators_Different::isNotObligatory(std::string theFeature, std::string theAttribute)