-double ParametersPlugin_EvalListener::evaluate(const std::string& theExpression, std::string& theError,
- const std::shared_ptr<ModelAPI_Document>& theDocument)
-{
- 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();
- for ( ; it != anExprParams.end(); it++) {
- double aValue;
- ResultParameterPtr aParamRes;
- if (!ModelAPI_Tools::findVariable(*it, aValue, aParamRes, theDocument)) continue;
-
- std::ostringstream sstream;
- sstream << aValue;
- std::string aParamValue = sstream.str();
- aContext.push_back(*it + "=" + aParamValue);
- }
- myInterp->extendLocalContext(aContext);
- double result = myInterp->evaluate(theExpression, theError);
- myInterp->clearLocalContext();
- return result;
-}
-
-std::set<std::string> toSet(const std::list<std::string>& theContainer)
-{
- return std::set<std::string>(theContainer.begin(), theContainer.end());
-}
-
-void ParametersPlugin_EvalListener::processEvaluationEvent(
- const std::shared_ptr<Events_Message>& theMessage)
-{
- std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
- std::dynamic_pointer_cast<ModelAPI_AttributeEvalMessage>(theMessage);
-
- if (aMessage->attribute()->attributeType() == ModelAPI_AttributeDouble::typeId()) {
- AttributeDoublePtr anAttribute =
- std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aMessage->attribute());
- std::string anError;
- double aValue = evaluate(anAttribute->text(), anError, anAttribute->owner()->document());
- bool isValid = anError.empty();
- if (isValid)
- anAttribute->setCalculatedValue(aValue);
- anAttribute->setUsedParameters(isValid ? toSet(myInterp->compile(anAttribute->text())) : std::set<std::string>());
- anAttribute->setExpressionInvalid(!isValid);
- anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
- } else
- if (aMessage->attribute()->attributeType() == GeomDataAPI_Point::typeId()) {
- AttributePointPtr anAttribute =
- std::dynamic_pointer_cast<GeomDataAPI_Point>(aMessage->attribute());
- std::string aText[] = {
- anAttribute->textX(),
- anAttribute->textY(),
- anAttribute->textZ()
- };
- double aCalculatedValue[] = {
- anAttribute->x(),
- anAttribute->y(),
- anAttribute->z()
- };
- for (int i = 0; i < 3; ++i) {
- std::string anError;
- double aValue = evaluate(aText[i], anError, anAttribute->owner()->document());
- bool isValid = anError.empty();
- if (isValid) aCalculatedValue[i] = aValue;
- anAttribute->setUsedParameters(i, isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::string>());
- 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()) {
- AttributePoint2DPtr anAttribute =
- std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aMessage->attribute());
- std::string aText[] = {
- anAttribute->textX(),
- anAttribute->textY()
- };
- double aCalculatedValue[] = {
- anAttribute->x(),
- anAttribute->y()
- };
- for (int i = 0; i < 2; ++i) {
- std::string anError;
- double aValue = evaluate(aText[i], anError, anAttribute->owner()->document());
- bool isValid = anError.empty();
- if (isValid) aCalculatedValue[i] = aValue;
- anAttribute->setUsedParameters(i, isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::string>());
- anAttribute->setExpressionInvalid(i, !isValid);
- anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
- }
- anAttribute->setCalculatedValue(aCalculatedValue[0],
- aCalculatedValue[1]);