#include <Model_ResultConstruction.h>
#include <Model_ResultBody.h>
#include <Model_ResultGroup.h>
+#include <Model_ResultParameter.h>
#include <ModelAPI_Validator.h>
#include <ModelAPI_CompositeFeature.h>
aResult = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(anOldObject);
}
if (!aResult) {
- aResult = std::shared_ptr<ModelAPI_ResultParameter>(new ModelAPI_ResultParameter);
+ aResult = std::shared_ptr<ModelAPI_ResultParameter>(new Model_ResultParameter);
storeResult(theFeatureData, aResult, theIndex);
}
return aResult;
// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-// File: Model_ResultParameter.h
+// File: Model_ResultParameter.cpp
// Created: 02 Apr 2015
// Author: Sergey BELASH
-#ifndef MODEL_RESULTPARAMETER_H_
-#define MODEL_RESULTPARAMETER_H_
+#include "Model_ResultParameter.h"
+#include <ModelAPI_AttributeDouble.h>
+#include <ModelAPI_AttributeString.h>
-#include "Model.h"
-#include <Model_Document.h>
+Model_ResultParameter::~Model_ResultParameter()
+{
-#include <ModelAPI_Data.h>
-#include <ModelAPI_ResultParameter.h>
+}
-/**\class Model_ResultParameter
- * \ingroup DataModel
- * \brief
- */
-class Model_ResultParameter : public ModelAPI_ResultParameter
+void Model_ResultParameter::initAttributes()
{
- public:
- MODEL_EXPORT virtual ~Model_ResultParameter();
- MODEL_EXPORT virtual void initAttributes();
-
- protected:
- Model_ResultParameter();
+ data()->addAttribute(ModelAPI_ResultParameter::VALUE(),
+ ModelAPI_AttributeDouble::typeId());
+ data()->addAttribute(ModelAPI_ResultParameter::STATE(),
+ ModelAPI_AttributeString::typeId());
+}
- friend class Model_Document;
-};
-#endif
+Model_ResultParameter::Model_ResultParameter()
+{
+ setIsConcealed(false);
+}
// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-// File: Model_ResultParameter.cpp
+// File: Model_ResultParameter.h
// Created: 02 Apr 2015
// Author: Sergey BELASH
-#include "Model_ResultParameter.h"
+#ifndef MODEL_RESULTPARAMETER_H_
+#define MODEL_RESULTPARAMETER_H_
-Model_ResultParameter::~Model_ResultParameter()
-{
+#include "Model.h"
+#include <Model_Document.h>
-}
+#include <ModelAPI_Data.h>
+#include <ModelAPI_ResultParameter.h>
-void Model_ResultParameter::initAttributes()
+/**\class Model_ResultParameter
+ * \ingroup DataModel
+ * \brief
+ */
+class Model_ResultParameter : public ModelAPI_ResultParameter
{
- data()->addAttribute(ModelAPI_ResultParameter::VALUE(),
- ModelAPI_AttributeDouble::typeId());
-}
+ public:
+ MODEL_EXPORT virtual ~Model_ResultParameter();
+ MODEL_EXPORT virtual void initAttributes();
+ protected:
+ Model_ResultParameter();
-Model_ResultParameter::Model_ResultParameter()
-{
- setIsConcealed(false);
-}
+ friend class Model_Document;
+};
+
+#endif
ModelAPI_Session.cpp
ModelAPI_ShapeValidator.cpp
ModelAPI_Tools.cpp
- ModelAPI_ResultParameter.cpp
ModelAPI_AttributeValidator.cpp
)
// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-// File: ModelAPI_ResultParameter.h
+// File: ModelAPI_ResultParameter.cpp
// Created: 07 Jul 2014
// Author: Vitaly SMETANNIKOV
#include "ModelAPI_ResultParameter.h"
-#include <ModelAPI_AttributeDouble.h>
-void ModelAPI_ResultParameter::initAttributes()
+ModelAPI_ResultParameter::~ModelAPI_ResultParameter()
{
- data()->addAttribute(ModelAPI_ResultParameter::VALUE(),
- ModelAPI_AttributeDouble::typeId());
}
return MY_VALUE_ID;
}
- MODELAPI_EXPORT virtual void initAttributes();
+ inline static const std::string& STATE()
+ {
+ static const std::string MY_VALUE_ID("State");
+ return MY_VALUE_ID;
+ }
+
+ virtual void initAttributes() = 0;
};
#include <ModelAPI_Data.h>
#include <ModelAPI_Object.h>
#include <ModelAPI_Validator.h>
+#include <ModelAPI_ResultParameter.h>
#include <Config_WidgetAPI.h>
QVBoxLayout* aMainLay = new QVBoxLayout(this);
ModuleBase_Tools::adjustMargins(aMainLay);
+ myResultLabel = new QLabel(this);
+ myResultLabel->setWordWrap(true);
+ aMainLay->addWidget(myResultLabel);
myEditor = new ExpressionEditor(this);
myEditor->setMinimumHeight(20);
aMainLay->addWidget(myEditor);
QString aWidgetValue = myEditor->toPlainText();
aStringAttr->setValue(aWidgetValue.toStdString());
updateObject(myFeature);
+
+ if(!myFeature->firstResult().get())
+ return true;
+
+ ResultParameterPtr aResult =
+ std::dynamic_pointer_cast<ModelAPI_ResultParameter>(myFeature->firstResult());
+ AttributeStringPtr aErrorAttr = aResult->data()->string(ModelAPI_ResultParameter::STATE());
+ myResultLabel->setText(QString::fromStdString(aErrorAttr->value()));
return true;
}
#include <ModuleBase_ModelWidget.h>
#include <QList>
+#include <QLabel>
#include <QString>
#include <QStringList>
#include <QPlainTextEdit>
private:
/// A line edit control
+ QLabel* myResultLabel;
ExpressionEditor* myEditor;
};
ParametersPlugin_Parameter::~ParametersPlugin_Parameter()
{
+ myInterp->destroy();
delete myInterp;
}
ModelAPI_AttributeString::typeId());
}
+bool ParametersPlugin_Parameter::isInHistory()
+{
+ return false;
+}
+
void ParametersPlugin_Parameter::execute()
{
+ ResultParameterPtr aParam = document()->createParameter(data());
+
std::string anExpression = string(ParametersPlugin_Parameter::EXPRESSION_ID())->value();
- if(anExpression.empty())
+ if(anExpression.empty()) {
+ // clear error/result if the expression is empty
+ aParam->data()->string(ModelAPI_ResultParameter::STATE())->setValue("");
return;
-
- ResultParameterPtr aParameterResult = document()->createParameter(data());
+ }
// Value
- AttributeDoublePtr aValueAttribute = aParameterResult->data()->real(ModelAPI_ResultParameter::VALUE());
- double aValue = evaluate(anExpression);
+ std::string outErrorMessage;
+ double aValue = evaluate(anExpression, outErrorMessage);
+ AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
aValueAttribute->setValue(aValue);
- setResult(aParameterResult);
+ setResult(aParam);
// Name
std::string aName = string(ParametersPlugin_Parameter::VARIABLE_ID())->value();
std::ostringstream sstream;
sstream << aValue;
std::string aParamValue = sstream.str();
data()->setName(aName + " ("+ aParamValue + ")");
- aParameterResult->data()->setName(aName);
+ aParam->data()->setName(aName);
+ // Error
+ AttributeStringPtr aErrorAttr = aParam->data()->string(ModelAPI_ResultParameter::STATE());
+ std::string aStateMsg;
+ if (outErrorMessage.empty()) {
+ aStateMsg = "Result: " + aParamValue;
+ } else {
+ aStateMsg = "Error:\n" + outErrorMessage;
+ }
+ aErrorAttr->setValue(aStateMsg);
+ //if(!outErrorMessage.empty()) {
+ // data()->execState(ModelAPI_StateExecFailed);
+ //}
}
-double ParametersPlugin_Parameter::evaluate(std::string theExpression)
+double ParametersPlugin_Parameter::evaluate(const std::string& theExpression, std::string& theError)
{
std::list<std::string> anExprParams = myInterp->compile(theExpression);
// find expression's params in the model
aContext.push_back(aParamName + "=" + aParamValue);
}
myInterp->extendLocalContext(aContext);
- std::string outError;
- return myInterp->evaluate(theExpression, outError);
-
+ return myInterp->evaluate(theExpression, theError);
}
/// Request for initialization of data model of the feature: adding all attributes
PARAMETERSPLUGIN_EXPORT virtual void initAttributes();
+ PARAMETERSPLUGIN_EXPORT virtual bool isInHistory(); //false
+ //bool isI
+
/// Use plugin manager for features creation
ParametersPlugin_Parameter();
protected:
- double evaluate(std::string);
+ double evaluate(const std::string& theExpression, std::string& theError);
private:
ParametersPlugin_PyInterp* myInterp;
std::list<std::string> ParametersPlugin_PyInterp::compile(const std::string& theExpression)
{
- PyGILState_STATE aGilState = PyGILState_Ensure();
- std::list<std::string> res = this->_compile(theExpression);
- PyGILState_Release(aGilState);
- return res;
-}
-
-void ParametersPlugin_PyInterp::extendLocalContext(const std::list<std::string>& theParameters)
-{
- PyGILState_STATE aGilState = PyGILState_Ensure();
- this->_extendLocalContext(theParameters);
- PyGILState_Release(aGilState);
-}
-
-double ParametersPlugin_PyInterp::evaluate(const std::string& theExpression,
- std::string& theError)
-{
- PyGILState_STATE aGilState = PyGILState_Ensure();
- double res = this->_evaluate(theExpression, theError);
- PyGILState_Release(aGilState);
- return res;
-}
-
-
-std::list<std::string> ParametersPlugin_PyInterp::_compile(const std::string& theExpression)
-{
-
+ PyLockWrapper lck; // Acquire GIL until the end of the method
std::list<std::string> aResult;
PyObject *aCodeopModule = PyImport_AddModule("codeop");
if(!aCodeopModule) { // Fatal error. No way to go on.
}
PyCodeObject* aCodeObj = (PyCodeObject*) aCodePyObj;
+ std::string aCodeName(PyString_AsString(aCodeObj->co_code));
// co_names should be tuple, but can be changed in modern versions of python (>2.7.3)
if(!PyTuple_Check(aCodeObj->co_names))
return aResult;
return aResult;
}
-void ParametersPlugin_PyInterp::_extendLocalContext(const std::list<std::string>& theParameters)
+void ParametersPlugin_PyInterp::extendLocalContext(const std::list<std::string>& theParameters)
{
+ PyLockWrapper lck; // Acquire GIL until the end of the method
if (theParameters.empty())
return;
std::list<std::string>::const_iterator it = theParameters.begin();
}
-double ParametersPlugin_PyInterp::_evaluate(const std::string& theExpression, std::string& theError)
+double ParametersPlugin_PyInterp::evaluate(const std::string& theExpression, std::string& theError)
{
+ PyLockWrapper lck; // Acquire GIL until the end of the method
PyCodeObject* anExprCode = (PyCodeObject *) Py_CompileString(theExpression.c_str(),
"<string>", Py_eval_input);
if(!anExprCode) {
Py_XDECREF(anExprCode);
Py_XDECREF(anEvalResult);
Py_XDECREF(anEvalStrObj);
- return std::stod(anEvalStr);
+ double result = 0.;
+ try {
+ result = std::stod(anEvalStr);
+ } catch (const std::invalid_argument&) {
+ theError = "Unable to eval " + anEvalStr;
+ }
+
+ return result;
}
std::string ParametersPlugin_PyInterp::errorMessage()
}
return aPyError;
}
+
+bool ParametersPlugin_PyInterp::initContext()
+{
+ PyObject *m = PyImport_AddModule("__main__"); // interpreter main module (module context)
+ if(!m){
+ PyErr_Print();
+ return false;
+ }
+ _global_context = PyModule_GetDict(m); // get interpreter global variable context
+ Py_INCREF(_global_context);
+ _local_context = _global_context;
+
+ return PyRun_SimpleString("from math import *") == 0;
+}
double evaluate(const std::string&, std::string&);
protected:
- std::list<std::string> _compile(const std::string&);
- void _extendLocalContext(const std::list<std::string>&);
- double _evaluate(const std::string&, std::string&);
std::string errorMessage();
+ // Overrides PyInterp_Interp
+ virtual bool initContext();
};
#endif /* PARAMETERSPLUGIN_PYINTERP_H_ */