Skip to content

Commit

Permalink
+ simplify porting of PartDesign module to Python3
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Jan 20, 2016
1 parent ebc6d37 commit 91d6e24
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 113 deletions.
16 changes: 6 additions & 10 deletions src/Mod/PartDesign/App/AppPartDesign.cpp
Expand Up @@ -49,15 +49,12 @@
#include "FeatureMultiTransform.h"
#include "FeatureHole.h"

extern struct PyMethodDef PartDesign_methods[];

PyDoc_STRVAR(module_PartDesign_doc,
"This module is the PartDesign module.");

namespace PartDesign {
extern PyObject* initModule();
}

/* Python entry */
extern "C" {
void PartDesignExport init_PartDesign()
PyMODINIT_FUNC init_PartDesign()
{
// load dependent module
try {
Expand All @@ -68,7 +65,8 @@ void PartDesignExport init_PartDesign()
PyErr_SetString(PyExc_ImportError, e.what());
return;
}
Py_InitModule3("_PartDesign", PartDesign_methods, module_PartDesign_doc); /* mod name, table ptr */

(void)PartDesign::initModule();
Base::Console().Log("Loading PartDesign module... done\n");


Expand Down Expand Up @@ -97,5 +95,3 @@ void PartDesignExport init_PartDesign()
PartDesign::Chamfer ::init();
PartDesign::Draft ::init();
}

} // extern "C"
142 changes: 80 additions & 62 deletions src/Mod/PartDesign/App/AppPartDesignPy.cpp
Expand Up @@ -26,78 +26,96 @@
# include <Python.h>
#endif

#include <CXX/Extensions.hxx>
#include <CXX/Objects.hxx>

#include <Base/GeometryPyCXX.h>
#include <Base/VectorPy.h>
#include <Base/Tools.h>

static PyObject * makeFilletArc(PyObject *self, PyObject *args)
namespace PartDesign {
class Module : public Py::ExtensionModule<Module>
{
PyObject *pM1;
PyObject *pP;
PyObject *pQ;
PyObject *pN;
double r2;
int ccw;
if (!PyArg_ParseTuple(args, "O!O!O!O!di",
&(Base::VectorPy::Type), &pM1,
&(Base::VectorPy::Type), &pP,
&(Base::VectorPy::Type), &pQ,
&(Base::VectorPy::Type), &pN,
&r2, &ccw))
return NULL;

Base::Vector3d M1 = Py::Vector(pM1, false).toVector();
Base::Vector3d P = Py::Vector(pP, false).toVector();
Base::Vector3d Q = Py::Vector(pQ, false).toVector();
Base::Vector3d N = Py::Vector(pN, false).toVector();

Base::Vector3d u = Q - P;
Base::Vector3d v = P - M1;
Base::Vector3d b;
if (ccw)
b = u % N;
else
b = N % u;
b.Normalize();

double uu = u * u;
double uv = u * v;
double r1 = v.Length();

// distinguish between internal and external fillets
r2 *= Base::sgn(uv);

double cc = 2.0 * r2 * (b * v - r1);
double d = uv * uv - uu * cc;
if (d < 0) {
PyErr_SetString(Base::BaseExceptionFreeCADError, "Unable to caluclate intersection points");
return NULL;
public:
Module() : Py::ExtensionModule<Module>("_PartDesign")
{
add_varargs_method("makeFilletArc",&Module::makeFilletArc,
"makeFilletArc(...) -- Fillet arc."
);
initialize("This module is the PartDesign module."); // register with Python
}

double t;
double t1 = (-uv + sqrt(d)) / uu;
double t2 = (-uv - sqrt(d)) / uu;
virtual ~Module() {}

if (fabs(t1) < fabs(t2))
t = t1;
else
t = t2;
private:
Py::Object makeFilletArc(const Py::Tuple& args)
{
PyObject *pM1;
PyObject *pP;
PyObject *pQ;
PyObject *pN;
double r2;
int ccw;
if (!PyArg_ParseTuple(args.ptr(), "O!O!O!O!di",
&(Base::VectorPy::Type), &pM1,
&(Base::VectorPy::Type), &pP,
&(Base::VectorPy::Type), &pQ,
&(Base::VectorPy::Type), &pN,
&r2, &ccw))
throw Py::Exception();

Base::Vector3d M2 = P + (u*t) + (b*r2);
Base::Vector3d S1 = (r2 * M1 + r1 * M2)/(r1+r2);
Base::Vector3d S2 = M2 - (b*r2);
Base::Vector3d M1 = Py::Vector(pM1, false).toVector();
Base::Vector3d P = Py::Vector(pP, false).toVector();
Base::Vector3d Q = Py::Vector(pQ, false).toVector();
Base::Vector3d N = Py::Vector(pN, false).toVector();

Py::Tuple tuple(3);
tuple.setItem(0, Py::Vector(S1));
tuple.setItem(1, Py::Vector(S2));
tuple.setItem(2, Py::Vector(M2));
Base::Vector3d u = Q - P;
Base::Vector3d v = P - M1;
Base::Vector3d b;
if (ccw)
b = u % N;
else
b = N % u;
b.Normalize();

return Py::new_reference_to(tuple);
}
double uu = u * u;
double uv = u * v;
double r1 = v.Length();

// distinguish between internal and external fillets
r2 *= Base::sgn(uv);

double cc = 2.0 * r2 * (b * v - r1);
double d = uv * uv - uu * cc;
if (d < 0) {
throw Py::RuntimeError("Unable to caluclate intersection points");
}

double t;
double t1 = (-uv + sqrt(d)) / uu;
double t2 = (-uv - sqrt(d)) / uu;

if (fabs(t1) < fabs(t2))
t = t1;
else
t = t2;

Base::Vector3d M2 = P + (u*t) + (b*r2);
Base::Vector3d S1 = (r2 * M1 + r1 * M2)/(r1+r2);
Base::Vector3d S2 = M2 - (b*r2);

/* registration table */
struct PyMethodDef PartDesign_methods[] = {
{"makeFilletArc" ,makeFilletArc,METH_VARARGS,
"makeFilletArc(...) -- Fillet arc."},
{NULL, NULL} /* end of table marker */
Py::Tuple tuple(3);
tuple.setItem(0, Py::Vector(S1));
tuple.setItem(1, Py::Vector(S2));
tuple.setItem(2, Py::Vector(M2));

return tuple;
}
};

PyObject* initModule()
{
return (new Module)->module().ptr();
}

} // namespace PartDesign
32 changes: 25 additions & 7 deletions src/Mod/PartDesign/Gui/AppPartDesignGui.cpp
Expand Up @@ -26,6 +26,9 @@
# include <Python.h>
#endif

#include <CXX/Extensions.hxx>
#include <CXX/Objects.hxx>

#include <Base/Console.h>
#include <Base/Interpreter.h>
#include <Gui/Application.h>
Expand Down Expand Up @@ -55,13 +58,30 @@ void loadPartDesignResource()
Gui::Translator::instance()->refresh();
}

