+ simplify porting of Robot module to Python3

This commit is contained in:
wmayer 2016-01-18 18:26:37 +01:00
parent 65fe62d93b
commit 108149297f
6 changed files with 80 additions and 141 deletions

View File

@ -29,9 +29,12 @@
#include <Base/Console.h> #include <Base/Console.h>
#include <Base/Interpreter.h> #include <Base/Interpreter.h>
#include <CXX/Extensions.hxx>
#include <CXX/Objects.hxx>
#include "Robot6AxisPy.h" #include "Robot6AxisPy.h"
#include "Robot6Axis.h" #include "Robot6Axis.h"
#include "Simulation.h"
#include "TrajectoryPy.h" #include "TrajectoryPy.h"
#include "Trajectory.h" #include "Trajectory.h"
#include "PropertyTrajectory.h" #include "PropertyTrajectory.h"
@ -43,15 +46,50 @@
#include "TrajectoryCompound.h" #include "TrajectoryCompound.h"
#include "TrajectoryDressUpObject.h" #include "TrajectoryDressUpObject.h"
extern struct PyMethodDef Robot_methods[]; namespace Robot {
class Module : public Py::ExtensionModule<Module>
{
public:
Module() : Py::ExtensionModule<Module>("Robot")
{
add_varargs_method("simulateToFile",&Module::simulateToFile,
"simulateToFile(Robot,Trajectory,TickSize,FileName) - runs the simulation and write the result to a file."
);
initialize("This module is the Robot module."); // register with Python
}
PyDoc_STRVAR(module_Robot_doc, virtual ~Module() {}
"This module is the Robot module.");
private:
Py::Object simulateToFile(const Py::Tuple& args)
{
PyObject *pcRobObj;
PyObject *pcTracObj;
float tick;
char* FileName;
if (!PyArg_ParseTuple(args.ptr(), "O!O!fs", &(Robot6AxisPy::Type), &pcRobObj,
&(TrajectoryPy::Type), &pcTracObj,
&tick,&FileName))
throw Py::Exception();
try {
Robot::Trajectory &Trac = * static_cast<TrajectoryPy*>(pcTracObj)->getTrajectoryPtr();
Robot::Robot6Axis &Rob = * static_cast<Robot6AxisPy*>(pcRobObj)->getRobot6AxisPtr();
Simulation Sim(Trac,Rob);
}
catch (const Base::Exception& e) {
throw Py::RuntimeError(e.what());
}
return Py::Float(0.0);
}
};
} // namespace Robot
/* Python entry */ /* Python entry */
extern "C" { PyMODINIT_FUNC initRobot()
void RobotExport initRobot()
{ {
// load dependent module // load dependent module
try { try {
@ -62,10 +100,9 @@ void RobotExport initRobot()
return; return;
} }
PyObject* robotModule = Py_InitModule3("Robot", Robot_methods, module_Robot_doc); /* mod name, table ptr */ PyObject* robotModule = (new Robot::Module())->module().ptr();
Base::Console().Log("Loading Robot module... done\n"); Base::Console().Log("Loading Robot module... done\n");
// Add Types to module // Add Types to module
Base::Interpreter().addType(&Robot::Robot6AxisPy ::Type,robotModule,"Robot6Axis"); Base::Interpreter().addType(&Robot::Robot6AxisPy ::Type,robotModule,"Robot6Axis");
Base::Interpreter().addType(&Robot::WaypointPy ::Type,robotModule,"Waypoint"); Base::Interpreter().addType(&Robot::WaypointPy ::Type,robotModule,"Waypoint");
@ -75,7 +112,7 @@ void RobotExport initRobot()
// NOTE: To finish the initialization of our own type objects we must // NOTE: To finish the initialization of our own type objects we must
// call PyType_Ready, otherwise we run into a segmentation fault, later on. // call PyType_Ready, otherwise we run into a segmentation fault, later on.
// This function is responsible for adding inherited slots from a type's base class. // This function is responsible for adding inherited slots from a type's base class.
Robot::Robot6Axis ::init(); Robot::Robot6Axis ::init();
Robot::RobotObject ::init(); Robot::RobotObject ::init();
Robot::TrajectoryObject ::init(); Robot::TrajectoryObject ::init();
@ -86,5 +123,3 @@ void RobotExport initRobot()
Robot::TrajectoryCompound ::init(); Robot::TrajectoryCompound ::init();
Robot::TrajectoryDressUpObject ::init(); Robot::TrajectoryDressUpObject ::init();
} }
} // extern "C"

