Salome HOME
SketchShapePlugin package is removed as not used.
[modules/shaper.git] / src / ParametersPlugin / ParametersPlugin_EvalListener.cpp
index 8e826aa1684e5db984c74e331ba6559fa4aa2129..126b0b0f0346ba509e2bfa9ff761066530d59eed 100644 (file)
@@ -13,6 +13,8 @@
 
 #include <Events_Error.h>
 
+#include <ModelAPI_AttributeDouble.h>
+#include <ModelAPI_AttributeInteger.h>
 #include <ModelAPI_AttributeRefList.h>
 #include <ModelAPI_AttributeString.h>
 #include <ModelAPI_AttributeValidator.h>
@@ -21,7 +23,6 @@
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Tools.h>
 
-#include <ModelAPI_AttributeDouble.h>
 #include <GeomDataAPI_Point.h>
 #include <GeomDataAPI_Point2D.h>
 
 #include <set>
 #include <sstream>
 
+//------------------------------------------------------------------------------
+// Tools
+
+std::string toStdString(double theValue)
+{
+  std::ostringstream sstream;
+  sstream << theValue;
+  return sstream.str();
+}
+
+std::set<std::string> toSet(const std::list<std::string>& theContainer)
+{
+  return std::set<std::string>(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<ParametersPlugin_PyInterp>(new ParametersPlugin_PyInterp());
   myInterp->initialize();
@@ -53,10 +76,14 @@ 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());
@@ -73,12 +100,10 @@ double ParametersPlugin_EvalListener::evaluate(const std::string& theExpression,
   for ( ; it != anExprParams.end(); it++) {
     double aValue;
     ResultParameterPtr aParamRes;
+    // 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);
@@ -86,17 +111,24 @@ 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)
 {
   std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
       std::dynamic_pointer_cast<ModelAPI_AttributeEvalMessage>(theMessage);
 
+  if (aMessage->attribute()->attributeType() == ModelAPI_AttributeInteger::typeId()) {
+    AttributeIntegerPtr anAttribute =
+        std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(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<std::string>());
+    anAttribute->setExpressionInvalid(!isValid);
+    anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
+  } else
   if (aMessage->attribute()->attributeType() == ModelAPI_AttributeDouble::typeId()) {
     AttributeDoublePtr anAttribute =
         std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aMessage->attribute());
@@ -222,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<ModelAPI_AttributeInteger>(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<ModelAPI_AttributeDouble>(theAttribute);
@@ -260,6 +300,29 @@ void ParametersPlugin_EvalListener::renameInAttribute(
   }
 }
 
+void ParametersPlugin_EvalListener::renameInDependents(std::shared_ptr<ModelAPI_ResultParameter> theResultParameter,
+                                                       const std::string& theOldName,
+                                                       const std::string& theNewName)
+{
+  std::set<std::shared_ptr<ModelAPI_Attribute> > anAttributes =
+      theResultParameter->data()->refsToMe();
+  std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anAttributeIt =
+      anAttributes.cbegin();
+  for (; anAttributeIt != anAttributes.cend(); ++anAttributeIt) {
+    const AttributePtr& anAttribute = *anAttributeIt;
+    if (anAttribute->attributeType() == ModelAPI_AttributeRefList::typeId()) {
+      std::shared_ptr<ParametersPlugin_Parameter> aParameter =
+          std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
+              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;
@@ -288,7 +351,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
@@ -314,24 +379,30 @@ void ParametersPlugin_EvalListener::processObjectRenamedEvent(
     return;
   }
 
-  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>(
-              anAttributeRefList->owner());
-      if (aParameter.get())
-        // Rename
-        renameInParameter(aParameter, aMessage->oldName(), aMessage->newName());
-    } else
-        // Rename
-        renameInAttribute(anAttribute, aMessage->oldName(), aMessage->newName());
-  }
+  renameInDependents(aResultParameter, aMessage->oldName(), aMessage->newName());
 }
 
+void ParametersPlugin_EvalListener::processReplaceParameterEvent(
+    const std::shared_ptr<Events_Message>& theMessage)
+{
+  std::shared_ptr<ModelAPI_ReplaceParameterMessage> aMessage =
+      std::dynamic_pointer_cast<ModelAPI_ReplaceParameterMessage>(theMessage);
+
+  // get parameter feature for the object
+  std::shared_ptr<ParametersPlugin_Parameter> aParameter =
+      std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
+          ModelAPI_Feature::feature(aMessage->object()));
+  if (!aParameter.get())
+    return;
+
+  ResultParameterPtr aResultParameter =
+      std::dynamic_pointer_cast<ModelAPI_ResultParameter>(
+          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);
+}