Skip to content

Commit

Permalink
+ simplify porting of Robot module to Python3
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Jan 18, 2016
1 parent 65fe62d commit 1081492
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 141 deletions.
55 changes: 45 additions & 10 deletions src/Mod/Robot/App/AppRobot.cpp
Expand Up @@ -29,9 +29,12 @@
#include <Base/Console.h>
#include <Base/Interpreter.h>

#include <CXX/Extensions.hxx>
#include <CXX/Objects.hxx>

#include "Robot6AxisPy.h"
#include "Robot6Axis.h"
#include "Simulation.h"
#include "TrajectoryPy.h"
#include "Trajectory.h"
#include "PropertyTrajectory.h"
Expand All @@ -43,15 +46,50 @@
#include "TrajectoryCompound.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
}

virtual ~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();

PyDoc_STRVAR(module_Robot_doc,
"This module is the Robot module.");
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 */
extern "C" {
void RobotExport initRobot()
PyMODINIT_FUNC initRobot()
{
// load dependent module
try {
Expand All @@ -62,10 +100,9 @@ void RobotExport initRobot()
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");


// Add Types to module
Base::Interpreter().addType(&Robot::Robot6AxisPy ::Type,robotModule,"Robot6Axis");
Base::Interpreter().addType(&Robot::WaypointPy ::Type,robotModule,"Waypoint");
Expand All @@ -75,7 +112,7 @@ void RobotExport initRobot()
// NOTE: To finish the initialization of our own type objects we must
// 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.

Robot::Robot6Axis ::init();
Robot::RobotObject ::init();
Robot::TrajectoryObject ::init();
Expand All @@ -86,5 +123,3 @@ void RobotExport initRobot()
Robot::TrajectoryCompound ::init();
Robot::TrajectoryDressUpObject ::init();
}

} // extern "C"
73 changes: 0 additions & 73 deletions src/Mod/Robot/App/AppRobotPy.cpp

This file was deleted.

1 change: 0 additions & 1 deletion src/Mod/Robot/App/CMakeLists.txt
Expand Up @@ -45,7 +45,6 @@ SET(Python_SRCS

SET(Mod_SRCS
AppRobot.cpp
AppRobotPy.cpp
PreCompiled.cpp
PreCompiled.h
)
Expand Down
38 changes: 25 additions & 13 deletions src/Mod/Robot/Gui/AppRobotGui.cpp
Expand Up @@ -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>
Expand All @@ -50,15 +53,26 @@ void loadRobotResource()
Gui::Translator::instance()->refresh();
}

/* registration table */
extern struct PyMethodDef RobotGui_Import_methods[];
namespace RobotGui {
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 */
extern "C" {
void RobotGuiExport initRobotGui()
PyMODINIT_FUNC initRobotGui()
{
if (!Gui::Application::Instance) {
if (!Gui::Application::Instance) {
PyErr_SetString(PyExc_ImportError, "Cannot load Gui module in console application.");
return;
}
Expand All @@ -82,7 +96,7 @@ void RobotGuiExport initRobotGui()
PyErr_SetString(PyExc_ImportError, e.what());
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");

// instantiating the commands
Expand All @@ -93,14 +107,12 @@ void RobotGuiExport initRobotGui()

// addition objects
RobotGui::Workbench ::init();
RobotGui::ViewProviderRobotObject ::init();
RobotGui::ViewProviderTrajectory ::init();
RobotGui::ViewProviderEdge2TracObject ::init();
RobotGui::ViewProviderTrajectoryCompound ::init();
RobotGui::ViewProviderTrajectoryDressUp ::init();
RobotGui::ViewProviderRobotObject ::init();
RobotGui::ViewProviderTrajectory ::init();
RobotGui::ViewProviderEdge2TracObject ::init();
RobotGui::ViewProviderTrajectoryCompound ::init();
RobotGui::ViewProviderTrajectoryDressUp ::init();

// add resources and reloads the translators
loadRobotResource();
}

} // extern "C" {
33 changes: 0 additions & 33 deletions src/Mod/Robot/Gui/AppRobotGuiPy.cpp

This file was deleted.

21 changes: 10 additions & 11 deletions src/Mod/Robot/Gui/CMakeLists.txt
Expand Up @@ -62,7 +62,6 @@ SOURCE_GROUP("Qt" FILES ${PartDesignGui_MOC_SRCS})

SET(RobotGui_SRCS_Module
AppRobotGui.cpp
AppRobotGuiPy.cpp
Resources/Robot.qrc
PreCompiled.cpp
PreCompiled.h
Expand Down Expand Up @@ -94,7 +93,7 @@ SET(RobotGui_SRCS_ViewProvider
SOURCE_GROUP("ViewProvider" FILES ${RobotGui_SRCS_ViewProvider})

SET(RobotGui_SRCS_TaskBoxes
TaskRobot6Axis.ui
TaskRobot6Axis.ui
TaskRobot6Axis.cpp
TaskRobot6Axis.h
TaskTrajectory.ui
Expand Down Expand Up @@ -125,21 +124,21 @@ SET(RobotGui_SRCS_TaskDlg
TaskDlgSimulate.cpp
TaskDlgEdge2Trac.h
TaskDlgEdge2Trac.cpp
TaskDlgTrajectoryCompound.h
TaskDlgTrajectoryCompound.cpp
TaskDlgTrajectoryDressUp.h
TaskDlgTrajectoryDressUp.cpp
TaskDlgTrajectoryCompound.h
TaskDlgTrajectoryCompound.cpp
TaskDlgTrajectoryDressUp.h
TaskDlgTrajectoryDressUp.cpp
)
SOURCE_GROUP("Task_Dialogs" FILES ${RobotGui_SRCS_TaskDlg})

SET(RobotGui_SRCS
${RobotGui_UIC_HDRS}
${RobotResource_SRCS}
${RobotGui_SRCS_Module}
${RobotGui_SRCS_ViewProvider}
${RobotGui_SRCS_Commands}
${RobotGui_SRCS_TaskBoxes}
${RobotGui_SRCS_TaskDlg}
${RobotGui_SRCS_Module}
${RobotGui_SRCS_ViewProvider}
${RobotGui_SRCS_Commands}
${RobotGui_SRCS_TaskBoxes}
${RobotGui_SRCS_TaskDlg}
)


Expand Down

0 comments on commit 1081492

Please sign in to comment.