Exposing Cplusplus to Python

From FreeCAD Documentation
Revision as of 18:12, 16 June 2019 by Kunda1 (talk | contribs) (stub, needs more work)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
This documentation is a stub and needs more work, perhaps a written example + links to commits demonstrating in the commit history

How to expose c++ functionality to Python

It becomes necessary at times to expand the FreeCAD API further by exposing functions that are available in the source code in c++ to the python. In so doing, providing an ability to trigger deep internal functionality with Python in real-time instead of needing to compile.

Below is an explanation of how one can achieve this.

The basic structure of a program to expose functionality to Python is something like this:

  • get the Py object parameters and convert them to c++ variables using PyArg_ParseTuple(),
  • use various c++ routines from OpenCascade and/or FreeCAD to produce the desired result,
  • convert the c++ result into Py object using routines like PyLong_AsLong(), Py::asObject(), etc,
  • return the Py object.

Source: https://forum.freecadweb.org/viewtopic.php?p=314796#p314617