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

View File

@ -26,6 +26,9 @@
# include <Python.h>
#endif
#include <CXX/Extensions.hxx>
#include <CXX/Objects.hxx>
#include <Base/Console.h>
#include <Base/VectorPy.h>
#include <Base/FileInfo.h>
@ -36,21 +39,36 @@
#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;
const char* DocName=0;
bool IgnoreErrors=true;
if (!PyArg_ParseTuple(args, "et|sb","utf-8",&Name,&DocName,&IgnoreErrors))
return NULL;
if (!PyArg_ParseTuple(args.ptr(), "et|sb","utf-8",&Name,&DocName,&IgnoreErrors))
throw Py::Exception();
std::string EncodedName = std::string(Name);
PyMem_Free(Name);
Base::FileInfo file(EncodedName.c_str());
if (!file.exists())
Py_Error(Base::BaseExceptionFreeCADError, "File doesn't exist");
throw Py::RuntimeError("File doesn't exist");
App::Document *pcDoc;
if (DocName)
pcDoc = App::GetApplication().getDocument(DocName);
@ -59,23 +77,23 @@ static PyObject * readDXF (PyObject *self, PyObject *args)
if (!pcDoc)
pcDoc = App::GetApplication().newDocument(DocName);
PY_TRY {
try {
// read the DXF file
DraftDxfRead dxf_file(EncodedName,pcDoc);
dxf_file.DoRead(IgnoreErrors);
pcDoc->recompute();
} PY_CATCH;
Py_Return;
}
catch (const Base::Exception& e) {
throw Py::RuntimeError(e.what());
}
/* 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 */
return Py::None();
}
};
PyObject* initModule()
{
return (new Module)->module().ptr();
}
} // namespace DraftUtils