Salome HOME
Fixed validators for Boolean and Intersection
[modules/shaper.git] / src / GeomValidators / GeomValidators_IntersectionSelection.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomValidators_IntersectionSelection.cpp
4 // Created:     16 Feb 2016
5 // Author:      Dmitry Bobylev
6
7 #include "GeomValidators_IntersectionSelection.h"
8
9 #include <Events_InfoMessage.h>
10
11 #include <ModelAPI_AttributeInteger.h>
12 #include <ModelAPI_AttributeSelectionList.h>
13 #include <ModelAPI_Feature.h>
14
15 bool GeomValidators_IntersectionSelection::isValid(const AttributePtr& theAttribute,
16                                                    const std::list<std::string>& theArguments,
17                                                    Events_InfoMessage& theError) const
18 {
19   if(!theAttribute.get()) {
20     theError = "Error: empty selection.";
21     return false;
22   }
23   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
24   AttributeSelectionListPtr anAttrSelectionList = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
25   for(int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
26     AttributeSelectionPtr anAttrSelection = anAttrSelectionList->value(anIndex);
27     if(!anAttrSelection.get()) {
28       theError = "Error: empty attribute selection.";
29       return false;
30     }
31     ResultPtr aContext = anAttrSelection->context();
32     if(!aContext.get()) {
33       theError = "Error: empty selection context.";
34       return false;
35     }
36     FeaturePtr aFeature = ModelAPI_Feature::feature(aContext);
37     if(!aFeature.get()) {
38       theError = "Error: empty feature.";
39       return false;
40     }
41     std::string aFeatureKind = aFeature->getKind();
42     if(aFeatureKind == "Sketch" ||
43        aFeatureKind == "Plane" ||
44        aFeatureKind == "Axis") {
45       theError = "Error: %1 shape is not allowed for selection.";
46       theError.arg(aFeatureKind);
47       return false;
48     }
49     std::shared_ptr<GeomAPI_Shape> aShape = anAttrSelection->value();
50     GeomShapePtr aContextShape = aContext->shape();
51     if(!aShape.get()) {
52       aShape = aContextShape;
53     }
54     if(!aShape.get()) {
55       theError = "Error: empty shape.";
56       return false;
57     }
58     if(!aShape->isEqual(aContextShape)) {
59       theError = "Error: Local selection not allowed.";
60       return false;
61     }
62
63     int aShapeType = aShape->shapeType();
64     // Allow to select edges, faces and solids.
65     if(aShapeType != GeomAPI_Shape::EDGE &&
66        aShapeType != GeomAPI_Shape::FACE &&
67        aShapeType != GeomAPI_Shape::SOLID &&
68        aShapeType != GeomAPI_Shape::COMPSOLID &&
69        aShapeType != GeomAPI_Shape::COMPOUND) {
70       theError = "Error: selected shape has the wrong type.";
71       return false;
72     }
73   }
74
75   return true;
76 }