]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_AttributeValidator.cpp
Salome HOME
ab01428148b1f47f90fab019e49e2182233914a0
[modules/shaper.git] / src / Model / Model_AttributeValidator.cpp
1 // Copyright (C) 2014-2019  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 "Model_AttributeValidator.h"
21
22 #include <Events_InfoMessage.h>
23
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>
30
31 #include <GeomDataAPI_Point.h>
32 #include <GeomDataAPI_Point2D.h>
33
34 bool Model_AttributeValidator::isValid(const AttributePtr& theAttribute,
35                                        const std::list<std::string>& theArguments,
36                                        Events_InfoMessage& theError) const
37 {
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());
44       return false;
45     }
46   } else
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());
53       return false;
54     }
55   } else
56   if (theAttribute->attributeType() == GeomDataAPI_Point::typeId()) {
57     AttributePointPtr anAttribue =
58         std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
59
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));
66     }
67     if (!anErrorMessage.empty()) {
68       theError = "Expression error: %1";
69       theError.arg(anErrorMessage);
70       return false;
71     }
72   } else
73   if (theAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
74     AttributePoint2DPtr anAttribue =
75         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
76
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));
83     }
84     if (!anErrorMessage.empty()) {
85       theError = "Expression error: %1";
86       theError.arg(anErrorMessage);
87       return false;
88     }
89   } else { // #2903 : check that concealed attribute refers to already concealed result
90     FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
91
92     std::set<ObjectPtr> alreadyProcessed; // optimization
93     if (aFeat.get() &&
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);
108             } else {
109               aReferencedFeature = aReferenced->document()->feature(
110                 std::dynamic_pointer_cast<ModelAPI_Result>(aReferenced));
111             }
112             if (alreadyProcessed.count(aReferencedFeature))
113               continue;
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) {
120               ObjectPtr aRefd;
121               if (aRefRes == aReferencedResults.end()) {
122                 aRefd = aReferencedFeature;
123                 aCheckFeature = false;
124               } else {
125                 aRefd = *aRefRes;
126                 if (aRefd->groupName() != ModelAPI_ResultBody::group())
127                   break;
128               }
129               if (!aRefd->data().get() || !aRefd->data()->isValid())
130                 continue;
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)
136                   continue;
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());
141                   return false;
142                 }
143               }
144               if (aCheckFeature)
145                 aRefRes++;
146             }
147           }
148         }
149       }
150     }
151   }
152   return true;
153 }