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