Salome HOME
updated copyright message
[modules/shaper.git] / src / Model / Model_AttributeValidator.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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             if (!aReferenced.get())
105               continue;
106             // get all results and feature that is referenced to see all references to them
107             FeaturePtr aReferencedFeature;
108             if (aReferenced->groupName() == ModelAPI_Feature::group()) {
109               aReferencedFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aReferenced);
110             } else {
111               aReferencedFeature = aReferenced->document()->feature(
112                 std::dynamic_pointer_cast<ModelAPI_Result>(aReferenced));
113             }
114             if (alreadyProcessed.count(aReferencedFeature))
115               continue;
116             alreadyProcessed.insert(aReferencedFeature);
117             /* it takes all results, not only concealed
118             std::list<ResultPtr> aReferencedResults;
119             ModelAPI_Tools::allResults(aReferencedFeature, aReferencedResults);
120             */
121             std::list<ResultPtr> aReferencedResults;
122             ResultBodyPtr aRefBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aReferenced);
123             if (aRefBody.get()) { // take only sub-results of this result or sub-result
124               ResultBodyPtr aRoot = ModelAPI_Tools::bodyOwner(aRefBody, true);
125               if (aRoot.get()) {
126                 ModelAPI_Tools::allSubs(aRoot, aReferencedResults, false);
127                 aReferencedResults.push_back(aRoot);
128               } else
129                 aReferencedResults.push_back(aRefBody);
130             }
131
132             std::list<ResultPtr>::iterator aRefRes = aReferencedResults.begin();
133             bool aCheckFeature = true; // the last iteration to check the feature
134             while(aRefRes != aReferencedResults.end() || aCheckFeature) {
135               ObjectPtr aRefd;
136               if (aRefRes == aReferencedResults.end()) {
137                 aRefd = aReferencedFeature;
138                 aCheckFeature = false;
139                 if (!aReferencedFeature->results().empty() &&
140                     aReferencedFeature->firstResult()->groupName() != ModelAPI_ResultBody::group())
141                   break;
142               } else {
143                 aRefd = *aRefRes;
144                 if (aRefd->groupName() != ModelAPI_ResultBody::group())
145                   break;
146               }
147               if (!aRefd->data().get() || !aRefd->data()->isValid())
148                 continue;
149               const std::set<AttributePtr>& aRefsToRef = aRefd->data()->refsToMe();
150               std::set<AttributePtr>::const_iterator aRR = aRefsToRef.cbegin();
151               for(; aRR != aRefsToRef.cend(); aRR++) {
152                 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRR)->owner());
153                 if (!aRefFeat.get() || aRefFeat == aFeat)
154                   continue;
155                 if (ModelAPI_Session::get()->validators()->isConcealed(
156                     aRefFeat->getKind(), (*aRR)->id())) {
157                   // check this feature is later than another referenced to make unit tests working
158                   //because of Test1757 and others: allow to move selection context of this to next
159                   if (aFeat->document()->isLater(aFeat, aRefFeat)) {
160                     theError = "Reference to concealed object %1";
161                     theError.arg(aRefd->data()->name());
162                     return false;
163                   }
164                 }
165               }
166               if (aCheckFeature)
167                 aRefRes++;
168             }
169           }
170         }
171       }
172     }
173   }
174   return true;
175 }