py3: ported Measure and TechDraw

This commit is contained in:
Yorik van Havre 2016-10-01 17:25:45 -03:00 committed by wmayer
parent f2dc8c5d31
commit e13d4a7882
10 changed files with 99 additions and 34 deletions

View File

@ -26,24 +26,36 @@
#endif
#include <Base/Console.h>
#include <Base/PyObjectBase.h>
#include <Base/Interpreter.h>
#include "Measurement.h"
#include "MeasurementPy.h"
struct PyMethodDef Measure_methods[] = {
// {"read" , read, 1},
{NULL, NULL, 0, NULL} /* end of table marker */
namespace Measure {
class Module : public Py::ExtensionModule<Module>
{
public:
Module() : Py::ExtensionModule<Module>("Measure")
{
initialize("This module is the Measure module."); // register with Python
}
virtual ~Module() {}
private:
};
PyObject* initModule()
{
return (new Module)->module().ptr();
}
PyDoc_STRVAR(module_Measure_doc,
"This module is the Measure module.");
} // namespace Measure
/* Python entry */
extern "C" {
void MeasureExport initMeasure()
PyMOD_INIT_FUNC(Measure)
{
// load dependent module
try {
@ -51,24 +63,18 @@ void MeasureExport initMeasure()
}
catch(const Base::Exception& e) {
PyErr_SetString(PyExc_ImportError, e.what());
return;
PyMOD_Return(0);
}
PyObject* measureModule = Py_InitModule3("Measure", Measure_methods, module_Measure_doc); /* mod name, table ptr */
PyObject* mod = Measure::initModule();
// Add Types to module
Base::Interpreter().addType(&Measure::MeasurementPy ::Type,measureModule,"Measurement");
Base::Console().Log("Loading Measure module... done\n");
Base::Interpreter().addType(&Measure::MeasurementPy ::Type,mod,"Measurement");
Base::Console().Log("Loading Inspection module... done\n");
Measure::Measurement ::init();
PyMOD_Return(mod);
}
} // extern "C"
// debug print for sketchsolv
void debugprint(std::string s)
{
Base::Console().Log(s.c_str());
}
}

View File

@ -15,6 +15,7 @@
#endif
#include <Base/Console.h>
#include <Base/PyObjectBase.h>
#include <Base/Interpreter.h>
#include "DrawPage.h"
@ -40,11 +41,11 @@
#include "DrawViewDetail.h"
namespace TechDraw {
extern PyObject* initModule();
extern PyObject* initModule();
}
/* Python entry */
PyMODINIT_FUNC initTechDraw()
PyMOD_INIT_FUNC(TechDraw)
{
// load dependent module
try {
@ -53,9 +54,9 @@ PyMODINIT_FUNC initTechDraw()
}
catch(const Base::Exception& e) {
PyErr_SetString(PyExc_ImportError, e.what());
return;
PyMOD_Return(0);
}
(void)TechDraw::initModule();
PyObject* mod = TechDraw::initModule();
Base::Console().Log("Loading TechDraw module... done\n");
@ -96,4 +97,5 @@ PyMODINIT_FUNC initTechDraw()
TechDraw::DrawViewMultiPython ::init();
TechDraw::DrawTemplatePython ::init();
TechDraw::DrawViewSymbolPython::init();
PyMOD_Return(mod);
}

View File

@ -38,8 +38,11 @@ PyObject* DrawPagePy::addView(PyObject* args)
DrawView* view = pyView->getDrawViewPtr(); //get DrawView for pyView
int rc = page->addView(view);
#if PY_MAJOR_VERSION < 3
return PyInt_FromLong((long) rc);
#else
return PyLong_FromLong((long) rc);
#endif
}
PyObject* DrawPagePy::removeView(PyObject* args)
@ -60,7 +63,11 @@ PyObject* DrawPagePy::removeView(PyObject* args)
int rc = page->removeView(view);
#if PY_MAJOR_VERSION < 3
return PyInt_FromLong((long) rc);
#else
return PyLong_FromLong((long) rc);
#endif
}

View File

@ -22,7 +22,7 @@
<Documentation>
<UserDocu>Number of geometry in template</UserDocu>
</Documentation>
<Parameter Name="GeometryCount" Type="Int"/>
<Parameter Name="GeometryCount" Type="Long"/>
</Attribute>
</PythonExport>
</GenerateModel>

View File

