// Author: Vitaly Smetannikov
#include "ModelAPI_Tools.h"
-#include <ModelAPI_ResultBody.h>
-#include <ModelAPI_ResultConstruction.h>
-#include <ModelAPI_ResultGroup.h>
+#include <ModelAPI_Session.h>
+#include <ModelAPI_Document.h>
+#include <ModelAPI_Object.h>
+#include <ModelAPI_AttributeDouble.h>
+#include <ModelAPI_ResultParameter.h>
+
+#include <list>
namespace ModelAPI_Tools {
- std::shared_ptr<GeomAPI_Shape> shape(const ResultPtr& theResult)
- {
+std::shared_ptr<GeomAPI_Shape> shape(const ResultPtr& theResult)
+{
/*
- ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theResult);
- if (aBody)
- return aBody->shape();
-
- ResultConstructionPtr aConstruct = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(
- theResult);
- if (aConstruct)
- return aConstruct->shape();
-
- ResultGroupPtr aGroup = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(theResult);
- if (aGroup)
- return aGroup->shape();
- return std::shared_ptr<GeomAPI_Shape>();
- */
- return theResult->shape();
+ ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theResult);
+ if (aBody)
+ return aBody->shape();
+
+ ResultConstructionPtr aConstruct = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(
+ theResult);
+ if (aConstruct)
+ return aConstruct->shape();
+
+ ResultGroupPtr aGroup = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(theResult);
+ if (aGroup)
+ return aGroup->shape();
+ return std::shared_ptr<GeomAPI_Shape>();
+ */
+ return theResult->shape();
+}
+
+bool findVariable(const std::string& theName, double& outValue)
+{
+ SessionPtr aSession = ModelAPI_Session::get();
+ std::list<DocumentPtr> aDocList;
+ DocumentPtr aDocument = aSession->activeDocument();
+ DocumentPtr aRootDocument = aSession->moduleDocument();
+ aDocList.push_back(aDocument);
+ if (aDocument != aRootDocument) {
+ aDocList.push_back(aRootDocument);
+ }
+ for(std::list<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
+ ObjectPtr aParamObj = (*it)->objectByName(ModelAPI_ResultParameter::group(), theName);
+ ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
+ if(!aParam.get())
+ continue;
+ AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
+ outValue = aValueAttribute->value();
+ return true;
}
+ return false;
}
+
+} // namespace ModelAPI_Tools
namespace ModelAPI_Tools {
/// Returns shape from the given Result object
MODELAPI_EXPORT std::shared_ptr<GeomAPI_Shape> shape(const ResultPtr& theResult);
+
+/*!
+ * Searches for variable with name \param theName in the active document (Part), when
+ * in the root document (PartSet). If found, set it value in the \param outValue
+ * and returns true.
+ */
+MODELAPI_EXPORT bool findVariable(const std::string& theName, double& outValue);
+
}
-;
#endif
// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
// File: ModuleBase_DoubleSpinBox.h
-// Author: Sergey TELKOV
+// Author: Sergey BELASH
//
#ifndef MODULEBASE_DOUBLESPINBOX_H_
#define MODULEBASE_DOUBLESPINBOX_H_
#include <ModelAPI_Document.h>
#include <ModelAPI_ResultParameter.h>
#include <ModelAPI_AttributeDouble.h>
+#include <ModelAPI_Tools.h>
#include <QKeyEvent>
#include <QLineEdit>
double& outValue) const
{
- SessionPtr aSession = ModelAPI_Session::get();
- DocumentPtr aDocument = aSession->activeDocument();
- ObjectPtr aParamObj = aDocument->objectByName(ModelAPI_ResultParameter::group(),
- theName.toStdString());
- ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
- if(!aParam.get())
- return false;
- AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
- outValue = aValueAttribute->value();
- return true;
+ return ModelAPI_Tools::findVariable(theName.toStdString(), outValue);
}
/*!
#include <ModelAPI_AttributeString.h>
#include <ModelAPI_ResultParameter.h>
#include <ModelAPI_AttributeDouble.h>
+#include <ModelAPI_Tools.h>
#include <string>
#include <sstream>
ParametersPlugin_Parameter::ParametersPlugin_Parameter()
{
myInterp = new ParametersPlugin_PyInterp();
- myInterp->initialize();
}
ParametersPlugin_Parameter::~ParametersPlugin_Parameter()
{
- myInterp->destroy();
delete myInterp;
}
double ParametersPlugin_Parameter::evaluate(const std::string& theExpression, std::string& theError)
{
+ myInterp->initialize();
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++) {
- std::string aParamName = *it;
- ObjectPtr aParamObj = document()->objectByName(ModelAPI_ResultParameter::group(), aParamName);
- ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
- if(!aParam.get()) continue;
- AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
+ double aValue;
+ if (!ModelAPI_Tools::findVariable(*it, aValue)) continue;
+
std::ostringstream sstream;
- sstream << aValueAttribute->value();
+ sstream << aValue;
std::string aParamValue = sstream.str();
- aContext.push_back(aParamName + "=" + aParamValue);
+ aContext.push_back(*it + "=" + aParamValue);
}
myInterp->extendLocalContext(aContext);
- return myInterp->evaluate(theExpression, theError);
+ double result = myInterp->evaluate(theExpression, theError);
+ myInterp->destroy();
+ return result;
}