Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / GeomValidators / GeomValidators_Positive.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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include "GeomValidators_Positive.h"
21
22 #include <Config_PropManager.h>
23 #include <Events_InfoMessage.h>
24
25 #include <ModelAPI_AttributeDouble.h>
26 #include <ModelAPI_AttributeInteger.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Validator.h>
29
30 #include <cmath>
31
32 /// Global instance for validators factory
33 GeomValidators_Positive MY_POSITIVE_INSTANCE;
34
35 GeomValidators_Positive::GeomValidators_Positive()
36 {
37   // this validator is registered in the factory on this library loading
38   SessionPtr aMgr = ModelAPI_Session::get();
39   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
40   aFactory->registerValidator("GeomValidators_Positive", this);
41 }
42
43 bool GeomValidators_Positive::isValid(const AttributePtr& theAttribute,
44                                       const std::list<std::string>& theArguments,
45                                       Events_InfoMessage& theError) const
46 {
47   double aMinValue = 1.e-5;
48   if(theArguments.size() == 1) {
49     std::list<std::string>::const_iterator anIt = theArguments.begin();
50     char *aErr;
51     double aValue = Config_PropManager::stringToDouble((*anIt).c_str());
52     if(aValue != 0) {
53       // very probably ok
54       aMinValue = aValue;
55     }
56   }
57
58   if (theAttribute->attributeType() == ModelAPI_AttributeDouble::typeId()) {
59     AttributeDoublePtr aDouble =
60         std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
61     if (!aDouble->isInitialized()) {
62       theError = "Double is not initialized.";
63       return false;
64     }
65     if (aDouble->value() <= aMinValue) {
66       theError = "Double is not positive.";
67       return false;
68     }
69   }
70
71   if (theAttribute->attributeType() == ModelAPI_AttributeInteger::typeId()) {
72     AttributeIntegerPtr aInteger =
73         std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttribute);
74     if (!aInteger->isInitialized()) {
75       theError = "Integer is not initialized.";
76       return false;
77     }
78     if (aInteger->value() <= floor(aMinValue)) {
79       theError = "Integer is not positive.";
80       return false;
81     }
82   }
83
84   return true;
85 }