1 // Copyright (C) 2017-2020 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 email : webmaster.salome@opencascade.com
20 #include "GeomValidators_ValueOrder.h"
22 #include <Config_PropManager.h>
23 #include <Events_InfoMessage.h>
25 #include <ModelAPI_AttributeDouble.h>
26 #include <ModelAPI_AttributeInteger.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Validator.h>
31 static double attributeValue(AttributePtr theAttr)
33 AttributeIntegerPtr anIntAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttr);
35 return (double)anIntAttr->value();
36 AttributeDoublePtr aDoubleAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttr);
38 return aDoubleAttr->value();
42 static bool isGreaterOrEqual(AttributePtr theFirstArg, AttributePtr theSecondArg)
44 if (theFirstArg && theFirstArg->isInitialized() &&
45 theSecondArg && theSecondArg->isInitialized())
46 return attributeValue(theFirstArg) >= attributeValue(theSecondArg);
50 static bool isLessOrEqual(AttributePtr theFirstArg, AttributePtr theSecondArg)
52 if (theFirstArg && theFirstArg->isInitialized() &&
53 theSecondArg && theSecondArg->isInitialized())
54 return attributeValue(theFirstArg) <= attributeValue(theSecondArg);
58 // Check the attributes are satisfy theCompare function
59 static bool isValidOrder(const AttributePtr& theAttribute,
60 const std::list<std::string>& theArguments,
61 Events_InfoMessage& theError,
62 bool (*theCompare)(AttributePtr, AttributePtr))
64 std::string anAttrType = theAttribute->attributeType();
65 if (anAttrType != ModelAPI_AttributeDouble::typeId() &&
66 anAttrType != ModelAPI_AttributeInteger::typeId()) {
67 theError = "Unsupported attribute type (integer or double applicable)";
71 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
73 theError = "Attribute without owner";
77 for (std::list<std::string>::const_iterator anIt = theArguments.begin();
78 anIt != theArguments.end(); ++anIt) {
79 // check the argument links to the attribute of the current feature
80 AttributePtr aCurAttr = anOwner->attribute(*anIt);
82 theError = "Arguments should be names of attributes of current feature";
87 if (!(*theCompare)(theAttribute, aCurAttr)) {
88 theError = "Attributes have incorrect order";
98 /// Global instance for validators factory
99 GeomValidators_GreaterOrEqual MY_GEQ_INSTANCE;
100 GeomValidators_LessOrEqual MY_LEQ_INSTANCE;
102 GeomValidators_GreaterOrEqual::GeomValidators_GreaterOrEqual()
104 // this validator is registered in the factory on this library loading
105 SessionPtr aMgr = ModelAPI_Session::get();
106 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
107 aFactory->registerValidator("GeomValidators_GreaterOrEqual", this);
111 bool GeomValidators_GreaterOrEqual::isValid(const AttributePtr& theAttribute,
112 const std::list<std::string>& theArguments,
113 Events_InfoMessage& theError) const
115 return isValidOrder(theAttribute, theArguments, theError, &isGreaterOrEqual);
120 GeomValidators_LessOrEqual::GeomValidators_LessOrEqual()
122 // this validator is registered in the factory on this library loading
123 SessionPtr aMgr = ModelAPI_Session::get();
124 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
125 aFactory->registerValidator("GeomValidators_LessOrEqual", this);
129 bool GeomValidators_LessOrEqual::isValid(const AttributePtr& theAttribute,
130 const std::list<std::string>& theArguments,
131 Events_InfoMessage& theError) const
133 return isValidOrder(theAttribute, theArguments, theError, &isLessOrEqual);