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