File size: 8,592 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | // SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2009 Werner Mayer <wmayer@users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
# include <memory>
#include "DocumentProtectorPy.h"
#include "DocumentProtector.h"
#include <Base/Exception.h>
#include <Base/Interpreter.h>
#include <App/DocumentPy.h>
#include <App/DocumentObject.h>
#include <App/DocumentObjectPy.h>
using namespace Sandbox;
void DocumentProtectorPy::init_type()
{
behaviors().name("DocumentProtectorPy");
behaviors().doc("Python binding class for the document protector class");
// you must have overwritten the virtual functions
behaviors().supportRepr();
behaviors().supportGetattr();
behaviors().supportSetattr();
add_varargs_method("addObject",&DocumentProtectorPy::addObject,"addObject(type,name)");
add_varargs_method("recompute",&DocumentProtectorPy::recompute,"recompute()");
}
DocumentProtectorPy::DocumentProtectorPy(App::DocumentPy *doc)
{
_dp = new DocumentProtector(doc->getDocumentPtr());
}
DocumentProtectorPy::~DocumentProtectorPy()
{
delete _dp;
}
Py::Object DocumentProtectorPy::repr()
{
if (!_dp)
throw Py::RuntimeError("Cannot print representation of deleted object");
return Py::String("Document protector");
}
DocumentProtectorPy::method_varargs_handler DocumentProtectorPy::pycxx_handler = 0;
PyObject *DocumentProtectorPy::method_varargs_ext_handler(PyObject *_self_and_name_tuple, PyObject *_args)
{
try {
return pycxx_handler(_self_and_name_tuple, _args);
}
catch (const Base::Exception& e) {
throw Py::RuntimeError(e.what());
}
catch (const std::exception& e) {
throw Py::RuntimeError(e.what());
}
catch(...) {
throw Py::RuntimeError("Unknown C++ exception");
}
}
Py::Object DocumentProtectorPy::getattr(const char * attr)
{
if (!_dp) {
std::string s;
std::ostringstream s_out;
s_out << "Cannot access attribute '" << attr << "' of deleted object";
throw Py::RuntimeError(s_out.str());
}
else {
Py::Object obj = Py::PythonExtension<DocumentProtectorPy>::getattr(attr);
if (PyCFunction_Check(obj.ptr())) {
PyCFunctionObject* op = reinterpret_cast<PyCFunctionObject*>(obj.ptr());
if (!pycxx_handler)
pycxx_handler = op->m_ml->ml_meth;
op->m_ml->ml_meth = method_varargs_ext_handler;
}
return obj;
}
}
int DocumentProtectorPy::setattr(const char * attr, const Py::Object & value)
{
if (!_dp) {
std::string s;
std::ostringstream s_out;
s_out << "Cannot access attribute '" << attr << "' of deleted object";
throw Py::RuntimeError(s_out.str());
}
else {
Base::PyGILStateRelease unlock;
return Py::PythonExtension<DocumentProtectorPy>::setattr(attr, value);
}
}
Py::Object DocumentProtectorPy::addObject(const Py::Tuple& args)
{
char* type;
char* name=0;
if (!PyArg_ParseTuple(args.ptr(), "s|s",&type, &name))
throw Py::Exception();
Base::PyGILStateRelease unlock;
if (!name)
name = type;
App::DocumentObject* obj = _dp->addObject(type, name);
if (!obj) {
std::string s;
std::ostringstream s_out;
s_out << "Could not create an object of type '" << type << "'";
throw Py::RuntimeError(s_out.str());
}
//return Py::asObject(obj->getPyObject());
return Py::asObject(new DocumentObjectProtectorPy(obj));
}
Py::Object DocumentProtectorPy::recompute(const Py::Tuple& args)
{
if (!PyArg_ParseTuple(args.ptr(), ""))
throw Py::Exception();
Base::PyGILStateRelease unlock;
_dp->recompute();
return Py::None();
}
// ----------------------------------------------------------------------------
void DocumentObjectProtectorPy::init_type()
{
behaviors().name("DocumentObjectProtectorPy");
behaviors().doc("Python binding class for the document object protector class");
// you must have overwritten the virtual functions
behaviors().supportRepr();
behaviors().supportGetattr();
behaviors().supportSetattr();
add_varargs_method("purgeTouched",&DocumentObjectProtectorPy::purgeTouched,"purgeTouched()");
}
DocumentObjectProtectorPy::DocumentObjectProtectorPy(App::DocumentObject *obj)
{
_dp = new DocumentObjectProtector(obj);
}
DocumentObjectProtectorPy::DocumentObjectProtectorPy(App::DocumentObjectPy *obj)
{
_dp = new DocumentObjectProtector(obj->getDocumentObjectPtr());
}
DocumentObjectProtectorPy::~DocumentObjectProtectorPy()
{
delete _dp;
}
Py::Object DocumentObjectProtectorPy::getObject() const
{
App::DocumentObject* obj = _dp->getObject();
PyObject* py = obj->getPyObject();
return Py::Object(py, true);
}
Py::Object DocumentObjectProtectorPy::repr()
{
if (!_dp)
throw Py::RuntimeError("Cannot print representation of deleted object");
return Py::String("Document object protector");
}
Py::Object DocumentObjectProtectorPy::getattr(const char * attr)
{
if (!_dp) {
std::string s;
std::ostringstream s_out;
s_out << "Cannot access attribute '" << attr << "' of deleted object";
throw Py::RuntimeError(s_out.str());
}
else {
App::DocumentObject* obj = _dp->getObject();
App::Property* prop = obj->getPropertyByName(attr);
if (!prop) {
return Py::PythonExtension<DocumentObjectProtectorPy>::getattr(attr);
}
return Py::asObject(prop->getPyObject());
}
}
int DocumentObjectProtectorPy::setattr(const char * attr, const Py::Object & value)
{
if (!_dp) {
std::string s;
std::ostringstream s_out;
s_out << "Cannot access attribute '" << attr << "' of deleted object";
throw Py::RuntimeError(s_out.str());
}
else {
App::DocumentObject* obj = _dp->getObject();
App::Property* prop = obj->getPropertyByName(attr);
if (!prop) {
std::string s;
std::ostringstream s_out;
s_out << "No such attribute '" << attr << "'";
throw Py::AttributeError(s_out.str());
}
Base::PyGILStateRelease unlock;
std::unique_ptr<App::Property> copy(static_cast<App::Property*>
(prop->getTypeId().createInstance()));
if (PyObject_TypeCheck(value.ptr(), DocumentObjectProtectorPy::type_object())) {
copy->setPyObject(static_cast<const DocumentObjectProtectorPy*>(value.ptr())->getObject().ptr());
}
else {
copy->setPyObject(value.ptr());
}
return _dp->setProperty(attr, *copy) ? 0 : -1;
}
}
Py::Object DocumentObjectProtectorPy::purgeTouched(const Py::Tuple&)
{
_dp->purgeTouched();
return Py::None();
}
|