+ simplify porting of Sketcher module to Python3
This commit is contained in:
parent
bfdaa46feb
commit
65fe62d93b
|
@ -28,7 +28,7 @@
|
||||||
|
|
||||||
#include <Base/Console.h>
|
#include <Base/Console.h>
|
||||||
#include <Base/Interpreter.h>
|
#include <Base/Interpreter.h>
|
||||||
|
|
||||||
#include "SketchObjectSF.h"
|
#include "SketchObjectSF.h"
|
||||||
#include "SketchObject.h"
|
#include "SketchObject.h"
|
||||||
#include "Constraint.h"
|
#include "Constraint.h"
|
||||||
|
@ -38,15 +38,12 @@
|
||||||
#include "PropertyConstraintList.h"
|
#include "PropertyConstraintList.h"
|
||||||
|
|
||||||
|
|
||||||
extern struct PyMethodDef Sketcher_methods[];
|
namespace Sketcher {
|
||||||
|
extern PyObject* initModule();
|
||||||
PyDoc_STRVAR(module_Sketcher_doc,
|
}
|
||||||
"This module is the Sketcher module.");
|
|
||||||
|
|
||||||
|
|
||||||
/* Python entry */
|
/* Python entry */
|
||||||
extern "C" {
|
PyMODINIT_FUNC initSketcher()
|
||||||
void SketcherExport initSketcher()
|
|
||||||
{
|
{
|
||||||
// load dependent module
|
// load dependent module
|
||||||
try {
|
try {
|
||||||
|
@ -56,8 +53,9 @@ void SketcherExport initSketcher()
|
||||||
PyErr_SetString(PyExc_ImportError, e.what());
|
PyErr_SetString(PyExc_ImportError, e.what());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
PyObject* sketcherModule = Py_InitModule3("Sketcher", Sketcher_methods, module_Sketcher_doc); /* mod name, table ptr */
|
|
||||||
|
PyObject* sketcherModule = Sketcher::initModule();
|
||||||
|
|
||||||
// Add Types to module
|
// Add Types to module
|
||||||
Base::Interpreter().addType(&Sketcher::ConstraintPy ::Type,sketcherModule,"Constraint");
|
Base::Interpreter().addType(&Sketcher::ConstraintPy ::Type,sketcherModule,"Constraint");
|
||||||
Base::Interpreter().addType(&Sketcher::SketchPy ::Type,sketcherModule,"Sketch");
|
Base::Interpreter().addType(&Sketcher::SketchPy ::Type,sketcherModule,"Sketch");
|
||||||
|
@ -75,15 +73,4 @@ void SketcherExport initSketcher()
|
||||||
Sketcher::PropertyConstraintList::init();
|
Sketcher::PropertyConstraintList::init();
|
||||||
|
|
||||||
Base::Console().Log("Loading Sketcher module... done\n");
|
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());
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,103 +36,95 @@
|
||||||
#include <App/Application.h>
|
#include <App/Application.h>
|
||||||
#include <App/Document.h>
|
#include <App/Document.h>
|
||||||
|
|
||||||
|
#include <CXX/Extensions.hxx>
|
||||||
|
#include <CXX/Objects.hxx>
|
||||||
|
|
||||||
// Things from the part module
|
// Things from the part module
|
||||||
#include <Mod/Part/App/TopoShape.h>
|
#include <Mod/Part/App/TopoShape.h>
|
||||||
#include <Mod/Part/App/TopoShapePy.h>
|
#include <Mod/Part/App/TopoShapePy.h>
|
||||||
|
|
||||||
#include "SketchObjectSF.h"
|
#include "SketchObjectSF.h"
|
||||||
|
|
||||||
using Base::Console;
|
|
||||||
using namespace Part;
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
|
namespace Sketcher {
|
||||||
/* module functions */
|
class Module : public Py::ExtensionModule<Module>
|
||||||
static PyObject * open(PyObject *self, PyObject *args)
|
|
||||||
{
|
{
|
||||||
char* Name;
|
public:
|
||||||
if (!PyArg_ParseTuple(args, "et","utf-8",&Name))
|
Module() : Py::ExtensionModule<Module>("Sketcher")
|
||||||
return NULL;
|
{
|
||||||
std::string EncodedName = std::string(Name);
|
add_varargs_method("open",&Module::open
|
||||||
PyMem_Free(Name);
|
);
|
||||||
|
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::Console().Log("Open in Part with %s",Name);
|
||||||
Base::FileInfo file(EncodedName.c_str());
|
Base::FileInfo file(EncodedName.c_str());
|
||||||
|
|
||||||
// extract ending
|
// extract extension
|
||||||
if (file.extension() == "")
|
if (file.extension().empty())
|
||||||
Py_Error(Base::BaseExceptionFreeCADError,"no file ending");
|
throw Py::RuntimeError("No file extension");
|
||||||
|
|
||||||
//if (file.hasExtension("igs") || file.hasExtension("iges")) {
|
throw Py::RuntimeError("Unknown file extension");
|
||||||
// // create new document and add Import feature
|
return Py::None();
|
||||||
// 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");
|
|
||||||
//}
|
|
||||||
|
|
||||||
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 */
|
try {
|
||||||
static PyObject * insert(PyObject *self, PyObject *args)
|
//Base::Console().Log("Insert in Part with %s",Name);
|
||||||
{
|
Base::FileInfo file(EncodedName.c_str());
|
||||||
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);
|
|
||||||
|
|
||||||
PY_TRY {
|
// extract extension
|
||||||
//Base::Console().Log("Insert in Part with %s",Name);
|
if (file.extension().empty())
|
||||||
Base::FileInfo file(EncodedName.c_str());
|
throw Py::RuntimeError("No file extension");
|
||||||
|
|
||||||
// extract ending
|
App::Document *pcDoc = App::GetApplication().getDocument(DocName);
|
||||||
if (file.extension() == "")
|
if (!pcDoc) {
|
||||||
Py_Error(Base::BaseExceptionFreeCADError,"no file ending");
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
catch (const Base::Exception& e) {
|
||||||
if (file.hasExtension("skf")) {
|
throw Py::RuntimeError(e.what());
|
||||||
|
|
||||||
Sketcher::SketchObjectSF *pcFeature = (Sketcher::SketchObjectSF *)pcDoc->addObject("Sketcher::SketchObjectSF",file.fileNamePure().c_str());
|
|
||||||
pcFeature->SketchFlatFile.setValue(EncodedName.c_str());
|
|
||||||
|
|
||||||
pcDoc->recompute();
|
|
||||||
}
|
}
|
||||||
else {
|
return Py::None();
|
||||||
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 */
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// @cond DOXERR
|
||||||
|
PyObject* initModule()
|
||||||
|
{
|
||||||
|
return (new Module())->module().ptr();
|
||||||
|
}
|
||||||
|
/// @endcond
|
||||||
|
|
||||||
|
} // namespace Sketcher
|
||||||
|
|
|
@ -26,6 +26,9 @@
|
||||||
# include <Python.h>
|
# include <Python.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <CXX/Extensions.hxx>
|
||||||
|
#include <CXX/Objects.hxx>
|
||||||
|
|
||||||
#include <Base/Console.h>
|
#include <Base/Console.h>
|
||||||
#include <Base/Interpreter.h>
|
#include <Base/Interpreter.h>
|
||||||
#include <Gui/Application.h>
|
#include <Gui/Application.h>
|
||||||
|
@ -54,13 +57,24 @@ void loadSketcherResource()
|
||||||
Gui::Translator::instance()->refresh();
|
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 */
|
/* Python entry */
|
||||||
extern "C" {
|
PyMODINIT_FUNC initSketcherGui()
|
||||||
void SketcherGuiExport initSketcherGui()
|
|
||||||
{
|
{
|
||||||
if (!Gui::Application::Instance) {
|
if (!Gui::Application::Instance) {
|
||||||
PyErr_SetString(PyExc_ImportError, "Cannot load Gui module in console application.");
|
PyErr_SetString(PyExc_ImportError, "Cannot load Gui module in console application.");
|
||||||
|
@ -75,7 +89,7 @@ void SketcherGuiExport initSketcherGui()
|
||||||
return;
|
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");
|
Base::Console().Log("Loading GUI of Sketcher module... done\n");
|
||||||
|
|
||||||
// instantiating the commands
|
// instantiating the commands
|
||||||
|
@ -101,5 +115,3 @@ void SketcherGuiExport initSketcherGui()
|
||||||
// add resources and reloads the translators
|
// add resources and reloads the translators
|
||||||
loadSketcherResource();
|
loadSketcherResource();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // extern "C" {
|
|
||||||
|
|
|
@ -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 */
|
|
||||||
};
|
|
|
@ -65,7 +65,6 @@ SET(SketcherGui_SRCS
|
||||||
${SketcherGui_SRCS}
|
${SketcherGui_SRCS}
|
||||||
${SketcherGui_UIC_HDRS}
|
${SketcherGui_UIC_HDRS}
|
||||||
AppSketcherGui.cpp
|
AppSketcherGui.cpp
|
||||||
AppSketcherGuiPy.cpp
|
|
||||||
GeometryCreationMode.h
|
GeometryCreationMode.h
|
||||||
Command.cpp
|
Command.cpp
|
||||||
CommandCreateGeo.cpp
|
CommandCreateGeo.cpp
|
||||||
|
|
Loading…
Reference in New Issue
Block a user