+ exposing onBeforeChange to Python

This commit is contained in:
wmayer 2014-12-01 10:58:25 +01:00
parent 9c2f8d785d
commit 73859191f3
2 changed files with 38 additions and 0 deletions

View File

@ -76,6 +76,39 @@ DocumentObjectExecReturn *FeaturePythonImp::execute()
return DocumentObject::StdReturn;
}
void FeaturePythonImp::onBeforeChange(const Property* prop)
{
// Run the execute method of the proxy object.
Base::PyGILStateLocker lock;
try {
Property* proxy = object->getPropertyByName("Proxy");
if (proxy && proxy->getTypeId() == PropertyPythonObject::getClassTypeId()) {
Py::Object feature = static_cast<PropertyPythonObject*>(proxy)->getValue();
if (feature.hasAttr(std::string("onBeforeChange"))) {
if (feature.hasAttr("__object__")) {
Py::Callable method(feature.getAttr(std::string("onBeforeChange")));
Py::Tuple args(1);
std::string prop_name = object->getPropertyName(prop);
args.setItem(0, Py::String(prop_name));
method.apply(args);
}
else {
Py::Callable method(feature.getAttr(std::string("onBeforeChange")));
Py::Tuple args(2);
args.setItem(0, Py::Object(object->getPyObject(), true));
std::string prop_name = object->getPropertyName(prop);
args.setItem(1, Py::String(prop_name));
method.apply(args);
}
}
}
}
catch (Py::Exception&) {
Base::PyException e; // extract the Python error text
e.ReportException();
}
}
void FeaturePythonImp::onChanged(const Property* prop)
{
// Run the execute method of the proxy object.

View File

@ -45,6 +45,7 @@ public:
~FeaturePythonImp();
DocumentObjectExecReturn *execute();
void onBeforeChange(const Property* prop);
void onChanged(const Property* prop);
PyObject *getPyObject(void);
@ -199,6 +200,10 @@ public:
}
protected:
virtual void onBeforeChange(const Property* prop) {
FeatureT::onBeforeChange(prop);
imp->onBeforeChange(prop);
}
virtual void onChanged(const Property* prop) {
imp->onChanged(prop);
FeatureT::onChanged(prop);