1 // Copyright (C) 2017-20xx CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "GeomValidators_ValueOrder.h"
23 #include <Config_PropManager.h>
24 #include <Events_InfoMessage.h>
26 #include <ModelAPI_AttributeDouble.h>
27 #include <ModelAPI_AttributeInteger.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Validator.h>
32 static double attributeValue(AttributePtr theAttr)
34 AttributeIntegerPtr anIntAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttr);
36 return (double)anIntAttr->value();
37 AttributeDoublePtr aDoubleAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttr);
39 return aDoubleAttr->value();
43 static bool isGreaterOrEqual(AttributePtr theFirstArg, AttributePtr theSecondArg)
45 if (theFirstArg && theFirstArg->isInitialized() &&
46 theSecondArg && theSecondArg->isInitialized())
47 return attributeValue(theFirstArg) >= attributeValue(theSecondArg);
51 static bool isLessOrEqual(AttributePtr theFirstArg, AttributePtr theSecondArg)
53 if (theFirstArg && theFirstArg->isInitialized() &&
54 theSecondArg && theSecondArg->isInitialized())
55 return attributeValue(theFirstArg) <= attributeValue(theSecondArg);
59 // Check the attributes are satisfy theCompare function
60 static bool isValidOrder(const AttributePtr& theAttribute,
61 const std::list<std::string>& theArguments,
62 Events_InfoMessage& theError,
63 bool (*theCompare)(AttributePtr, AttributePtr))
65 std::string anAttrType = theAttribute->attributeType();
66 if (anAttrType != ModelAPI_AttributeDouble::typeId() &&
67 anAttrType != ModelAPI_AttributeInteger::typeId()) {
68 theError = "Unsupported attribute type (integer or double applicable)";
72 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
74 theError = "Attribute without owner";
78 for (std::list<std::string>::const_iterator anIt = theArguments.begin();
79 anIt != theArguments.end(); ++anIt) {
80 // check the argument links to the attribute of the current feature
81 AttributePtr aCurAttr = anOwner->attribute(*anIt);
83 theError = "Arguments should be names of attributes of current feature";
88 if (!(*theCompare)(theAttribute, aCurAttr)) {
89 theError = "Attributes have incorrect order";
99 /// Global instance for validators factory
100 GeomValidators_GreaterOrEqual MY_GEQ_INSTANCE;
101 GeomValidators_LessOrEqual MY_LEQ_INSTANCE;
103 GeomValidators_GreaterOrEqual::GeomValidators_GreaterOrEqual()
105 // this validator is registered in the factory on this library loading
106 SessionPtr aMgr = ModelAPI_Session::get();
107 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
108 aFactory->registerValidator("GeomValidators_GreaterOrEqual", this);
112 bool GeomValidators_GreaterOrEqual::isValid(const AttributePtr& theAttribute,
113 const std::list<std::string>& theArguments,
114 Events_InfoMessage& theError) const
116 return isValidOrder(theAttribute, theArguments, theError, &isGreaterOrEqual);
121 GeomValidators_LessOrEqual::GeomValidators_LessOrEqual()
123 // this validator is registered in the factory on this library loading
124 SessionPtr aMgr = ModelAPI_Session::get();
125 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
126 aFactory->registerValidator("GeomValidators_LessOrEqual", this);
130 bool GeomValidators_LessOrEqual::isValid(const AttributePtr& theAttribute,
131 const std::list<std::string>& theArguments,
132 Events_InfoMessage& theError) const
134 return isValidOrder(theAttribute, theArguments, theError, &isLessOrEqual);