Exemplo n.º 1
0
PyObject* SelectionObjectPy::isObjectTypeOf(PyObject * args)
{
    char* type;
    if (!PyArg_ParseTuple(args, "s", &type))
        return 0;
    Base::Type id = Base::Type::fromName(type);
    if (id.isBad()) {
        PyErr_SetString(PyExc_TypeError, "Not a valid type");
        return 0;
    }

    bool ok = getSelectionObjectPtr()->isObjectTypeOf(id);

    return Py_BuildValue("O", (ok ? Py_True : Py_False));
}
PyObject* ExtensionContainerPy::hasExtension(PyObject *args) {

    char *type;
    if (!PyArg_ParseTuple(args, "s", &type)) 
        return NULL;                                         // NULL triggers exception 

    //get the extension type asked for
    Base::Type extension =  Base::Type::fromName(type);
    if(extension.isBad() || !extension.isDerivedFrom(App::Extension::getExtensionClassTypeId())) {
        std::stringstream str;
        str << "No extension found of type '" << type << "'" << std::ends;
        throw Py::Exception(Base::BaseExceptionFreeCADError,str.str());
    }

    bool val = false;
    if (getExtensionContainerPtr()->hasExtension(extension)) {
        val = true;
    }

    return PyBool_FromLong(val ? 1 : 0);
}
PyObject* ExtensionContainerPy::addExtension(PyObject *args) {

    char *typeId;
    PyObject* proxy;
    if (!PyArg_ParseTuple(args, "sO", &typeId, &proxy))
        return NULL;

    //get the extension type asked for
    Base::Type extension =  Base::Type::fromName(typeId);
    if (extension.isBad() || !extension.isDerivedFrom(App::Extension::getExtensionClassTypeId())) {
        std::stringstream str;
        str << "No extension found of type '" << typeId << "'" << std::ends;
        throw Py::Exception(Base::BaseExceptionFreeCADError,str.str());
    }

    //register the extension
    App::Extension* ext = static_cast<App::Extension*>(extension.createInstance());
    //check if this really is a python extension!
    if (!ext->isPythonExtension()) {
        delete ext;
        std::stringstream str;
        str << "Extension is not a python addable version: '" << typeId << "'" << std::ends;
        throw Py::Exception(Base::BaseExceptionFreeCADError,str.str());
    }

    ext->initExtension(getExtensionContainerPtr());

    //set the proxy to allow python overrides
    App::Property* pp = ext->extensionGetPropertyByName("ExtensionProxy");
    if (!pp) {
        std::stringstream str;
        str << "Accessing the proxy property failed!" << std::ends;
        throw Py::Exception(Base::BaseExceptionFreeCADError,str.str());
    }
    static_cast<PropertyPythonObject*>(pp)->setPyObject(proxy);

    // The PyTypeObject is shared by all instances of this type and therefore
    // we have to add new methods only once.
    PyObject* obj = ext->getExtensionPyObject();
    PyMethodDef* meth = reinterpret_cast<PyMethodDef*>(obj->ob_type->tp_methods);
    PyTypeObject *type = this->ob_type;
    PyObject *dict = type->tp_dict;

    // make sure to do the initialization only once
    if (meth->ml_name) {
        PyObject* item = PyDict_GetItemString(dict, meth->ml_name);
        if (item == NULL) {
            // Note: this adds the methods to the type object to make sure
            // it appears in the call tips. The function will not be bound
            // to an instance
            Py_INCREF(dict);
            while (meth->ml_name) {
                PyObject *func;
                func = PyCFunction_New(meth, 0);
                if (func == NULL)
                    break;
                if (PyDict_SetItemString(dict, meth->ml_name, func) < 0)
                    break;
                Py_DECREF(func);
                ++meth;
            }

            Py_DECREF(dict);
        }
    }

    Py_DECREF(obj);

    Py_Return;
}