Salome HOME
Ubuntu problem correction: zero deflection was used because of another system local...
[modules/shaper.git] / src / GeomValidators / GeomValidators_Positive.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomValidators_ValidatorPositive.cpp
4 // Created:     16 Sep 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include "GeomValidators_Positive.h"
8
9 #include <Config_PropManager.h>
10 #include <Events_InfoMessage.h>
11
12 #include <ModelAPI_AttributeDouble.h>
13 #include <ModelAPI_AttributeInteger.h>
14 #include <ModelAPI_Session.h>
15 #include <ModelAPI_Validator.h>
16
17 #include <cmath>
18
19 /// Global instance for validators factory
20 GeomValidators_Positive MY_POSITIVE_INSTANCE;
21
22 GeomValidators_Positive::GeomValidators_Positive()
23 {
24   // this validator is registered in the factory on this library loading
25   SessionPtr aMgr = ModelAPI_Session::get();
26   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
27   aFactory->registerValidator("GeomValidators_Positive", this);
28 }
29
30 bool GeomValidators_Positive::isValid(const AttributePtr& theAttribute,
31                                       const std::list<std::string>& theArguments,
32                                       Events_InfoMessage& theError) const
33 {
34   double aMinValue = 1.e-5;
35   if(theArguments.size() == 1) {
36     std::list<std::string>::const_iterator anIt = theArguments.begin();
37     char *aErr;
38     double aValue = Config_PropManager::stringToDouble((*anIt).c_str());
39     if(aValue != 0) {
40       // very probably ok
41       aMinValue = aValue;
42     }
43   }
44
45   if (theAttribute->attributeType() == ModelAPI_AttributeDouble::typeId()) {
46     AttributeDoublePtr aDouble =
47         std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
48     if (!aDouble->isInitialized()) {
49       theError = "Double is not initialized.";
50       return false;
51     }
52     if (aDouble->value() <= aMinValue) {
53       theError = "Double is not positive.";
54       return false;
55     }
56   }
57
58   if (theAttribute->attributeType() == ModelAPI_AttributeInteger::typeId()) {
59     AttributeIntegerPtr aInteger =
60         std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttribute);
61     if (!aInteger->isInitialized()) {
62       theError = "Integer is not initialized.";
63       return false;
64     }
65     if (aInteger->value() <= floor(aMinValue)) {
66       theError = "Integer is not positive.";
67       return false;
68     }
69   }
70
71   return true;
72 }