From f845f0d8829b0b04c5e1e552134cefadb1e7249c Mon Sep 17 00:00:00 2001 From: sbh Date: Mon, 6 Apr 2015 20:51:33 +0300 Subject: [PATCH] Referencing between parameters added --- src/ModelAPI/CMakeLists.txt | 1 + src/ModelAPI/ModelAPI_ResultParameter.cpp | 14 ++++++++ src/ModelAPI/ModelAPI_ResultParameter.h | 3 ++ .../ParametersPlugin_Parameter.cpp | 33 +++++++++++++++---- .../ParametersPlugin_PyInterp.cpp | 12 +++++-- 5 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 src/ModelAPI/ModelAPI_ResultParameter.cpp diff --git a/src/ModelAPI/CMakeLists.txt b/src/ModelAPI/CMakeLists.txt index 8bcae17fd..513ddcdef 100644 --- a/src/ModelAPI/CMakeLists.txt +++ b/src/ModelAPI/CMakeLists.txt @@ -68,6 +68,7 @@ SET(PROJECT_SOURCES ModelAPI_Session.cpp ModelAPI_ShapeValidator.cpp ModelAPI_Tools.cpp + ModelAPI_ResultParameter.cpp ) SET(PROJECT_LIBRARIES diff --git a/src/ModelAPI/ModelAPI_ResultParameter.cpp b/src/ModelAPI/ModelAPI_ResultParameter.cpp new file mode 100644 index 000000000..781fe33df --- /dev/null +++ b/src/ModelAPI/ModelAPI_ResultParameter.cpp @@ -0,0 +1,14 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + +// File: ModelAPI_ResultParameter.h +// Created: 07 Jul 2014 +// Author: Vitaly SMETANNIKOV + +#include "ModelAPI_ResultParameter.h" +#include + +void ModelAPI_ResultParameter::initAttributes() +{ + data()->addAttribute(ModelAPI_ResultParameter::VALUE(), + ModelAPI_AttributeDouble::typeId()); +} diff --git a/src/ModelAPI/ModelAPI_ResultParameter.h b/src/ModelAPI/ModelAPI_ResultParameter.h index 9d877376f..f4b4734c4 100644 --- a/src/ModelAPI/ModelAPI_ResultParameter.h +++ b/src/ModelAPI/ModelAPI_ResultParameter.h @@ -37,6 +37,9 @@ class ModelAPI_ResultParameter : public ModelAPI_Result static const std::string MY_VALUE_ID("Value"); return MY_VALUE_ID; } + + MODELAPI_EXPORT virtual void initAttributes(); + }; //! Pointer on feature object diff --git a/src/ParametersPlugin/ParametersPlugin_Parameter.cpp b/src/ParametersPlugin/ParametersPlugin_Parameter.cpp index 745d0c765..073c83cb5 100644 --- a/src/ParametersPlugin/ParametersPlugin_Parameter.cpp +++ b/src/ParametersPlugin/ParametersPlugin_Parameter.cpp @@ -11,6 +11,8 @@ #include #include +#include +#include ParametersPlugin_Parameter::ParametersPlugin_Parameter() { @@ -36,21 +38,40 @@ void ParametersPlugin_Parameter::execute() std::string anExpression = string(ParametersPlugin_Parameter::EXPRESSION_ID())->value(); if(anExpression.empty()) return; - ResultParameterPtr aParameterResult = document()->createParameter(data()); - //AttributeDoublePtr anAttr = aParameterResult->data()->real(ModelAPI_ResultParameter::VALUE()); + ResultParameterPtr aParameterResult = document()->createParameter(data()); + // Value + AttributeDoublePtr aValueAttribute = aParameterResult->data()->real(ModelAPI_ResultParameter::VALUE()); double aValue = evaluate(anExpression); - //anAttr->setValue(aValue); + aValueAttribute->setValue(aValue); setResult(aParameterResult); - + // Name + std::string aName = string(ParametersPlugin_Parameter::VARIABLE_ID())->value(); + std::ostringstream sstream; + sstream << aValue; + std::string aParamValue = sstream.str(); + data()->setName(aName + " ("+ aParamValue + ")"); + aParameterResult->data()->setName(aName); } double ParametersPlugin_Parameter::evaluate(std::string theExpression) { std::list anExprParams = myInterp->compile(theExpression); // find expression's params in the model - // todo - // myInterp->extendLocalContext(); + 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); + } + myInterp->extendLocalContext(aContext); std::string outError; return myInterp->evaluate(theExpression, outError); diff --git a/src/ParametersPlugin/ParametersPlugin_PyInterp.cpp b/src/ParametersPlugin/ParametersPlugin_PyInterp.cpp index 92812f50a..c3546ea92 100644 --- a/src/ParametersPlugin/ParametersPlugin_PyInterp.cpp +++ b/src/ParametersPlugin/ParametersPlugin_PyInterp.cpp @@ -84,6 +84,11 @@ void ParametersPlugin_PyInterp::_extendLocalContext(const std::list { if (theParameters.empty()) return; + std::list::const_iterator it = theParameters.begin(); + for ( ; it != theParameters.cend(); it++) { + std::string aParamValue = *it; + simpleRun(aParamValue.c_str(), false); + } } @@ -107,9 +112,10 @@ double ParametersPlugin_PyInterp::_evaluate(const std::string& theExpression, st PyObject* anEvalStrObj = PyObject_Str(anEvalResult); std::string anEvalStr(PyString_AsString(anEvalStrObj)); - double result = std::stod(anEvalStr); - - return result; + Py_XDECREF(anExprCode); + Py_XDECREF(anEvalResult); + Py_XDECREF(anEvalStrObj); + return std::stod(anEvalStr); } std::string ParametersPlugin_PyInterp::errorMessage() -- 2.39.2