From 65fe62d93bd473d61272dcd96f7ad813128edc61 Mon Sep 17 00:00:00 2001 From: wmayer Date: Mon, 18 Jan 2016 12:19:54 +0100 Subject: [PATCH] + simplify porting of Sketcher module to Python3 --- src/Mod/Sketcher/App/AppSketcher.cpp | 29 ++--- src/Mod/Sketcher/App/AppSketcherPy.cpp | 152 ++++++++++------------ src/Mod/Sketcher/Gui/AppSketcherGui.cpp | 26 +++- src/Mod/Sketcher/Gui/AppSketcherGuiPy.cpp | 69 ---------- src/Mod/Sketcher/Gui/CMakeLists.txt | 1 - 5 files changed, 99 insertions(+), 178 deletions(-) delete mode 100644 src/Mod/Sketcher/Gui/AppSketcherGuiPy.cpp diff --git a/src/Mod/Sketcher/App/AppSketcher.cpp b/src/Mod/Sketcher/App/AppSketcher.cpp index 487a7473b..29b28c793 100644 --- a/src/Mod/Sketcher/App/AppSketcher.cpp +++ b/src/Mod/Sketcher/App/AppSketcher.cpp @@ -28,7 +28,7 @@ #include #include - + #include "SketchObjectSF.h" #include "SketchObject.h" #include "Constraint.h" @@ -38,15 +38,12 @@ #include "PropertyConstraintList.h" -extern struct PyMethodDef Sketcher_methods[]; - -PyDoc_STRVAR(module_Sketcher_doc, -"This module is the Sketcher module."); - +namespace Sketcher { +extern PyObject* initModule(); +} /* Python entry */ -extern "C" { -void SketcherExport initSketcher() +PyMODINIT_FUNC initSketcher() { // load dependent module try { @@ -56,8 +53,9 @@ void SketcherExport initSketcher() PyErr_SetString(PyExc_ImportError, e.what()); return; } - PyObject* sketcherModule = Py_InitModule3("Sketcher", Sketcher_methods, module_Sketcher_doc); /* mod name, table ptr */ - + + PyObject* sketcherModule = Sketcher::initModule(); + // Add Types to module Base::Interpreter().addType(&Sketcher::ConstraintPy ::Type,sketcherModule,"Constraint"); Base::Interpreter().addType(&Sketcher::SketchPy ::Type,sketcherModule,"Sketch"); @@ -75,15 +73,4 @@ void SketcherExport initSketcher() Sketcher::PropertyConstraintList::init(); Base::Console().Log("Loading Sketcher module... done\n"); - -} - - - -} // extern "C" - -// debug print for sketchsolv -void debugprint(std::string s) -{ - Base::Console().Log(s.c_str()); } diff --git a/src/Mod/Sketcher/App/AppSketcherPy.cpp b/src/Mod/Sketcher/App/AppSketcherPy.cpp index d75f9d85a..b26e57294 100644 --- a/src/Mod/Sketcher/App/AppSketcherPy.cpp +++ b/src/Mod/Sketcher/App/AppSketcherPy.cpp @@ -36,103 +36,95 @@ #include #include +#include +#include + // Things from the part module #include #include #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 { - 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("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 diff --git a/src/Mod/Sketcher/Gui/AppSketcherGui.cpp b/src/Mod/Sketcher/Gui/AppSketcherGui.cpp index 1c51a371a..1d9bace66 100644 --- a/src/Mod/Sketcher/Gui/AppSketcherGui.cpp +++ b/src/Mod/Sketcher/Gui/AppSketcherGui.cpp @@ -26,6 +26,9 @@ # include #endif +#include +#include + #include #include #include @@ -54,13 +57,24 @@ void loadSketcherResource() Gui::Translator::instance()->refresh(); } -/* registration table */ -extern struct PyMethodDef SketcherGui_Import_methods[]; +namespace SketcherGui { +class Module : public Py::ExtensionModule +{ +public: + Module() : Py::ExtensionModule("SketcherGui") + { + initialize("This module is the SketcherGui module."); // register with Python + } + + virtual ~Module() {} + +private: +}; +} // namespace SketcherGui /* Python entry */ -extern "C" { -void SketcherGuiExport initSketcherGui() +PyMODINIT_FUNC initSketcherGui() { if (!Gui::Application::Instance) { PyErr_SetString(PyExc_ImportError, "Cannot load Gui module in console application."); @@ -75,7 +89,7 @@ void SketcherGuiExport initSketcherGui() return; } - (void) Py_InitModule("SketcherGui", SketcherGui_Import_methods); /* mod name, table ptr */ + (void)new SketcherGui::Module(); Base::Console().Log("Loading GUI of Sketcher module... done\n"); // instantiating the commands @@ -101,5 +115,3 @@ void SketcherGuiExport initSketcherGui() // add resources and reloads the translators loadSketcherResource(); } - -} // extern "C" { diff --git a/src/Mod/Sketcher/Gui/AppSketcherGuiPy.cpp b/src/Mod/Sketcher/Gui/AppSketcherGuiPy.cpp deleted file mode 100644 index 703b3a2fa..000000000 --- a/src/Mod/Sketcher/Gui/AppSketcherGuiPy.cpp +++ /dev/null @@ -1,69 +0,0 @@ -/*************************************************************************** - * Copyright (c) 2008 Werner Mayer * - * * - * This file is part of the FreeCAD CAx development system. * - * * - * This library is free software; you can redistribute it and/or * - * modify it under the terms of the GNU Library General Public * - * License as published by the Free Software Foundation; either * - * version 2 of the License, or (at your option) any later version. * - * * - * This library is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU Library General Public License for more details. * - * * - * You should have received a copy of the GNU Library General Public * - * License along with this library; see the file COPYING.LIB. If not, * - * write to the Free Software Foundation, Inc., 59 Temple Place, * - * Suite 330, Boston, MA 02111-1307, USA * - * * - ***************************************************************************/ - -#include "PreCompiled.h" -#ifndef _PreComp_ -# include -#endif - - -#include -#include -#include -#include -#include -#include - - - -/* module functions */ -static PyObject * -open(PyObject *self, PyObject *args) -{ - char* Name; - if (!PyArg_ParseTuple(args, "et","utf-8",&Name)) - return NULL; - std::string EncodedName = std::string(Name); - PyMem_Free(Name); - - PY_TRY { - } PY_CATCH; - - Py_Return; -} - - -/* module functions */ -static PyObject * -insert(PyObject *self, PyObject *args) -{ - // not supported to insert an image (by dropping on an image view) - // hence do nothing - Py_Return; -} - -/* registration table */ -struct PyMethodDef SketcherGui_Import_methods[] = { - {"open" ,open , 1}, /* method name, C func ptr, always-tuple */ - {"insert" ,insert, 1}, - {NULL, NULL} /* end of table marker */ -}; diff --git a/src/Mod/Sketcher/Gui/CMakeLists.txt b/src/Mod/Sketcher/Gui/CMakeLists.txt index 3a88305c0..a3e29e0ff 100644 --- a/src/Mod/Sketcher/Gui/CMakeLists.txt +++ b/src/Mod/Sketcher/Gui/CMakeLists.txt @@ -65,7 +65,6 @@ SET(SketcherGui_SRCS ${SketcherGui_SRCS} ${SketcherGui_UIC_HDRS} AppSketcherGui.cpp - AppSketcherGuiPy.cpp GeometryCreationMode.h Command.cpp CommandCreateGeo.cpp