+ simplify porting of DraftUtils module to Python3

This commit is contained in:
wmayer 2016-01-20 11:57:59 +01:00
parent cbe9c39a96
commit ebc6d37262
2 changed files with 71 additions and 57 deletions

View File

@ -29,14 +29,12 @@
#include <Base/Console.h> #include <Base/Console.h>
#include <Base/Interpreter.h> #include <Base/Interpreter.h>
extern struct PyMethodDef DraftUtils_methods[]; namespace DraftUtils {
extern PyObject* initModule();
PyDoc_STRVAR(module_DraftUtils_doc, "The DraftUtils module contains utility functions for the Draft module."); }
/* Python entry */ /* Python entry */
extern "C" { PyMODINIT_FUNC initDraftUtils()
void DraftUtilsExport initDraftUtils()
{ {
// load dependent module // load dependent module
try { try {
@ -46,8 +44,6 @@ extern "C" {
PyErr_SetString(PyExc_ImportError, e.what()); PyErr_SetString(PyExc_ImportError, e.what());
return; return;
} }
Py_InitModule3("DraftUtils", DraftUtils_methods, module_DraftUtils_doc); /* mod name, table ptr */ (void)DraftUtils::initModule();
Base::Console().Log("Loading DraftUtils module... done\n"); Base::Console().Log("Loading DraftUtils module... done\n");
} }
} // extern "C"

View File

@ -26,6 +26,9 @@
# include <Python.h> # include <Python.h>
#endif #endif
#include <CXX/Extensions.hxx>
#include <CXX/Objects.hxx>
#include <Base/Console.h> #include <Base/Console.h>
#include <Base/VectorPy.h> #include <Base/VectorPy.h>
#include <Base/FileInfo.h> #include <Base/FileInfo.h>
@ -36,21 +39,36 @@
#include "DraftDxf.h" #include "DraftDxf.h"
using namespace DraftUtils; namespace DraftUtils {
class Module : public Py::ExtensionModule<Module>
{
public:
Module() : Py::ExtensionModule<Module>("DraftUtils")
{
add_varargs_method("readDXF",&Module::readDXF,
"readDXF(filename,[document,ignore_errors]): Imports a DXF file into the given document. ignore_errors is True by default."
);
initialize("The DraftUtils module contains utility functions for the Draft module."); // register with Python
}
static PyObject * readDXF (PyObject *self, PyObject *args) virtual ~Module() {}
private:
Py::Object readDXF(const Py::Tuple& args)
{ {
char* Name; char* Name;
const char* DocName=0; const char* DocName=0;
bool IgnoreErrors=true; bool IgnoreErrors=true;
if (!PyArg_ParseTuple(args, "et|sb","utf-8",&Name,&DocName,&IgnoreErrors)) if (!PyArg_ParseTuple(args.ptr(), "et|sb","utf-8",&Name,&DocName,&IgnoreErrors))
return NULL; throw Py::Exception();
std::string EncodedName = std::string(Name); std::string EncodedName = std::string(Name);
PyMem_Free(Name); PyMem_Free(Name);
Base::FileInfo file(EncodedName.c_str()); Base::FileInfo file(EncodedName.c_str());
if (!file.exists()) if (!file.exists())
Py_Error(Base::BaseExceptionFreeCADError, "File doesn't exist"); throw Py::RuntimeError("File doesn't exist");
App::Document *pcDoc; App::Document *pcDoc;
if (DocName) if (DocName)
pcDoc = App::GetApplication().getDocument(DocName); pcDoc = App::GetApplication().getDocument(DocName);
@ -59,23 +77,23 @@ static PyObject * readDXF (PyObject *self, PyObject *args)
if (!pcDoc) if (!pcDoc)
pcDoc = App::GetApplication().newDocument(DocName); pcDoc = App::GetApplication().newDocument(DocName);
PY_TRY { try {
// read the DXF file // read the DXF file
DraftDxfRead dxf_file(EncodedName,pcDoc); DraftDxfRead dxf_file(EncodedName,pcDoc);
dxf_file.DoRead(IgnoreErrors); dxf_file.DoRead(IgnoreErrors);
pcDoc->recompute(); pcDoc->recompute();
} PY_CATCH; }
Py_Return; catch (const Base::Exception& e) {
throw Py::RuntimeError(e.what());
} }
return Py::None();
}
/* registration table */
struct PyMethodDef DraftUtils_methods[] = {
{"readDXF" ,readDXF ,METH_VARARGS,
"readDXF(filename,[document,ignore_errors]): Imports a DXF file into the given document. ignore_errors is True by default."},
{NULL, NULL} /* end of table marker */
}; };
PyObject* initModule()
{
return (new Module)->module().ptr();
}
} // namespace DraftUtils