]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomValidators/GeomValidators_Different.cpp
Salome HOME
Error management -- Feature validator returns an error.
[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 <GeomDataAPI_Point2D.h>
10
11 #include <algorithm>
12 #include <map>
13 #include <list>
14
15 //=================================================================================================
16 /* Help
17 To extend GeomValidators_Different validator with new attribute types:
18 1. Modify function isEqualAttributes
19 2. Create new implementation of isEqual() for the new type
20 */
21 //=================================================================================================
22
23 bool isEqual(const AttributePoint2DPtr& theLeft, const AttributePoint2DPtr& theRight)
24 {
25   return theLeft->x() == theRight->x() &&
26          theLeft->y() == theRight->y();
27 }
28
29 bool isEqualAttributes(const AttributePtr& theLeft, const AttributePtr& theRight)
30 {
31   if (theLeft->attributeType() == GeomDataAPI_Point2D::typeId() && 
32       theRight->attributeType() == GeomDataAPI_Point2D::typeId())
33     return isEqual(std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theLeft),
34                    std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theRight));
35   return false;
36 }
37
38 class IsEqual {
39   AttributePtr myAttribute;
40 public:
41   IsEqual(const AttributePtr& theAttribute) : myAttribute(theAttribute) {}
42   bool operator()(const AttributePtr& theAttribute) {
43     return isEqualAttributes(myAttribute, theAttribute);
44   }
45 };
46
47 bool GeomValidators_Different::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
48                                        const std::list<std::string>& theArguments,
49                                        std::string& theError) const
50 {
51   std::map<std::string, std::list<AttributePtr> > anAttributesMap;
52   // For all attributes referred by theArguments 
53   // sort it using attributeType() and store into anAttributesMap 
54   std::list<std::string>::const_iterator anArgumentIt = theArguments.begin();
55   for (; anArgumentIt != theArguments.end(); ++anArgumentIt) {
56     AttributePtr anAttribute = theFeature->attribute(*anArgumentIt);
57     anAttributesMap[anAttribute->attributeType()].push_back(anAttribute);
58   }
59
60   // Search differences inside each attribute list
61   std::map<std::string, std::list<AttributePtr> >::const_iterator anAttributesMapIt = anAttributesMap.begin();
62   for (; anAttributesMapIt != anAttributesMap.end(); ++anAttributesMapIt) {
63     const std::list<AttributePtr>& anAttributes = anAttributesMapIt->second;
64     // for the list of attributes check that all elements are unique
65     std::list<AttributePtr>::const_iterator anAttributeIt = anAttributes.begin();
66     if (anAttributeIt != anAttributes.end()) {
67       std::list<AttributePtr>::const_iterator aNextIt = anAttributeIt; ++aNextIt;
68       while (aNextIt != anAttributes.end()) {
69         // if equal attribute is found then all attributes are not different
70         if (std::find_if(aNextIt, anAttributes.end(), IsEqual(*anAttributeIt)) != anAttributes.end()) 
71           return false;
72         ++anAttributeIt;
73         ++aNextIt; 
74       }
75     }
76   }
77
78   return true;
79 }
80
81 bool GeomValidators_Different::isNotObligatory(std::string theFeature, std::string theAttribute)
82 {
83   return true;
84 }