X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FParametersPlugin%2FParametersPlugin_Parameter.cpp;h=bb118bb058e8d17e8ab332e961f1d222be26d851;hb=50a8df0c6a66da8067b16155e5ae39f8f26a7ebc;hp=413e7dc9bcacf220e88d7737843b866cdf1cde7f;hpb=fd4dd622a85a0dd19ff5616391f94e980abc26a1;p=modules%2Fshaper.git diff --git a/src/ParametersPlugin/ParametersPlugin_Parameter.cpp b/src/ParametersPlugin/ParametersPlugin_Parameter.cpp index 413e7dc9b..bb118bb05 100644 --- a/src/ParametersPlugin/ParametersPlugin_Parameter.cpp +++ b/src/ParametersPlugin/ParametersPlugin_Parameter.cpp @@ -1,37 +1,61 @@ -// Copyright (C) 2014-20xx CEA/DEN, EDF R&D --> +// Copyright (C) 2014-2020 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 +// -// File: ParametersPlugin_Parameter.cpp -// Created: 23 MArch 2015 -// Author: sbh +#include #include "ParametersPlugin_Parameter.h" -#include #include #include #include +#include +#include +#include +#include +#include #include #include ParametersPlugin_Parameter::ParametersPlugin_Parameter() { - myInterp = new ParametersPlugin_PyInterp(); - myInterp->initialize(); } ParametersPlugin_Parameter::~ParametersPlugin_Parameter() { - myInterp->destroy(); - delete myInterp; } void ParametersPlugin_Parameter::initAttributes() { - data()->addAttribute(ParametersPlugin_Parameter::VARIABLE_ID(), - ModelAPI_AttributeString::typeId()); - data()->addAttribute(ParametersPlugin_Parameter::EXPRESSION_ID(), - ModelAPI_AttributeString::typeId()); + 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()); + + data()->addAttribute(ARGUMENTS_ID(), ModelAPI_AttributeRefList::typeId()); + data()->reflist(ARGUMENTS_ID())->setIsArgument(false); + ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ARGUMENTS_ID()); } bool ParametersPlugin_Parameter::isInHistory() @@ -39,59 +63,117 @@ bool ParametersPlugin_Parameter::isInHistory() return false; } -void ParametersPlugin_Parameter::attributeChanged(const std::string&) +void ParametersPlugin_Parameter::attributeChanged(const std::string& theID) +{ + if (theID == EXPRESSION_ID()) + updateExpression(); + + data()->execState(ModelAPI_StateMustBeUpdated); +} + +void ParametersPlugin_Parameter::updateName() { + std::string aName = string(VARIABLE_ID())->value(); + data()->setName(aName); + ResultParameterPtr aParam = document()->createParameter(data()); + std::string anOldName = aParam->data()->name(); + aParam->data()->setName(aName); + setResult(aParam); + - std::string anExpression = string(ParametersPlugin_Parameter::EXPRESSION_ID())->value(); - if(anExpression.empty()) { - // clear error/result if the expression is empty - setError("", false); - return; + // #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 aNames; // collect names in the root document that must be checked + aNames.push_back(aName); + if (anOldName != aName) { + aNames.push_back(anOldName); + } + std::list::iterator aNIter = aNames.begin(); + for (; aNIter != aNames.end(); aNIter++) { + double aValue; + ResultParameterPtr aRootParam; + FeaturePtr aThis = + std::dynamic_pointer_cast(string(VARIABLE_ID())->owner()); + if (ModelAPI_Tools::findVariable(aThis, *aNIter, aValue, aRootParam, aRootDoc)) { + std::set > anAttributes = + aRootParam->data()->refsToMe(); + std::set >::const_iterator anAttributeIt = + anAttributes.cbegin(); + for (; anAttributeIt != anAttributes.cend(); ++anAttributeIt) { + const AttributePtr& anAttribute = *anAttributeIt; + ModelAPI_AttributeEvalMessage::send(anAttribute, NULL); + } + } + } } +} + +bool ParametersPlugin_Parameter::updateExpression() +{ + std::string anExpression = string(EXPRESSION_ID())->value(); + std::string outErrorMessage; double aValue = evaluate(anExpression, outErrorMessage); - // Name - std::string aName = string(ParametersPlugin_Parameter::VARIABLE_ID())->value(); - std::ostringstream sstream; - sstream << aValue; - std::string aParamValue = sstream.str(); - data()->setName(aName + " ("+ aParamValue + ")"); - aParam->data()->setName(aName); - // Error - std::string aStateMsg; - if (!outErrorMessage.empty()) { - aStateMsg = "Error: " + outErrorMessage; - data()->execState(ModelAPI_StateExecFailed); - } - setError(aStateMsg, false); - // Value + + data()->string(EXPRESSION_ERROR_ID())->setValue(outErrorMessage); + if (!outErrorMessage.empty()) + return false; + + ResultParameterPtr aParam = document()->createParameter(data()); AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE()); aValueAttribute->setValue(aValue); setResult(aParam); + + return true; } void ParametersPlugin_Parameter::execute() { + updateName(); + if (!updateExpression()) + setError("Expression error.", false); } double ParametersPlugin_Parameter::evaluate(const std::string& theExpression, std::string& theError) { - std::list anExprParams = myInterp->compile(theExpression); - // find expression's params in the model - std::list aContext; - std::list::iterator it = anExprParams.begin(); - for ( ; it != anExprParams.end(); it++) { - std::string aParamName = *it; - ObjectPtr aParamObj = document()->objectByName(ModelAPI_ResultParameter::group(), aParamName); - ResultParameterPtr aParam = std::dynamic_pointer_cast(aParamObj); - if(!aParam.get()) continue; - AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE()); - std::ostringstream sstream; - sstream << aValueAttribute->value(); - std::string aParamValue = sstream.str(); - aContext.push_back(aParamName + "=" + aParamValue); + FeaturePtr aMyPtr = std::dynamic_pointer_cast(data()->owner()); + std::shared_ptr aProcessMessage = + ModelAPI_ParameterEvalMessage::send(aMyPtr, this); + + double aResult = 0; + if (aProcessMessage->isProcessed()) { + const std::list& 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::const_iterator aNewIter = aParamsList.begin(); + std::list anOldList = aParams->list(); + std::list::const_iterator anOldIter = anOldList.begin(); + for(; !aDifferent && aNewIter != aParamsList.end(); aNewIter++, anOldIter++) { + if (*aNewIter != *anOldIter) + aDifferent = true; + } + } + if (aDifferent) { + aParams->clear(); + std::list::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); - return myInterp->evaluate(theExpression, theError); + return aResult; +} + +bool ParametersPlugin_Parameter::isPreviewNeeded() const +{ + return true; }