Salome HOME
Issue #2090: Line not selectable when creating an arc by its center
[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 #include <GeomAPI_Shape.h>
15
16 bool GeomValidators_DifferentShapes::isValid(const AttributePtr& theAttribute,
17                                       const std::list<std::string>& theArguments,
18                                       Events_InfoMessage& theError) const
19 {
20   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
21   AttributeSelectionPtr aSelectionAttribute =
22                      std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
23   GeomShapePtr aShape = aSelectionAttribute->value();
24   if (!aShape.get()) {
25     ResultPtr aResult = aSelectionAttribute->context();
26     if (aResult.get())
27       aShape = aResult->shape();
28   }
29
30   std::string aCurrentAttributeId = theAttribute->id();
31   // get all feature attributes
32   std::list<AttributePtr> anAttrs =
33       aFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
34   if (anAttrs.size() > 0 && aShape.get() != NULL) {
35     std::list<AttributePtr>::iterator anAttr = anAttrs.begin();
36     for(; anAttr != anAttrs.end(); anAttr++) {
37       AttributePtr anAttribute = *anAttr;
38       // take into concideration only other attributes
39       if (anAttribute.get() != NULL && anAttribute->id() != aCurrentAttributeId) {
40         aSelectionAttribute =
41           std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
42         // the shape of the attribute should be not the same
43         if (aSelectionAttribute.get() != NULL) {
44           GeomShapePtr anAttrShape = aSelectionAttribute->value();
45           if (!anAttrShape.get()) {
46             ResultPtr aResult = aSelectionAttribute->context();
47             if (aResult.get())
48               anAttrShape = aResult->shape();
49           }
50           if (aShape->isEqual(anAttrShape)) {
51             theError = "The feature uses equal shapes.";
52             return false;
53           }
54         }
55       }
56     }
57   }
58   return true;
59 }