FreeCAD Logo FreeCAD 1.0
  • English Afrikaans Arabic Belarusian Catalan Czech German Greek Spanish Spanish Basque Finnish Filipino French Galician Croatian Hungarian Indonesian Italian Japanese Kabyle Korean Lithuanian Dutch Norwegian Bokmal Polish Portuguese Portuguese Romanian Russian Slovak Slovenian Serbian Swedish Turkish Ukrainian Valencian Vietnamese Chinese Chinese
  • Features
  • Download
  • Blog
  • Documentation
    Documentation index Getting started Users documentation The FreeCAD manual Workbenches documentation Python coding documentation C++ coding documentation Tutorials Frequently asked questions Privacy policy About FreeCAD
  • Contribute
    How to help Sponsor Report a bug Make a pull request Jobs and funding Contribution guidelines Developers handbook Translations
  • Community
    Code of conduct Forum The FPA GitHub GitLab Codeberg Mastodon Matrix IRC IRC via Webchat Gitter Discord Reddit Twitter Facebook LinkedIn Calendar
  • ♥ Donate

Donate

$
SEPA Information
Please set up your SEPA bank transfer to:
Beneficiary: The FreeCAD project association
IBAN: BE04 0019 2896 4531
BIC/SWIFT: GEBABEBBXXX
Bank agency: BNP Paribas Fortis
Address: Rue de la Station 64, 1360 Perwez, Belgium

While Stripe doesn't support monthly donations, you can still become a sponsor! Simply make a one-time donation equivalent to 12 months of support, and you'll gain access to the corresponding sponsoring tier. It's an easy and flexible way to contribute.

If you are not sure or not able to commit to a regular donation, but still want to help the project, you can do a one-time donation, of any amount.

Choose freely the amount you wish to donate one time only.

You can support FreeCAD by sponsoring it as an individual or organization through various platforms. Sponsorship provides a steady income for developers, allowing the FPA to plan ahead and enabling greater investment in FreeCAD. To encourage sponsorship, we offer different tiers, and unless you choose to remain anonymous, your name or company logo will be featured on our website accordingly.

from 1 USD / 1 EUR per month. You will not have your name displayed here, but you will have helped the project a lot anyway. Together, normal sponsors maintain the project on its feet as much as the bigger sponsors.

from 25 USD / 25 EUR per month. Your name or company name is displayed on this page.

from 100 USD / 100 EUR per month. Your name or company name is displayed on this page, with a link to your website, and a one-line description text.

from 200 USD / 200 EUR per month. Your name or company name and logo displayed on this page, with a link to your website and a custom description text. Companies that have helped FreeCAD early on also appear under Gold sponsors.

Instead of donating each month, you might find it more comfortable to make a one-time donation that, when divided by twelve, would give you right to enter a sponsoring tier. Don't hesitate to do so!

Choose freely the amount you wish to donate each month.

Please inform your forum name or twitter handle as a notein your transfer, or reach to us, so we can give you proper credits!

Wrapping a Cplusplus class in Python

This article is a stub. Please contribute your knowledge to it!

Background

FreeCAD uses a custom XML-based system to create the Python wrapper for a C++ class. To wrap a C++ class for use in Python, two files must be manually created, and two files are automatically generated by the CMake build system (in addition to the C++ header and implementation files for the class).

You must create:

  • [YourClass]Py.xml

  • [YourClass]PyImp.cpp

Edit the appropriate CMakeLists.txt file to add references to these two files. From the XML file, the build system will then create:

  • [YourClass]Py.cpp

  • [YourClass]Py.h

Class Description XML File

The XML file [YourClass]Py.xml provides information about the functions and attributes that the Python class implements, as well as the user documentation for those items that displays in the FreeCAD Python console.

For this example, we will look at the wrapper for the Axis C++ class. The XML description file begins with:

