How did FreeCAD combine C++ with Python?

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
YangGang
Posts: 3
Joined: Wed Aug 25, 2021 1:35 am

How did FreeCAD combine C++ with Python?

Post by YangGang »

How did FreeCAD combine C++ with Python? I see a lot of functions of PyRun_string,Py::Module,etc. I can't understand how to debug FreeCAD.
I am going to watch the running conditions of python code in FreeCAD source. How can I step into these python code? My enviroment is ubunutu20.04.
Thanks for any answer!
wmayer
Founder
Posts: 20319
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: How did FreeCAD combine C++ with Python?

Post by wmayer »

How did FreeCAD combine C++ with Python?
There are two concepts how to combine C++ and Python

Embedding:
Embedding is when you use Python functionality in C++ code. This can be e.g. done with PyRun_String where you execute Python code in a string representation. Or you can also use all the functions of Python's C API.

The classes that exist in the name space Py:: is taken from another project called PyCXX that implements a C++ wrapper around most of the functions of the C API. This simplifies life a lot because you have to not worry too much about incrementing or decrementing the reference counter of the Python objects.

Extending
This is so to speak the opposite of embedding. When you have working a C++ function or class then you can expose it to Python so that in Python code you can invoke the C++ code.
There exist many ways how to expose C/C++ to Python. There is e.g. the wrapper generator framework called SWIG, the boost.python library, the pybind11 template library, also PyCXX or you can do everything directly with Python's C API.
I can't understand how to debug FreeCAD.
I am going to watch the running conditions of python code in FreeCAD source.
AFAIK you can use VisualStudio Code to debug C++ and Python code but I never tried that.

Since v0.19 FreeCAD offers a way to attach itself to a Python debugger. Currently supported are winpdb and VS Code. E.g. winpdb is available for all major platforms. Since you are on Ubuntu you should give it a try
falviani
Posts: 253
Joined: Tue May 07, 2019 8:49 pm

Re: How did FreeCAD combine C++ with Python?

Post by falviani »

Hello all,

Here is my configuration:
OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.20.25739 (Git)
Build type: Release
Branch: master
Hash: 1ceaa09d179832c5fd785ff7e6c24c25ec76ce61
Python version: 3.8.6+
Qt version: 5.15.2
Coin version: 4.0.1
OCC version: 7.5.0
Locale: English/United States (en_US)

How do I attach a FreeCad instance to the VS Code python debugger? I'm using the default python debugger.

I build my copy of FreeCad itself locally from source pulled from master, and generate release PDB files. I am trying to develop a workbench in Python to generate tabbed boxes, focused on cutting them with a CNC; the intent is to use the Path workbench facilities because the diameter of the cutting tool needs to be accounted for when creating the outlines. (All existing tools I've seen or used are for a laser cutter and assume the cutting tool has a 0 diameter).

My skeleton is based on the workbench starter kit from Joel Graff. The project folder is in the mod/ directory so that FreeCad should see it on startup.

I've spent significant time searching the forum for help with this. The overall documentation lists external workbenches but provides no information on how to develop them. If I can get a functional workflow running I'd be happy to try and document that if the community thinks it would be useful.

I'm feeling a bit desperate at this point. :(

Thanks in advance,
Frank
User avatar
Roy_043
Veteran
Posts: 8578
Joined: Thu Dec 27, 2018 12:28 pm

Re: How did FreeCAD combine C++ with Python?

Post by Roy_043 »

Have you seen Debugging?
User avatar
ebrahim raeyat
Posts: 625
Joined: Sun Sep 09, 2018 7:00 pm
Location: Iran
Contact:

Re: How did FreeCAD combine C++ with Python?

Post by ebrahim raeyat »

You can look at my test for my punch software:

https://github.com/ebrahimraeyat/Civil/ ... _report.py

you can use vscode for debugging then, without any configuration.
Post Reply