X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FParametersPlugin%2FParametersPlugin_EvalListener.cpp;h=126b0b0f0346ba509e2bfa9ff761066530d59eed;hb=5a4405dc843479d921b53b83c181cbe9359414fe;hp=b5acdb33e644bbc8cd633950b5f13414150be821;hpb=d41b09234c411b20f4473c46939a8afa0b932afa;p=modules%2Fshaper.git diff --git a/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp b/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp index b5acdb33e..126b0b0f0 100644 --- a/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp +++ b/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp @@ -13,14 +13,16 @@ #include +#include +#include +#include #include +#include #include #include #include #include -#include -#include #include #include @@ -28,13 +30,35 @@ #include #include +//------------------------------------------------------------------------------ +// Tools + +std::string toStdString(double theValue) +{ + std::ostringstream sstream; + sstream << theValue; + return sstream.str(); +} + +std::set toSet(const std::list& theContainer) +{ + return std::set(theContainer.begin(), theContainer.end()); +} + +//------------------------------------------------------------------------------ + ParametersPlugin_EvalListener::ParametersPlugin_EvalListener() { Events_Loop* aLoop = Events_Loop::loop(); - const Events_ID kEvaluationEvent = ModelAPI_AttributeEvalMessage::eventId(); - aLoop->registerListener(this, kEvaluationEvent, NULL, true); - const Events_ID kObjectRenamedEvent = ModelAPI_ObjectRenamedMessage::eventId(); - aLoop->registerListener(this, kObjectRenamedEvent, NULL, true); + + Events_ID anEvents_IDs[] = { + ModelAPI_AttributeEvalMessage::eventId(), + ModelAPI_ObjectRenamedMessage::eventId(), + ModelAPI_ReplaceParameterMessage::eventId() + }; + + for (int i = 0; i < sizeof(anEvents_IDs)/sizeof(anEvents_IDs[0]); ++i) + aLoop->registerListener(this, anEvents_IDs[i], NULL, true); myInterp = std::shared_ptr(new ParametersPlugin_PyInterp()); myInterp->initialize(); @@ -52,18 +76,22 @@ void ParametersPlugin_EvalListener::processEvent( const Events_ID kEvaluationEvent = ModelAPI_AttributeEvalMessage::eventId(); const Events_ID kObjectRenamedEvent = ModelAPI_ObjectRenamedMessage::eventId(); + const Events_ID kReplaceParameterEvent = ModelAPI_ReplaceParameterMessage::eventId(); + if (theMessage->eventID() == kEvaluationEvent) { processEvaluationEvent(theMessage); } else if (theMessage->eventID() == kObjectRenamedEvent) { processObjectRenamedEvent(theMessage); + } else if (theMessage->eventID() == kReplaceParameterEvent) { + processReplaceParameterEvent(theMessage); } else { Events_Error::send(std::string("ParametersPlugin python interpreter, unhandled message caught: ") + theMessage->eventID().eventText()); } } -double ParametersPlugin_EvalListener::evaluate(const std::string& theExpression, - std::string& theError) +double ParametersPlugin_EvalListener::evaluate(const std::string& theExpression, std::string& theError, + const std::shared_ptr& theDocument) { std::list anExprParams = myInterp->compile(theExpression); // find expression's params in the model @@ -72,12 +100,10 @@ double ParametersPlugin_EvalListener::evaluate(const std::string& theExpression, for ( ; it != anExprParams.end(); it++) { double aValue; ResultParameterPtr aParamRes; - if (!ModelAPI_Tools::findVariable(*it, aValue, aParamRes)) continue; + // If variable does not exist python interpreter will generate an error. It is OK. + if (!ModelAPI_Tools::findVariable(*it, aValue, aParamRes, theDocument)) continue; - std::ostringstream sstream; - sstream << aValue; - std::string aParamValue = sstream.str(); - aContext.push_back(*it + "=" + aParamValue); + aContext.push_back(*it + "=" + toStdString(aValue)); } myInterp->extendLocalContext(aContext); double result = myInterp->evaluate(theExpression, theError); @@ -85,22 +111,29 @@ double ParametersPlugin_EvalListener::evaluate(const std::string& theExpression, return result; } -std::set toSet(const std::list& theContainer) -{ - return std::set(theContainer.begin(), theContainer.end()); -} - void ParametersPlugin_EvalListener::processEvaluationEvent( const std::shared_ptr& theMessage) { std::shared_ptr aMessage = std::dynamic_pointer_cast(theMessage); + if (aMessage->attribute()->attributeType() == ModelAPI_AttributeInteger::typeId()) { + AttributeIntegerPtr anAttribute = + std::dynamic_pointer_cast(aMessage->attribute()); + std::string anError; + int aValue = (int)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()); + anAttribute->setExpressionInvalid(!isValid); + anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError); + } else if (aMessage->attribute()->attributeType() == ModelAPI_AttributeDouble::typeId()) { AttributeDoublePtr anAttribute = std::dynamic_pointer_cast(aMessage->attribute()); std::string anError; - double aValue = evaluate(anAttribute->text(), anError); + double aValue = evaluate(anAttribute->text(), anError, anAttribute->owner()->document()); bool isValid = anError.empty(); if (isValid) anAttribute->setCalculatedValue(aValue); @@ -123,7 +156,7 @@ void ParametersPlugin_EvalListener::processEvaluationEvent( }; for (int i = 0; i < 3; ++i) { std::string anError; - double aValue = evaluate(aText[i], 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()); @@ -147,7 +180,7 @@ void ParametersPlugin_EvalListener::processEvaluationEvent( }; for (int i = 0; i < 2; ++i) { std::string anError; - double aValue = evaluate(aText[i], 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()); @@ -221,6 +254,14 @@ void ParametersPlugin_EvalListener::renameInAttribute( const std::string& theOldName, const std::string& theNewName) { + if (theAttribute->attributeType() == ModelAPI_AttributeInteger::typeId()) { + AttributeIntegerPtr anAttribute = + std::dynamic_pointer_cast(theAttribute); + std::string anExpressionString = anAttribute->text(); + anExpressionString = renameInPythonExpression(anExpressionString, + theOldName, theNewName); + anAttribute->setText(anExpressionString); + } else if (theAttribute->attributeType() == ModelAPI_AttributeDouble::typeId()) { AttributeDoublePtr anAttribute = std::dynamic_pointer_cast(theAttribute); @@ -259,6 +300,29 @@ void ParametersPlugin_EvalListener::renameInAttribute( } } +void ParametersPlugin_EvalListener::renameInDependents(std::shared_ptr theResultParameter, + const std::string& theOldName, + const std::string& theNewName) +{ + std::set > anAttributes = + theResultParameter->data()->refsToMe(); + std::set >::const_iterator anAttributeIt = + anAttributes.cbegin(); + for (; anAttributeIt != anAttributes.cend(); ++anAttributeIt) { + const AttributePtr& anAttribute = *anAttributeIt; + if (anAttribute->attributeType() == ModelAPI_AttributeRefList::typeId()) { + std::shared_ptr aParameter = + std::dynamic_pointer_cast( + anAttribute->owner()); + if (aParameter.get()) + // Rename + renameInParameter(aParameter, theOldName, theNewName); + } else + // Rename + renameInAttribute(anAttribute, theOldName, theNewName); + } +} + bool isValidAttribute(const AttributePtr& theAttribute) { std::string aValidator, anError; @@ -287,7 +351,9 @@ void ParametersPlugin_EvalListener::processObjectRenamedEvent( std::shared_ptr aMessage = std::dynamic_pointer_cast(theMessage); - if (!aMessage.get() || aMessage->oldName().empty() || aMessage->newName().empty()) + // Empty new name is not available too but it will be rejected by + // name validator in isValidAttribute. + if (!aMessage.get() || aMessage->oldName().empty()) return; // check if the renamed object is a result parameter @@ -313,44 +379,30 @@ void ParametersPlugin_EvalListener::processObjectRenamedEvent( return; } - // List of documents to process - std::list aDocList; - SessionPtr aSession = ModelAPI_Session::get(); - DocumentPtr aDocument = aSession->activeDocument(); - DocumentPtr aRootDocument = aSession->moduleDocument(); - aDocList.push_back(aDocument); - if (aDocument != aRootDocument) { - aDocList.push_back(aRootDocument); - } + renameInDependents(aResultParameter, aMessage->oldName(), aMessage->newName()); +} - // Find all features - for (std::list::const_iterator aDicumentIt = aDocList.begin(); - aDicumentIt != aDocList.end(); ++aDicumentIt) { - const DocumentPtr& aDocument = *aDicumentIt; - std::list aFeatures = aDocument->allFeatures(); - std::list::iterator aFeatureIt = aFeatures.begin(); - for (; aFeatureIt != aFeatures.end(); ++aFeatureIt) { - const FeaturePtr& aFeature = *aFeatureIt; - - // If Parameter feature then rename its expression - std::shared_ptr aParameter = - std::dynamic_pointer_cast(aFeature); - if (aParameter.get()) { - // Rename - renameInParameter(aParameter, aMessage->oldName(), aMessage->newName()); - continue; - } +void ParametersPlugin_EvalListener::processReplaceParameterEvent( + const std::shared_ptr& theMessage) +{ + std::shared_ptr aMessage = + std::dynamic_pointer_cast(theMessage); - // Find all attributes - std::list anAttributes = aFeature->data()->attributes(std::string()); - std::list::const_iterator anAttributeIt = anAttributes.begin(); - for (; anAttributeIt != anAttributes.end(); ++anAttributeIt) { - const AttributePtr& anAttribute = *anAttributeIt; + // get parameter feature for the object + std::shared_ptr aParameter = + std::dynamic_pointer_cast( + ModelAPI_Feature::feature(aMessage->object())); + if (!aParameter.get()) + return; - // Rename - renameInAttribute(anAttribute, aMessage->oldName(), aMessage->newName()); - } - } - } -} + ResultParameterPtr aResultParameter = + std::dynamic_pointer_cast( + aParameter->firstResult()); + if (!aResultParameter.get()) + return; + + double aRealValue = aResultParameter->data()->real(ModelAPI_ResultParameter::VALUE())->value(); + std::string aValue = toStdString(aRealValue); + renameInDependents(aResultParameter, aResultParameter->data()->name(), aValue); +}