{{Code|lang=xml|code= <?xml version="1.0" encoding="UTF-8"?>

User documentation here
    </UserDocu>
    <DeveloperDocu>Developer documentation here</DeveloperDocu>
</Documentation>

}}

Following this preamble, a list of methods and attributes is given. The format of a method is:

{{Code|lang=xml|code=

move(Vector) Move the axis base along the vector }}

The format of an attribute is:

{{Code|lang=xml|code=

Direction vector of the Axis }}

For an attribute, if \"ReadOnly\" is false, you will provide both a getter and a setter function. If it is true, only a getter is allowed. In this case we will be required to provide two functions in the implementation C++ file:

{{Code|lang=cpp|code= Py::Object AxisPy::getDirection(void) const }}

and:

{{Code|lang=cpp|code= void AxisPy::setDirection(Py::Object arg) }}

Implementation Cplusplus File

The implementation C++ file [YourClass]PyImp.cpp provides the \"glue\" that connects the C++ and Python structures together, effectively translating from one language to the other. The FreeCAD C++-to-Python system provides a number of C++ classes that map to their corresponding Python type. The most fundamental of these is the Py::Object class -- rarely created directly, this class provides the base of the inheritance tree, and is used as the return type for any function that is returning Python data.

Include Files

Your C++ implementation file will include the following files:

{{Code|lang=cpp|code=

include "PreCompiled.h"

include "[YourClass].h"

// Inclusion of the generated files (generated out of [YourClass]Py.xml)

include "[YourClass]Py.h"

include "[YourClass]Py.cpp"

}}

Of course, you may include whatever other C++ headers your code requires to function as well.

Constructor

Your C++ implementation must contain the definition of the PyInit function: for example, for the Axis class wrapper, this is

{{Code|lang=cpp|code= int AxisPy::PyInit(PyObject args, PyObject /kwd/) }}

Within this function you will most likely need to parse incoming arguments to the constructor: the most important function for this purpose is the Python-provided PyArg_ParseTuple. It takes in the passed argument list, a descriptor for the expected arguments that it should parse, and type information and storage locations for the parsed results. For example:

{{Code|lang=cpp|code= PyObject d; if (PyArg_ParseTuple(args, "O!O", &(Base::VectorPy::Type), &o, &(Base::VectorPy::Type), &d)) { // NOTE: The first parameter defines the base (origin) and the second the direction. getAxisPtr() = Base::Axis(static_cast<Base::VectorPy>(o)->value(), static_cast<Base::VectorPy>(d)->value()); return 0; } }}

For a complete list of format specifiers see Python C API documentation. Note that several related functions are also defined which allow the use of keywords, etc. The complete set is:

{{Code|lang=cpp|code= PyAPI_FUNC(int) PyArg_Parse (PyObject , const char , ...); PyAPI_FUNC(int) PyArg_ParseTuple (PyObject , const char , ...); PyAPI_FUNC(int) PyArg_ParseTupleAndKeywords (PyObject , PyObject , const char *, char *, ...); PyAPI_FUNC(int) PyArg_VaParse (PyObject , const char , va_list); PyAPI_FUNC(int) PyArg_VaParseTupleAndKeywords (PyObject , PyObject , const char , char **, va_list); }}

Links

  • Exposing Cplusplus to Python
  • Commit 20b86e5, exposing OCC\'s precision methods to Python

⏵ documentation index > Developer > Developer Documentation > Wrapping a Cplusplus class in Python

This page is retrieved from https://github.com/FreeCAD/FreeCAD-documentation/blob/main/wiki/Wrapping_a_Cplusplus_class_in_Python.md

Get in touch!
Forum GitHub Mastodon Matrix IRC Gitter.im Discord Reddit Twitter Facebook LinkedIn

© The FreeCAD Team. Homepage image credits (top to bottom): ppemawm, r-frank, epileftric, regis, rider_mortagnais, bejant.

This project is supported by: , KiCad Services Corp. and other sponsors

GitHubImprove this page on GitHub