Salome HOME
updated copyright message
[modules/shaper.git] / src / GeomValidators / GeomValidators_IntersectionSelection.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomValidators_IntersectionSelection.h"
21
22 #include <GeomAPI_Shape.h>
23
24 #include <Events_InfoMessage.h>
25
26 #include <ModelAPI_AttributeInteger.h>
27 #include <ModelAPI_AttributeSelectionList.h>
28 #include <ModelAPI_Feature.h>
29
30 bool GeomValidators_IntersectionSelection::isValid(const AttributePtr& theAttribute,
31                                                    const std::list<std::string>& theArguments,
32                                                    Events_InfoMessage& theError) const
33 {
34   if(!theAttribute.get()) {
35     theError = "Error: empty selection.";
36     return false;
37   }
38   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
39   AttributeSelectionListPtr anAttrSelectionList =
40     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
41   for(int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
42     AttributeSelectionPtr anAttrSelection = anAttrSelectionList->value(anIndex);
43     if(!anAttrSelection.get()) {
44       theError = "Error: empty attribute selection.";
45       return false;
46     }
47     FeaturePtr aFeature;
48     ResultPtr aContext = anAttrSelection->context();
49     if(!aContext.get()) {
50       aFeature = anAttrSelection->contextFeature();
51       if (!aFeature.get()) {
52         theError = "Error: Empty selection context.";
53         return false;
54       }
55     } else {
56       aFeature = ModelAPI_Feature::feature(aContext);
57     }
58
59     if(!aFeature.get()) {
60       theError = "Error: empty feature.";
61       return false;
62     }
63     std::string aFeatureKind = aFeature->getKind();
64     if(aFeatureKind == "Sketch" ||
65        aFeatureKind == "Plane" ||
66        aFeatureKind == "Axis") {
67       theError = "Error: %1 shape is not allowed for selection.";
68       theError.arg(aFeatureKind);
69       return false;
70     }
71     std::shared_ptr<GeomAPI_Shape> aShape = anAttrSelection->value();
72     GeomShapePtr aContextShape = aContext->shape();
73     if(!aShape.get()) {
74       aShape = aContextShape;
75     }
76     if(!aShape.get()) {
77       theError = "Error: empty shape.";
78       return false;
79     }
80     if(!aShape->isEqual(aContextShape)) {
81       theError = "Error: Local selection not allowed.";
82       return false;
83     }
84
85     int aShapeType = aShape->shapeType();
86     // Allow to select edges, faces and solids.
87     if(aShapeType != GeomAPI_Shape::EDGE &&
88        aShapeType != GeomAPI_Shape::FACE &&
89        aShapeType != GeomAPI_Shape::SOLID &&
90        aShapeType != GeomAPI_Shape::COMPSOLID &&
91        aShapeType != GeomAPI_Shape::COMPOUND) {
92       theError = "Error: selected shape has the wrong type.";
93       return false;
94     }
95   }
96
97   return true;
98 }