Salome HOME
Issue #1834: Fix length of lines
[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 = 
25     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
26   for(int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
27     AttributeSelectionPtr anAttrSelection = anAttrSelectionList->value(anIndex);
28     if(!anAttrSelection.get()) {
29       theError = "Error: empty attribute selection.";
30       return false;
31     }
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: %1 shape is not allowed for selection.";
47       theError.arg(aFeatureKind);
48       return false;
49     }
50     std::shared_ptr<GeomAPI_Shape> aShape = anAttrSelection->value();
51     GeomShapePtr aContextShape = aContext->shape();
52     if(!aShape.get()) {
53       aShape = aContextShape;
54     }
55     if(!aShape.get()) {
56       theError = "Error: empty shape.";
57       return false;
58     }
59     if(!aShape->isEqual(aContextShape)) {
60       theError = "Error: Local selection not allowed.";
61       return false;
62     }
63
64     int aShapeType = aShape->shapeType();
65     // Allow to select edges, faces and solids.
66     if(aShapeType != GeomAPI_Shape::EDGE &&
67        aShapeType != GeomAPI_Shape::FACE &&
68        aShapeType != GeomAPI_Shape::SOLID &&
69        aShapeType != GeomAPI_Shape::COMPSOLID &&
70        aShapeType != GeomAPI_Shape::COMPOUND) {
71       theError = "Error: selected shape has the wrong type.";
72       return false;
73     }
74   }
75
76   return true;
77 }