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