Salome HOME
Roll back the modification, not yet approved
[modules/shaper.git] / src / ParametersPlugin / ParametersPlugin_EvalListener.cpp
index 13dfc182a7d00a108b82df06cf04446b034dbb84..029800afa4e2dcf7315b865d1cf863d20045f0b2 100644 (file)
 
 #include <Events_Error.h>
 
+#include <ModelAPI_AttributeRefList.h>
 #include <ModelAPI_AttributeString.h>
+#include <ModelAPI_AttributeValidator.h>
 #include <ModelAPI_Document.h>
 #include <ModelAPI_Events.h>
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Tools.h>
-#include <ModelAPI_AttributeValidator.h>
 
 #include <ModelAPI_AttributeDouble.h>
 #include <GeomDataAPI_Point.h>
 #include <GeomDataAPI_Point2D.h>
 
 #include <string>
+#include <set>
 #include <sstream>
 
 ParametersPlugin_EvalListener::ParametersPlugin_EvalListener()
@@ -61,8 +63,8 @@ void ParametersPlugin_EvalListener::processEvent(
   }
 }
 
-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<ModelAPI_Document>& theDocument)
 {
   std::list<std::string> anExprParams = myInterp->compile(theExpression);
   // find expression's params in the model
@@ -71,7 +73,8 @@ 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;
@@ -84,6 +87,11 @@ double ParametersPlugin_EvalListener::evaluate(const std::string& theExpression,
   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)
 {
@@ -94,10 +102,11 @@ void ParametersPlugin_EvalListener::processEvaluationEvent(
     AttributeDoublePtr anAttribute =
         std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(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);
+    anAttribute->setUsedParameters(isValid ? toSet(myInterp->compile(anAttribute->text())) : std::set<std::string>());
     anAttribute->setExpressionInvalid(!isValid);
     anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
   } else
@@ -116,9 +125,10 @@ 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<std::string>());
       anAttribute->setExpressionInvalid(i, !isValid);
       anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
     }
@@ -139,9 +149,10 @@ 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<std::string>());
       anAttribute->setExpressionInvalid(i, !isValid);
       anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
     }
@@ -278,7 +289,9 @@ void ParametersPlugin_EvalListener::processObjectRenamedEvent(
   std::shared_ptr<ModelAPI_ObjectRenamedMessage> aMessage =
       std::dynamic_pointer_cast<ModelAPI_ObjectRenamedMessage>(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
@@ -304,44 +317,24 @@ void ParametersPlugin_EvalListener::processObjectRenamedEvent(
     return;
   }
 
-  // List of documents to process
-  std::list<DocumentPtr> 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);
-  }
-
-  // Find all features
-  for (std::list<DocumentPtr>::const_iterator aDicumentIt = aDocList.begin(); 
-       aDicumentIt != aDocList.end(); ++aDicumentIt) {
-    const DocumentPtr& aDocument = *aDicumentIt;
-    std::list<FeaturePtr> aFeatures = aDocument->allFeatures();
-    std::list<FeaturePtr>::iterator aFeatureIt = aFeatures.begin();
-    for (; aFeatureIt != aFeatures.end(); ++aFeatureIt) {
-      const FeaturePtr& aFeature = *aFeatureIt;
-      
-      // If Parameter feature then rename its expression
+  std::set<std::shared_ptr<ModelAPI_Attribute> > anAttributes = 
+      aResultParameter->data()->refsToMe();
+  std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anAttributeIt =
+      anAttributes.cbegin();
+  for (; anAttributeIt != anAttributes.cend(); ++anAttributeIt) {
+    const AttributePtr& anAttribute = *anAttributeIt;
+    AttributeRefListPtr anAttributeRefList =
+        std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
+    if (anAttributeRefList.get()) {
       std::shared_ptr<ParametersPlugin_Parameter> aParameter =
-          std::dynamic_pointer_cast<ParametersPlugin_Parameter>(aFeature);
-      if (aParameter.get()) {
+          std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
+              anAttributeRefList->owner());
+      if (aParameter.get())
         // Rename
         renameInParameter(aParameter, aMessage->oldName(), aMessage->newName());
-        continue;
-      }      
-
-      // Find all attributes
-      std::list<AttributePtr> anAttributes = aFeature->data()->attributes(std::string());
-      std::list<AttributePtr>::const_iterator anAttributeIt = anAttributes.begin();
-      for (; anAttributeIt != anAttributes.end(); ++anAttributeIt) {
-        const AttributePtr& anAttribute = *anAttributeIt;
-
+    } else
         // Rename
         renameInAttribute(anAttribute, aMessage->oldName(), aMessage->newName());
-      }
-    }
   }
 }