X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FInitializationPlugin%2FInitializationPlugin_EvalListener.cpp;h=f698026f44e20f36c4a6964af04220af14308ef6;hb=fc72d43b677baa05ae7fd317346fd8b723b799ed;hp=6e5e7d29c10a3589d4ba72945f7e90f75dcdca96;hpb=a0fa395fd2a0ddb16cb9aed4dab180f2681b4447;p=modules%2Fshaper.git diff --git a/src/InitializationPlugin/InitializationPlugin_EvalListener.cpp b/src/InitializationPlugin/InitializationPlugin_EvalListener.cpp index 6e5e7d29c..f698026f4 100644 --- a/src/InitializationPlugin/InitializationPlugin_EvalListener.cpp +++ b/src/InitializationPlugin/InitializationPlugin_EvalListener.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2020 CEA/DEN, EDF R&D +// Copyright (C) 2014-2023 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 @@ -20,15 +20,21 @@ #include #include -#include #include +#include + +#include + #include +#include + #include #include #include #include +#include #include #include #include @@ -42,13 +48,15 @@ #include #include #include +#include +#include -//------------------------------------------------------------------------------ +//================================================================================================= // Tools -std::string toStdString(double theValue) +std::wstring toString(double theValue) { - std::ostringstream sstream; + std::wostringstream sstream; // write value in scientific format with 16 digits, // thus, not check the dot position sstream.precision(16); @@ -56,29 +64,32 @@ std::string toStdString(double theValue) return sstream.str(); } -std::set toSet(const std::list& theContainer) +std::set toSet(const std::list& theContainer) { - return std::set(theContainer.begin(), theContainer.end()); + return std::set(theContainer.begin(), theContainer.end()); } -//------------------------------------------------------------------------------ - +//================================================================================================= InitializationPlugin_EvalListener::InitializationPlugin_EvalListener() { Events_Loop* aLoop = Events_Loop::loop(); aLoop->registerListener(this, ModelAPI_AttributeEvalMessage::eventId(), NULL, true); aLoop->registerListener(this, ModelAPI_ParameterEvalMessage::eventId(), NULL, true); + aLoop->registerListener(this, ModelAPI_BuildEvalMessage::eventId(), NULL, true); aLoop->registerListener(this, ModelAPI_ComputePositionsMessage::eventId(), NULL, true); + aLoop->registerListener(this, ModelAPI_ImportParametersMessage::eventId(), NULL, true); myInterp = std::shared_ptr(new InitializationPlugin_PyInterp()); myInterp->initialize(); } +//================================================================================================= InitializationPlugin_EvalListener::~InitializationPlugin_EvalListener() { } +//================================================================================================= void InitializationPlugin_EvalListener::processEvent( const std::shared_ptr& theMessage) { @@ -91,7 +102,10 @@ void InitializationPlugin_EvalListener::processEvent( std::shared_ptr aMsg = std::dynamic_pointer_cast(theMessage); FeaturePtr aParam = aMsg->parameter(); - std::string anExp = aParam->string(ParametersPlugin_Parameter::EXPRESSION_ID())->value(); + AttributeStringPtr anExprAttr = aParam->string(ParametersPlugin_Parameter::EXPRESSION_ID()); + std::wstring anExp = anExprAttr->isUValue() ? + Locale::Convert::toWString(anExprAttr->valueU()) : + Locale::Convert::toWString(anExprAttr->value()); std::string anError; std::list > aParamsList; double aResult = evaluate(aParam, anExp, anError, aParamsList, true); @@ -100,19 +114,136 @@ void InitializationPlugin_EvalListener::processEvent( std::shared_ptr aMsg = std::dynamic_pointer_cast(theMessage); aMsg->setPositions(myInterp->positions(aMsg->expression(), aMsg->parameter())); + } else if (theMessage->eventID() == ModelAPI_BuildEvalMessage::eventId()) { + std::shared_ptr aMsg = + std::dynamic_pointer_cast(theMessage); + FeaturePtr aParam = aMsg->parameter(); + + AttributeStringPtr anVariableAttr = aParam->string(BuildPlugin_Interpolation::VARIABLE_ID()); + std::wstring anVar = anVariableAttr->isUValue() ? + Locale::Convert::toWString(anVariableAttr->valueU()) : + Locale::Convert::toWString(anVariableAttr->value()); + AttributeTablesPtr anValueAttr = aParam->tables(BuildPlugin_Interpolation::VALUE_ID()); + std::string anError; + + std::list > aParamsList; + + AttributeStringPtr anExprAttr; + ModelAPI_AttributeTables::Value aVal; + bool anIsFirstTime = true; + anExprAttr = aParam->string(BuildPlugin_Interpolation::XT_ID()); + std::wstring anExpX = anExprAttr->isUValue() ? + Locale::Convert::toWString(anExprAttr->valueU()) : + Locale::Convert::toWString(anExprAttr->value()); + anExpX.erase(std::remove(anExpX.begin(), anExpX.end(), ' '), anExpX.end()); + anExprAttr = aParam->string(BuildPlugin_Interpolation::YT_ID()); + std::wstring anExpY = anExprAttr->isUValue() ? + Locale::Convert::toWString(anExprAttr->valueU()) : + Locale::Convert::toWString(anExprAttr->value()); + anExpY.erase(std::remove(anExpY.begin(), anExpY.end(), ' '), anExpY.end()); + anExprAttr = aParam->string(BuildPlugin_Interpolation::ZT_ID()); + std::wstring anExpZ = anExprAttr->isUValue() ? + Locale::Convert::toWString(anExprAttr->valueU()) : + Locale::Convert::toWString(anExprAttr->value()); + anExpZ.erase(std::remove(anExpZ.begin(),anExpZ.end(), ' '), anExpZ.end()); + + for (int step =0; step < anValueAttr->rows(); step++) { + aVal.myDouble = evaluate(anVar, + anValueAttr->value(step,0).myDouble, + aParam, + anExpX, + anError, + aParamsList, + anIsFirstTime); + if (!anError.empty()) break; + anValueAttr->setValue(aVal,step,1); + aVal.myDouble = evaluate(anVar, + anValueAttr->value(step,0).myDouble, + aParam, + anExpY, + anError, + aParamsList, + anIsFirstTime); + if (!anError.empty()) break; + anValueAttr->setValue(aVal,step,2); + aVal.myDouble = evaluate(anVar, + anValueAttr->value(step,0).myDouble, + aParam, + anExpZ, + anError, + aParamsList, + anIsFirstTime); + if (!anError.empty()) break; + anValueAttr->setValue(aVal,step,3); + if (anIsFirstTime) + anIsFirstTime = false; + } + aMsg->setResults(aParamsList, anError); + } + else if (theMessage->eventID() == ModelAPI_ImportParametersMessage::eventId()) + { + std::shared_ptr aMsg = + std::dynamic_pointer_cast(theMessage); + std::string anImportScript("from salome.shaper import model;"); + std::string aDocScript("doc = model.activeDocument();"); + std::string anParamImpScript("model.importParameters(doc, \""); + std::string aPath = aMsg->filename(); + myInterp->runString(anImportScript + aDocScript + anParamImpScript + aPath + "\")"); + } +} + +//================================================================================================= +double InitializationPlugin_EvalListener::evaluate(std::wstring& theVariable, + double theValueVariable, + FeaturePtr theParameter, + const std::wstring& theExpression, + std::string& theError, + std::list >& theParamsList, + bool theIsFirstTime) +{ + std::list aContext; + aContext.push_back(theVariable + L"=" + toString(theValueVariable)); + myInterp->extendLocalContext(aContext); + aContext.clear(); + + std::list anExprParams = myInterp->compile(theExpression); + // find expression's params in the model + std::list::iterator it = anExprParams.begin(); + for (; it != anExprParams.end(); it++) { + double aValue; + ResultParameterPtr aParamRes; + // If variable does not exist python interpreter will generate an error. + if (!ModelAPI_Tools::findVariable(FeaturePtr(), + *it, aValue, aParamRes, theParameter->document())) + continue; + + if (theIsFirstTime) + { + std::list::iterator anIter = + std::find(theParamsList.begin(),theParamsList.end(), aParamRes); + if (anIter == theParamsList.end()) + theParamsList.push_back(aParamRes); + } + + aContext.push_back(*it + L"=" + toString(aValue)); } + myInterp->extendLocalContext(aContext); + double result = myInterp->evaluate(theExpression, theError); + myInterp->clearLocalContext(); + return result; } +//================================================================================================= double InitializationPlugin_EvalListener::evaluate(FeaturePtr theParameter, - const std::string& theExpression, std::string& theError, + const std::wstring& theExpression, std::string& theError, std::list >& theParamsList, const bool theIsParameter) { - std::list anExprParams = myInterp->compile(theExpression); + 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::list aContext; + std::list::iterator it = anExprParams.begin(); + for (; it != anExprParams.end(); it++) { double aValue; ResultParameterPtr aParamRes; // If variable does not exist python interpreter will generate an error. It is OK. @@ -124,7 +255,7 @@ double InitializationPlugin_EvalListener::evaluate(FeaturePtr theParameter, if (theIsParameter) theParamsList.push_back(aParamRes); - aContext.push_back(*it + "=" + toStdString(aValue)); + aContext.push_back(*it + L"=" + toString(aValue)); } myInterp->extendLocalContext(aContext); double result = myInterp->evaluate(theExpression, theError); @@ -132,6 +263,7 @@ double InitializationPlugin_EvalListener::evaluate(FeaturePtr theParameter, return result; } +//================================================================================================= void InitializationPlugin_EvalListener::processEvaluationEvent( const std::shared_ptr& theMessage) { @@ -150,11 +282,10 @@ void InitializationPlugin_EvalListener::processEvaluationEvent( if (isValid) anAttribute->setCalculatedValue(aValue); anAttribute->setUsedParameters(isValid ? - toSet(myInterp->compile(anAttribute->text())) : std::set()); + toSet(myInterp->compile(anAttribute->text())) : std::set()); anAttribute->setExpressionInvalid(!isValid); anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError); - } else - if (aMessage->attribute()->attributeType() == ModelAPI_AttributeDouble::typeId()) { + } else if (aMessage->attribute()->attributeType() == ModelAPI_AttributeDouble::typeId()) { AttributeDoublePtr anAttribute = std::dynamic_pointer_cast(aMessage->attribute()); std::string anError; @@ -163,14 +294,13 @@ void InitializationPlugin_EvalListener::processEvaluationEvent( if (isValid) anAttribute->setCalculatedValue(aValue); anAttribute->setUsedParameters(isValid ? - toSet(myInterp->compile(anAttribute->text())) : std::set()); + toSet(myInterp->compile(anAttribute->text())) : std::set()); anAttribute->setExpressionInvalid(!isValid); anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError); - } else - if (aMessage->attribute()->attributeType() == GeomDataAPI_Point::typeId()) { + } else if (aMessage->attribute()->attributeType() == GeomDataAPI_Point::typeId()) { AttributePointPtr anAttribute = std::dynamic_pointer_cast(aMessage->attribute()); - std::string aText[] = { + std::wstring aText[] = { anAttribute->textX(), anAttribute->textY(), anAttribute->textZ() @@ -186,18 +316,17 @@ void InitializationPlugin_EvalListener::processEvaluationEvent( bool isValid = anError.empty(); if (isValid) aCalculatedValue[i] = aValue; anAttribute->setUsedParameters(i, - isValid ? toSet(myInterp->compile(aText[i])) : std::set()); + isValid ? toSet(myInterp->compile(aText[i])) : std::set()); anAttribute->setExpressionInvalid(i, !isValid); anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError); } anAttribute->setCalculatedValue(aCalculatedValue[0], aCalculatedValue[1], aCalculatedValue[2]); - } else - if (aMessage->attribute()->attributeType() == GeomDataAPI_Point2D::typeId()) { + } else if (aMessage->attribute()->attributeType() == GeomDataAPI_Point2D::typeId()) { AttributePoint2DPtr anAttribute = std::dynamic_pointer_cast(aMessage->attribute()); - std::string aText[] = { + std::wstring aText[] = { anAttribute->textX(), anAttribute->textY() }; @@ -211,7 +340,7 @@ void InitializationPlugin_EvalListener::processEvaluationEvent( bool isValid = anError.empty(); if (isValid) aCalculatedValue[i] = aValue; anAttribute->setUsedParameters(i, - isValid ? toSet(myInterp->compile(aText[i])) : std::set()); + isValid ? toSet(myInterp->compile(aText[i])) : std::set()); anAttribute->setExpressionInvalid(i, !isValid); anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError); } @@ -220,7 +349,8 @@ void InitializationPlugin_EvalListener::processEvaluationEvent( } } +//================================================================================================= void InitializationPlugin_EvalListener::initDataModel() { - myInterp->runString("salome_iapp.register_module_in_study(\"Shaper\")"); + myInterp->runString("import salome_iapp;salome_iapp.register_module_in_study(\"Shaper\")"); }