Salome HOME
Error management -- Feature validator returns an error.
[modules/shaper.git] / src / Model / Model_FeatureValidator.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_FeatureValidator.cpp
4 // Created:     8 Jul 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #include <Model_FeatureValidator.h>
8 #include <Model_Validator.h>
9 #include <ModelAPI_Attribute.h>
10 #include <ModelAPI_Data.h>
11 #include <ModelAPI_Feature.h>
12 #include <ModelAPI_Object.h>
13 #include <ModelAPI_Session.h>
14
15 #include <list>
16 #include <memory>
17
18 bool Model_FeatureValidator::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
19                                      const std::list<std::string>& theArguments,
20                                      std::string& theError) const
21 {
22   static Model_ValidatorsFactory* aValidators = 
23     static_cast<Model_ValidatorsFactory*>(ModelAPI_Session::get()->validators());
24
25   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
26   // "Action" features has no data, but still valid. e.g "Remove Part"  
27   if (!aData->isValid()) {
28     return theFeature->isAction();
29   }
30   if (!aData->isValid())
31     return false;
32   const std::string kAllTypes = "";
33   std::list<std::string> aLtAttributes = aData->attributesIDs(kAllTypes);
34   std::list<std::string>::iterator it = aLtAttributes.begin();
35   for (; it != aLtAttributes.end(); it++) {
36     AttributePtr anAttr = aData->attribute(*it);
37     if (!aValidators->isCase(theFeature, anAttr->id()))
38       continue; // this attribute is not participated in the current case
39     if (!anAttr->isInitialized()) { // attribute is not initialized
40       std::map<std::string, std::set<std::string> >::const_iterator aFeatureFind = 
41         myNotObligatory.find(theFeature->getKind());
42       if (aFeatureFind == myNotObligatory.end() || // and it is obligatory for filling
43           aFeatureFind->second.find(*it) == aFeatureFind->second.end()) {
44         return false;
45       }
46     }
47   }
48   return true;
49 }
50
51 void Model_FeatureValidator::registerNotObligatory(std::string theFeature, std::string theAttribute)
52 {
53   std::set<std::string>& anAttrs = myNotObligatory[theFeature];
54   anAttrs.insert(theAttribute);
55 }
56
57 bool Model_FeatureValidator::isNotObligatory(std::string theFeature, std::string theAttribute)
58 {
59   std::set<std::string>& anAttrs = myNotObligatory[theFeature];
60   return anAttrs.find(theAttribute) != anAttrs.end();
61 }