#include <Events_Error.h>
#include <TDataStd_Name.hxx>
+#include <TDataStd_AsciiString.hxx>
#include <TDF_AttributeIterator.hxx>
#include <TDF_ChildIterator.hxx>
#include <TDF_RelocationTable.hxx>
return ModelAPI_StateMustBeUpdated; // default value
}
-void Model_Data::setError(const std::string& theError)
+void Model_Data::setError(const std::string& theError, bool theSend)
{
execState(ModelAPI_StateExecFailed);
- Events_Error::send(theError);
+ if (theSend) {
+ Events_Error::send(theError);
+ }
+ TDataStd_AsciiString::Set(myLab, theError.c_str());
+}
+
+std::string Model_Data::error() const
+{
+ Handle(TDataStd_AsciiString) anErrorAttr;
+ if (myLab.FindAttribute(TDataStd_AsciiString::GetID(), anErrorAttr)) {
+ return std::string(anErrorAttr->Get().ToCString());
+ }
+ return std::string();
}
int Model_Data::featureId() const
MODEL_EXPORT virtual ModelAPI_ExecState execState();
/// Registers error during the execution, causes the ExecutionFailed state
- MODEL_EXPORT virtual void setError(const std::string& theError);
+ MODEL_EXPORT virtual void setError(const std::string& theError, bool theSend = true);
+
+ /// Registers error during the execution, causes the ExecutionFailed state
+ MODEL_EXPORT virtual std::string error() const;
/// Returns the identifier of feature-owner, unique in this document
MODEL_EXPORT virtual int featureId() const;
{
data()->addAttribute(ModelAPI_ResultParameter::VALUE(),
ModelAPI_AttributeDouble::typeId());
- data()->addAttribute(ModelAPI_ResultParameter::STATE(),
- ModelAPI_AttributeString::typeId());
}
-
Model_ResultParameter::Model_ResultParameter()
{
setIsConcealed(false);
virtual ModelAPI_ExecState execState() = 0;
/// Registers error during the execution, causes the ExecutionFailed state
- virtual void setError(const std::string& theError) = 0;
+ virtual void setError(const std::string& theError, bool theSend = true) = 0;
+
+ /// Returns error, arose during the execution
+ virtual std::string error() const = 0;
/// Returns the identifier of feature-owner, unique in this document
virtual int featureId() const = 0;
virtual bool compute(const std::string& theAttributeId) { return false; };
/// Registers error during the execution, causes the ExecutionFailed state
- virtual void setError(const std::string& theError) {
- data()->setError(theError);
+ virtual void setError(const std::string& theError, bool isSend = true) {
+ data()->setError(theError, isSend);
+ }
+
+ /// Returns error, arose during the execution
+ virtual std::string error() const {
+ return data()->error();
}
/// returns the current results of the feature
// either a string starting with a letter, or a string starting with
// an underscore followed by at least one alphanumeric character
if (isAcceptVariables()) {
- QRegExp varNameMask("(([a-z]|[A-Z])([a-z]|[A-Z]|[0-9]|_)*)|"
- "(_([a-z]|[A-Z]|[0-9])+([a-z]|[A-Z]|[0-9]|_)*)");
+ QRegExp varNameMask("[_a-zA-Z][a-zA-Z0-9_]*");
if (varNameMask.exactMatch(str))
res = QValidator::Acceptable;
#include <ModuleBase_WidgetExprEditor.h>
#include <ModuleBase_Tools.h>
-#include <ModelAPI_AttributeString.h>
#include <ModelAPI_Data.h>
#include <ModelAPI_Object.h>
#include <ModelAPI_Validator.h>
#include <ModelAPI_ResultParameter.h>
+#include <ModelAPI_AttributeString.h>
+#include <ModelAPI_AttributeDouble.h>
#include <Config_WidgetAPI.h>
aStringAttr->setValue(aWidgetValue.toStdString());
updateObject(myFeature);
- if(!myFeature->firstResult().get())
- return true;
-
- ResultParameterPtr aResult =
+ // Try to get the value
+ QString aStateMsg;
+ std::string anErrorMessage = myFeature->error();
+ if (anErrorMessage.empty()) {
+ ResultParameterPtr aParam =
std::dynamic_pointer_cast<ModelAPI_ResultParameter>(myFeature->firstResult());
- AttributeStringPtr aErrorAttr = aResult->data()->string(ModelAPI_ResultParameter::STATE());
- myResultLabel->setText(QString::fromStdString(aErrorAttr->value()));
+ if(aParam.get()) {
+ AttributeDoublePtr aValueAttr =
+ aParam->data()->real(ModelAPI_ResultParameter::VALUE());
+ if (aValueAttr.get()) {
+ double aValue = aValueAttr->value();
+ aStateMsg = "Result: " + QString::number(aValue);
+ }
+ }
+ } else {
+ aStateMsg = QString::fromStdString(anErrorMessage);
+ }
+ myResultLabel->setText(aStateMsg);
return true;
}
ParametersPlugin_Plugin.h
ParametersPlugin_Parameter.h
ParametersPlugin_PyInterp.h
+ ParametersPlugin_Validators.h
)
SET(PROJECT_SOURCES
ParametersPlugin_Plugin.cpp
ParametersPlugin_Parameter.cpp
ParametersPlugin_PyInterp.cpp
+ ParametersPlugin_Validators.cpp
)
SET(XML_RESOURCES
return false;
}
-void ParametersPlugin_Parameter::execute()
+void ParametersPlugin_Parameter::attributeChanged(const std::string&)
{
ResultParameterPtr aParam = document()->createParameter(data());
std::string anExpression = string(ParametersPlugin_Parameter::EXPRESSION_ID())->value();
if(anExpression.empty()) {
// clear error/result if the expression is empty
- aParam->data()->string(ModelAPI_ResultParameter::STATE())->setValue("");
+ setError("", false);
return;
}
- // Value
std::string outErrorMessage;
double aValue = evaluate(anExpression, outErrorMessage);
- AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
- aValueAttribute->setValue(aValue);
- setResult(aParam);
// Name
std::string aName = string(ParametersPlugin_Parameter::VARIABLE_ID())->value();
std::ostringstream sstream;
data()->setName(aName + " ("+ aParamValue + ")");
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);
- //}
+ if (!outErrorMessage.empty()) {
+ aStateMsg = "Error: " + outErrorMessage;
+ data()->execState(ModelAPI_StateExecFailed);
+ }
+ setError(aStateMsg, false);
+ // Value
+ AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
+ aValueAttribute->setValue(aValue);
+ setResult(aParam);
+}
+
+void ParametersPlugin_Parameter::execute()
+{
}
double ParametersPlugin_Parameter::evaluate(const std::string& theExpression, std::string& theError)
PARAMETERSPLUGIN_EXPORT virtual void initAttributes();
PARAMETERSPLUGIN_EXPORT virtual bool isInHistory(); //false
- //bool isI
+
+ PARAMETERSPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID);
/// Use plugin manager for features creation
ParametersPlugin_Parameter();
#include <ParametersPlugin_Plugin.h>
#include <ParametersPlugin_Parameter.h>
+#include <ParametersPlugin_Validators.h>
#include <ModelAPI_Session.h>
+#include <ModelAPI_Validator.h>
#include <memory>
// register this plugin
SessionPtr aSession = ModelAPI_Session::get();
aSession->registerPlugin(this);
+
+ ModelAPI_ValidatorsFactory* aFactory = aSession->validators();
+ aFactory->registerValidator("Parameters_VariableValidator",
+ new ParametersPlugin_VariableValidator);
+ aFactory->registerValidator("Parameters_ExpressionValidator",
+ new ParametersPlugin_ExpressionValidator);
}
FeaturePtr ParametersPlugin_Plugin::createFeature(std::string theFeatureID)
--- /dev/null
+/*
+ * Parameters_VariableValidator.cpp
+ *
+ * Created on: Apr 9, 2015
+ * Author: sbh
+ */
+
+#include <ParametersPlugin_Validators.h>
+
+#include <ModelAPI_AttributeString.h>
+#include <ModelAPI_Feature.h>
+#include <ModelAPI_ResultParameter.h>
+
+ParametersPlugin_VariableValidator::ParametersPlugin_VariableValidator()
+{
+ myPyVariableRegex = std::regex("[_a-zA-Z][a-zA-Z0-9_]*");
+}
+
+ParametersPlugin_VariableValidator::~ParametersPlugin_VariableValidator()
+{
+}
+
+bool ParametersPlugin_VariableValidator::isValid(const AttributePtr& theAttribute,
+ const std::list<std::string>& theArguments) const
+{
+ AttributeStringPtr aStrAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
+ bool result = std::regex_match(aStrAttr->value(), myPyVariableRegex);
+ return result;
+}
+
+ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator()
+{
+
+}
+
+ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
+{
+
+}
+
+bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
+ const std::list<std::string>& theArguments) const
+{
+ FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
+ ResultParameterPtr aParam =
+ std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aFeature->firstResult());
+
+ AttributeStringPtr aStrAttr =
+ std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
+ bool isEmptyExpr = aStrAttr->value().empty();
+ if(isEmptyExpr)
+ return false;
+
+ if(!aParam.get())
+ return false;
+
+ return aFeature->error().empty();
+}
--- /dev/null
+/*
+ * Parameters_VariableValidator.h
+ *
+ * Created on: Apr 9, 2015
+ * Author: sbh
+ */
+
+#ifndef PARAMETERSPLUGIN_VARIABLEVALIDATOR_H_
+#define PARAMETERSPLUGIN_VARIABLEVALIDATOR_H_
+
+#include <ParametersPlugin.h>
+#include <ParametersPlugin_Validators.h>
+
+#include <ModelAPI_AttributeValidator.h>
+
+#include <regex>
+#include <memory>
+
+class ParametersPlugin_VariableValidator : public ModelAPI_AttributeValidator
+{
+ public:
+ PARAMETERSPLUGIN_EXPORT ParametersPlugin_VariableValidator();
+ PARAMETERSPLUGIN_EXPORT virtual ~ParametersPlugin_VariableValidator();
+
+ //! returns true if attribute is valid
+ //! \param theAttribute the checked attribute
+ //! \param theArguments arguments of the attribute
+ PARAMETERSPLUGIN_EXPORT virtual bool isValid(const AttributePtr& theAttribute,
+ const std::list<std::string>& theArguments) const;
+
+ private:
+ std::regex myPyVariableRegex;
+};
+
+class ParametersPlugin_ExpressionValidator: public ModelAPI_AttributeValidator
+{
+ public:
+ PARAMETERSPLUGIN_EXPORT ParametersPlugin_ExpressionValidator();
+ PARAMETERSPLUGIN_EXPORT virtual ~ParametersPlugin_ExpressionValidator();
+
+ //! returns true if attribute is valid
+ //! \param theAttribute the checked attribute
+ //! \param theArguments arguments of the attribute
+ PARAMETERSPLUGIN_EXPORT virtual bool isValid(const AttributePtr& theAttribute,
+ const std::list<std::string>& theArguments) const;
+};
+
+
+#endif /* PARAMETERSPLUGIN_VARIABLEVALIDATOR_H_ */
<workbench id="Part">
<group id="Parameters">
<feature id="Parameter" title="New Variable" tooltip="Creates a variable" icon=":pictures/expression.png">
- <stringvalue id="variable" label="Name" icon=":pictures/expression.png"/>
- <expr_editor id="expression"/>
+ <stringvalue id="variable" label="Name" icon=":pictures/expression.png">
+ <validator id="Parameters_VariableValidator"/>
+ </stringvalue>
+ <expr_editor id="expression">
+ <validator id="Parameters_ExpressionValidator"/>
+ </expr_editor>
</feature>
</group>
</workbench>