View File

@ -1,73 +0,0 @@
/***************************************************************************
* Copyright (c) 2008 Jürgen Riegel (juergen.riegel@web.de) *
* *
* 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 <Python.h>
#endif
#include <Base/Console.h>
#include <Base/VectorPy.h>
#include "TrajectoryPy.h"
#include "Robot6AxisPy.h"
#include "Simulation.h"
#include "RobotAlgos.h"
using namespace Robot;
static PyObject *
simulateToFile(PyObject *self, PyObject *args)
{
PyObject *pcRobObj;
PyObject *pcTracObj;
float tick;
char* FileName;
if (!PyArg_ParseTuple(args, "O!O!fs", &(Robot6AxisPy::Type), &pcRobObj,
&(TrajectoryPy::Type), &pcTracObj,
&tick,&FileName
) )
return NULL; // NULL triggers exception
PY_TRY {
Robot::Trajectory &Trac = * static_cast<TrajectoryPy*>(pcTracObj)->getTrajectoryPtr();
Robot::Robot6Axis &Rob = * static_cast<Robot6AxisPy*>(pcRobObj)->getRobot6AxisPtr();
Simulation Sim(Trac,Rob);
} PY_CATCH;
return Py::new_reference_to(Py::Float(0.0));
}
/* registration table */
struct PyMethodDef Robot_methods[] = {
{"simulateToFile" ,simulateToFile ,METH_VARARGS,
"void simulateToFile(Robot,Trajectory,TickSize,FileName) - runs the simulation and write the result to a file."},
{NULL, NULL} /* end of table marker */
};

View File

@ -45,7 +45,6 @@ SET(Python_SRCS
SET(Mod_SRCS SET(Mod_SRCS
AppRobot.cpp AppRobot.cpp
AppRobotPy.cpp
PreCompiled.cpp PreCompiled.cpp
PreCompiled.h PreCompiled.h
) )

View File

@ -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>
@ -50,15 +53,26 @@ void loadRobotResource()
Gui::Translator::instance()->refresh(); Gui::Translator::instance()->refresh();
} }
/* registration table */ namespace RobotGui {
extern struct PyMethodDef RobotGui_Import_methods[]; class Module : public Py::ExtensionModule<Module>
{
public:
Module() : Py::ExtensionModule<Module>("RobotGui")
{
initialize("This module is the RobotGui module."); // register with Python
}
virtual ~Module() {}
private:
};
} // namespace RobotGui
/* Python entry */ /* Python entry */
extern "C" { PyMODINIT_FUNC initRobotGui()
void RobotGuiExport initRobotGui()
{ {
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.");
return; return;
} }
@ -82,7 +96,7 @@ void RobotGuiExport initRobotGui()
PyErr_SetString(PyExc_ImportError, e.what()); PyErr_SetString(PyExc_ImportError, e.what());
return; return;
} }
(void) Py_InitModule("RobotGui", RobotGui_Import_methods); /* mod name, table ptr */ (void)new RobotGui::Module();
Base::Console().Log("Loading GUI of Robot module... done\n"); Base::Console().Log("Loading GUI of Robot module... done\n");
// instantiating the commands // instantiating the commands
@ -93,14 +107,12 @@ void RobotGuiExport initRobotGui()
// addition objects // addition objects
RobotGui::Workbench ::init(); RobotGui::Workbench ::init();
RobotGui::ViewProviderRobotObject ::init(); RobotGui::ViewProviderRobotObject ::init();
RobotGui::ViewProviderTrajectory ::init(); RobotGui::ViewProviderTrajectory ::init();
RobotGui::ViewProviderEdge2TracObject ::init(); RobotGui::ViewProviderEdge2TracObject ::init();
RobotGui::ViewProviderTrajectoryCompound ::init(); RobotGui::ViewProviderTrajectoryCompound ::init();
RobotGui::ViewProviderTrajectoryDressUp ::init(); RobotGui::ViewProviderTrajectoryDressUp ::init();
// add resources and reloads the translators // add resources and reloads the translators
loadRobotResource(); loadRobotResource();
} }
} // extern "C" {