/* registration table */
extern struct PyMethodDef PartDesignGui_Import_methods[];
namespace PartDesignGui {
class Module : public Py::ExtensionModule<Module>
{
public:
Module() : Py::ExtensionModule<Module>("PartDesignGui")
{
initialize("This module is the PartDesignGui module."); // register with Python
}

virtual ~Module() {}

private:
};

PyObject* initModule()
{
return (new Module)->module().ptr();
}

} // namespace PartDesignGui


/* Python entry */
extern "C" {
void PartDesignGuiExport initPartDesignGui()
PyMODINIT_FUNC initPartDesignGui()
{
if (!Gui::Application::Instance) {
PyErr_SetString(PyExc_ImportError, "Cannot load Gui module in console application.");
Expand All @@ -77,7 +97,7 @@ void PartDesignGuiExport initPartDesignGui()
return;
}

(void) Py_InitModule("PartDesignGui", PartDesignGui_Import_methods); /* mod name, table ptr */
(void)PartDesignGui::initModule();
Base::Console().Log("Loading GUI of PartDesign module... done\n");

// instantiating the commands
Expand All @@ -101,5 +121,3 @@ void PartDesignGuiExport initPartDesignGui()
// add resources and reloads the translators
loadPartDesignResource();
}

} // extern "C" {
33 changes: 0 additions & 33 deletions src/Mod/PartDesign/Gui/AppPartDesignGuiPy.cpp

This file was deleted.

1 change: 0 additions & 1 deletion src/Mod/PartDesign/Gui/CMakeLists.txt
Expand Up @@ -157,7 +157,6 @@ SOURCE_GROUP("TaskDialogs" FILES ${PartDesignGuiTaskDlgs_SRCS})

SET(PartDesignGuiModule_SRCS
AppPartDesignGui.cpp
AppPartDesignGuiPy.cpp
Command.cpp
Resources/PartDesign.qrc
PreCompiled.cpp
Expand Down

0 comments on commit 91d6e24

Please sign in to comment.