Salome HOME
c748c2cdc18ea11a82e14a9d497619c998facadb
[modules/shaper.git] / src / GeomValidators / GeomValidators_ConstructionComposite.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include "GeomValidators_ConstructionComposite.h"
4
5 #include <Events_InfoMessage.h>
6
7 #include "ModelAPI_AttributeSelection.h"
8 #include "ModelAPI_ResultConstruction.h"
9 #include "ModelAPI_CompositeFeature.h"
10
11 bool GeomValidators_ConstructionComposite::isValid(const AttributePtr& theAttribute,
12                                                    const std::list<std::string>& theArguments,
13                                                    Events_InfoMessage& theError) const
14 {
15   bool aValid = true;
16   if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
17     aValid = false;
18     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
19     return aValid;
20   }
21
22   AttributeSelectionPtr aSelectionAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
23                                                                 (theAttribute);
24   ResultPtr aResult = aSelectionAttr->context();
25   // global selection should be ignored, the filter processes only selected sub-shapes
26   // that means, that the shape of the context result is equal to the shape value
27   ///*ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
28   if (!aResult.get()) {
29     aValid = false;
30     theError = "The result is empty";
31   }
32   else {
33     GeomShapePtr aShape = aSelectionAttr->value();
34     GeomShapePtr aShapePtr = aResult->shape();
35     // it is important to call isEqual of the shape of result.
36     // It is a GeomAPI_Vertex shape for the point. The shape of the parameter is 
37     // GeomAPI_Shape. It is important to use the realization of the isEqual method from
38     // GeomAPI_Vertex class
39     if (!aShape.get()) {
40       // an empty shape is used in attribute selection if the shape of the result is equal to
41       // the selected shape, so according to the upper condition, the result is true
42       aValid = true;
43     }
44     else {
45       if (aShapePtr->isEqual(aShape)) {
46         aValid = true;
47       }
48       else {
49         ResultConstructionPtr aConstr =
50           std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aResult);
51         if (!aConstr.get()) {
52           // non-construction results should be accepted by this filter, e.g. body results
53           aValid = true;
54         }
55         else {
56           // it provides selection only on composite features, construction without composite
57           // feature is not selectable.
58           FeaturePtr aFeature = ModelAPI_Feature::feature(aConstr);
59           CompositeFeaturePtr aComposite =
60             std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
61           aValid = aComposite && aComposite->numberOfSubs() > 0;
62           if (!aValid)
63             theError = "Uses composite construction feature without sub-features.";
64         }
65       }
66     }
67   }
68   return aValid;
69 }