View File

@ -1,33 +0,0 @@
/***************************************************************************
* Copyright (c) 2008 Werner Mayer <werner.wm.mayer@gmx.de> *
* *
* 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 <Python.h>
#endif
/* registration table */
struct PyMethodDef RobotGui_Import_methods[] = {
{NULL, NULL} /* end of table marker */
};

View File

@ -62,7 +62,6 @@ SOURCE_GROUP("Qt" FILES ${PartDesignGui_MOC_SRCS})
SET(RobotGui_SRCS_Module SET(RobotGui_SRCS_Module
AppRobotGui.cpp AppRobotGui.cpp
AppRobotGuiPy.cpp
Resources/Robot.qrc Resources/Robot.qrc
PreCompiled.cpp PreCompiled.cpp
PreCompiled.h PreCompiled.h
@ -94,7 +93,7 @@ SET(RobotGui_SRCS_ViewProvider
SOURCE_GROUP("ViewProvider" FILES ${RobotGui_SRCS_ViewProvider}) SOURCE_GROUP("ViewProvider" FILES ${RobotGui_SRCS_ViewProvider})
SET(RobotGui_SRCS_TaskBoxes SET(RobotGui_SRCS_TaskBoxes
TaskRobot6Axis.ui TaskRobot6Axis.ui
TaskRobot6Axis.cpp TaskRobot6Axis.cpp
TaskRobot6Axis.h TaskRobot6Axis.h
TaskTrajectory.ui TaskTrajectory.ui
@ -125,21 +124,21 @@ SET(RobotGui_SRCS_TaskDlg
TaskDlgSimulate.cpp TaskDlgSimulate.cpp
TaskDlgEdge2Trac.h TaskDlgEdge2Trac.h
TaskDlgEdge2Trac.cpp TaskDlgEdge2Trac.cpp
TaskDlgTrajectoryCompound.h TaskDlgTrajectoryCompound.h
TaskDlgTrajectoryCompound.cpp TaskDlgTrajectoryCompound.cpp
TaskDlgTrajectoryDressUp.h TaskDlgTrajectoryDressUp.h
TaskDlgTrajectoryDressUp.cpp TaskDlgTrajectoryDressUp.cpp
) )
SOURCE_GROUP("Task_Dialogs" FILES ${RobotGui_SRCS_TaskDlg}) SOURCE_GROUP("Task_Dialogs" FILES ${RobotGui_SRCS_TaskDlg})
SET(RobotGui_SRCS SET(RobotGui_SRCS
${RobotGui_UIC_HDRS} ${RobotGui_UIC_HDRS}
${RobotResource_SRCS} ${RobotResource_SRCS}
${RobotGui_SRCS_Module} ${RobotGui_SRCS_Module}
${RobotGui_SRCS_ViewProvider} ${RobotGui_SRCS_ViewProvider}
${RobotGui_SRCS_Commands} ${RobotGui_SRCS_Commands}
${RobotGui_SRCS_TaskBoxes} ${RobotGui_SRCS_TaskBoxes}
${RobotGui_SRCS_TaskDlg} ${RobotGui_SRCS_TaskDlg}
) )