+ simplify porting of Drawing module to Python3
This commit is contained in:
parent
bf10bf33f6
commit
aa7aa64724
|
@ -27,15 +27,13 @@
|
||||||
#include "FeatureClip.h"
|
#include "FeatureClip.h"
|
||||||
#include "PageGroup.h"
|
#include "PageGroup.h"
|
||||||
|
|
||||||
extern struct PyMethodDef Drawing_methods[];
|
|
||||||
|
|
||||||
PyDoc_STRVAR(module_drawing_doc,
|
|
||||||
"This module is the drawing module.");
|
|
||||||
|
|
||||||
|
namespace Drawing {
|
||||||
|
extern PyObject* initModule();
|
||||||
|
}
|
||||||
|
|
||||||
/* Python entry */
|
/* Python entry */
|
||||||
extern "C" {
|
PyMODINIT_FUNC initDrawing()
|
||||||
void DrawingExport initDrawing()
|
|
||||||
{
|
{
|
||||||
// load dependent module
|
// load dependent module
|
||||||
try {
|
try {
|
||||||
|
@ -46,7 +44,7 @@ void DrawingExport initDrawing()
|
||||||
PyErr_SetString(PyExc_ImportError, e.what());
|
PyErr_SetString(PyExc_ImportError, e.what());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Py_InitModule3("Drawing", Drawing_methods, module_drawing_doc); /* mod name, table ptr */
|
(void)Drawing::initModule();
|
||||||
Base::Console().Log("Loading Drawing module... done\n");
|
Base::Console().Log("Loading Drawing module... done\n");
|
||||||
|
|
||||||
|
|
||||||
|
@ -66,5 +64,3 @@ void DrawingExport initDrawing()
|
||||||
Drawing::FeatureClip ::init();
|
Drawing::FeatureClip ::init();
|
||||||
Drawing::FeatureViewSpreadsheet ::init();
|
Drawing::FeatureViewSpreadsheet ::init();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // extern "C"
|
|
||||||
|
|
|
@ -26,6 +26,9 @@
|
||||||
# include <Python.h>
|
# include <Python.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <CXX/Extensions.hxx>
|
||||||
|
#include <CXX/Objects.hxx>
|
||||||
|
|
||||||
#include <Mod/Part/App/TopoShapePy.h>
|
#include <Mod/Part/App/TopoShapePy.h>
|
||||||
#include "ProjectionAlgos.h"
|
#include "ProjectionAlgos.h"
|
||||||
#include <Base/Console.h>
|
#include <Base/Console.h>
|
||||||
|
@ -34,21 +37,84 @@
|
||||||
|
|
||||||
#include <Mod/Part/App/OCCError.h>
|
#include <Mod/Part/App/OCCError.h>
|
||||||
|
|
||||||
using namespace Drawing;
|
using Part::TopoShapePy;
|
||||||
using namespace Part;
|
using Part::TopoShape;
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
static PyObject *
|
namespace Drawing {
|
||||||
project(PyObject *self, PyObject *args)
|
class Module : public Py::ExtensionModule<Module>
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
Module() : Py::ExtensionModule<Module>("Drawing")
|
||||||
|
{
|
||||||
|
add_varargs_method("project",&Module::project,
|
||||||
|
"[visiblyG0,visiblyG1,hiddenG0,hiddenG1] = project(TopoShape[,App.Vector Direction, string type])\n"
|
||||||
|
" -- Project a shape and return the visible/invisible parts of it."
|
||||||
|
);
|
||||||
|
add_varargs_method("projectEx",&Module::projectEx,
|
||||||
|
"[V,V1,VN,VO,VI,H,H1,HN,HO,HI] = projectEx(TopoShape[,App.Vector Direction, string type])\n"
|
||||||
|
" -- Project a shape and return the all parts of it."
|
||||||
|
);
|
||||||
|
add_varargs_method("projectToSVG",&Module::projectToSVG,
|
||||||
|
"string = projectToSVG(TopoShape[,App.Vector Direction, string type])\n"
|
||||||
|
" -- Project a shape and return the SVG representation as string."
|
||||||
|
);
|
||||||
|
add_varargs_method("projectToDXF",&Module::projectToDXF,
|
||||||
|
"string = projectToDXF(TopoShape[,App.Vector Direction, string type])\n"
|
||||||
|
" -- Project a shape and return the DXF representation as string."
|
||||||
|
);
|
||||||
|
add_varargs_method("removeSvgTags",&Module::removeSvgTags,
|
||||||
|
"string = removeSvgTags(string) -- Removes the opening and closing svg tags\n"
|
||||||
|
"and other metatags from a svg code, making it embeddable"
|
||||||
|
);
|
||||||
|
initialize("This module is the Drawing module."); // register with Python
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~Module() {}
|
||||||
|
|
||||||
|
private:
|
||||||
|
virtual Py::Object invoke_method_varargs(void *method_def, const Py::Tuple &args)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return Py::ExtensionModule<Module>::invoke_method_varargs(method_def, args);
|
||||||
|
}
|
||||||
|
catch (const Standard_Failure &e) {
|
||||||
|
std::string str;
|
||||||
|
Standard_CString msg = e.GetMessageString();
|
||||||
|
str += typeid(e).name();
|
||||||
|
str += " ";
|
||||||
|
if (msg) {str += msg;}
|
||||||
|
else {str += "No OCCT Exception Message";}
|
||||||
|
Base::Console().Error("%s\n", str.c_str());
|
||||||
|
throw Py::Exception(Part::PartExceptionOCCError, str);
|
||||||
|
}
|
||||||
|
catch (const Base::Exception &e) {
|
||||||
|
std::string str;
|
||||||
|
str += "FreeCAD exception thrown (";
|
||||||
|
str += e.what();
|
||||||
|
str += ")";
|
||||||
|
e.ReportException();
|
||||||
|
throw Py::RuntimeError(str);
|
||||||
|
}
|
||||||
|
catch (const std::exception &e) {
|
||||||
|
std::string str;
|
||||||
|
str += "C++ exception thrown (";
|
||||||
|
str += e.what();
|
||||||
|
str += ")";
|
||||||
|
Base::Console().Error("%s\n", str.c_str());
|
||||||
|
throw Py::RuntimeError(str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Py::Object project(const Py::Tuple& args)
|
||||||
|
{
|
||||||
PyObject *pcObjShape;
|
PyObject *pcObjShape;
|
||||||
PyObject *pcObjDir=0;
|
PyObject *pcObjDir=0;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "O!|O!", &(TopoShapePy::Type), &pcObjShape,&(Base::VectorPy::Type), &pcObjDir)) // convert args: Python->C
|
if (!PyArg_ParseTuple(args.ptr(), "O!|O!",
|
||||||
return NULL; // NULL triggers exception
|
&(Part::TopoShapePy::Type), &pcObjShape,
|
||||||
|
&(Base::VectorPy::Type), &pcObjDir))
|
||||||
|
throw Py::Exception();
|
||||||
|
|
||||||
PY_TRY {
|
Part::TopoShapePy* pShape = static_cast<Part::TopoShapePy*>(pcObjShape);
|
||||||
TopoShapePy* pShape = static_cast<TopoShapePy*>(pcObjShape);
|
|
||||||
Base::Vector3d Vector(0,0,1);
|
Base::Vector3d Vector(0,0,1);
|
||||||
if (pcObjDir)
|
if (pcObjDir)
|
||||||
Vector = *static_cast<Base::VectorPy*>(pcObjDir)->getVectorPtr();
|
Vector = *static_cast<Base::VectorPy*>(pcObjDir)->getVectorPtr();
|
||||||
|
@ -56,27 +122,23 @@ project(PyObject *self, PyObject *args)
|
||||||
ProjectionAlgos Alg(pShape->getTopoShapePtr()->_Shape,Vector);
|
ProjectionAlgos Alg(pShape->getTopoShapePtr()->_Shape,Vector);
|
||||||
|
|
||||||
Py::List list;
|
Py::List list;
|
||||||
list.append(Py::Object(new TopoShapePy(new TopoShape(Alg.V)) , true));
|
list.append(Py::Object(new Part::TopoShapePy(new Part::TopoShape(Alg.V)) , true));
|
||||||
list.append(Py::Object(new TopoShapePy(new TopoShape(Alg.V1)), true));
|
list.append(Py::Object(new Part::TopoShapePy(new Part::TopoShape(Alg.V1)), true));
|
||||||
list.append(Py::Object(new TopoShapePy(new TopoShape(Alg.H)) , true));
|
list.append(Py::Object(new Part::TopoShapePy(new Part::TopoShape(Alg.H)) , true));
|
||||||
list.append(Py::Object(new TopoShapePy(new TopoShape(Alg.H1)), true));
|
list.append(Py::Object(new Part::TopoShapePy(new Part::TopoShape(Alg.H1)), true));
|
||||||
|
|
||||||
return Py::new_reference_to(list);
|
return list;
|
||||||
|
}
|
||||||
} PY_CATCH_OCC;
|
Py::Object projectEx(const Py::Tuple& args)
|
||||||
|
{
|
||||||
}
|
|
||||||
|
|
||||||
static PyObject *
|
|
||||||
projectEx(PyObject *self, PyObject *args)
|
|
||||||
{
|
|
||||||
PyObject *pcObjShape;
|
PyObject *pcObjShape;
|
||||||
PyObject *pcObjDir=0;
|
PyObject *pcObjDir=0;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "O!|O!", &(TopoShapePy::Type), &pcObjShape,&(Base::VectorPy::Type), &pcObjDir)) // convert args: Python->C
|
if (!PyArg_ParseTuple(args.ptr(), "O!|O!",
|
||||||
return NULL; // NULL triggers exception
|
&(TopoShapePy::Type), &pcObjShape,
|
||||||
|
&(Base::VectorPy::Type), &pcObjDir))
|
||||||
|
throw Py::Exception();
|
||||||
|
|
||||||
PY_TRY {
|
|
||||||
TopoShapePy* pShape = static_cast<TopoShapePy*>(pcObjShape);
|
TopoShapePy* pShape = static_cast<TopoShapePy*>(pcObjShape);
|
||||||
Base::Vector3d Vector(0,0,1);
|
Base::Vector3d Vector(0,0,1);
|
||||||
if (pcObjDir)
|
if (pcObjDir)
|
||||||
|
@ -96,25 +158,21 @@ projectEx(PyObject *self, PyObject *args)
|
||||||
list.append(Py::Object(new TopoShapePy(new TopoShape(Alg.HO)), true));
|
list.append(Py::Object(new TopoShapePy(new TopoShape(Alg.HO)), true));
|
||||||
list.append(Py::Object(new TopoShapePy(new TopoShape(Alg.HI)), true));
|
list.append(Py::Object(new TopoShapePy(new TopoShape(Alg.HI)), true));
|
||||||
|
|
||||||
return Py::new_reference_to(list);
|
return list;
|
||||||
|
}
|
||||||
} PY_CATCH_OCC;
|
Py::Object projectToSVG(const Py::Tuple& args)
|
||||||
}
|
{
|
||||||
|
|
||||||
static PyObject *
|
|
||||||
projectToSVG(PyObject *self, PyObject *args)
|
|
||||||
{
|
|
||||||
PyObject *pcObjShape;
|
PyObject *pcObjShape;
|
||||||
PyObject *pcObjDir=0;
|
PyObject *pcObjDir=0;
|
||||||
const char *type=0;
|
const char *type=0;
|
||||||
float scale=1.0f;
|
float scale=1.0f;
|
||||||
float tol=0.1f;
|
float tol=0.1f;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "O!|O!sff", &(TopoShapePy::Type), &pcObjShape,
|
if (!PyArg_ParseTuple(args.ptr(), "O!|O!sff",
|
||||||
|
&(TopoShapePy::Type), &pcObjShape,
|
||||||
&(Base::VectorPy::Type), &pcObjDir, &type, &scale, &tol))
|
&(Base::VectorPy::Type), &pcObjDir, &type, &scale, &tol))
|
||||||
return NULL;
|
throw Py::Exception();
|
||||||
|
|
||||||
PY_TRY {
|
|
||||||
TopoShapePy* pShape = static_cast<TopoShapePy*>(pcObjShape);
|
TopoShapePy* pShape = static_cast<TopoShapePy*>(pcObjShape);
|
||||||
Base::Vector3d Vector(0,0,1);
|
Base::Vector3d Vector(0,0,1);
|
||||||
if (pcObjDir)
|
if (pcObjDir)
|
||||||
|
@ -126,25 +184,21 @@ projectToSVG(PyObject *self, PyObject *args)
|
||||||
hidden = true;
|
hidden = true;
|
||||||
|
|
||||||
Py::String result(Alg.getSVG(hidden?ProjectionAlgos::WithHidden:ProjectionAlgos::Plain, scale, tol));
|
Py::String result(Alg.getSVG(hidden?ProjectionAlgos::WithHidden:ProjectionAlgos::Plain, scale, tol));
|
||||||
return Py::new_reference_to(result);
|
return result;
|
||||||
|
}
|
||||||
} PY_CATCH_OCC;
|
Py::Object projectToDXF(const Py::Tuple& args)
|
||||||
}
|
{
|
||||||
|
|
||||||
static PyObject *
|
|
||||||
projectToDXF(PyObject *self, PyObject *args)
|
|
||||||
{
|
|
||||||
PyObject *pcObjShape;
|
PyObject *pcObjShape;
|
||||||
PyObject *pcObjDir=0;
|
PyObject *pcObjDir=0;
|
||||||
const char *type=0;
|
const char *type=0;
|
||||||
float scale=1.0f;
|
float scale=1.0f;
|
||||||
float tol=0.1f;
|
float tol=0.1f;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "O!|O!sff", &(TopoShapePy::Type), &pcObjShape,
|
if (!PyArg_ParseTuple(args.ptr(), "O!|O!sff",
|
||||||
|
&(TopoShapePy::Type), &pcObjShape,
|
||||||
&(Base::VectorPy::Type), &pcObjDir, &type, &scale, &tol))
|
&(Base::VectorPy::Type), &pcObjDir, &type, &scale, &tol))
|
||||||
return NULL;
|
throw Py::Exception();
|
||||||
|
|
||||||
PY_TRY {
|
|
||||||
TopoShapePy* pShape = static_cast<TopoShapePy*>(pcObjShape);
|
TopoShapePy* pShape = static_cast<TopoShapePy*>(pcObjShape);
|
||||||
Base::Vector3d Vector(0,0,1);
|
Base::Vector3d Vector(0,0,1);
|
||||||
if (pcObjDir)
|
if (pcObjDir)
|
||||||
|
@ -156,23 +210,18 @@ projectToDXF(PyObject *self, PyObject *args)
|
||||||
hidden = true;
|
hidden = true;
|
||||||
|
|
||||||
Py::String result(Alg.getDXF(hidden?ProjectionAlgos::WithHidden:ProjectionAlgos::Plain, scale, tol));
|
Py::String result(Alg.getDXF(hidden?ProjectionAlgos::WithHidden:ProjectionAlgos::Plain, scale, tol));
|
||||||
return Py::new_reference_to(result);
|
return result;
|
||||||
|
}
|
||||||
} PY_CATCH_OCC;
|
Py::Object removeSvgTags(const Py::Tuple& args)
|
||||||
}
|
{
|
||||||
|
|
||||||
static PyObject *
|
|
||||||
removeSvgTags(PyObject *self, PyObject *args)
|
|
||||||
{
|
|
||||||
const char* svgcode;
|
const char* svgcode;
|
||||||
if (!PyArg_ParseTuple(args, "s",&svgcode))
|
if (!PyArg_ParseTuple(args.ptr(), "s",&svgcode))
|
||||||
return NULL;
|
throw Py::Exception();
|
||||||
|
|
||||||
PY_TRY {
|
std::string svg(svgcode);
|
||||||
string svg(svgcode);
|
std::string empty = "";
|
||||||
string empty = "";
|
std::string endline = "--endOfLine--";
|
||||||
string endline = "--endOfLine--";
|
std::string linebreak = "\\n";
|
||||||
string linebreak = "\\n";
|
|
||||||
// removing linebreaks for regex to work
|
// removing linebreaks for regex to work
|
||||||
boost::regex e1 ("\\n");
|
boost::regex e1 ("\\n");
|
||||||
svg = boost::regex_replace(svg, e1, endline);
|
svg = boost::regex_replace(svg, e1, endline);
|
||||||
|
@ -195,23 +244,13 @@ removeSvgTags(PyObject *self, PyObject *args)
|
||||||
boost::regex e7 ("--endOfLine--");
|
boost::regex e7 ("--endOfLine--");
|
||||||
svg = boost::regex_replace(svg, e7, linebreak);
|
svg = boost::regex_replace(svg, e7, linebreak);
|
||||||
Py::String result(svg);
|
Py::String result(svg);
|
||||||
return Py::new_reference_to(result);
|
return result;
|
||||||
} PY_CATCH_OCC;
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
PyObject* initModule()
|
||||||
|
{
|
||||||
|
return (new Module)->module().ptr();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace Drawing
|
||||||
|
|
||||||
/* registration table */
|
|
||||||
struct PyMethodDef Drawing_methods[] = {
|
|
||||||
{"project" ,project ,METH_VARARGS,
|
|
||||||
"[visiblyG0,visiblyG1,hiddenG0,hiddenG1] = project(TopoShape[,App.Vector Direction, string type]) -- Project a shape and return the visible/invisible parts of it."},
|
|
||||||
{"projectEx" ,projectEx ,METH_VARARGS,
|
|
||||||
"[V,V1,VN,VO,VI,H,H1,HN,HO,HI] = projectEx(TopoShape[,App.Vector Direction, string type]) -- Project a shape and return the all parts of it."},
|
|
||||||
{"projectToSVG" ,projectToSVG ,METH_VARARGS,
|
|
||||||
"string = projectToSVG(TopoShape[,App.Vector Direction, string type]) -- Project a shape and return the SVG representation as string."},
|
|
||||||
{"projectToDXF" ,projectToDXF ,METH_VARARGS,
|
|
||||||
"string = projectToDXF(TopoShape[,App.Vector Direction, string type]) -- Project a shape and return the DXF representation as string."},
|
|
||||||
{"removeSvgTags" ,removeSvgTags ,METH_VARARGS,
|
|
||||||
"string = removeSvgTags(string) -- Removes the opening and closing svg tags and other metatags from a svg code, making it embeddable"},
|
|
||||||
{NULL, NULL} /* end of table marker */
|
|
||||||
};
|
|
||||||
|
|
|
@ -43,20 +43,20 @@ void loadDrawingResource()
|
||||||
Gui::Translator::instance()->refresh();
|
Gui::Translator::instance()->refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* registration table */
|
namespace DrawingGui {
|
||||||
extern struct PyMethodDef DrawingGui_Import_methods[];
|
extern PyObject* initModule();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Python entry */
|
/* Python entry */
|
||||||
extern "C" {
|
PyMODINIT_FUNC initDrawingGui()
|
||||||
void DrawingGuiExport initDrawingGui()
|
|
||||||
{
|
{
|
||||||
if (!Gui::Application::Instance) {
|
if (!Gui::Application::Instance) {
|
||||||
PyErr_SetString(PyExc_ImportError, "Cannot load Gui module in console application.");
|
PyErr_SetString(PyExc_ImportError, "Cannot load Gui module in console application.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
(void) Py_InitModule("DrawingGui", DrawingGui_Import_methods); /* mod name, table ptr */
|
(void) DrawingGui::initModule();
|
||||||
Base::Console().Log("Loading GUI of Drawing module... done\n");
|
Base::Console().Log("Loading GUI of Drawing module... done\n");
|
||||||
|
|
||||||
// instantiating the commands
|
// instantiating the commands
|
||||||
|
@ -71,5 +71,3 @@ void DrawingGuiExport initDrawingGui()
|
||||||
// add resources and reloads the translators
|
// add resources and reloads the translators
|
||||||
loadDrawingResource();
|
loadDrawingResource();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // extern "C" {
|
|
||||||
|
|
|
@ -28,6 +28,9 @@
|
||||||
# include <sstream>
|
# include <sstream>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <CXX/Extensions.hxx>
|
||||||
|
#include <CXX/Objects.hxx>
|
||||||
|
|
||||||
#include "DrawingView.h"
|
#include "DrawingView.h"
|
||||||
#include <Mod/Drawing/App/FeaturePage.h>
|
#include <Mod/Drawing/App/FeaturePage.h>
|
||||||
#include <Mod/Drawing/App/FeatureViewPart.h>
|
#include <Mod/Drawing/App/FeatureViewPart.h>
|
||||||
|
@ -43,20 +46,45 @@
|
||||||
#include <Gui/MainWindow.h>
|
#include <Gui/MainWindow.h>
|
||||||
#include <Gui/BitmapFactory.h>
|
#include <Gui/BitmapFactory.h>
|
||||||
|
|
||||||
using namespace DrawingGui;
|
namespace DrawingGui {
|
||||||
|
class Module : public Py::ExtensionModule<Module>
|
||||||
|
|
||||||
/* module functions */
|
|
||||||
static PyObject *
|
|
||||||
open(PyObject *self, PyObject *args)
|
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
Module() : Py::ExtensionModule<Module>("DrawingGui")
|
||||||
|
{
|
||||||
|
add_varargs_method("open",&Module::open
|
||||||
|
);
|
||||||
|
add_varargs_method("insert",&Module::importer
|
||||||
|
);
|
||||||
|
add_varargs_method("export",&Module::exporter
|
||||||
|
);
|
||||||
|
initialize("This module is the DrawingGui module."); // register with Python
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~Module() {}
|
||||||
|
|
||||||
|
private:
|
||||||
|
virtual Py::Object invoke_method_varargs(void *method_def, const Py::Tuple &args)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return Py::ExtensionModule<Module>::invoke_method_varargs(method_def, args);
|
||||||
|
}
|
||||||
|
catch (const Base::Exception &e) {
|
||||||
|
throw Py::RuntimeError(e.what());
|
||||||
|
}
|
||||||
|
catch (const std::exception &e) {
|
||||||
|
throw Py::RuntimeError(e.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Py::Object open(const Py::Tuple& args)
|
||||||
|
{
|
||||||
char* Name;
|
char* Name;
|
||||||
if (!PyArg_ParseTuple(args, "et","utf-8",&Name))
|
if (!PyArg_ParseTuple(args.ptr(), "et","utf-8",&Name))
|
||||||
return NULL;
|
throw Py::Exception();
|
||||||
|
|
||||||
std::string EncodedName = std::string(Name);
|
std::string EncodedName = std::string(Name);
|
||||||
PyMem_Free(Name);
|
PyMem_Free(Name);
|
||||||
|
|
||||||
PY_TRY {
|
|
||||||
Base::FileInfo file(EncodedName.c_str());
|
Base::FileInfo file(EncodedName.c_str());
|
||||||
if (file.hasExtension("svg") || file.hasExtension("svgz")) {
|
if (file.hasExtension("svg") || file.hasExtension("svgz")) {
|
||||||
QString fileName = QString::fromUtf8(EncodedName.c_str());
|
QString fileName = QString::fromUtf8(EncodedName.c_str());
|
||||||
|
@ -70,26 +98,21 @@ open(PyObject *self, PyObject *args)
|
||||||
Gui::getMainWindow()->addWindow(view);
|
Gui::getMainWindow()->addWindow(view);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
PyErr_SetString(Base::BaseExceptionFreeCADError, "unknown filetype");
|
throw Py::Exception(Base::BaseExceptionFreeCADError, "unknown filetype");
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
} PY_CATCH;
|
|
||||||
|
|
||||||
Py_Return;
|
return Py::None();
|
||||||
}
|
}
|
||||||
|
Py::Object importer(const Py::Tuple& args)
|
||||||
/* module functions */
|
{
|
||||||
static PyObject *
|
|
||||||
importer(PyObject *self, PyObject *args)
|
|
||||||
{
|
|
||||||
char* Name;
|
char* Name;
|
||||||
const char* dummy;
|
const char* dummy;
|
||||||
if (!PyArg_ParseTuple(args, "et|s","utf-8",&Name,&dummy))
|
if (!PyArg_ParseTuple(args.ptr(), "et|s","utf-8",&Name,&dummy))
|
||||||
return NULL;
|
throw Py::Exception();
|
||||||
|
|
||||||
std::string EncodedName = std::string(Name);
|
std::string EncodedName = std::string(Name);
|
||||||
PyMem_Free(Name);
|
PyMem_Free(Name);
|
||||||
|
|
||||||
PY_TRY {
|
|
||||||
Base::FileInfo file(EncodedName.c_str());
|
Base::FileInfo file(EncodedName.c_str());
|
||||||
if (file.hasExtension("svg") || file.hasExtension("svgz")) {
|
if (file.hasExtension("svg") || file.hasExtension("svgz")) {
|
||||||
QString fileName = QString::fromUtf8(EncodedName.c_str());
|
QString fileName = QString::fromUtf8(EncodedName.c_str());
|
||||||
|
@ -102,25 +125,21 @@ importer(PyObject *self, PyObject *args)
|
||||||
view->resize( 400, 300 );
|
view->resize( 400, 300 );
|
||||||
Gui::getMainWindow()->addWindow(view);
|
Gui::getMainWindow()->addWindow(view);
|
||||||
} else {
|
} else {
|
||||||
PyErr_SetString(Base::BaseExceptionFreeCADError, "unknown filetype");
|
throw Py::Exception(Base::BaseExceptionFreeCADError, "unknown filetype");
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
} PY_CATCH;
|
|
||||||
|
|
||||||
Py_Return;
|
return Py::None();
|
||||||
}
|
}
|
||||||
|
Py::Object exporter(const Py::Tuple& args)
|
||||||
static PyObject *
|
{
|
||||||
exporter(PyObject *self, PyObject *args)
|
|
||||||
{
|
|
||||||
PyObject* object;
|
PyObject* object;
|
||||||
char* Name;
|
char* Name;
|
||||||
if (!PyArg_ParseTuple(args, "Oet",&object,"utf-8",&Name))
|
if (!PyArg_ParseTuple(args.ptr(), "Oet",&object,"utf-8",&Name))
|
||||||
return NULL;
|
throw Py::Exception();
|
||||||
|
|
||||||
std::string EncodedName = std::string(Name);
|
std::string EncodedName = std::string(Name);
|
||||||
PyMem_Free(Name);
|
PyMem_Free(Name);
|
||||||
|
|
||||||
PY_TRY {
|
|
||||||
Py::Sequence list(object);
|
Py::Sequence list(object);
|
||||||
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
|
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
|
||||||
PyObject* item = (*it).ptr();
|
PyObject* item = (*it).ptr();
|
||||||
|
@ -132,8 +151,7 @@ exporter(PyObject *self, PyObject *args)
|
||||||
if (!str_out) {
|
if (!str_out) {
|
||||||
std::stringstream str;
|
std::stringstream str;
|
||||||
str << "Cannot open file '" << EncodedName << "' for writing";
|
str << "Cannot open file '" << EncodedName << "' for writing";
|
||||||
PyErr_SetString(PyExc_IOError, str.str().c_str());
|
throw Py::Exception(PyExc_IOError, str.str().c_str());
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
if (fi_out.hasExtension("svg")) {
|
if (fi_out.hasExtension("svg")) {
|
||||||
std::string fn = static_cast<Drawing::FeaturePage*>(obj)->PageResult.getValue();
|
std::string fn = static_cast<Drawing::FeaturePage*>(obj)->PageResult.getValue();
|
||||||
|
@ -142,8 +160,7 @@ exporter(PyObject *self, PyObject *args)
|
||||||
if (!str_in) {
|
if (!str_in) {
|
||||||
std::stringstream str;
|
std::stringstream str;
|
||||||
str << "Cannot open file '" << fn << "' for reading";
|
str << "Cannot open file '" << fn << "' for reading";
|
||||||
PyErr_SetString(PyExc_IOError, str.str().c_str());
|
throw Py::Exception(PyExc_IOError, str.str().c_str());
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
str_in >> str_out.rdbuf();
|
str_in >> str_out.rdbuf();
|
||||||
|
@ -159,12 +176,10 @@ exporter(PyObject *self, PyObject *args)
|
||||||
std::string viewName = view->Label.getValue();
|
std::string viewName = view->Label.getValue();
|
||||||
App::DocumentObject* link = view->Source.getValue();
|
App::DocumentObject* link = view->Source.getValue();
|
||||||
if (!link) {
|
if (!link) {
|
||||||
PyErr_SetString(Base::BaseExceptionFreeCADError, "No object linked");
|
throw Py::Exception(Base::BaseExceptionFreeCADError, "No object linked");
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
if (!link->getTypeId().isDerivedFrom(Part::Feature::getClassTypeId())) {
|
if (!link->getTypeId().isDerivedFrom(Part::Feature::getClassTypeId())) {
|
||||||
PyErr_SetString(PyExc_TypeError, "Linked object is not a Part object");
|
throw Py::TypeError("Linked object is not a Part object");
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
TopoDS_Shape shape = static_cast<Part::Feature*>(link)->Shape.getShape()._Shape;
|
TopoDS_Shape shape = static_cast<Part::Feature*>(link)->Shape.getShape()._Shape;
|
||||||
if (!shape.IsNull()) {
|
if (!shape.IsNull()) {
|
||||||
|
@ -187,25 +202,22 @@ exporter(PyObject *self, PyObject *args)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
PyErr_SetString(PyExc_TypeError, "Export of page object as this file format is not supported by Drawing module");
|
throw Py::TypeError("Export of page object as this file format is not supported by Drawing module");
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
PyErr_SetString(PyExc_TypeError, "Export of this object type is not supported by Drawing module");
|
throw Py::TypeError("Export of this object type is not supported by Drawing module");
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} PY_CATCH;
|
|
||||||
|
|
||||||
Py_Return;
|
return Py::None();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
PyObject* initModule()
|
||||||
|
{
|
||||||
|
return (new Module)->module().ptr();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* registration table */
|
} // namespace DrawingGui
|
||||||
struct PyMethodDef DrawingGui_Import_methods[] = {
|
|
||||||
{"open" ,open , METH_VARARGS}, /* method name, C func ptr, always-tuple */
|
|
||||||
{"insert" ,importer, METH_VARARGS},
|
|
||||||
{"export" ,exporter, METH_VARARGS},
|
|
||||||
{NULL, NULL} /* end of table marker */
|
|
||||||
};
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user