Salome HOME
c840e8c2be18ef823e85c0fdbaa8c1a6f6ab1923
[modules/shaper.git] / src / Model / Model_FeatureValidator.cpp
1 // Copyright (C) 2014-2023  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_FeatureValidator.h>
21
22 #include <Events_InfoMessage.h>
23
24 #include <Model_Validator.h>
25 #include <ModelAPI_Attribute.h>
26 #include <ModelAPI_Data.h>
27 #include <ModelAPI_Feature.h>
28 #include <ModelAPI_FiltersFeature.h>
29 #include <ModelAPI_Object.h>
30 #include <ModelAPI_Session.h>
31
32 #include <list>
33 #include <memory>
34
35 bool Model_FeatureValidator::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
36                                      const std::list<std::string>& /*theArguments*/,
37                                      Events_InfoMessage& theError) const
38 {
39   static Model_ValidatorsFactory* aValidators =
40     static_cast<Model_ValidatorsFactory*>(ModelAPI_Session::get()->validators());
41
42   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
43   // "Action" features has no data, but still valid. e.g "Remove Part"
44   if (!aData->isValid()) {
45     if (!theFeature->isAction())
46       theError = "There is no data.";
47     return theFeature->isAction();
48   }
49   const std::string kAllTypes = "";
50   std::list<std::string> aLtAttributes = aData->attributesIDs(kAllTypes);
51   std::list<std::string>::iterator it = aLtAttributes.begin();
52   for (; it != aLtAttributes.end(); it++) {
53     AttributePtr anAttr = aData->attribute(*it);
54     if (!aValidators->isCase(theFeature, anAttr->id()))
55       continue; // this attribute is not participated in the current case
56     if (!anAttr->isInitialized()) { // attribute is not initialized
57       std::map<std::string, std::set<std::string> >::const_iterator aFeatureFind =
58         myNotObligatory.find(theFeature->getKind());
59       if (aFeatureFind == myNotObligatory.end() || // and it is obligatory for filling
60           aFeatureFind->second.find(*it) == aFeatureFind->second.end()) {
61         theError = "Attribute \"%1\" is not initialized.";
62         theError.addParameter(anAttr->id());
63         // workaround for the filters selection feature: do not append the attribute id
64         if (std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(theFeature))
65           theError.setContext(theFeature->getKind());
66         else
67           theError.setContext(theFeature->getKind() + ":" + anAttr->id());
68         return false;
69       }
70     }
71   }
72   return true;
73 }
74
75 void Model_FeatureValidator::registerNotObligatory(std::string theFeature, std::string theAttribute)
76 {
77   std::set<std::string>& anAttrs = myNotObligatory[theFeature];
78   anAttrs.insert(theAttribute);
79 }
80
81 bool Model_FeatureValidator::isNotObligatory(std::string theFeature, std::string theAttribute)
82 {
83   std::set<std::string>& anAttrs = myNotObligatory[theFeature];
84   return anAttrs.find(theAttribute) != anAttrs.end();
85 }