Salome HOME
Rectangle correction to have coincidence with point/line selected for the first point...
[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     std::shared_ptr<GeomAPI_Shape> aShape = anAttrSelection->value();
31     if(!aShape.get()) {
32       ResultPtr aContext = anAttrSelection->context();
33       if(!aContext.get()) {
34         theError = "Error: empty selection context.";
35         return false;
36       }
37       FeaturePtr aFeature = ModelAPI_Feature::feature(aContext);
38       if(!aFeature.get()) {
39         theError = "Error: empty feature.";
40         return false;
41       }
42       std::string aFeatureKind = aFeature->getKind();
43       if(aFeatureKind == "Sketch" || 
44          aFeatureKind == "Plane" ||
45          aFeatureKind == "Axis") {
46         theError = "Error: ";
47         theError += aFeatureKind;
48         theError += " shape is not allowed for selection.";
49         return false;
50       }
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 }