void ViewProviderPythonFeatureImp::onChanged(const App::Property* prop) { // Run the onChanged method of the proxy object. Base::PyGILStateLocker lock; try { App::Property* proxy = object->getPropertyByName("Proxy"); if (proxy && proxy->getTypeId() == App::PropertyPythonObject::getClassTypeId()) { Py::Object vp = static_cast<App::PropertyPythonObject*>(proxy)->getValue(); if (vp.hasAttr(std::string("onChanged"))) { if (vp.hasAttr("__object__")) { Py::Callable method(vp.getAttr(std::string("onChanged"))); Py::Tuple args(1); std::string prop_name = object->getName(prop); args.setItem(0, Py::String(prop_name)); method.apply(args); } else { Py::Callable method(vp.getAttr(std::string("onChanged"))); Py::Tuple args(2); args.setItem(0, Py::Object(object->getPyObject(), true)); std::string prop_name = object->getName(prop); args.setItem(1, Py::String(prop_name)); method.apply(args); } } } } catch (Py::Exception&) { Base::PyException e; // extract the Python error text const char* name = object->getObject()->Label.getValue(); Base::Console().Error("ViewProviderPythonFeature::onChanged (%s): %s\n", name, e.what()); } }
void FeaturePythonImp::onChanged(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("onChanged"))) { if (feature.hasAttr("__object__")) { Py::Callable method(feature.getAttr(std::string("onChanged"))); Py::Tuple args(1); std::string prop_name = object->getName(prop); args.setItem(0, Py::String(prop_name)); method.apply(args); } else { Py::Callable method(feature.getAttr(std::string("onChanged"))); Py::Tuple args(2); args.setItem(0, Py::Object(object->getPyObject(), true)); std::string prop_name = object->getName(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 ViewProviderPythonFeatureImp::attach(App::DocumentObject *pcObject) { // Run the attach method of the proxy object. Base::PyGILStateLocker lock; try { App::Property* proxy = object->getPropertyByName("Proxy"); if (proxy && proxy->getTypeId() == App::PropertyPythonObject::getClassTypeId()) { Py::Object vp = static_cast<App::PropertyPythonObject*>(proxy)->getValue(); if (vp.hasAttr(std::string("attach"))) { if (vp.hasAttr("__object__")) { Py::Callable method(vp.getAttr(std::string("attach"))); Py::Tuple args(0); method.apply(args); } else { Py::Callable method(vp.getAttr(std::string("attach"))); Py::Tuple args(1); args.setItem(0, Py::Object(object->getPyObject(), true)); method.apply(args); } // #0000415: Now simulate a property change event to call // claimChildren if implemented. pcObject->Label.touch(); } } } catch (Py::Exception&) { Base::PyException e; // extract the Python error text const char* name = object->getObject()->Label.getValue(); Base::Console().Error("ViewProviderPythonFeature::attach (%s): %s\n", name, e.what()); } }
DocumentObjectExecReturn *FeaturePythonImp::execute() { // 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("__object__")) { Py::Callable method(feature.getAttr(std::string("execute"))); Py::Tuple args(0); method.apply(args); } else { Py::Callable method(feature.getAttr(std::string("execute"))); Py::Tuple args(1); args.setItem(0, Py::Object(object->getPyObject(), true)); method.apply(args); } } } catch (Py::Exception&) { Base::PyException e; // extract the Python error text e.ReportException(); std::stringstream str; str << object->Label.getValue() << ": " << e.what(); return new App::DocumentObjectExecReturn(str.str()); } return DocumentObject::StdReturn; }
PyObject* DocumentPy::addObject(PyObject *args) { char *sType,*sName=0; PyObject* obj=0; PyObject* view=0; if (!PyArg_ParseTuple(args, "s|sOO", &sType,&sName,&obj,&view)) // convert args: Python->C return NULL; // NULL triggers exception DocumentObject *pcFtr; pcFtr = getDocumentPtr()->addObject(sType,sName); if (pcFtr) { // Allows to hide the handling with Proxy in client python code if (obj) { try { // the python binding class to the document object Py::Object pyftr = Py::asObject(pcFtr->getPyObject()); // 'pyobj' is the python class with the implementation for DocumentObject Py::Object pyobj(obj); if (pyobj.hasAttr("__object__")) { pyobj.setAttr("__object__", pyftr); } pyftr.setAttr("Proxy", pyobj); // if a document class is set we also need a view provider defined which must be // something different to None Py::Object pyvp; if (view) pyvp = Py::Object(view); if (pyvp.isNone()) pyvp = Py::Int(1); // 'pyvp' is the python class with the implementation for ViewProvider if (pyvp.hasAttr("__vobject__")) { pyvp.setAttr("__vobject__", pyftr.getAttr("ViewObject")); } pyftr.getAttr("ViewObject").setAttr("Proxy", pyvp); return Py::new_reference_to(pyftr); } catch (Py::Exception& e) { e.clear(); } } return pcFtr->getPyObject(); } else { std::stringstream str; str << "No document object found of type '" << sType << "'" << std::ends; throw Py::Exception(PyExc_Exception,str.str()); } }
agg::rgba RendererAgg::get_color(const Py::Object& gc) { _VERBOSE("RendererAgg::get_color"); Py::Tuple rgb = Py::Tuple( gc.getAttr("_rgb") ); double alpha = Py::Float( gc.getAttr("_alpha") ); double r = Py::Float(rgb[0]); double g = Py::Float(rgb[1]); double b = Py::Float(rgb[2]); return agg::rgba(r, g, b, alpha); }
SoDetail* ViewProviderPythonFeatureImp::getDetail(const char* name) const { // Run the onChanged method of the proxy object. Base::PyGILStateLocker lock; try { App::Property* proxy = object->getPropertyByName("Proxy"); if (proxy && proxy->getTypeId() == App::PropertyPythonObject::getClassTypeId()) { Py::Object vp = static_cast<App::PropertyPythonObject*>(proxy)->getValue(); if (vp.hasAttr(std::string("getDetail"))) { Py::Callable method(vp.getAttr(std::string("getDetail"))); Py::Tuple args(1); args.setItem(0, Py::String(name)); Py::Object det(method.apply(args)); void* ptr = 0; Base::Interpreter().convertSWIGPointerObj("pivy.coin", "SoDetail *", det.ptr(), &ptr, 0); SoDetail* detail = reinterpret_cast<SoDetail*>(ptr); return detail ? detail->copy() : 0; } } } catch (const Base::Exception& e) { e.ReportException(); } catch (Py::Exception&) { Base::PyException e; // extract the Python error text e.ReportException(); } return 0; }
std::string ViewProviderPythonFeatureImp::getElement(const SoDetail *det) const { // Run the onChanged method of the proxy object. Base::PyGILStateLocker lock; try { App::Property* proxy = object->getPropertyByName("Proxy"); if (proxy && proxy->getTypeId() == App::PropertyPythonObject::getClassTypeId()) { Py::Object vp = static_cast<App::PropertyPythonObject*>(proxy)->getValue(); if (vp.hasAttr(std::string("getElement"))) { PyObject* pivy = 0; // Note: As there is no ref'counting mechanism for the SoDetail class we must // pass '0' as the last parameter so that the Python object does not 'own' // the detail object. pivy = Base::Interpreter().createSWIGPointerObj("pivy.coin", "SoDetail *", (void*)det, 0); Py::Callable method(vp.getAttr(std::string("getElement"))); Py::Tuple args(1); args.setItem(0, Py::Object(pivy, true)); Py::String name(method.apply(args)); return (std::string)name; } } } catch (const Base::Exception& e) { e.ReportException(); } catch (Py::Exception&) { Base::PyException e; // extract the Python error text e.ReportException(); } return ""; }
std::string ViewProviderPythonFeatureImp::setDisplayMode(const char* ModeName) { // Run the setDisplayMode method of the proxy object. Base::PyGILStateLocker lock; try { App::Property* proxy = object->getPropertyByName("Proxy"); if (proxy && proxy->getTypeId() == App::PropertyPythonObject::getClassTypeId()) { Py::Object vp = static_cast<App::PropertyPythonObject*>(proxy)->getValue(); if (vp.hasAttr(std::string("setDisplayMode"))) { Py::Callable method(vp.getAttr(std::string("setDisplayMode"))); Py::Tuple args(1); args.setItem(0, Py::String(ModeName)); Py::String str(method.apply(args)); return str.as_std_string(); } } } catch (Py::Exception&) { Base::PyException e; // extract the Python error text const char* name = object->getObject()->Label.getValue(); Base::Console().Error("ViewProviderPythonFeature::setDisplayMode (%s): %s\n", name, e.what()); } return ModeName; }
Py::Object DocumentObjectPy::getViewObject(void) const { try { Py::Module module(PyImport_ImportModule("FreeCADGui"),true); if (!module.hasAttr("getDocument")) { // in v0.14+, the GUI module can be loaded in console mode (but doesn't have all its document methods) return Py::None(); } Py::Callable method(module.getAttr("getDocument")); Py::Tuple arg(1); arg.setItem(0, Py::String(getDocumentObjectPtr()->getDocument()->getName())); Py::Object doc = method.apply(arg); method = doc.getAttr("getObject"); const char* internalName = getDocumentObjectPtr()->getNameInDocument(); if (!internalName) { throw Py::RuntimeError("Object has been removed from document"); } arg.setItem(0, Py::String(internalName)); Py::Object obj = method.apply(arg); return obj; } catch (Py::Exception& e) { if (PyErr_ExceptionMatches(PyExc_ImportError)) { // the GUI is not up, hence None is returned e.clear(); return Py::None(); } // FreeCADGui is loaded, so there must be wrong something else throw; // re-throw } }
const char* ViewProviderPythonFeatureImp::getDefaultDisplayMode() const { // Run the getDefaultDisplayMode method of the proxy object. Base::PyGILStateLocker lock; static std::string mode; try { App::Property* proxy = object->getPropertyByName("Proxy"); if (proxy && proxy->getTypeId() == App::PropertyPythonObject::getClassTypeId()) { Py::Object vp = static_cast<App::PropertyPythonObject*>(proxy)->getValue(); if (vp.hasAttr(std::string("getDefaultDisplayMode"))) { Py::Callable method(vp.getAttr(std::string("getDefaultDisplayMode"))); Py::Tuple args(0); Py::String str(method.apply(args)); if (str.isUnicode()) str = str.encode("ascii"); // json converts strings into unicode mode = str.as_std_string(); return mode.c_str(); } } } catch (Py::Exception&) { Base::PyException e; // extract the Python error text const char* name = object->getObject()->Label.getValue(); Base::Console().Error("ViewProviderPythonFeature::getDefaultDisplayMode (%s): %s\n", name, e.what()); } return 0; }
int RendererAgg::antialiased(const Py::Object& gc) { //return 1 if gc is antialiased _VERBOSE("RendererAgg::antialiased"); int isaa = Py::Int( gc.getAttr( "_antialiased") ); return isaa; }
ViewProviderPythonFeatureImp::ValueT ViewProviderPythonFeatureImp::canDropObject(App::DocumentObject* obj) const { // Run the onChanged method of the proxy object. Base::PyGILStateLocker lock; try { App::Property* proxy = object->getPropertyByName("Proxy"); if (proxy && proxy->getTypeId() == App::PropertyPythonObject::getClassTypeId()) { Py::Object vp = static_cast<App::PropertyPythonObject*>(proxy)->getValue(); if (vp.hasAttr(std::string("canDropObject"))) { Py::Callable method(vp.getAttr(std::string("canDropObject"))); Py::Tuple args(1); args.setItem(0, Py::Object(obj->getPyObject(), true)); Py::Boolean ok(method.apply(args)); return static_cast<bool>(ok) ? Accepted : Rejected; } else { return NotImplemented; } } } catch (Py::Exception&) { Base::PyException e; // extract the Python error text e.ReportException(); } return Rejected; }
std::vector<App::DocumentObject*> ViewProviderPythonFeatureImp::claimChildren(const std::vector<App::DocumentObject*>& base) const { std::vector<App::DocumentObject*> children; Base::PyGILStateLocker lock; try { App::Property* proxy = object->getPropertyByName("Proxy"); if (proxy && proxy->getTypeId() == App::PropertyPythonObject::getClassTypeId()) { Py::Object vp = static_cast<App::PropertyPythonObject*>(proxy)->getValue(); if (vp.hasAttr(std::string("claimChildren"))) { Py::Callable method(vp.getAttr(std::string("claimChildren"))); Py::Tuple args(0); Py::Sequence list(method.apply(args)); for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { PyObject* item = (*it).ptr(); if (PyObject_TypeCheck(item, &(App::DocumentObjectPy::Type))) { App::DocumentObject* obj = static_cast<App::DocumentObjectPy*>(item)->getDocumentObjectPtr(); children.push_back(obj); } } } else { children = base; } } } catch (Py::Exception&) { Base::PyException e; // extract the Python error text Base::Console().Error("ViewProviderPythonFeature::claimChildren: %s\n", e.what()); } return children; }
PyObject* Application::sAddWorkbenchHandler(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/) { PyObject* pcObject; std::string item; if (!PyArg_ParseTuple(args, "O", &pcObject)) // convert args: Python->C return NULL; // NULL triggers exception try { // get the class object 'Workbench' from the main module that is expected // to be base class for all workbench classes Py::Module module("__main__"); Py::Object baseclass(module.getAttr(std::string("Workbench"))); // check whether it is an instance or class object Py::Object object(pcObject); Py::String name; if (PyObject_IsSubclass(object.ptr(), baseclass.ptr()) == 1) { // create an instance of this class name = object.getAttr(std::string("__name__")); Py::Tuple args; Py::Callable creation(object); object = creation.apply(args); } else if (PyObject_IsInstance(object.ptr(), baseclass.ptr()) == 1) { // extract the class name of the instance PyErr_Clear(); // PyObject_IsSubclass set an exception Py::Object classobj = object.getAttr(std::string("__class__")); name = classobj.getAttr(std::string("__name__")); } else { PyErr_SetString(PyExc_TypeError, "arg must be a subclass or an instance of " "a subclass of 'Workbench'"); return NULL; } // Search for some methods and members without invoking them Py::Callable(object.getAttr(std::string("Initialize"))); Py::Callable(object.getAttr(std::string("GetClassName"))); item = name.as_std_string(); PyObject* wb = PyDict_GetItemString(Instance->_pcWorkbenchDictionary,item.c_str()); if (wb) { PyErr_Format(PyExc_KeyError, "'%s' already exists.", item.c_str()); return NULL; } PyDict_SetItemString(Instance->_pcWorkbenchDictionary,item.c_str(),object.ptr()); Instance->signalAddWorkbench(item.c_str()); } catch (const Py::Exception&) { return NULL; } Py_INCREF(Py_None); return Py_None; }
QIcon ViewProviderPythonFeatureImp::getIcon() const { // default icon //static QPixmap px = BitmapFactory().pixmap("Tree_Python"); // Run the getIcon method of the proxy object. Base::PyGILStateLocker lock; try { App::Property* proxy = object->getPropertyByName("Proxy"); if (proxy && proxy->getTypeId() == App::PropertyPythonObject::getClassTypeId()) { Py::Object vp = static_cast<App::PropertyPythonObject*>(proxy)->getValue(); if (vp.hasAttr(std::string("getIcon"))) { Py::Callable method(vp.getAttr(std::string("getIcon"))); Py::Tuple args(0); Py::String str(method.apply(args)); std::string content = str.as_std_string(); QPixmap icon; // Check if the passed string is a filename, otherwise treat as xpm data QFileInfo fi(QString::fromAscii(content.c_str())); if (fi.isFile() && fi.exists()) { icon.load(fi.absoluteFilePath()); } else { QByteArray ary; int strlen = (int)content.size(); ary.resize(strlen); for (int j=0; j<strlen; j++) ary[j]=content[j]; // Make sure to remove crap around the XPM data QList<QByteArray> lines = ary.split('\n'); QByteArray buffer; buffer.reserve(ary.size()+lines.size()); for (QList<QByteArray>::iterator it = lines.begin(); it != lines.end(); ++it) { QByteArray trim = it->trimmed(); if (!trim.isEmpty()) { buffer.append(trim); buffer.append('\n'); } } icon.loadFromData(buffer, "XPM"); } if (!icon.isNull()) { return icon; } } } } catch (Py::Exception&) { Base::PyException e; // extract the Python error text Base::Console().Error("ViewProviderPythonFeature::getIcon: %s\n", e.what()); } return QIcon(); }
std::vector<std::string> ViewProviderPythonFeatureImp::getDisplayModes(void) const { // Run the getDisplayModes method of the proxy object. Base::PyGILStateLocker lock; std::vector<std::string> modes; try { App::Property* proxy = object->getPropertyByName("Proxy"); if (proxy && proxy->getTypeId() == App::PropertyPythonObject::getClassTypeId()) { Py::Object vp = static_cast<App::PropertyPythonObject*>(proxy)->getValue(); if (vp.hasAttr(std::string("getDisplayModes"))) { if (vp.hasAttr("__object__")) { Py::Callable method(vp.getAttr(std::string("getDisplayModes"))); Py::Tuple args(0); Py::List list(method.apply(args)); for (Py::List::iterator it = list.begin(); it != list.end(); ++it) { Py::String str(*it); modes.push_back(str.as_std_string()); } } else { Py::Callable method(vp.getAttr(std::string("getDisplayModes"))); Py::Tuple args(1); args.setItem(0, Py::Object(object->getPyObject(), true)); Py::List list(method.apply(args)); for (Py::List::iterator it = list.begin(); it != list.end(); ++it) { Py::String str(*it); modes.push_back(str.as_std_string()); } } } } } catch (Py::Exception&) { Base::PyException e; // extract the Python error text const char* name = object->getObject()->Label.getValue(); Base::Console().Error("ViewProviderPythonFeature::getDisplayModes (%s): %s\n", name, e.what()); } return modes; }
bool ViewProviderPythonFeatureImp::onDelete(const std::vector<std::string> & sub) { Base::PyGILStateLocker lock; try { App::Property* proxy = object->getPropertyByName("Proxy"); if (proxy && proxy->getTypeId() == App::PropertyPythonObject::getClassTypeId()) { Py::Object vp = static_cast<App::PropertyPythonObject*>(proxy)->getValue(); if (vp.hasAttr(std::string("onDelete"))) { Py::Tuple seq(sub.size()); int index=0; for (std::vector<std::string>::const_iterator it = sub.begin(); it != sub.end(); ++it) { seq.setItem(index++, Py::String(*it)); } if (vp.hasAttr("__object__")) { Py::Callable method(vp.getAttr(std::string("onDelete"))); Py::Tuple args(1); args.setItem(0, seq); Py::Boolean ok(method.apply(args)); return (bool)ok; } else { Py::Callable method(vp.getAttr(std::string("onDelete"))); Py::Tuple args(2); args.setItem(0, Py::Object(object->getPyObject(), true)); args.setItem(1, seq); Py::Boolean ok(method.apply(args)); return (bool)ok; } } } } catch (Py::Exception&) { Base::PyException e; // extract the Python error text e.ReportException(); } return true; }
PyObject* DocumentObjectGroupPy::addObject(PyObject *args) { PyObject *object; if (!PyArg_ParseTuple(args, "O!", &(DocumentObjectPy::Type), &object)) // convert args: Python->C return NULL; // NULL triggers exception DocumentObjectPy* docObj = static_cast<DocumentObjectPy*>(object); if (!docObj->getDocumentObjectPtr() || !docObj->getDocumentObjectPtr()->getNameInDocument()) { PyErr_SetString(Base::BaseExceptionFreeCADError, "Cannot add an invalid object"); return NULL; } if (docObj->getDocumentObjectPtr()->getDocument() != getDocumentObjectGroupPtr()->getDocument()) { PyErr_SetString(Base::BaseExceptionFreeCADError, "Cannot add an object from another document to this group"); return NULL; } if (docObj->getDocumentObjectPtr() == this->getDocumentObjectGroupPtr()) { PyErr_SetString(Base::BaseExceptionFreeCADError, "Cannot add a group object to itself"); return NULL; } if (docObj->getDocumentObjectPtr()->getTypeId().isDerivedFrom(DocumentObjectGroup::getClassTypeId())) { App::DocumentObjectGroup* docGrp = static_cast<DocumentObjectGroup*>(docObj->getDocumentObjectPtr()); if (this->getDocumentObjectGroupPtr()->isChildOf(docGrp)) { PyErr_SetString(Base::BaseExceptionFreeCADError, "Cannot add a group object to a child group"); return NULL; } } DocumentObjectGroup* grp = getDocumentObjectGroupPtr(); if (grp->getTypeId().isDerivedFrom(App::DocumentObjectGroupPython::getClassTypeId())) { DocumentObjectGroupPython* grppy = static_cast<DocumentObjectGroupPython*>(grp); App::Property* proxy = grppy->getPropertyByName("Proxy"); if (proxy && proxy->getTypeId() == App::PropertyPythonObject::getClassTypeId()) { Py::Object vp = static_cast<App::PropertyPythonObject*>(proxy)->getValue(); if (vp.hasAttr(std::string("addObject"))) { Py::Callable method(vp.getAttr(std::string("addObject"))); // check which this method belongs to to avoid an infinite recursion if (method.getAttr(std::string("__self__")) != Py::Object(this)) { Py::Tuple args(1); args[0] = Py::Object(object); method.apply(args); Py_Return; } } } } grp->addObject(docObj->getDocumentObjectPtr()); Py_Return; }
Py::Tuple RendererAgg::get_dashes(const Py::Object& gc) { //return the dashOffset, dashes sequence tuple. _VERBOSE("RendererAgg::get_dashes"); Py::Tuple _dashes = gc.getAttr("_dashes"); size_t N = _dashes.length(); if (N!=2) throw Py::ValueError("GC _dashes must be a length 2 tuple"); return _dashes; }
ViewProviderPythonFeatureImp::ValueT ViewProviderPythonFeatureImp::unsetEdit(int ModNum) { // Run the onChanged method of the proxy object. Base::PyGILStateLocker lock; try { App::Property* proxy = object->getPropertyByName("Proxy"); if (proxy && proxy->getTypeId() == App::PropertyPythonObject::getClassTypeId()) { Py::Object vp = static_cast<App::PropertyPythonObject*>(proxy)->getValue(); if (vp.hasAttr(std::string("unsetEdit"))) { if (vp.hasAttr("__object__")) { Py::Callable method(vp.getAttr(std::string("unsetEdit"))); Py::Tuple args(1); args.setItem(0, Py::Int(ModNum)); Py::Boolean ok(method.apply(args)); bool value = (bool)ok; return value ? Accepted : Rejected; } else { Py::Callable method(vp.getAttr(std::string("unsetEdit"))); Py::Tuple args(2); args.setItem(0, Py::Object(object->getPyObject(), true)); args.setItem(1, Py::Int(ModNum)); Py::Boolean ok(method.apply(args)); bool value = (bool)ok; return value ? Accepted : Rejected; } } } } catch (Py::Exception&) { Base::PyException e; // extract the Python error text e.ReportException(); } return NotImplemented; }
void ViewProviderPythonFeatureImp::setupContextMenu(QMenu* menu) { // Run the attach method of the proxy object. Base::PyGILStateLocker lock; try { App::Property* proxy = object->getPropertyByName("Proxy"); if (proxy && proxy->getTypeId() == App::PropertyPythonObject::getClassTypeId()) { Py::Object vp = static_cast<App::PropertyPythonObject*>(proxy)->getValue(); if (vp.hasAttr(std::string("setupContextMenu"))) { if (vp.hasAttr("__object__")) { PythonWrapper wrap; wrap.loadGuiModule(); wrap.loadWidgetsModule(); Py::Callable method(vp.getAttr(std::string("setupContextMenu"))); Py::Tuple args(1); args.setItem(0, wrap.fromQWidget(menu, "QMenu")); method.apply(args); } else { PythonWrapper wrap; wrap.loadGuiModule(); wrap.loadWidgetsModule(); Py::Callable method(vp.getAttr(std::string("setupContextMenu"))); Py::Tuple args(2); args.setItem(0, Py::Object(object->getPyObject(), true)); args.setItem(1, wrap.fromQWidget(menu, "QMenu")); method.apply(args); } } } } catch (Py::Exception&) { Base::PyException e; // extract the Python error text e.ReportException(); } }
void ViewProviderPythonFeatureImp::updateData(const App::Property* prop) { // Run the updateData method of the proxy object. Base::PyGILStateLocker lock; try { App::Property* proxy = object->getPropertyByName("Proxy"); if (proxy && proxy->getTypeId() == App::PropertyPythonObject::getClassTypeId()) { Py::Object vp = static_cast<App::PropertyPythonObject*>(proxy)->getValue(); if (vp.hasAttr(std::string("updateData"))) { if (vp.hasAttr("__object__")) { Py::Callable method(vp.getAttr(std::string("updateData"))); Py::Tuple args(1); const char* prop_name = object->getObject()->getPropertyName(prop); if (prop_name) { args.setItem(0, Py::String(prop_name)); method.apply(args); } } else { Py::Callable method(vp.getAttr(std::string("updateData"))); Py::Tuple args(2); args.setItem(0, Py::Object(object->getObject()->getPyObject(), true)); const char* prop_name = object->getObject()->getPropertyName(prop); if (prop_name) { args.setItem(1, Py::String(prop_name)); method.apply(args); } } } } } catch (Py::Exception&) { Base::PyException e; // extract the Python error text e.ReportException(); } }
agg::vcgen_stroke::line_cap_e RendererAgg::get_linecap(const Py::Object& gc) { _VERBOSE("RendererAgg::get_linecap"); std::string capstyle = Py::String( gc.getAttr( "_capstyle" ) ); if (capstyle=="butt") return agg::vcgen_stroke::butt_cap; else if (capstyle=="round") return agg::vcgen_stroke::round_cap; else if(capstyle=="projecting") return agg::vcgen_stroke::square_cap; else throw Py::ValueError("GC _capstyle attribute must be one of butt, round, projecting"); }
agg::vcgen_stroke::line_join_e RendererAgg::get_joinstyle(const Py::Object& gc) { _VERBOSE("RendererAgg::get_joinstyle"); std::string joinstyle = Py::String( gc.getAttr("_joinstyle") ); if (joinstyle=="miter") return agg::vcgen_stroke::miter_join; else if (joinstyle=="round") return agg::vcgen_stroke::round_join; else if(joinstyle=="bevel") return agg::vcgen_stroke::bevel_join; else throw Py::ValueError("GC _joinstyle attribute must be one of butt, round, projecting"); }
Py::Object RendererAgg::draw_ellipse(const Py::Tuple& args) { _VERBOSE("RendererAgg::draw_ellipse"); args.verify_length(6); Py::Object gcEdge = args[0]; Py::Object rgbFaceMaybeNone = args[1]; double x = Py::Float( args[2] ); double y = Py::Float( args[3] ); double w = Py::Float( args[4] ); double h = Py::Float( args[5] ); set_clip_rectangle(gcEdge); //last arg is num steps agg::ellipse path(x, height-y, w, h, 100); agg::rgba edgecolor = get_color(gcEdge); if (rgbFaceMaybeNone.ptr() != Py_None) { Py::SeqBase<Py::Object> rgbFace = rgbFaceMaybeNone; agg::rgba facecolor = rgb_to_color(rgbFace, edgecolor.a); theRenderer->color(facecolor); theRasterizer->add_path(path); theRasterizer->render(*slineP8, *theRenderer); } //now fill the edge double lw = points_to_pixels ( gcEdge.getAttr("_linewidth") ) ; agg::conv_stroke<agg::ellipse> stroke(path); stroke.width(lw); theRenderer->color(edgecolor); //self->theRasterizer->gamma(agg::gamma_power(gamma)); theRasterizer->add_path(stroke); theRasterizer->render(*slineP8, *theRenderer); return Py::Object(); }
Py::Object DocumentObjectPy::getViewObject(void) const { try { Py::Module module(PyImport_ImportModule("FreeCADGui"),true); Py::Callable method(module.getAttr("getDocument")); Py::Tuple arg(1); arg.setItem(0, Py::String(getDocumentObjectPtr()->getDocument()->getName())); Py::Object doc = method.apply(arg); method = doc.getAttr("getObject"); arg.setItem(0, Py::String(getDocumentObjectPtr()->getNameInDocument())); Py::Object obj = method.apply(arg); return obj; } catch (Py::Exception& e) { if (PyErr_ExceptionMatches(PyExc_ImportError)) { // the GUI is not up, hence None is returned e.clear(); return Py::None(); } // FreeCADGui is loaded, so there must be wrong something else throw; // re-throw } }
bool ViewProviderPythonFeatureImp::useNewSelectionModel() const { // Run the useNewSelectionModel method of the proxy object. Base::PyGILStateLocker lock; try { App::Property* proxy = object->getPropertyByName("Proxy"); if (proxy && proxy->getTypeId() == App::PropertyPythonObject::getClassTypeId()) { Py::Object vp = static_cast<App::PropertyPythonObject*>(proxy)->getValue(); if (vp.hasAttr(std::string("useNewSelectionModel"))) { Py::Callable method(vp.getAttr(std::string("useNewSelectionModel"))); Py::Tuple args; Py::Boolean ok(method.apply(args)); return (bool)ok; } } } catch (Py::Exception&) { Base::PyException e; // extract the Python error text e.ReportException(); } return true; }
void RendererAgg::set_clip_rectangle( const Py::Object& gc) { //set the clip rectangle from the gc _VERBOSE("RendererAgg::set_clip_rectangle"); Py::Object o ( gc.getAttr( "_cliprect" ) ); if (o.ptr()==Py_None) { // set clipping to false and return success theRasterizer->reset_clipping(); return; } Py::SeqBase<Py::Object> rect( o ); double l = Py::Float(rect[0]) ; double b = Py::Float(rect[1]) ; double w = Py::Float(rect[2]) ; double h = Py::Float(rect[3]) ; theRasterizer->clip_box(l, height-(b+h), l+w, height-b); }
PyObject* DocumentObjectGroupPy::removeObject(PyObject *args) { PyObject *object; if (!PyArg_ParseTuple(args, "O!", &(DocumentObjectPy::Type), &object)) // convert args: Python->C return NULL; // NULL triggers exception DocumentObjectPy* docObj = static_cast<DocumentObjectPy*>(object); if (!docObj->getDocumentObjectPtr() || !docObj->getDocumentObjectPtr()->getNameInDocument()) { PyErr_SetString(Base::BaseExceptionFreeCADError, "Cannot remove an invalid object"); return NULL; } if (docObj->getDocumentObjectPtr()->getDocument() != getDocumentObjectGroupPtr()->getDocument()) { PyErr_SetString(Base::BaseExceptionFreeCADError, "Cannot remove an object from another document from this group"); return NULL; } DocumentObjectGroup* grp = getDocumentObjectGroupPtr(); if (grp->getTypeId().isDerivedFrom(App::DocumentObjectGroupPython::getClassTypeId())) { DocumentObjectGroupPython* grppy = static_cast<DocumentObjectGroupPython*>(grp); App::Property* proxy = grppy->getPropertyByName("Proxy"); if (proxy && proxy->getTypeId() == App::PropertyPythonObject::getClassTypeId()) { Py::Object vp = static_cast<App::PropertyPythonObject*>(proxy)->getValue(); if (vp.hasAttr(std::string("removeObject"))) { Py::Callable method(vp.getAttr(std::string("removeObject"))); Py::Tuple args(1); args[0] = Py::Object(object); method.apply(args); Py_Return; } } } grp->removeObject(docObj->getDocumentObjectPtr()); Py_Return; }