Salome HOME
Added geometrical selection flag to attribute selection list.
[modules/shaper.git] / src / Model / Model_FeatureValidator.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <Model_FeatureValidator.h>
22
23 #include <Events_InfoMessage.h>
24
25 #include <Model_Validator.h>
26 #include <ModelAPI_Attribute.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_Feature.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         // TODO(spo): exceptional case for translation
62         theError = "Attribute \"" + anAttr->id() + "\" is not initialized.";
63 //        theError.arg(anAttr->id());
64         return false;
65       }
66     }
67   }
68   return true;
69 }
70
71 void Model_FeatureValidator::registerNotObligatory(std::string theFeature, std::string theAttribute)
72 {
73   std::set<std::string>& anAttrs = myNotObligatory[theFeature];
74   anAttrs.insert(theAttribute);
75 }
76
77 bool Model_FeatureValidator::isNotObligatory(std::string theFeature, std::string theAttribute)
78 {
79   std::set<std::string>& anAttrs = myNotObligatory[theFeature];
80   return anAttrs.find(theAttribute) != anAttrs.end();
81 }