Salome HOME
113656c6c77d623fcde5c5e3fbd38cf5b1a3a671
[modules/shaper.git] / src / GeomValidators / GeomValidators_DifferentShapes.cpp
1 // Copyright (C) 2014-2022  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomValidators_DifferentShapes.h"
21
22 #include <Events_InfoMessage.h>
23
24 #include <ModelAPI_AttributeSelection.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include "ModelAPI_Object.h"
27
28 #include <GeomAPI_Shape.h>
29 #include <GeomAPI_Tools.h>
30
31 //=================================================================================================
32 bool GeomValidators_DifferentShapes::isValid(const AttributePtr& theAttribute,
33                                       const std::list<std::string>& /*theArguments*/,
34                                       Events_InfoMessage& theError) const
35 {
36   bool isValid = false;
37
38   std::string anAttributeType = theAttribute->attributeType();
39   bool isList = anAttributeType == ModelAPI_AttributeSelectionList::typeId();
40
41   std::list<AttributePtr> anAttrs;
42   if (isList) {
43     AttributeSelectionListPtr aListAttr =
44       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
45     // get all selection attributes from the list
46     for (int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
47       anAttrs.push_back(aListAttr->value(anIndex));
48     }
49
50     isValid = checkEquals(anAttrs);
51   }
52   else {
53     // get all feature selection attributes
54     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
55     anAttrs = aFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
56
57     isValid = checkEqualToCurrent(anAttrs, theAttribute);
58   }
59
60   if (!isValid) {
61     theError = isList ? "The selection list contains equal shapes." :
62                         "The feature uses equal shapes.";
63     return false;
64   }
65
66   return true;
67 }
68
69 //=================================================================================================
70 bool GeomValidators_DifferentShapes::checkEquals(std::list<AttributePtr>& theAttributes)
71 {
72   std::list<AttributePtr>::iterator anIt = theAttributes.begin();
73   for (; anIt != theAttributes.end(); anIt++) {
74     AttributePtr anAttribute = *anIt;
75
76     std::list<AttributePtr>::iterator anOthersIt = std::next(anIt);
77     for (; anOthersIt != theAttributes.end(); anOthersIt++) {
78       AttributePtr anOtherAttribute = *anOthersIt;
79       if (isAttrShapesEqual(anAttribute, anOtherAttribute)) {
80         return false;
81       }
82     }
83   }
84
85   return true;
86 }
87
88 //=================================================================================================
89 bool GeomValidators_DifferentShapes::checkEqualToCurrent(std::list<AttributePtr>& theAttributes,
90   const AttributePtr& theCurrentAttribute)
91 {
92   AttributeSelectionPtr aSelectionAttribute =
93     std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theCurrentAttribute);
94
95   GeomShapePtr aShape = aSelectionAttribute->value();
96   ResultPtr aResultContext = aSelectionAttribute->context();
97   if (!aShape.get()) {
98     if (aResultContext.get())
99       aShape = aResultContext->shape();
100   }
101   // whole feature selection
102   FeaturePtr aFeature = aSelectionAttribute->contextFeature();
103
104   std::string aCurrentAttributeId = theCurrentAttribute->id();
105   if (theAttributes.size() > 0 && aShape.get() != NULL) {
106     std::list<AttributePtr>::iterator anAttr = theAttributes.begin();
107     for (; anAttr != theAttributes.end(); anAttr++) {
108       AttributePtr anAttribute = *anAttr;
109       // take into concideration only other attributes
110       if (anAttribute.get() != NULL && anAttribute->id() != aCurrentAttributeId) {
111         aSelectionAttribute =
112           std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
113         // the shape of the attribute should be not the same
114         if (aSelectionAttribute.get() != NULL) {
115           GeomShapePtr anAttrShape = aSelectionAttribute->value();
116           ResultPtr aResult = aSelectionAttribute->context();
117           if (!anAttrShape.get()) {
118             if (aResult.get())
119               anAttrShape = aResult->shape();
120           }
121           if (aShape->isEqual(anAttrShape)) {
122             return false;
123           }
124           if (aFeature.get()) {
125             if (aResult.get()) { // check result is in feature
126               if (aResult->document()->feature(aResult) == aFeature)
127                 return false;
128             }
129             else { // check selection of the same features
130               if (aFeature == aSelectionAttribute->contextFeature())
131                 return false;
132             }
133           }
134           else {
135             if (!aResult.get() && aResultContext.get()) {
136               FeaturePtr aSelectedFeature = aSelectionAttribute->contextFeature();
137               if (aResultContext->document()->feature(aResultContext) == aSelectedFeature)
138                 return false;
139             }
140           }
141         }
142       }
143     }
144   }
145
146   return true;
147 }
148
149 bool GeomValidators_DifferentShapes::isAttrShapesEqual(const AttributePtr& theAttribute,
150                                                        const AttributePtr& theOtherAttribute)
151 {
152   AttributeSelectionPtr aSelectionAttribute =
153     std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
154   AttributeSelectionPtr anOtherSelectionAttribute =
155     std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theOtherAttribute);
156
157   GeomShapePtr aShape = aSelectionAttribute->value();
158   if (!aShape.get()) {
159     ResultPtr aResult = aSelectionAttribute->context();
160     if (aResult.get())
161       aShape = aResult->shape();
162   }
163   GeomShapePtr aTypedShape = GeomAPI_Tools::getTypedShape(aShape);
164
165   GeomShapePtr anOtherShape = anOtherSelectionAttribute->value();
166   if (!anOtherShape.get()) {
167     ResultPtr aResult = anOtherSelectionAttribute->context();
168     if (aResult.get())
169       anOtherShape = aResult->shape();
170   }
171   GeomShapePtr aOtherTypedShape = GeomAPI_Tools::getTypedShape(anOtherShape);
172
173   if (!aTypedShape.get())
174     return !aTypedShape.get() && !aOtherTypedShape.get();
175   return aTypedShape->isEqual(aOtherTypedShape);
176 }