+ simplify porting of Drawing module to Python3
This commit is contained in:
@@ -26,6 +26,9 @@
|
||||
# include <Python.h>
|
||||
#endif
|
||||
|
||||
#include <CXX/Extensions.hxx>
|
||||
#include <CXX/Objects.hxx>
|
||||
|
||||
#include <Mod/Part/App/TopoShapePy.h>
|
||||
#include "ProjectionAlgos.h"
|
||||
#include <Base/Console.h>
|
||||
@@ -34,21 +37,84 @@
|
||||
|
||||
#include <Mod/Part/App/OCCError.h>
|
||||
|
||||
using namespace Drawing;
|
||||
using namespace Part;
|
||||
using namespace std;
|
||||
using Part::TopoShapePy;
|
||||
using Part::TopoShape;
|
||||
|
||||
static PyObject *
|
||||
project(PyObject *self, PyObject *args)
|
||||
namespace Drawing {
|
||||
class Module : public Py::ExtensionModule<Module>
|
||||
{
|
||||
PyObject *pcObjShape;
|
||||
PyObject *pcObjDir=0;
|
||||
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
|
||||
}
|
||||
|
||||
if (!PyArg_ParseTuple(args, "O!|O!", &(TopoShapePy::Type), &pcObjShape,&(Base::VectorPy::Type), &pcObjDir)) // convert args: Python->C
|
||||
return NULL; // NULL triggers exception
|
||||
virtual ~Module() {}
|
||||
|
||||
PY_TRY {
|
||||
TopoShapePy* pShape = static_cast<TopoShapePy*>(pcObjShape);
|
||||
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 *pcObjDir=0;
|
||||
|
||||
if (!PyArg_ParseTuple(args.ptr(), "O!|O!",
|
||||
&(Part::TopoShapePy::Type), &pcObjShape,
|
||||
&(Base::VectorPy::Type), &pcObjDir))
|
||||
throw Py::Exception();
|
||||
|
||||
Part::TopoShapePy* pShape = static_cast<Part::TopoShapePy*>(pcObjShape);
|
||||
Base::Vector3d Vector(0,0,1);
|
||||
if (pcObjDir)
|
||||
Vector = *static_cast<Base::VectorPy*>(pcObjDir)->getVectorPtr();
|
||||
@@ -56,27 +122,23 @@ project(PyObject *self, PyObject *args)
|
||||
ProjectionAlgos Alg(pShape->getTopoShapePtr()->_Shape,Vector);
|
||||
|
||||
Py::List list;
|
||||
list.append(Py::Object(new TopoShapePy(new TopoShape(Alg.V)) , true));
|
||||
list.append(Py::Object(new TopoShapePy(new TopoShape(Alg.V1)), true));
|
||||
list.append(Py::Object(new TopoShapePy(new 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.V)) , true));
|
||||
list.append(Py::Object(new Part::TopoShapePy(new Part::TopoShape(Alg.V1)), true));
|
||||
list.append(Py::Object(new Part::TopoShapePy(new Part::TopoShape(Alg.H)) , true));
|
||||
list.append(Py::Object(new Part::TopoShapePy(new Part::TopoShape(Alg.H1)), true));
|
||||
|
||||
return Py::new_reference_to(list);
|
||||
return list;
|
||||
}
|
||||
Py::Object projectEx(const Py::Tuple& args)
|
||||
{
|
||||
PyObject *pcObjShape;
|
||||
PyObject *pcObjDir=0;
|
||||
|
||||
} PY_CATCH_OCC;
|
||||
if (!PyArg_ParseTuple(args.ptr(), "O!|O!",
|
||||
&(TopoShapePy::Type), &pcObjShape,
|
||||
&(Base::VectorPy::Type), &pcObjDir))
|
||||
throw Py::Exception();
|
||||
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
projectEx(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *pcObjShape;
|
||||
PyObject *pcObjDir=0;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "O!|O!", &(TopoShapePy::Type), &pcObjShape,&(Base::VectorPy::Type), &pcObjDir)) // convert args: Python->C
|
||||
return NULL; // NULL triggers exception
|
||||
|
||||
PY_TRY {
|
||||
TopoShapePy* pShape = static_cast<TopoShapePy*>(pcObjShape);
|
||||
Base::Vector3d Vector(0,0,1);
|
||||
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.HI)), true));
|
||||
|
||||
return Py::new_reference_to(list);
|
||||
return list;
|
||||
}
|
||||
Py::Object projectToSVG(const Py::Tuple& args)
|
||||
{
|
||||
PyObject *pcObjShape;
|
||||
PyObject *pcObjDir=0;
|
||||
const char *type=0;
|
||||
float scale=1.0f;
|
||||
float tol=0.1f;
|
||||
|
||||
} PY_CATCH_OCC;
|
||||
}
|
||||
if (!PyArg_ParseTuple(args.ptr(), "O!|O!sff",
|
||||
&(TopoShapePy::Type), &pcObjShape,
|
||||
&(Base::VectorPy::Type), &pcObjDir, &type, &scale, &tol))
|
||||
throw Py::Exception();
|
||||
|
||||
static PyObject *
|
||||
projectToSVG(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *pcObjShape;
|
||||
PyObject *pcObjDir=0;
|
||||
const char *type=0;
|
||||
float scale=1.0f;
|
||||
float tol=0.1f;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "O!|O!sff", &(TopoShapePy::Type), &pcObjShape,
|
||||
&(Base::VectorPy::Type), &pcObjDir, &type, &scale, &tol))
|
||||
return NULL;
|
||||
|
||||
PY_TRY {
|
||||
TopoShapePy* pShape = static_cast<TopoShapePy*>(pcObjShape);
|
||||
Base::Vector3d Vector(0,0,1);
|
||||
if (pcObjDir)
|
||||
@@ -126,25 +184,21 @@ projectToSVG(PyObject *self, PyObject *args)
|
||||
hidden = true;
|
||||
|
||||
Py::String result(Alg.getSVG(hidden?ProjectionAlgos::WithHidden:ProjectionAlgos::Plain, scale, tol));
|
||||
return Py::new_reference_to(result);
|
||||
return result;
|
||||
}
|
||||
Py::Object projectToDXF(const Py::Tuple& args)
|
||||
{
|
||||
PyObject *pcObjShape;
|
||||
PyObject *pcObjDir=0;
|
||||
const char *type=0;
|
||||
float scale=1.0f;
|
||||
float tol=0.1f;
|
||||
|
||||
} PY_CATCH_OCC;
|
||||
}
|
||||
if (!PyArg_ParseTuple(args.ptr(), "O!|O!sff",
|
||||
&(TopoShapePy::Type), &pcObjShape,
|
||||
&(Base::VectorPy::Type), &pcObjDir, &type, &scale, &tol))
|
||||
throw Py::Exception();
|
||||
|
||||
static PyObject *
|
||||
projectToDXF(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *pcObjShape;
|
||||
PyObject *pcObjDir=0;
|
||||
const char *type=0;
|
||||
float scale=1.0f;
|
||||
float tol=0.1f;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "O!|O!sff", &(TopoShapePy::Type), &pcObjShape,
|
||||
&(Base::VectorPy::Type), &pcObjDir, &type, &scale, &tol))
|
||||
return NULL;
|
||||
|
||||
PY_TRY {
|
||||
TopoShapePy* pShape = static_cast<TopoShapePy*>(pcObjShape);
|
||||
Base::Vector3d Vector(0,0,1);
|
||||
if (pcObjDir)
|
||||
@@ -156,23 +210,18 @@ projectToDXF(PyObject *self, PyObject *args)
|
||||
hidden = true;
|
||||
|
||||
Py::String result(Alg.getDXF(hidden?ProjectionAlgos::WithHidden:ProjectionAlgos::Plain, scale, tol));
|
||||
return Py::new_reference_to(result);
|
||||
return result;
|
||||
}
|
||||
Py::Object removeSvgTags(const Py::Tuple& args)
|
||||
{
|
||||
const char* svgcode;
|
||||
if (!PyArg_ParseTuple(args.ptr(), "s",&svgcode))
|
||||
throw Py::Exception();
|
||||
|
||||
} PY_CATCH_OCC;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
removeSvgTags(PyObject *self, PyObject *args)
|
||||
{
|
||||
const char* svgcode;
|
||||
if (!PyArg_ParseTuple(args, "s",&svgcode))
|
||||
return NULL;
|
||||
|
||||
PY_TRY {
|
||||
string svg(svgcode);
|
||||
string empty = "";
|
||||
string endline = "--endOfLine--";
|
||||
string linebreak = "\\n";
|
||||
std::string svg(svgcode);
|
||||
std::string empty = "";
|
||||
std::string endline = "--endOfLine--";
|
||||
std::string linebreak = "\\n";
|
||||
// removing linebreaks for regex to work
|
||||
boost::regex e1 ("\\n");
|
||||
svg = boost::regex_replace(svg, e1, endline);
|
||||
@@ -195,23 +244,13 @@ removeSvgTags(PyObject *self, PyObject *args)
|
||||
boost::regex e7 ("--endOfLine--");
|
||||
svg = boost::regex_replace(svg, e7, linebreak);
|
||||
Py::String result(svg);
|
||||
return Py::new_reference_to(result);
|
||||
} PY_CATCH_OCC;
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
PyObject* initModule()
|
||||
{
|
||||
return (new Module)->module().ptr();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* 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 */
|
||||
};
|
||||
} // namespace Drawing
|
||||
|
||||
Reference in New Issue
Block a user