@ -80,8 +80,16 @@ PyObject* DrawParametricTemplatePy::drawLine(PyObject *args)
}
#if PY_MAJOR_VERSION < 3
Py::Int DrawParametricTemplatePy::getGeometryCount(void) const
{
int size = getDrawParametricTemplatePtr()->getGeometry().size();
return Py::Int(size);
}
#else
Py::Long DrawParametricTemplatePy::getGeometryCount(void) const
{
int size = getDrawParametricTemplatePtr()->getGeometry().size();
return Py::Long(size);
}
#endif

View File

@ -49,7 +49,11 @@ PyObject* DrawProjGroupPy::removeProjection(PyObject* args)
DrawProjGroup* projGroup = getDrawProjGroupPtr();
int i = projGroup->removeProjection(projType);
#if PY_MAJOR_VERSION < 3
return PyInt_FromLong((long) i);
#else
return PyLong_FromLong((long) i);
#endif
}
PyObject* DrawProjGroupPy::purgeProjections(PyObject* /*args*/)
@ -57,7 +61,11 @@ PyObject* DrawProjGroupPy::purgeProjections(PyObject* /*args*/)
DrawProjGroup* projGroup = getDrawProjGroupPtr();
int i = projGroup->purgeProjections();
#if PY_MAJOR_VERSION < 3
return PyInt_FromLong((long) i);
#else
return PyLong_FromLong((long) i);
#endif
}
PyObject* DrawProjGroupPy::getItemByLabel(PyObject* args)

View File

@ -50,7 +50,11 @@ PyObject* DrawSVGTemplatePy::getEditFieldContent(PyObject* args)
}
std::string content = getDrawSVGTemplatePtr()->EditableTexts[fieldName];
if (!content.empty()) {
#if PY_MAJOR_VERSION < 3
result = PyString_FromString(content.c_str());
#else
result = PyUnicode_FromString(content.c_str());
#endif
}
return result;
}

View File

@ -82,7 +82,11 @@ PyObject* DrawViewClipPy::getChildViewNames(PyObject* args)
std::vector<std::string>::iterator it = strings.begin();
for( ; it != strings.end(); it++) {
#if PY_MAJOR_VERSION < 3
PyObject* pString = PyString_FromString(it->c_str()); //TODO: unicode & py3
#else
PyObject* pString = PyUnicode_FromString(it->c_str());
#endif
//int rc =
static_cast<void> (PyList_Append(result, pString));
}

View File

@ -29,7 +29,11 @@ PyObject* DrawViewCollectionPy::addView(PyObject* args)
int i = collect->addView(view);
#if PY_MAJOR_VERSION < 3
return PyInt_FromLong((long) i);
#else
return PyLong_FromLong((long) i);
#endif
}
PyObject* DrawViewCollectionPy::removeView(PyObject* args)
@ -47,7 +51,11 @@ PyObject* DrawViewCollectionPy::removeView(PyObject* args)
int i = collect->removeView(view);
#if PY_MAJOR_VERSION < 3
return PyInt_FromLong((long) i);
#else
return PyLong_FromLong((long) i);
#endif
}

View File

@ -28,6 +28,7 @@
#endif
#include <Base/Console.h>
#include <Base/PyObjectBase.h>
#include <Gui/Application.h>
#include <Gui/Language/Translator.h>
#include <Gui/WidgetFactory.h>
@ -65,21 +66,38 @@ void loadTechDrawResource()
Gui::Translator::instance()->refresh();
}
/* registration table */
extern struct PyMethodDef TechDrawGui_Import_methods[];
namespace TechDrawGui {
class Module : public Py::ExtensionModule<Module>
{
public:
Module() : Py::ExtensionModule<Module>("TechDrawGui")
{
initialize("This module is the TechDrawGui module."); // register with Python
}
virtual ~Module() {}
private:
};
PyObject* initModule()
{
return (new Module)->module().ptr();
}
} // namespace TechDrawGui
/* Python entry */
extern "C" {
void TechDrawGuiExport initTechDrawGui()
PyMOD_INIT_FUNC(TechDrawGui)
{
if (!Gui::Application::Instance) {
PyErr_SetString(PyExc_ImportError, "Cannot load Gui module in console application.");
return;
PyMOD_Return(0);
}
PyObject* mod = TechDrawGui::initModule();
(void) Py_InitModule("TechDrawGui", TechDrawGui_Import_methods); /* mod name, table ptr */
Base::Console().Log("Loading GUI of TechDraw module... done\n");
Base::Console().Log("Loading TechDrawGui module... done\n");
// instantiating the commands
CreateTechDrawCommands();
@ -113,6 +131,6 @@ void TechDrawGuiExport initTechDrawGui()
// add resources and reloads the translators
loadTechDrawResource();
}
} // extern "C" {
PyMOD_Return(mod);
}