+ 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/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
{ try {
// load dependent module Base::Interpreter().loadModule("Part");
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");
} }
catch(const Base::Exception& e) {
} // extern "C" 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> # 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,46 +39,61 @@
#include "DraftDxf.h" #include "DraftDxf.h"
using namespace DraftUtils; namespace DraftUtils {
class Module : public Py::ExtensionModule<Module>
static PyObject * readDXF (PyObject *self, PyObject *args)
{ {
char* Name; public:
const char* DocName=0; Module() : Py::ExtensionModule<Module>("DraftUtils")
bool IgnoreErrors=true; {
if (!PyArg_ParseTuple(args, "et|sb","utf-8",&Name,&DocName,&IgnoreErrors)) add_varargs_method("readDXF",&Module::readDXF,
return NULL; "readDXF(filename,[document,ignore_errors]): Imports a DXF file into the given document. ignore_errors is True by default."
std::string EncodedName = std::string(Name); );
PyMem_Free(Name); initialize("The DraftUtils module contains utility functions for the Draft module."); // register with Python
}
Base::FileInfo file(EncodedName.c_str()); virtual ~Module() {}
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);
PY_TRY { private:
// read the DXF file Py::Object readDXF(const Py::Tuple& args)
DraftDxfRead dxf_file(EncodedName,pcDoc); {
dxf_file.DoRead(IgnoreErrors); char* Name;
pcDoc->recompute(); const char* DocName=0;
} PY_CATCH; bool IgnoreErrors=true;
Py_Return; 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();
} }
} // namespace DraftUtils
/* 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 */
};