+ simplify porting of Path module to Python3
This commit is contained in:
parent
281447780b
commit
f85d168ef0
|
@ -42,17 +42,14 @@
|
|||
#include "FeaturePathCompound.h"
|
||||
#include "FeaturePathShape.h"
|
||||
|
||||
extern struct PyMethodDef Path_methods[];
|
||||
|
||||
PyDoc_STRVAR(module_Path_doc,
|
||||
"This module is the Path module.");
|
||||
|
||||
namespace Path {
|
||||
extern PyObject* initModule();
|
||||
}
|
||||
|
||||
/* Python entry */
|
||||
extern "C" {
|
||||
void PathExport initPath()
|
||||
PyMODINIT_FUNC initPath()
|
||||
{
|
||||
PyObject* pathModule = Py_InitModule3("Path", Path_methods, module_Path_doc); /* mod name, table ptr */
|
||||
PyObject* pathModule = Path::initModule();
|
||||
Base::Console().Log("Loading Path module... done\n");
|
||||
|
||||
|
||||
|
@ -78,5 +75,3 @@ void PathExport initPath()
|
|||
Path::FeatureShape ::init();
|
||||
Path::FeatureShapePython ::init();
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
|
|
|
@ -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>
|
||||
|
@ -40,15 +43,33 @@
|
|||
#include "FeaturePath.h"
|
||||
#include "FeaturePathCompound.h"
|
||||
|
||||
using namespace Path;
|
||||
namespace Path {
|
||||
class Module : public Py::ExtensionModule<Module>
|
||||
{
|
||||
public:
|
||||
Module() : Py::ExtensionModule<Module>("Path")
|
||||
{
|
||||
add_varargs_method("write",&Module::write,
|
||||
"write(object,filename): Exports a given path object to a GCode file"
|
||||
);
|
||||
add_varargs_method("read",&Module::read,
|
||||
"read(filename,[document]): Imports a GCode file into the given document"
|
||||
);
|
||||
add_varargs_method("show",&Module::show,
|
||||
"show(path): Add the path to the active document or create one if no document exists"
|
||||
);
|
||||
initialize("This module is the Path module."); // register with Python
|
||||
}
|
||||
|
||||
virtual ~Module() {}
|
||||
|
||||
static PyObject * write (PyObject *self, PyObject *args)
|
||||
private:
|
||||
Py::Object write(const Py::Tuple& args)
|
||||
{
|
||||
char* Name;
|
||||
PyObject* pObj;
|
||||
if (!PyArg_ParseTuple(args, "Oet",&pObj,"utf-8",&Name))
|
||||
return NULL;
|
||||
if (!PyArg_ParseTuple(args.ptr(), "Oet",&pObj,"utf-8",&Name))
|
||||
throw Py::Exception();
|
||||
std::string EncodedName = std::string(Name);
|
||||
PyMem_Free(Name);
|
||||
Base::FileInfo file(EncodedName.c_str());
|
||||
|
@ -61,25 +82,28 @@ static PyObject * write (PyObject *self, PyObject *args)
|
|||
std::ofstream ofile(EncodedName.c_str());
|
||||
ofile << gcode;
|
||||
ofile.close();
|
||||
} else
|
||||
Py_Error(Base::BaseExceptionFreeCADError, "The given file is not a path");
|
||||
}
|
||||
Py_Return;
|
||||
else {
|
||||
throw Py::RuntimeError("The given file is not a path");
|
||||
}
|
||||
}
|
||||
|
||||
return Py::None();
|
||||
}
|
||||
|
||||
static PyObject * read (PyObject *self, PyObject *args)
|
||||
Py::Object read(const Py::Tuple& args)
|
||||
{
|
||||
char* Name;
|
||||
const char* DocName=0;
|
||||
if (!PyArg_ParseTuple(args, "et|s","utf-8",&Name,&DocName))
|
||||
return NULL;
|
||||
if (!PyArg_ParseTuple(args.ptr(), "et|s","utf-8",&Name,&DocName))
|
||||
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);
|
||||
|
@ -88,7 +112,7 @@ static PyObject * read (PyObject *self, PyObject *args)
|
|||
if (!pcDoc)
|
||||
pcDoc = App::GetApplication().newDocument(DocName);
|
||||
|
||||
PY_TRY {
|
||||
try {
|
||||
// read the gcode file
|
||||
std::ifstream filestr(file.filePath().c_str());
|
||||
std::stringstream buffer;
|
||||
|
@ -99,18 +123,21 @@ static PyObject * read (PyObject *self, PyObject *args)
|
|||
Path::Feature *object = static_cast<Path::Feature *>(pcDoc->addObject("Path::Feature",file.fileNamePure().c_str()));
|
||||
object->Path.setValue(path);
|
||||
pcDoc->recompute();
|
||||
} PY_CATCH;
|
||||
Py_Return;
|
||||
}
|
||||
catch (const Base::Exception& e) {
|
||||
throw Py::RuntimeError(e.what());
|
||||
}
|
||||
|
||||
return Py::None();
|
||||
}
|
||||
|
||||
static PyObject * show (PyObject *self, PyObject *args)
|
||||
Py::Object show(const Py::Tuple& args)
|
||||
{
|
||||
PyObject *pcObj;
|
||||
if (!PyArg_ParseTuple(args, "O!", &(PathPy::Type), &pcObj)) // convert args: Python->C
|
||||
return NULL; // NULL triggers exception
|
||||
if (!PyArg_ParseTuple(args.ptr(), "O!", &(PathPy::Type), &pcObj))
|
||||
throw Py::Exception();
|
||||
|
||||
PY_TRY {
|
||||
try {
|
||||
App::Document *pcDoc = App::GetApplication().getActiveDocument();
|
||||
if (!pcDoc)
|
||||
pcDoc = App::GetApplication().newDocument();
|
||||
|
@ -118,25 +145,23 @@ static PyObject * show (PyObject *self, PyObject *args)
|
|||
Path::Feature *pcFeature = (Path::Feature *)pcDoc->addObject("Path::Feature", "Path");
|
||||
Path::Toolpath* pa = pPath->getToolpathPtr();
|
||||
if (!pa) {
|
||||
PyErr_SetString(PyExc_ReferenceError,
|
||||
"object doesn't reference a valid path");
|
||||
return 0;
|
||||
throw Py::Exception(PyExc_ReferenceError, "object doesn't reference a valid path");
|
||||
}
|
||||
|
||||
// copy the data
|
||||
pcFeature->Path.setValue(*pa);
|
||||
} PY_CATCH;
|
||||
|
||||
Py_Return;
|
||||
}
|
||||
catch (const Base::Exception& e) {
|
||||
throw Py::RuntimeError(e.what());
|
||||
}
|
||||
|
||||
|
||||
/* registration table */
|
||||
struct PyMethodDef Path_methods[] = {
|
||||
{"write" ,write ,METH_VARARGS,
|
||||
"write(object,filename): Exports a given path object to a GCode file"},
|
||||
{"read" ,read ,METH_VARARGS,
|
||||
"read(filename,[document]): Imports a GCode file into the given document"},
|
||||
{"show" ,show ,METH_VARARGS,
|
||||
"show(path): Add the path to the active document or create one if no document exists."},
|
||||
{NULL, NULL} /* end of table marker */
|
||||
return Py::None();
|
||||
}
|
||||
};
|
||||
|
||||
PyObject* initModule()
|
||||
{
|
||||
return (new Module)->module().ptr();
|
||||
}
|
||||
|
||||
} // namespace Path
|
||||
|
|
|
@ -46,13 +46,12 @@ void loadPathResource()
|
|||
Gui::Translator::instance()->refresh();
|
||||
}
|
||||
|
||||
/* registration table */
|
||||
extern struct PyMethodDef PathGui_methods[];
|
||||
|
||||
namespace PathGui {
|
||||
extern PyObject* initModule();
|
||||
}
|
||||
|
||||
/* Python entry */
|
||||
extern "C" {
|
||||
void PathGuiExport initPathGui()
|
||||
PyMODINIT_FUNC initPathGui()
|
||||
{
|
||||
if (!Gui::Application::Instance) {
|
||||
PyErr_SetString(PyExc_ImportError, "Cannot load Gui module in console application.");
|
||||
|
@ -65,7 +64,7 @@ void PathGuiExport initPathGui()
|
|||
PyErr_SetString(PyExc_ImportError, e.what());
|
||||
return;
|
||||
}
|
||||
(void) Py_InitModule("PathGui", PathGui_methods); /* mod name, table ptr */
|
||||
(void)PathGui::initModule();
|
||||
Base::Console().Log("Loading GUI of Path module... done\n");
|
||||
|
||||
// instantiating the commands
|
||||
|
@ -84,5 +83,3 @@ void PathGuiExport initPathGui()
|
|||
// register preferences pages
|
||||
new Gui::PrefPageProducer<PathGui::DlgSettingsPathColor> ("Path");
|
||||
}
|
||||
|
||||
} // extern "C" {
|
||||
|
|
|
@ -25,6 +25,9 @@
|
|||
# include <Python.h>
|
||||
#endif
|
||||
|
||||
#include <CXX/Extensions.hxx>
|
||||
#include <CXX/Objects.hxx>
|
||||
|
||||
#include <QDir>
|
||||
#include <QFileInfo>
|
||||
|
||||
|
@ -43,22 +46,42 @@
|
|||
#include "DlgProcessorChooser.h"
|
||||
#include "ui_DlgProcessorChooser.h"
|
||||
|
||||
using namespace PathGui;
|
||||
namespace PathGui {
|
||||
class Module : public Py::ExtensionModule<Module>
|
||||
{
|
||||
public:
|
||||
Module() : Py::ExtensionModule<Module>("PathGui")
|
||||
{
|
||||
add_varargs_method("open",&Module::open,
|
||||
"open(filename): Opens a GCode file as a new document"
|
||||
);
|
||||
add_varargs_method("insert",&Module::insert,
|
||||
"insert(filename,docname): Imports a given GCode file into the given document"
|
||||
);
|
||||
add_varargs_method("export",&Module::exporter,
|
||||
"export(objectslist,filename): Exports a given list of Path objects to a GCode file"
|
||||
);
|
||||
initialize("This module is the PathGui module."); // register with Python
|
||||
}
|
||||
|
||||
static PyObject * open(PyObject *self, PyObject *args)
|
||||
virtual ~Module() {}
|
||||
|
||||
private:
|
||||
Py::Object open(const Py::Tuple& args)
|
||||
{
|
||||
char* Name;
|
||||
if (!PyArg_ParseTuple(args, "et","utf-8",&Name))
|
||||
return NULL;
|
||||
if (!PyArg_ParseTuple(args.ptr(), "et","utf-8",&Name))
|
||||
throw Py::Exception();
|
||||
std::string EncodedName = std::string(Name);
|
||||
PyMem_Free(Name);
|
||||
Base::FileInfo fi(EncodedName);
|
||||
if (!fi.exists())
|
||||
Py_Error(Base::BaseExceptionFreeCADError, "File not found");
|
||||
throw Py::RuntimeError("File not found");
|
||||
|
||||
Gui::WaitCursor wc;
|
||||
wc.restoreCursor();
|
||||
|
||||
PY_TRY {
|
||||
try {
|
||||
std::string path = App::GetApplication().getHomePath();
|
||||
path += "Mod/Path/PathScripts/";
|
||||
QDir dir1(QString::fromUtf8(path.c_str()), QString::fromLatin1("*_pre.py"));
|
||||
|
@ -75,7 +98,7 @@ static PyObject * open(PyObject *self, PyObject *args)
|
|||
std::string selected;
|
||||
PathGui::DlgProcessorChooser Dlg(scripts);
|
||||
if (Dlg.exec() != QDialog::Accepted) {
|
||||
Py_Return;
|
||||
return Py::None();
|
||||
}
|
||||
selected = Dlg.getSelected();
|
||||
|
||||
|
@ -101,25 +124,31 @@ static PyObject * open(PyObject *self, PyObject *args)
|
|||
}
|
||||
}
|
||||
}
|
||||
} PY_CATCH;
|
||||
Py_Return;
|
||||
}
|
||||
catch (const Base::Exception& e) {
|
||||
throw Py::RuntimeError(e.what());
|
||||
}
|
||||
|
||||
static PyObject * importer(PyObject *self, PyObject *args)
|
||||
return Py::None();
|
||||
}
|
||||
|
||||
Py::Object insert(const Py::Tuple& args)
|
||||
{
|
||||
char* Name;
|
||||
char* DocName=0;
|
||||
if (!PyArg_ParseTuple(args, "et|s","utf-8",&Name,&DocName))
|
||||
return NULL;
|
||||
if (!PyArg_ParseTuple(args.ptr(), "et|s","utf-8",&Name,&DocName))
|
||||
throw Py::Exception();
|
||||
|
||||
std::string EncodedName = std::string(Name);
|
||||
PyMem_Free(Name);
|
||||
Base::FileInfo fi(EncodedName);
|
||||
if (!fi.exists())
|
||||
Py_Error(Base::BaseExceptionFreeCADError, "File not found");
|
||||
throw Py::RuntimeError("File not found");
|
||||
|
||||
Gui::WaitCursor wc;
|
||||
wc.restoreCursor();
|
||||
|
||||
PY_TRY {
|
||||
try {
|
||||
std::string path = App::GetApplication().getHomePath();
|
||||
path += "Mod/Path/PathScripts/";
|
||||
QDir dir1(QString::fromUtf8(path.c_str()), QString::fromLatin1("*_pre.py"));
|
||||
|
@ -136,7 +165,7 @@ static PyObject * importer(PyObject *self, PyObject *args)
|
|||
std::string selected;
|
||||
PathGui::DlgProcessorChooser Dlg(scripts);
|
||||
if (Dlg.exec() != QDialog::Accepted) {
|
||||
Py_Return;
|
||||
return Py::None();
|
||||
}
|
||||
selected = Dlg.getSelected();
|
||||
|
||||
|
@ -171,25 +200,31 @@ static PyObject * importer(PyObject *self, PyObject *args)
|
|||
}
|
||||
}
|
||||
}
|
||||
} PY_CATCH;
|
||||
Py_Return;
|
||||
}
|
||||
catch (const Base::Exception& e) {
|
||||
throw Py::RuntimeError(e.what());
|
||||
}
|
||||
|
||||
static PyObject * exporter(PyObject *self, PyObject *args)
|
||||
return Py::None();
|
||||
}
|
||||
|
||||
Py::Object exporter(const Py::Tuple& args)
|
||||
{
|
||||
PyObject* object;
|
||||
char* Name;
|
||||
if (!PyArg_ParseTuple(args, "Oet",&object,"utf-8",&Name))
|
||||
return NULL;
|
||||
if (!PyArg_ParseTuple(args.ptr(), "Oet",&object,"utf-8",&Name))
|
||||
throw Py::Exception();
|
||||
|
||||
std::string EncodedName = std::string(Name);
|
||||
PyMem_Free(Name);
|
||||
Gui::WaitCursor wc;
|
||||
wc.restoreCursor();
|
||||
|
||||
PY_TRY {
|
||||
try {
|
||||
Py::Sequence objlist(object);
|
||||
if (objlist.size() == 0)
|
||||
Py_Error(Base::BaseExceptionFreeCADError, "No object to export");
|
||||
throw Py::RuntimeError("No object to export");
|
||||
|
||||
std::string path = App::GetApplication().getHomePath();
|
||||
path += "Mod/Path/PathScripts/";
|
||||
QDir dir1(QString::fromUtf8(path.c_str()), QString::fromLatin1("*_post.py"));
|
||||
|
@ -206,7 +241,7 @@ static PyObject * exporter(PyObject *self, PyObject *args)
|
|||
std::string selected;
|
||||
PathGui::DlgProcessorChooser Dlg(scripts);
|
||||
if (Dlg.exec() != QDialog::Accepted) {
|
||||
Py_Return;
|
||||
return Py::None();
|
||||
}
|
||||
selected = Dlg.getSelected();
|
||||
|
||||
|
@ -214,7 +249,7 @@ static PyObject * exporter(PyObject *self, PyObject *args)
|
|||
std::ostringstream cmd;
|
||||
if (selected.empty()) {
|
||||
if (objlist.size() > 1) {
|
||||
Py_Error(Base::BaseExceptionFreeCADError, "Cannot export more than one object without using a post script");
|
||||
throw Py::RuntimeError("Cannot export more than one object without using a post script");
|
||||
}
|
||||
PyObject* item = objlist[0].ptr();
|
||||
if (PyObject_TypeCheck(item, &(App::DocumentObjectPy::Type))) {
|
||||
|
@ -224,7 +259,7 @@ static PyObject * exporter(PyObject *self, PyObject *args)
|
|||
cmd << "Path.write(FreeCAD.getDocument(\"" << doc->getName() << "\").getObject(\"" << obj->getNameInDocument() << "\"),\"" << EncodedName << "\")";
|
||||
Gui::Command::runCommand(Gui::Command::Gui,cmd.str().c_str());
|
||||
} else {
|
||||
Py_Return;
|
||||
return Py::None();
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < list.size(); ++i) {
|
||||
|
@ -241,17 +276,18 @@ static PyObject * exporter(PyObject *self, PyObject *args)
|
|||
}
|
||||
}
|
||||
}
|
||||
} PY_CATCH;
|
||||
Py_Return;
|
||||
}
|
||||
catch (const Base::Exception& e) {
|
||||
throw Py::RuntimeError(e.what());
|
||||
}
|
||||
|
||||
/* registration table */
|
||||
struct PyMethodDef PathGui_methods[] = {
|
||||
{"open" ,open ,METH_VARARGS,
|
||||
"open(filename): Opens a GCode file as a new document"},
|
||||
{"insert" ,importer ,METH_VARARGS,
|
||||
"insert(filename,docname): Imports a given GCode file into the given document"},
|
||||
{"export" ,exporter ,METH_VARARGS,
|
||||
"export(objectslist,filename): Exports a given list of Path objects to a GCode file"},
|
||||
{NULL, NULL} /* end of table marker */
|
||||
return Py::None();
|
||||
}
|
||||
};
|
||||
|
||||
PyObject* initModule()
|
||||
{
|
||||
return (new Module)->module().ptr();
|
||||
}
|
||||
|
||||
} // namespace PathGui
|
||||
|
|
Loading…
Reference in New Issue
Block a user