Salome HOME
3. Mixed topology: solids, faces, edges and vertices
[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         theError = "Error: sketch shape is selected, but only objects are acceptable.";
45         return false;
46       }
47       aShape = aContext->shape();
48     }
49     if(!aShape.get()) {
50       theError = "Error: empty shape.";
51       return false;
52     }
53     int aShapeType = aShape->shapeType();
54     if(anOperationType == 1) {
55       // Fuse operation. Allow to select edges, faces and solids.
56       if(aShapeType != GeomAPI_Shape::EDGE &&
57          aShapeType != GeomAPI_Shape::FACE &&
58          aShapeType != GeomAPI_Shape::SOLID &&
59          aShapeType != GeomAPI_Shape::COMPSOLID &&
60          aShapeType != GeomAPI_Shape::COMPOUND) {
61         theError = "Error: selected shape has the wrong type.";
62         return false;
63       }
64     } else {
65       if(aShapeType != GeomAPI_Shape::SOLID &&
66          aShapeType != GeomAPI_Shape::COMPSOLID &&
67          aShapeType != GeomAPI_Shape::COMPOUND) {
68         theError = "Error: selected shape has the wrong type.";
69         return false;
70       }
71     }
72   }
73
74   return true;
75 }