Salome HOME
Issue #2608: Invalid shape when select compound as a tool object for cut
[modules/shaper.git] / src / GeomValidators / GeomValidators_NotSelfIntersected.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_NotSelfIntersected.h"
22
23 #include <GeomAPI_Shape.h>
24
25 #include <Events_InfoMessage.h>
26
27 #include <ModelAPI_AttributeSelection.h>
28 #include <ModelAPI_AttributeSelectionList.h>>
29 #include <ModelAPI_Feature.h>
30
31 bool GeomValidators_NotSelfIntersected::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
32                                                 const std::list<std::string>& theArguments,
33                                                 Events_InfoMessage& theError) const
34 {
35   if (theArguments.empty()) {
36     theError = "Error: empty selection.";
37     return false;
38   }
39
40   for (std::list<std::string>::const_iterator anIt = theArguments.cbegin();
41        anIt != theArguments.cend();
42        ++anIt)
43   {
44     std::string anArgument = *anIt;
45     AttributePtr anAttribute = theFeature->attribute(anArgument);
46     if (!anAttribute.get()) {
47       theError = std::string("Error: Feature does not contain attribute: ") + anArgument;
48       return false;
49     }
50     if (anAttribute->attributeType() == ModelAPI_AttributeSelectionList::typeId()) {
51       AttributeSelectionListPtr anAttrSelectionList =
52         std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
53       for (int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
54         AttributeSelectionPtr anAttrSelection = anAttrSelectionList->value(anIndex);
55         if (!anAttrSelection.get()) {
56           theError = "Error: Empty attribute selection.";
57           return false;
58         }
59         ResultPtr aContext = anAttrSelection->context();
60         if (!aContext.get()) {
61           FeaturePtr aContFeat = anAttrSelection->contextFeature();
62           if (!aContFeat.get() || !aContFeat->results().size()) {
63             theError = "Error: Empty selection context.";
64             return false;
65           }
66         }
67         std::shared_ptr<GeomAPI_Shape> aShape = anAttrSelection->value();
68         if (!aShape.get() && aContext.get()) {
69           GeomShapePtr aContextShape = aContext->shape();
70           aShape = aContextShape;
71         }
72         if (!aShape.get()) {
73           theError = "Error: Empty shape.";
74           return false;
75         }
76
77         if (aShape->isSelfIntersected()) {
78           theError = "Error: One of selected shapes are self-intersected.";
79           return false;
80         }
81       }
82     }
83     else {
84       theError = std::string("Error: validator does not support attribute with type: ")
85         + anAttribute->attributeType();
86     }
87   }
88 }
89
90 bool GeomValidators_NotSelfIntersected::isNotObligatory(std::string /*theFeature*/,
91                                                         std::string /*theAttribute*/)
92 {
93   return false;
94 }