-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
-
-// File: ParametersPlugin_Parameter.cpp
-// Created: 23 MArch 2015
-// Author: sbh
+// Copyright (C) 2014-2019 CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
#include <pyconfig.h>
#include "ParametersPlugin_Parameter.h"
-#include <ParametersPlugin_PyInterp.h>
#include <ModelAPI_AttributeString.h>
#include <ModelAPI_ResultParameter.h>
#include <ModelAPI_Tools.h>
#include <ModelAPI_Session.h>
#include <ModelAPI_Validator.h>
+#include <ModelAPI_Events.h>
#include <string>
#include <sstream>
ParametersPlugin_Parameter::ParametersPlugin_Parameter()
{
- myInterp = std::shared_ptr<ParametersPlugin_PyInterp>(new ParametersPlugin_PyInterp());
- myInterp->initialize();
}
ParametersPlugin_Parameter::~ParametersPlugin_Parameter()
data()->addAttribute(VARIABLE_ID(), ModelAPI_AttributeString::typeId());
data()->addAttribute(EXPRESSION_ID(), ModelAPI_AttributeString::typeId());
+ data()->addAttribute(COMMENT_ID(), ModelAPI_AttributeString::typeId());
+ ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), COMMENT_ID());
+
data()->addAttribute(EXPRESSION_ERROR_ID(), ModelAPI_AttributeString::typeId());
data()->string(EXPRESSION_ERROR_ID())->setIsArgument(false);
ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXPRESSION_ERROR_ID());
{
if (theID == EXPRESSION_ID())
updateExpression();
+
+ data()->execState(ModelAPI_StateMustBeUpdated);
}
void ParametersPlugin_Parameter::updateName()
{
std::string aName = string(VARIABLE_ID())->value();
- data()->setName(aName);
+ data()->setName(ModelAPI_Tools::toWString(aName));
ResultParameterPtr aParam = document()->createParameter(data());
- aParam->data()->setName(aName);
+ std::string anOldName = ModelAPI_Tools::toString(aParam->data()->name());
+ aParam->data()->setName(ModelAPI_Tools::toWString(aName));
setResult(aParam);
+
+
+ // #2474 : if parameter name now hides/shows the higher level parameter name,
+ // update the depended expressions
+ DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
+ if (aParam->document() != aRootDoc) {
+ std::list<std::string> aNames; // collect names in the root document that must be checked
+ aNames.push_back(aName);
+ if (anOldName != aName) {
+ aNames.push_back(anOldName);
+ }
+ std::list<std::string>::iterator aNIter = aNames.begin();
+ for (; aNIter != aNames.end(); aNIter++) {
+ double aValue;
+ ResultParameterPtr aRootParam;
+ FeaturePtr aThis =
+ std::dynamic_pointer_cast<ModelAPI_Feature>(string(VARIABLE_ID())->owner());
+ if (ModelAPI_Tools::findVariable(aThis, *aNIter, aValue, aRootParam, aRootDoc)) {
+ std::set<std::shared_ptr<ModelAPI_Attribute> > anAttributes =
+ aRootParam->data()->refsToMe();
+ std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anAttributeIt =
+ anAttributes.cbegin();
+ for (; anAttributeIt != anAttributes.cend(); ++anAttributeIt) {
+ const AttributePtr& anAttribute = *anAttributeIt;
+ ModelAPI_AttributeEvalMessage::send(anAttribute, NULL);
+ }
+ }
+ }
+ }
}
-void ParametersPlugin_Parameter::updateExpression()
+bool ParametersPlugin_Parameter::updateExpression()
{
std::string anExpression = string(EXPRESSION_ID())->value();
double aValue = evaluate(anExpression, outErrorMessage);
data()->string(EXPRESSION_ERROR_ID())->setValue(outErrorMessage);
- if (!outErrorMessage.empty()) {
- setError("Expression error.", false);
- data()->execState(ModelAPI_StateExecFailed);
- return;
- }
+ if (!outErrorMessage.empty())
+ return false;
ResultParameterPtr aParam = document()->createParameter(data());
AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
aValueAttribute->setValue(aValue);
setResult(aParam);
- setError("", false);
- data()->execState(ModelAPI_StateDone);
+ return true;
}
void ParametersPlugin_Parameter::execute()
{
updateName();
- updateExpression();
+ if (!updateExpression())
+ setError("Expression error.", false);
}
double ParametersPlugin_Parameter::evaluate(const std::string& theExpression, std::string& theError)
{
- std::list<std::string> anExprParams = myInterp->compile(theExpression);
- // find expression's params in the model
- std::list<std::string> aContext;
- std::list<std::string>::iterator it = anExprParams.begin();
- std::list<ResultParameterPtr> aParamsList;
- for ( ; it != anExprParams.end(); it++) {
- std::string& aVariableName = *it;
-
- double aValue;
- ResultParameterPtr aParamRes;
- if (!ModelAPI_Tools::findVariable(aVariableName, aValue, aParamRes, document())) continue;
- aParamsList.push_back(aParamRes);
-
- std::ostringstream sstream;
- sstream << aValue;
- std::string aParamValue = sstream.str();
- aContext.push_back(*it + "=" + aParamValue);
- }
- // compare the list of parameters to store if changed
- AttributeRefListPtr aParams = reflist(ARGUMENTS_ID());
- bool aDifferent = aParams->size() != aParamsList.size();
- if (!aDifferent) {
- std::list<ResultParameterPtr>::iterator aNewIter = aParamsList.begin();
- std::list<ObjectPtr> anOldList = aParams->list();
- std::list<ObjectPtr>::iterator anOldIter = anOldList.begin();
- for(; !aDifferent && aNewIter != aParamsList.end(); aNewIter++, anOldIter++) {
- if (*aNewIter != *anOldIter)
- aDifferent = true;
+ FeaturePtr aMyPtr = std::dynamic_pointer_cast<ModelAPI_Feature>(data()->owner());
+ std::shared_ptr<ModelAPI_ParameterEvalMessage> aProcessMessage =
+ ModelAPI_ParameterEvalMessage::send(aMyPtr, this);
+
+ double aResult = 0;
+ if (aProcessMessage->isProcessed()) {
+ const std::list<ResultParameterPtr>& aParamsList = aProcessMessage->params();
+ aResult = aProcessMessage->result();
+ theError = aProcessMessage->error();
+ // compare the list of parameters to store if changed
+ AttributeRefListPtr aParams = reflist(ARGUMENTS_ID());
+ bool aDifferent = aParams->size() != aParamsList.size();
+ if (!aDifferent) {
+ std::list<ResultParameterPtr>::const_iterator aNewIter = aParamsList.begin();
+ std::list<ObjectPtr> anOldList = aParams->list();
+ std::list<ObjectPtr>::const_iterator anOldIter = anOldList.begin();
+ for(; !aDifferent && aNewIter != aParamsList.end(); aNewIter++, anOldIter++) {
+ if (*aNewIter != *anOldIter)
+ aDifferent = true;
+ }
}
- }
- if (aDifferent) {
- aParams->clear();
- std::list<ResultParameterPtr>::iterator aNewIter = aParamsList.begin();
- for(; aNewIter != aParamsList.end(); aNewIter++) {
- aParams->append(*aNewIter);
+ if (aDifferent) {
+ aParams->clear();
+ std::list<ResultParameterPtr>::const_iterator aNewIter = aParamsList.begin();
+ for(; aNewIter != aParamsList.end(); aNewIter++) {
+ aParams->append(*aNewIter);
+ }
}
+ } else { // error: python interpreter is not active
+ theError = "Python interpreter is not available";
}
-
- myInterp->extendLocalContext(aContext);
- double result = myInterp->evaluate(theExpression, theError);
- myInterp->clearLocalContext();
- return result;
+ return aResult;
}
bool ParametersPlugin_Parameter::isPreviewNeeded() const
{
- return false;
+ return true;
}