1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "GeomValidators_DifferentShapes.h"
23 #include <Events_InfoMessage.h>
25 #include <ModelAPI_AttributeSelection.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include "ModelAPI_Object.h"
29 #include <GeomAPI_Shape.h>
30 #include <GeomAPI_Tools.h>
32 //=================================================================================================
33 bool GeomValidators_DifferentShapes::isValid(const AttributePtr& theAttribute,
34 const std::list<std::string>& theArguments,
35 Events_InfoMessage& theError) const
39 std::string anAttributeType = theAttribute->attributeType();
40 bool isList = anAttributeType == ModelAPI_AttributeSelectionList::typeId();
42 std::list<AttributePtr> anAttrs;
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));
51 isValid = checkEquals(anAttrs);
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());
58 isValid = checkEqualToCurrent(anAttrs, theAttribute);
62 theError = isList ? "The selection list contains equal shapes." :
63 "The feature uses equal shapes.";
70 //=================================================================================================
71 bool GeomValidators_DifferentShapes::checkEquals(std::list<AttributePtr>& theAttributes)
73 std::list<AttributePtr>::iterator anIt = theAttributes.begin();
74 for (; anIt != theAttributes.end(); anIt++) {
75 AttributePtr anAttribute = *anIt;
77 std::list<AttributePtr>::iterator anOthersIt = std::next(anIt);
78 for (; anOthersIt != theAttributes.end(); anOthersIt++) {
79 AttributePtr anOtherAttribute = *anOthersIt;
80 if (isAttrShapesEqual(anAttribute, anOtherAttribute)) {
89 //=================================================================================================
90 bool GeomValidators_DifferentShapes::checkEqualToCurrent(std::list<AttributePtr>& theAttributes,
91 const AttributePtr& theCurrentAttribute)
93 AttributeSelectionPtr aSelectionAttribute =
94 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theCurrentAttribute);
96 GeomShapePtr aShape = aSelectionAttribute->value();
98 ResultPtr aResult = aSelectionAttribute->context();
100 aShape = aResult->shape();
103 std::string aCurrentAttributeId = theCurrentAttribute->id();
104 if (theAttributes.size() > 0 && aShape.get() != NULL) {
105 std::list<AttributePtr>::iterator anAttr = theAttributes.begin();
106 for (; anAttr != theAttributes.end(); anAttr++) {
107 AttributePtr anAttribute = *anAttr;
108 // take into concideration only other attributes
109 if (anAttribute.get() != NULL && anAttribute->id() != aCurrentAttributeId) {
110 aSelectionAttribute =
111 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
112 // the shape of the attribute should be not the same
113 if (aSelectionAttribute.get() != NULL) {
114 GeomShapePtr anAttrShape = aSelectionAttribute->value();
115 if (!anAttrShape.get()) {
116 ResultPtr aResult = aSelectionAttribute->context();
118 anAttrShape = aResult->shape();
120 if (aShape->isEqual(anAttrShape)) {
131 bool GeomValidators_DifferentShapes::isAttrShapesEqual(const AttributePtr& theAttribute,
132 const AttributePtr& theOtherAttribute)
134 AttributeSelectionPtr aSelectionAttribute =
135 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
136 AttributeSelectionPtr anOtherSelectionAttribute =
137 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theOtherAttribute);
139 GeomShapePtr aShape = aSelectionAttribute->value();
141 ResultPtr aResult = aSelectionAttribute->context();
143 aShape = aResult->shape();
145 GeomShapePtr aTypedShape = GeomAPI_Tools::getTypedShape(aShape);
147 GeomShapePtr anOtherShape = anOtherSelectionAttribute->value();
148 if (!anOtherShape.get()) {
149 ResultPtr aResult = anOtherSelectionAttribute->context();
151 anOtherShape = aResult->shape();
153 GeomShapePtr aOtherTypedShape = GeomAPI_Tools::getTypedShape(anOtherShape);
155 return aTypedShape->isEqual(aOtherTypedShape);