Salome HOME
[Code coverage GeomValidators]: Exclude "fool-tolerance" validations
[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 // LCOV_EXCL_START
36   if (theArguments.empty()) {
37     theError = "Error: empty selection.";
38     return false;
39   }
40 // LCOV_EXCL_STOP
41
42   for (std::list<std::string>::const_iterator anIt = theArguments.cbegin();
43        anIt != theArguments.cend();
44        ++anIt)
45   {
46     std::string anArgument = *anIt;
47     AttributePtr anAttribute = theFeature->attribute(anArgument);
48     if (!anAttribute.get()) {
49 // LCOV_EXCL_START
50       theError = std::string("Error: Feature does not contain attribute: ") + anArgument;
51       return false;
52 // LCOV_EXCL_STOP
53     }
54     if (anAttribute->attributeType() == ModelAPI_AttributeSelectionList::typeId()) {
55       AttributeSelectionListPtr anAttrSelectionList =
56         std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
57       for (int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
58         AttributeSelectionPtr anAttrSelection = anAttrSelectionList->value(anIndex);
59         if (!anAttrSelection.get()) {
60 // LCOV_EXCL_START
61           theError = "Error: Empty attribute selection.";
62           return false;
63 // LCOV_EXCL_STOP
64         }
65         ResultPtr aContext = anAttrSelection->context();
66         if (!aContext.get()) {
67           FeaturePtr aContFeat = anAttrSelection->contextFeature();
68           if (!aContFeat.get() || !aContFeat->results().size()) {
69             theError = "Error: Empty selection context.";
70             return false;
71           }
72         }
73         std::shared_ptr<GeomAPI_Shape> aShape = anAttrSelection->value();
74         if (!aShape.get() && aContext.get()) {
75           GeomShapePtr aContextShape = aContext->shape();
76           aShape = aContextShape;
77         }
78         if (!aShape.get()) {
79           theError = "Error: Empty shape.";
80           return false;
81         }
82
83         /* optimization if (aShape->isSelfIntersected(4)) {
84           theError = "Error: One of selected shapes are self-intersected.";
85           return false;
86         }*/
87       }
88     } else {
89 // LCOV_EXCL_START
90       theError = std::string("Error: validator does not support attribute with type: ")
91         + anAttribute->attributeType();
92       return false;
93 // LCOV_EXCL_STOP
94     }
95   }
96
97   return true;
98 }
99
100 bool GeomValidators_NotSelfIntersected::isNotObligatory(std::string /*theFeature*/,
101                                                         std::string /*theAttribute*/)
102 {
103   return false;
104 }