+ simplify porting of Sketcher module to Python3

This commit is contained in:
wmayer
2016-01-18 12:19:54 +01:00
parent bfdaa46feb
commit 65fe62d93b
5 changed files with 99 additions and 178 deletions

View File

@@ -36,103 +36,95 @@
#include <App/Application.h>
#include <App/Document.h>
#include <CXX/Extensions.hxx>
#include <CXX/Objects.hxx>
// Things from the part module
#include <Mod/Part/App/TopoShape.h>
#include <Mod/Part/App/TopoShapePy.h>
#include "SketchObjectSF.h"
using Base::Console;
using namespace Part;
using namespace std;
/* module functions */
static PyObject * open(PyObject *self, PyObject *args)
namespace Sketcher {
class Module : public Py::ExtensionModule<Module>
{
char* Name;
if (!PyArg_ParseTuple(args, "et","utf-8",&Name))
return NULL;
std::string EncodedName = std::string(Name);
PyMem_Free(Name);
public:
Module() : Py::ExtensionModule<Module>("Sketcher")
{
add_varargs_method("open",&Module::open
);
add_varargs_method("insert",&Module::insert
);
initialize("This module is the Sketcher module."); // register with Python
}
virtual ~Module() {}
private:
Py::Object open(const Py::Tuple& args)
{
char* Name;
if (!PyArg_ParseTuple(args.ptr(), "et","utf-8",&Name))
throw Py::Exception();
std::string EncodedName = std::string(Name);
PyMem_Free(Name);
PY_TRY {
} PY_CATCH;
//Base::Console().Log("Open in Part with %s",Name);
Base::FileInfo file(EncodedName.c_str());
// extract ending
if (file.extension() == "")
Py_Error(Base::BaseExceptionFreeCADError,"no file ending");
// extract extension
if (file.extension().empty())
throw Py::RuntimeError("No file extension");
//if (file.hasExtension("igs") || file.hasExtension("iges")) {
// // create new document and add Import feature
// App::Document *pcDoc = App::GetApplication().newDocument(file.fileNamePure().c_str());
// Part::ImportIges *pcFeature = (Part::ImportIges*) pcDoc->addObject("Part::ImportIges",file.fileNamePure().c_str());
// pcFeature->FileName.setValue(Name);
// pcDoc->recompute();
//}
// else {
Py_Error(Base::BaseExceptionFreeCADError,"unknown file ending");
//}
throw Py::RuntimeError("Unknown file extension");
return Py::None();
}
Py_Return;
}
Py::Object insert(const Py::Tuple& args)
{
char* Name;
const char* DocName;
if (!PyArg_ParseTuple(args.ptr(), "ets","utf-8",&Name,&DocName))
throw Py::Exception();
std::string EncodedName = std::string(Name);
PyMem_Free(Name);
/* module functions */
static PyObject * insert(PyObject *self, PyObject *args)
{
char* Name;
const char* DocName;
if (!PyArg_ParseTuple(args, "ets","utf-8",&Name,&DocName))
return NULL;
std::string EncodedName = std::string(Name);
PyMem_Free(Name);
try {
//Base::Console().Log("Insert in Part with %s",Name);
Base::FileInfo file(EncodedName.c_str());
PY_TRY {
//Base::Console().Log("Insert in Part with %s",Name);
Base::FileInfo file(EncodedName.c_str());
// extract extension
if (file.extension().empty())
throw Py::RuntimeError("No file extension");
// extract ending
if (file.extension() == "")
Py_Error(Base::BaseExceptionFreeCADError,"no file ending");
App::Document *pcDoc = App::GetApplication().getDocument(DocName);
if (!pcDoc) {
pcDoc = App::GetApplication().newDocument(DocName);
App::Document *pcDoc = App::GetApplication().getDocument(DocName);
if (!pcDoc) {
pcDoc = App::GetApplication().newDocument(DocName);
}
if (file.hasExtension("skf")) {
Sketcher::SketchObjectSF *pcFeature = (Sketcher::SketchObjectSF *)pcDoc->addObject("Sketcher::SketchObjectSF",file.fileNamePure().c_str());
pcFeature->SketchFlatFile.setValue(EncodedName.c_str());
pcDoc->recompute();
}
else {
throw Py::RuntimeError("Unknown file extension");
}
}
if (file.hasExtension("skf")) {
Sketcher::SketchObjectSF *pcFeature = (Sketcher::SketchObjectSF *)pcDoc->addObject("Sketcher::SketchObjectSF",file.fileNamePure().c_str());
pcFeature->SketchFlatFile.setValue(EncodedName.c_str());
pcDoc->recompute();
catch (const Base::Exception& e) {
throw Py::RuntimeError(e.what());
}
else {
Py_Error(Base::BaseExceptionFreeCADError,"unknown file ending");
}
} PY_CATCH;
Py_Return;
}
/* module functions */
//static PyObject * read(PyObject *self, PyObject *args)
//{
// const char* Name;
// if (!PyArg_ParseTuple(args, "s",&Name))
// return NULL;
// PY_TRY {
// } PY_CATCH;
//
// Py_Return;
//}
/* registration table */
struct PyMethodDef Sketcher_methods[] = {
{"open" , open, 1},
{"insert" , insert, 1},
// {"read" , read, 1},
{NULL, NULL} /* end of table marker */
return Py::None();
}
};
/// @cond DOXERR
PyObject* initModule()
{
return (new Module())->module().ptr();
}
/// @endcond
} // namespace Sketcher