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