Salome HOME
Issue #2079 : Select parent feature for default Constructions. Disable popup menu...
[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 <Events_InfoMessage.h>
10
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_AttributeInteger.h>
13 #include <ModelAPI_Session.h>
14 #include <ModelAPI_Validator.h>
15
16 #include <cmath>
17
18 /// Global instance for validators factory
19 GeomValidators_Positive MY_POSITIVE_INSTANCE;
20
21 GeomValidators_Positive::GeomValidators_Positive()
22 {
23   // this validator is registered in the factory on this library loading
24   SessionPtr aMgr = ModelAPI_Session::get();
25   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
26   aFactory->registerValidator("GeomValidators_Positive", this);
27 }
28
29 bool GeomValidators_Positive::isValid(const AttributePtr& theAttribute,
30                                       const std::list<std::string>& theArguments,
31                                       Events_InfoMessage& theError) const
32 {
33   double aMinValue = 1.e-5;
34   if(theArguments.size() == 1) {
35     std::list<std::string>::const_iterator anIt = theArguments.begin();
36     char *aErr;
37     double aValue = strtod((*anIt).c_str(), &aErr);
38     if(*aErr == 0) {
39       // very probably ok
40       aMinValue = aValue;
41     }
42   }
43
44   if (theAttribute->attributeType() == ModelAPI_AttributeDouble::typeId()) {
45     AttributeDoublePtr aDouble =
46         std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
47     if (!aDouble->isInitialized()) {
48       theError = "Double is not initialized.";
49       return false;
50     }
51     if (aDouble->value() <= aMinValue) {
52       theError = "Double is not positive.";
53       return false;
54     }
55   }
56
57   if (theAttribute->attributeType() == ModelAPI_AttributeInteger::typeId()) {
58     AttributeIntegerPtr aInteger =
59         std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttribute);
60     if (!aInteger->isInitialized()) {
61       theError = "Integer is not initialized.";
62       return false;
63     }
64     if (aInteger->value() <= floor(aMinValue)) {
65       theError = "Integer is not positive.";
66       return false;
67     }
68   }
69
70   return true;
71 }