1 // Copyright (C) 2014-2019 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
22 #include "ParametersPlugin_Parameter.h"
24 #include <ModelAPI_AttributeString.h>
25 #include <ModelAPI_ResultParameter.h>
26 #include <ModelAPI_AttributeDouble.h>
27 #include <ModelAPI_AttributeRefList.h>
28 #include <ModelAPI_Tools.h>
29 #include <ModelAPI_Session.h>
30 #include <ModelAPI_Validator.h>
31 #include <ModelAPI_Events.h>
36 ParametersPlugin_Parameter::ParametersPlugin_Parameter()
40 ParametersPlugin_Parameter::~ParametersPlugin_Parameter()
44 void ParametersPlugin_Parameter::initAttributes()
46 data()->addAttribute(VARIABLE_ID(), ModelAPI_AttributeString::typeId());
47 data()->addAttribute(EXPRESSION_ID(), ModelAPI_AttributeString::typeId());
49 data()->addAttribute(COMMENT_ID(), ModelAPI_AttributeString::typeId());
50 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), COMMENT_ID());
52 data()->addAttribute(EXPRESSION_ERROR_ID(), ModelAPI_AttributeString::typeId());
53 data()->string(EXPRESSION_ERROR_ID())->setIsArgument(false);
54 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXPRESSION_ERROR_ID());
56 data()->addAttribute(ARGUMENTS_ID(), ModelAPI_AttributeRefList::typeId());
57 data()->reflist(ARGUMENTS_ID())->setIsArgument(false);
58 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ARGUMENTS_ID());
61 bool ParametersPlugin_Parameter::isInHistory()
66 void ParametersPlugin_Parameter::attributeChanged(const std::string& theID)
68 if (theID == EXPRESSION_ID())
71 data()->execState(ModelAPI_StateMustBeUpdated);
74 void ParametersPlugin_Parameter::updateName()
76 std::string aName = string(VARIABLE_ID())->value();
77 data()->setName(aName);
79 ResultParameterPtr aParam = document()->createParameter(data());
80 std::string anOldName = aParam->data()->name();
81 aParam->data()->setName(aName);
85 // #2474 : if parameter name now hides/shows the higher level parameter name,
86 // update the depended expressions
87 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
88 if (aParam->document() != aRootDoc) {
89 std::list<std::string> aNames; // collect names in the root document that must be checked
90 aNames.push_back(aName);
91 if (anOldName != aName) {
92 aNames.push_back(anOldName);
94 std::list<std::string>::iterator aNIter = aNames.begin();
95 for (; aNIter != aNames.end(); aNIter++) {
97 ResultParameterPtr aRootParam;
99 std::dynamic_pointer_cast<ModelAPI_Feature>(string(VARIABLE_ID())->owner());
100 if (ModelAPI_Tools::findVariable(aThis, *aNIter, aValue, aRootParam, aRootDoc)) {
101 std::set<std::shared_ptr<ModelAPI_Attribute> > anAttributes =
102 aRootParam->data()->refsToMe();
103 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anAttributeIt =
104 anAttributes.cbegin();
105 for (; anAttributeIt != anAttributes.cend(); ++anAttributeIt) {
106 const AttributePtr& anAttribute = *anAttributeIt;
107 ModelAPI_AttributeEvalMessage::send(anAttribute, NULL);
114 bool ParametersPlugin_Parameter::updateExpression()
116 std::string anExpression = string(EXPRESSION_ID())->value();
118 std::string outErrorMessage;
119 double aValue = evaluate(anExpression, outErrorMessage);
121 data()->string(EXPRESSION_ERROR_ID())->setValue(outErrorMessage);
122 if (!outErrorMessage.empty())
125 ResultParameterPtr aParam = document()->createParameter(data());
126 AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
127 aValueAttribute->setValue(aValue);
133 void ParametersPlugin_Parameter::execute()
136 if (!updateExpression())
137 setError("Expression error.", false);
140 double ParametersPlugin_Parameter::evaluate(const std::string& theExpression, std::string& theError)
142 FeaturePtr aMyPtr = std::dynamic_pointer_cast<ModelAPI_Feature>(data()->owner());
143 std::shared_ptr<ModelAPI_ParameterEvalMessage> aProcessMessage =
144 ModelAPI_ParameterEvalMessage::send(aMyPtr, this);
147 if (aProcessMessage->isProcessed()) {
148 const std::list<ResultParameterPtr>& aParamsList = aProcessMessage->params();
149 aResult = aProcessMessage->result();
150 theError = aProcessMessage->error();
151 // compare the list of parameters to store if changed
152 AttributeRefListPtr aParams = reflist(ARGUMENTS_ID());
153 bool aDifferent = aParams->size() != aParamsList.size();
155 std::list<ResultParameterPtr>::const_iterator aNewIter = aParamsList.begin();
156 std::list<ObjectPtr> anOldList = aParams->list();
157 std::list<ObjectPtr>::const_iterator anOldIter = anOldList.begin();
158 for(; !aDifferent && aNewIter != aParamsList.end(); aNewIter++, anOldIter++) {
159 if (*aNewIter != *anOldIter)
165 std::list<ResultParameterPtr>::const_iterator aNewIter = aParamsList.begin();
166 for(; aNewIter != aParamsList.end(); aNewIter++) {
167 aParams->append(*aNewIter);
170 } else { // error: python interpreter is not active
171 theError = "Python interpreter is not available";
176 bool ParametersPlugin_Parameter::isPreviewNeeded() const