Salome HOME
#2027 Sketcher Trim Feature: 1. preview/selected attributes in trim; 2. avoid includi...
[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 <GeomAPI_Shape.h>
10
11 #include <Events_InfoMessage.h>
12
13 #include <ModelAPI_AttributeInteger.h>
14 #include <ModelAPI_AttributeSelectionList.h>
15 #include <ModelAPI_Feature.h>
16
17 bool GeomValidators_IntersectionSelection::isValid(const AttributePtr& theAttribute,
18                                                    const std::list<std::string>& theArguments,
19                                                    Events_InfoMessage& theError) const
20 {
21   if(!theAttribute.get()) {
22     theError = "Error: empty selection.";
23     return false;
24   }
25   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
26   AttributeSelectionListPtr anAttrSelectionList =
27     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
28   for(int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
29     AttributeSelectionPtr anAttrSelection = anAttrSelectionList->value(anIndex);
30     if(!anAttrSelection.get()) {
31       theError = "Error: empty attribute selection.";
32       return false;
33     }
34     ResultPtr aContext = anAttrSelection->context();
35     if(!aContext.get()) {
36       theError = "Error: empty selection context.";
37       return false;
38     }
39     FeaturePtr aFeature = ModelAPI_Feature::feature(aContext);
40     if(!aFeature.get()) {
41       theError = "Error: empty feature.";
42       return false;
43     }
44     std::string aFeatureKind = aFeature->getKind();
45     if(aFeatureKind == "Sketch" ||
46        aFeatureKind == "Plane" ||
47        aFeatureKind == "Axis") {
48       theError = "Error: %1 shape is not allowed for selection.";
49       theError.arg(aFeatureKind);
50       return false;
51     }
52     std::shared_ptr<GeomAPI_Shape> aShape = anAttrSelection->value();
53     GeomShapePtr aContextShape = aContext->shape();
54     if(!aShape.get()) {
55       aShape = aContextShape;
56     }
57     if(!aShape.get()) {
58       theError = "Error: empty shape.";
59       return false;
60     }
61     if(!aShape->isEqual(aContextShape)) {
62       theError = "Error: Local selection not allowed.";
63       return false;
64     }
65
66     int aShapeType = aShape->shapeType();
67     // Allow to select edges, faces and solids.
68     if(aShapeType != GeomAPI_Shape::EDGE &&
69        aShapeType != GeomAPI_Shape::FACE &&
70        aShapeType != GeomAPI_Shape::SOLID &&
71        aShapeType != GeomAPI_Shape::COMPSOLID &&
72        aShapeType != GeomAPI_Shape::COMPOUND) {
73       theError = "Error: selected shape has the wrong type.";
74       return false;
75     }
76   }
77
78   return true;
79 }