Salome HOME
Implementation of names of features selection.
[modules/shaper.git] / src / GeomValidators / GeomValidators_IntersectionSelection.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "GeomValidators_IntersectionSelection.h"
22
23 #include <GeomAPI_Shape.h>
24
25 #include <Events_InfoMessage.h>
26
27 #include <ModelAPI_AttributeInteger.h>
28 #include <ModelAPI_AttributeSelectionList.h>
29 #include <ModelAPI_Feature.h>
30
31 bool GeomValidators_IntersectionSelection::isValid(const AttributePtr& theAttribute,
32                                                    const std::list<std::string>& theArguments,
33                                                    Events_InfoMessage& theError) const
34 {
35   if(!theAttribute.get()) {
36     theError = "Error: empty selection.";
37     return false;
38   }
39   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
40   AttributeSelectionListPtr anAttrSelectionList =
41     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
42   for(int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
43     AttributeSelectionPtr anAttrSelection = anAttrSelectionList->value(anIndex);
44     if(!anAttrSelection.get()) {
45       theError = "Error: empty attribute selection.";
46       return false;
47     }
48     FeaturePtr aFeature;
49     ResultPtr aContext = anAttrSelection->context();
50     if(!aContext.get()) {
51       aFeature = anAttrSelection->contextFeature();
52       if (!aFeature.get()) {
53         theError = "Error: Empty selection context.";
54         return false;
55       }
56     } else {
57       aFeature = ModelAPI_Feature::feature(aContext);
58     }
59
60     if(!aFeature.get()) {
61       theError = "Error: empty feature.";
62       return false;
63     }
64     std::string aFeatureKind = aFeature->getKind();
65     if(aFeatureKind == "Sketch" ||
66        aFeatureKind == "Plane" ||
67        aFeatureKind == "Axis") {
68       theError = "Error: %1 shape is not allowed for selection.";
69       theError.arg(aFeatureKind);
70       return false;
71     }
72     std::shared_ptr<GeomAPI_Shape> aShape = anAttrSelection->value();
73     GeomShapePtr aContextShape = aContext->shape();
74     if(!aShape.get()) {
75       aShape = aContextShape;
76     }
77     if(!aShape.get()) {
78       theError = "Error: empty shape.";
79       return false;
80     }
81     if(!aShape->isEqual(aContextShape)) {
82       theError = "Error: Local selection not allowed.";
83       return false;
84     }
85
86     int aShapeType = aShape->shapeType();
87     // Allow to select edges, faces and solids.
88     if(aShapeType != GeomAPI_Shape::EDGE &&
89        aShapeType != GeomAPI_Shape::FACE &&
90        aShapeType != GeomAPI_Shape::SOLID &&
91        aShapeType != GeomAPI_Shape::COMPSOLID &&
92        aShapeType != GeomAPI_Shape::COMPOUND) {
93       theError = "Error: selected shape has the wrong type.";
94       return false;
95     }
96   }
97
98   return true;
99 }