+ 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,25 +29,21 @@
#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()
{
// load dependent module
try {
Base::Interpreter().loadModule("Part");
}
catch(const Base::Exception& e) {
PyErr_SetString(PyExc_ImportError, e.what());
return;
}
Py_InitModule3("DraftUtils", DraftUtils_methods, module_DraftUtils_doc); /* mod name, table ptr */
Base::Console().Log("Loading DraftUtils module... done\n");
PyMODINIT_FUNC initDraftUtils()
{
// load dependent module
try {
Base::Interpreter().loadModule("Part");
}
} // extern "C"
catch(const Base::Exception& e) {
PyErr_SetString(PyExc_ImportError, e.what());
return;
}
(void)DraftUtils::initModule();
Base::Console().Log("Loading DraftUtils module... done\n");
}

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,46 +39,61 @@
#include "DraftDxf.h"
using namespace DraftUtils;
static PyObject * readDXF (PyObject *self, PyObject *args)
namespace DraftUtils {
class Module : public Py::ExtensionModule<Module>
{
char* Name;
const char* DocName=0;
bool IgnoreErrors=true;
if (!PyArg_ParseTuple(args, "et|sb","utf-8",&Name,&DocName,&IgnoreErrors))
return NULL;
std::string EncodedName = std::string(Name);
PyMem_Free(Name);
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
}
Base::FileInfo file(EncodedName.c_str());
if (!file.exists())
Py_Error(Base::BaseExceptionFreeCADError, "File doesn't exist");
App::Document *pcDoc;
if (DocName)
pcDoc = App::GetApplication().getDocument(DocName);
else
pcDoc = App::GetApplication().getActiveDocument();
if (!pcDoc)
pcDoc = App::GetApplication().newDocument(DocName);
virtual ~Module() {}
PY_TRY {
// read the DXF file
DraftDxfRead dxf_file(EncodedName,pcDoc);
dxf_file.DoRead(IgnoreErrors);
pcDoc->recompute();
} PY_CATCH;
Py_Return;
private:
Py::Object readDXF(const Py::Tuple& args)
{
char* Name;
const char* DocName=0;
bool IgnoreErrors=true;
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())
throw Py::RuntimeError("File doesn't exist");
App::Document *pcDoc;
if (DocName)
pcDoc = App::GetApplication().getDocument(DocName);
else
pcDoc = App::GetApplication().getActiveDocument();
if (!pcDoc)
pcDoc = App::GetApplication().newDocument(DocName);
try {
// read the DXF file
DraftDxfRead dxf_file(EncodedName,pcDoc);
dxf_file.DoRead(IgnoreErrors);
pcDoc->recompute();
}
catch (const Base::Exception& e) {
throw Py::RuntimeError(e.what());
}
return Py::None();
}
};
PyObject* initModule()
{
return (new Module)->module().ptr();
}
/* 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 */
};
} // namespace DraftUtils