Salome HOME
01eb0409a75857c9c14d2d6614ebc9d804a80a5d
[modules/shaper.git] / src / GeomValidators / GeomValidators_DifferentShapes.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomValidators_DifferentShapes.cpp
4 // Created:     2 Feb 2015
5 // Author:      Natalia ERMOLAEVA
6
7 #include "GeomValidators_DifferentShapes.h"
8
9 #include <Events_InfoMessage.h>
10
11 #include <ModelAPI_AttributeSelection.h>
12 #include "ModelAPI_Object.h"
13
14 bool GeomValidators_DifferentShapes::isValid(const AttributePtr& theAttribute,
15                                       const std::list<std::string>& theArguments,
16                                       Events_InfoMessage& theError) const
17 {
18   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
19   AttributeSelectionPtr aSelectionAttribute =
20                      std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
21   GeomShapePtr aShape = aSelectionAttribute->value();
22   if (!aShape.get()) {
23     ResultPtr aResult = aSelectionAttribute->context();
24     if (aResult.get())
25       aShape = aResult->shape();
26   }
27
28   std::string aCurrentAttributeId = theAttribute->id();
29   // get all feature attributes
30   std::list<AttributePtr> anAttrs =
31       aFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
32   if (anAttrs.size() > 0 && aShape.get() != NULL) {
33     std::list<AttributePtr>::iterator anAttr = anAttrs.begin();
34     for(; anAttr != anAttrs.end(); anAttr++) {
35       AttributePtr anAttribute = *anAttr;
36       // take into concideration only other attributes
37       if (anAttribute.get() != NULL && anAttribute->id() != aCurrentAttributeId) {
38         aSelectionAttribute =
39           std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
40         // the shape of the attribute should be not the same
41         if (aSelectionAttribute.get() != NULL) {
42           GeomShapePtr anAttrShape = aSelectionAttribute->value();
43           if (!anAttrShape.get()) {
44             ResultPtr aResult = aSelectionAttribute->context();
45             if (aResult.get())
46               anAttrShape = aResult->shape();
47           }
48           if (aShape->isEqual(anAttrShape)) {
49             theError = "The feature uses equal shapes.";
50             return false;
51           }
52         }
53       }
54     }
55   }
56   return true;
57 }