Salome HOME
Temporary modification to check whether the compilation on Linux is correct. OCCT...
[modules/shaper.git] / src / GeomValidators / GeomValidators_Different.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomValidators_Different.cpp
4 // Created:     08 July 2015
5 // Author:      Sergey POKHODENKO
6
7 #include <GeomValidators_Different.h>
8
9 #include <Events_InfoMessage.h>
10
11 #include <GeomDataAPI_Point2D.h>
12 #include <GeomAPI_Pnt2d.h>
13
14 #include <algorithm>
15 #include <map>
16 #include <list>
17
18 const double tolerance = 1e-7;
19
20 //=================================================================================================
21 /* Help
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
25 */
26 //=================================================================================================
27
28 bool isEqual(const AttributePoint2DPtr& theLeft, const AttributePoint2DPtr& theRight)
29 {
30   return theLeft->pnt()->distance(theRight->pnt()) < tolerance;
31 }
32
33 bool isEqualAttributes(const AttributePtr& theLeft, const AttributePtr& theRight)
34 {
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));
39   return false;
40 }
41
42 /** \class IsEqual
43  *  \ingroup Validators
44  *  \brief Auxiliary class used in std::find_if
45  */
46 class IsEqual {
47   AttributePtr myAttribute;
48 public:
49   /// Constructor
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);
54   }
55 };
56
57 bool GeomValidators_Different::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
58                                        const std::list<std::string>& theArguments,
59                                        Events_InfoMessage& theError) const
60 {
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);
68   }
69
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." ;
84           return false;
85         }
86         ++anAttributeIt;
87         ++aNextIt; 
88       }
89     }
90   }
91
92   return true;
93 }
94
95 bool GeomValidators_Different::isNotObligatory(std::string theFeature, std::string theAttribute)
96 {
97   return true;
98 }