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