App: add convenience method DocumentObject::clearExpression()

This commit is contained in:
wmayer
2022-03-10 12:09:57 +01:00
parent c1d86b2f0b
commit b34a00fe14
4 changed files with 35 additions and 8 deletions

View File

@@ -949,6 +949,16 @@ void DocumentObject::setExpression(const ObjectIdentifier &path, std::shared_ptr
ExpressionEngine.setValue(path, expr);
}
/**
* @brief Clear the expression of the object identifier \a path in this document object.
* @param path Target object identifier
*/
void DocumentObject::clearExpression(const ObjectIdentifier & path)
{
setExpression(path, std::shared_ptr<Expression>());
}
/**
* @brief Get expression information associated with \a path.
* @param path Object identifier

View File

@@ -425,6 +425,8 @@ public:
virtual void setExpression(const ObjectIdentifier & path, std::shared_ptr<App::Expression> expr);
void clearExpression(const ObjectIdentifier & path);
virtual const PropertyExpressionEngine::ExpressionInfo getExpression(const ObjectIdentifier &path) const;
virtual void renameObjectIdentifiers(const std::map<App::ObjectIdentifier, App::ObjectIdentifier> & paths);

View File

@@ -55,6 +55,11 @@
<UserDocu>Register an expression for a property</UserDocu>
</Documentation>
</Methode>
<Methode Name="clearExpression">
<Documentation>
<UserDocu>Clear the expression for a property</UserDocu>
</Documentation>
</Methode>
<Methode Name="evalExpression" Class="true">
<Documentation>
<UserDocu>Evaluate an expression</UserDocu>

View File

@@ -314,21 +314,31 @@ PyObject* DocumentObjectPy::setExpression(PyObject * args)
App::ObjectIdentifier p(ObjectIdentifier::parse(getDocumentObjectPtr(), path));
if (Py::Object(expr).isNone())
getDocumentObjectPtr()->setExpression(p, std::shared_ptr<Expression>());
if (Py::Object(expr).isNone()) {
getDocumentObjectPtr()->clearExpression(p);
Py_Return;
}
else if (PyUnicode_Check(expr)) {
const char * exprStr = PyUnicode_AsUTF8(expr);
std::shared_ptr<Expression> shared_expr(Expression::parse(getDocumentObjectPtr(), exprStr));
if(shared_expr && comment)
if (shared_expr && comment)
shared_expr->comment = comment;
getDocumentObjectPtr()->setExpression(p, shared_expr);
Py_Return;
}
else if (PyUnicode_Check(expr)) {
std::string exprStr = PyUnicode_AsUTF8(expr);
}
else
throw Py::TypeError("String or None expected.");
throw Py::TypeError("String or None expected.");
}
PyObject* DocumentObjectPy::clearExpression(PyObject * args)
{
char * path = nullptr;
if (!PyArg_ParseTuple(args, "s", &path))
return nullptr;
App::ObjectIdentifier p(ObjectIdentifier::parse(getDocumentObjectPtr(), path));
getDocumentObjectPtr()->clearExpression(p);
Py_Return;
}