1 // Copyright (C) 2014-2019 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 "Model_AttributeValidator.h"
22 #include <Events_InfoMessage.h>
24 #include <ModelAPI_AttributeDouble.h>
25 #include <ModelAPI_AttributeInteger.h>
26 #include <ModelAPI_Session.h>
27 #include <ModelAPI_Tools.h>
28 #include <ModelAPI_ResultBody.h>
29 #include <ModelAPI_ResultPart.h>
31 #include <GeomDataAPI_Point.h>
32 #include <GeomDataAPI_Point2D.h>
34 bool Model_AttributeValidator::isValid(const AttributePtr& theAttribute,
35 const std::list<std::string>& theArguments,
36 Events_InfoMessage& theError) const
38 if (theAttribute->attributeType() == ModelAPI_AttributeInteger::typeId()) {
39 AttributeIntegerPtr anAttribue =
40 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttribute);
41 if (!anAttribue->expressionError().empty()) {
42 theError = "Expression error: %1";
43 theError.arg(anAttribue->expressionError());
47 if (theAttribute->attributeType() == ModelAPI_AttributeDouble::typeId()) {
48 AttributeDoublePtr anAttribue =
49 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
50 if (!anAttribue->expressionError().empty()) {
51 theError = "Expression error: %1";
52 theError.arg(anAttribue->expressionError());
56 if (theAttribute->attributeType() == GeomDataAPI_Point::typeId()) {
57 AttributePointPtr anAttribue =
58 std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
60 const char* aComponent[] = {"X", "Y", "Z"};
61 std::string anErrorMessage;
62 for (int i = 0; i < 3; ++i) {
63 if (!anAttribue->expressionError(i).empty())
64 anErrorMessage.append("\n").append(aComponent[i])
65 .append(": ").append(anAttribue->expressionError(i));
67 if (!anErrorMessage.empty()) {
68 theError = "Expression error: %1";
69 theError.arg(anErrorMessage);
73 if (theAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
74 AttributePoint2DPtr anAttribue =
75 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
77 const char* aComponent[] = {"X", "Y"};
78 std::string anErrorMessage;
79 for (int i = 0; i < 2; ++i) {
80 if (!anAttribue->expressionError(i).empty())
81 anErrorMessage.append("\n").append(aComponent[i])
82 .append(": ").append(anAttribue->expressionError(i));
84 if (!anErrorMessage.empty()) {
85 theError = "Expression error: %1";
86 theError.arg(anErrorMessage);
89 } else { // #2903 : check that concealed attribute refers to already concealed result
90 FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
92 std::set<ObjectPtr> alreadyProcessed; // optimization
94 ModelAPI_Session::get()->validators()->isConcealed(aFeat->getKind(), theAttribute->id())) {
95 std::list<std::pair<std::string, std::list<ObjectPtr> > > allRefs;
96 aFeat->data()->referencesToObjects(allRefs);
97 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator anIter = allRefs.begin();
98 for(; anIter != allRefs.end(); anIter++) {
99 if (anIter->first == theAttribute->id()) {
100 const std::list<ObjectPtr>& aReferencedList = anIter->second;
101 std::list<ObjectPtr>::const_iterator aRefIter = aReferencedList.cbegin();
102 for(; aRefIter != aReferencedList.cend(); aRefIter++) {
103 const ObjectPtr& aReferenced = *aRefIter;
104 // get all results and feature that is referenced to see all references to them
105 FeaturePtr aReferencedFeature;
106 if (aReferenced->groupName() == ModelAPI_Feature::group()) {
107 aReferencedFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aReferenced);
109 aReferencedFeature = aReferenced->document()->feature(
110 std::dynamic_pointer_cast<ModelAPI_Result>(aReferenced));
112 if (alreadyProcessed.count(aReferencedFeature))
114 alreadyProcessed.insert(aReferencedFeature);
115 std::list<ResultPtr> aReferencedResults;
116 ModelAPI_Tools::allResults(aReferencedFeature, aReferencedResults);
117 std::list<ResultPtr>::iterator aRefRes = aReferencedResults.begin();
118 bool aCheckFeature = true; // the last iteration to check the feature
119 while(aRefRes != aReferencedResults.end() || aCheckFeature) {
121 if (aRefRes == aReferencedResults.end()) {
122 aRefd = aReferencedFeature;
123 aCheckFeature = false;
126 if (aRefd->groupName() != ModelAPI_ResultBody::group())
129 if (!aRefd->data().get() || !aRefd->data()->isValid())
131 const std::set<AttributePtr>& aRefsToRef = aRefd->data()->refsToMe();
132 std::set<AttributePtr>::const_iterator aRR = aRefsToRef.cbegin();
133 for(; aRR != aRefsToRef.cend(); aRR++) {
134 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRR)->owner());
135 if (!aRefFeat.get() || aRefFeat == aFeat)
137 if (ModelAPI_Session::get()->validators()->isConcealed(
138 aRefFeat->getKind(), (*aRR)->id())) {
139 theError = "Reference to concealed object %1";
140 theError.arg(aRefd->data()->name());