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 <GeomDataAPI_Point2D.h>
10 #include <GeomAPI_Pnt2d.h>
16 const double tolerance = 1e-7;
18 //=================================================================================================
20 To extend GeomValidators_Different validator with new attribute types:
21 1. Modify function isEqualAttributes
22 2. Create new implementation of isEqual() for the new type
24 //=================================================================================================
26 bool isEqual(const AttributePoint2DPtr& theLeft, const AttributePoint2DPtr& theRight)
28 return theLeft->pnt()->distance(theRight->pnt()) < tolerance;
31 bool isEqualAttributes(const AttributePtr& theLeft, const AttributePtr& theRight)
33 if (theLeft->attributeType() == GeomDataAPI_Point2D::typeId() &&
34 theRight->attributeType() == GeomDataAPI_Point2D::typeId())
35 return isEqual(std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theLeft),
36 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theRight));
41 AttributePtr myAttribute;
43 IsEqual(const AttributePtr& theAttribute) : myAttribute(theAttribute) {}
44 bool operator()(const AttributePtr& theAttribute) {
45 return isEqualAttributes(myAttribute, theAttribute);
49 bool GeomValidators_Different::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
50 const std::list<std::string>& theArguments,
51 std::string& theError) const
53 std::map<std::string, std::list<AttributePtr> > anAttributesMap;
54 // For all attributes referred by theArguments
55 // sort it using attributeType() and store into anAttributesMap
56 std::list<std::string>::const_iterator anArgumentIt = theArguments.begin();
57 for (; anArgumentIt != theArguments.end(); ++anArgumentIt) {
58 AttributePtr anAttribute = theFeature->attribute(*anArgumentIt);
59 anAttributesMap[anAttribute->attributeType()].push_back(anAttribute);
62 // Search differences inside each attribute list
63 std::map<std::string, std::list<AttributePtr> >::const_iterator anAttributesMapIt = anAttributesMap.begin();
64 for (; anAttributesMapIt != anAttributesMap.end(); ++anAttributesMapIt) {
65 const std::list<AttributePtr>& anAttributes = anAttributesMapIt->second;
66 // for the list of attributes check that all elements are unique
67 std::list<AttributePtr>::const_iterator anAttributeIt = anAttributes.begin();
68 if (anAttributeIt != anAttributes.end()) {
69 std::list<AttributePtr>::const_iterator aNextIt = anAttributeIt; ++aNextIt;
70 while (aNextIt != anAttributes.end()) {
71 // if equal attribute is found then all attributes are not different
72 std::list<AttributePtr>::const_iterator aFindIt =
73 std::find_if(aNextIt, anAttributes.end(), IsEqual(*anAttributeIt));
74 if (aFindIt != anAttributes.end()) {
75 theError = "Attributes " + anAttributeIt->id() + " and " + aFindIt->id() + " are equal." ;
87 bool GeomValidators_Different::isNotObligatory(std::string theFeature, std::string theAttribute)