+ simplify porting of Sketcher module to Python3

This commit is contained in:
wmayer 2016-01-18 12:19:54 +01:00
parent bfdaa46feb
commit 65fe62d93b
5 changed files with 99 additions and 178 deletions

View File

@ -28,7 +28,7 @@
#include <Base/Console.h>
#include <Base/Interpreter.h>
#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());
}

View File

@ -36,103 +36,95 @@
#include <App/Application.h>
#include <App/Document.h>
#include <CXX/Extensions.hxx>
#include <CXX/Objects.hxx>
// Things from the part module
#include <Mod/Part/App/TopoShape.h>
#include <Mod/Part/App/TopoShapePy.h>
#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<Module>
{
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<Module>("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

View File

@ -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>
@ -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<Module>
{
public:
Module() : Py::ExtensionModule<Module>("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" {

View File

@ -1,69 +0,0 @@
/***************************************************************************
* Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* 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 <qimage.h>
#endif
#include <Base/Console.h>
#include <Base/Exception.h>
#include <Base/FileInfo.h>
#include <App/Application.h>
#include <Gui/MainWindow.h>
#include <Gui/BitmapFactory.h>
/* 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 */
};

View File

@ -65,7 +65,6 @@ SET(SketcherGui_SRCS
${SketcherGui_SRCS}
${SketcherGui_UIC_HDRS}
AppSketcherGui.cpp
AppSketcherGuiPy.cpp
GeometryCreationMode.h
Command.cpp
CommandCreateGeo.cpp