1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "GeomValidators_DifferentShapes.h"
22 #include <Events_InfoMessage.h>
24 #include <ModelAPI_AttributeSelection.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include "ModelAPI_Object.h"
28 #include <GeomAPI_Shape.h>
29 #include <GeomAPI_Tools.h>
31 //=================================================================================================
32 bool GeomValidators_DifferentShapes::isValid(const AttributePtr& theAttribute,
33 const std::list<std::string>& /*theArguments*/,
34 Events_InfoMessage& theError) const
38 std::string anAttributeType = theAttribute->attributeType();
39 bool isList = anAttributeType == ModelAPI_AttributeSelectionList::typeId();
41 std::list<AttributePtr> anAttrs;
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));
50 isValid = checkEquals(anAttrs);
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());
57 isValid = checkEqualToCurrent(anAttrs, theAttribute);
61 theError = isList ? "The selection list contains equal shapes." :
62 "The feature uses equal shapes.";
69 //=================================================================================================
70 bool GeomValidators_DifferentShapes::checkEquals(std::list<AttributePtr>& theAttributes)
72 std::list<AttributePtr>::iterator anIt = theAttributes.begin();
73 for (; anIt != theAttributes.end(); anIt++) {
74 AttributePtr anAttribute = *anIt;
76 std::list<AttributePtr>::iterator anOthersIt = std::next(anIt);
77 for (; anOthersIt != theAttributes.end(); anOthersIt++) {
78 AttributePtr anOtherAttribute = *anOthersIt;
79 if (isAttrShapesEqual(anAttribute, anOtherAttribute)) {
88 //=================================================================================================
89 bool GeomValidators_DifferentShapes::checkEqualToCurrent(std::list<AttributePtr>& theAttributes,
90 const AttributePtr& theCurrentAttribute)
92 AttributeSelectionPtr aSelectionAttribute =
93 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theCurrentAttribute);
95 GeomShapePtr aShape = aSelectionAttribute->value();
96 ResultPtr aResultContext = aSelectionAttribute->context();
98 if (aResultContext.get())
99 aShape = aResultContext->shape();
101 // whole feature selection
102 FeaturePtr aFeature = aSelectionAttribute->contextFeature();
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()) {
119 anAttrShape = aResult->shape();
121 if (aShape->isEqual(anAttrShape)) {
124 if (aFeature.get()) {
125 if (aResult.get()) { // check result is in feature
126 if (aResult->document()->feature(aResult) == aFeature)
129 else { // check selection of the same features
130 if (aFeature == aSelectionAttribute->contextFeature())
135 if (!aResult.get() && aResultContext.get()) {
136 FeaturePtr aSelectedFeature = aSelectionAttribute->contextFeature();
137 if (aResultContext->document()->feature(aResultContext) == aSelectedFeature)
149 bool GeomValidators_DifferentShapes::isAttrShapesEqual(const AttributePtr& theAttribute,
150 const AttributePtr& theOtherAttribute)
152 AttributeSelectionPtr aSelectionAttribute =
153 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
154 AttributeSelectionPtr anOtherSelectionAttribute =
155 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theOtherAttribute);
157 GeomShapePtr aShape = aSelectionAttribute->value();
159 ResultPtr aResult = aSelectionAttribute->context();
161 aShape = aResult->shape();
163 GeomShapePtr aTypedShape = GeomAPI_Tools::getTypedShape(aShape);
165 GeomShapePtr anOtherShape = anOtherSelectionAttribute->value();
166 if (!anOtherShape.get()) {
167 ResultPtr aResult = anOtherSelectionAttribute->context();
169 anOtherShape = aResult->shape();
171 GeomShapePtr aOtherTypedShape = GeomAPI_Tools::getTypedShape(anOtherShape);
173 if (!aTypedShape.get())
174 return !aTypedShape.get() && !aOtherTypedShape.get();
175 return aTypedShape->isEqual(aOtherTypedShape);