X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FParametersPlugin%2FParametersPlugin_Parameter.cpp;h=803ea9e43983c4b7403c967b18120636cb6bb364;hb=5a4405dc843479d921b53b83c181cbe9359414fe;hp=58280994c84c8f539cb36813329816f454046332;hpb=506840f28e9ee8b4c520230193bcb28835757f59;p=modules%2Fshaper.git diff --git a/src/ParametersPlugin/ParametersPlugin_Parameter.cpp b/src/ParametersPlugin/ParametersPlugin_Parameter.cpp index 58280994c..803ea9e43 100644 --- a/src/ParametersPlugin/ParametersPlugin_Parameter.cpp +++ b/src/ParametersPlugin/ParametersPlugin_Parameter.cpp @@ -4,6 +4,8 @@ // Created: 23 MArch 2015 // Author: sbh +#include + #include "ParametersPlugin_Parameter.h" #include @@ -32,7 +34,13 @@ void ParametersPlugin_Parameter::initAttributes() { data()->addAttribute(VARIABLE_ID(), ModelAPI_AttributeString::typeId()); data()->addAttribute(EXPRESSION_ID(), ModelAPI_AttributeString::typeId()); + + 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()); } @@ -43,59 +51,70 @@ bool ParametersPlugin_Parameter::isInHistory() void ParametersPlugin_Parameter::attributeChanged(const std::string& theID) { - if (theID == EXPRESSION_ID()) { // recompute only on change of the expression - ResultParameterPtr aParam = document()->createParameter(data()); - - std::string anExpression = string(EXPRESSION_ID())->value(); - if(anExpression.empty()) { - // clear error/result if the expression is empty - setError("", false); - return; - } - std::string outErrorMessage; - double aValue = evaluate(anExpression, outErrorMessage); - // Name - std::string aName = string(VARIABLE_ID())->value(); - std::ostringstream sstream; - sstream << aValue; - std::string aParamValue = sstream.str(); - data()->setName(aName); - aParam->data()->setName(aName); - // Error - if (!outErrorMessage.empty()) { - std::string aStateMsg("Error: " + outErrorMessage); - data()->execState(ModelAPI_StateExecFailed); - setError(aStateMsg, false); - } else { - static const std::string anEmptyMsg(""); // it is checked in the validator by the empty message - setError(anEmptyMsg, false); - data()->execState(ModelAPI_StateDone); - } - // Value - AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE()); - aValueAttribute->setValue(aValue); - setResult(aParam); - } + 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()); + aParam->data()->setName(aName); + setResult(aParam); +} + +bool ParametersPlugin_Parameter::updateExpression() +{ + std::string anExpression = string(EXPRESSION_ID())->value(); + + std::string outErrorMessage; + double aValue = evaluate(anExpression, outErrorMessage); + + 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() { - // just call recompute - attributeChanged(EXPRESSION_ID()); + 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(); std::list aParamsList; for ( ; it != anExprParams.end(); it++) { + std::string& aVariableName = *it; + + // Parameter with the same name should be searched in the parent document. + // For the PartSet assume that the parameter is absent. + // Currently there is no way to get parent document, so we get PartSet for all. + DocumentPtr aDocument = document(); + if (data()->name() == aVariableName) { + if (aDocument == ModelAPI_Session::get()->moduleDocument()) + continue; + aDocument = ModelAPI_Session::get()->moduleDocument(); + } + double aValue; ResultParameterPtr aParamRes; - if (!ModelAPI_Tools::findVariable(*it, aValue, aParamRes)) continue; + if (!ModelAPI_Tools::findVariable(aVariableName, aValue, aParamRes, aDocument)) continue; aParamsList.push_back(aParamRes); std::ostringstream sstream;