Salome HOME
Updated copyright comment
[modules/shaper.git] / src / GeomValidators / GeomValidators_Positive.cpp
1 // Copyright (C) 2014-2024  CEA, EDF
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
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-12;
48   if (theArguments.size() == 1) {
49     std::list<std::string>::const_iterator anIt = theArguments.begin();
50     double aValue = Config_PropManager::stringToDouble((*anIt).c_str());
51     if (aValue != 0) {
52       // very probably ok
53       aMinValue = aValue;
54     }
55   }
56
57   if (theAttribute->attributeType() == ModelAPI_AttributeDouble::typeId()) {
58     AttributeDoublePtr aDouble =
59         std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
60     if (!aDouble->isInitialized()) {
61       theError = "Double is not initialized.";
62       return false;
63     }
64     if (aDouble->value() <= aMinValue) {
65       theError = "Value is too small.";
66       return false;
67     }
68   }
69
70   if (theAttribute->attributeType() == ModelAPI_AttributeInteger::typeId()) {
71     AttributeIntegerPtr aInteger =
72         std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttribute);
73     if (!aInteger->isInitialized()) {
74       theError = "Integer is not initialized.";
75       return false;
76     }
77     if (aInteger->value() <= floor(aMinValue)) {
78       theError = "Integer is not positive.";
79       return false;
80     }
81   }
82   return true;
83 }