Controlling FreeCAD with python4Delphi

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
Nextjob
Posts: 8
Joined: Mon Dec 30, 2019 7:43 pm

Controlling FreeCAD with python4Delphi

Post by Nextjob »

Kunda1 requested I start a new thread on this subject.
I have been playing with python4delphi to access the python interpreter imbedded in FreeCAD.
The attached screen shots show a sample application program made up of a simple text editor module, the interface module to FreeCAD and the “controlled” FreeCAD application. The sample application was created with the Lazarus IDE and complied with Free Pascal.

Python4delphi provides a connection to the Python dll, it allows for the execution of scripts, creation of python methods and variables all accessible within the Pascal program.

The sample program:
Creates a python module with a method to pass data to the sample application (used with selection observer script).
Executes a script to start FreeCAD
Executes a script to define and add a selection observer (FreeCADGui.Selection.addObserver(s))

Please note my knowledge of FreeCAD objects is very limited and this is a work in progress. The selection observer script is very simple. It assumes it is dealing with edges or points. At this time is does not work well with Faces.

Line Selection
LineSelection.png
LineSelection.png (159.08 KiB) Viewed 1701 times
Arc Selection
ArcSelection.png
ArcSelection.png (163.03 KiB) Viewed 1701 times
Circle Selection
CircleSelection.png
CircleSelection.png (161.47 KiB) Viewed 1701 times
If anyone is interested in the code, send me an e-mail. I will provide a link for download of the Lazarus project.
Last edited by Nextjob on Sat Jan 18, 2020 3:13 pm, edited 1 time in total.
User avatar
mmiscool
Posts: 73
Joined: Sun Dec 18, 2016 9:07 pm
Location: Connecticut
Contact:

Re: Controlling FreeCAD from an application program with Python

Post by mmiscool »

Hello,

This is interesting to me.
Would love to see the code. I saw you were talking about sending emails but is there a github link?
My main project is https://autodrop3d.com
Effort on https://github.com/mmiscool/FC-Docker FreeCAD for cloud with browser based access has been discontinued in favor of focused development on https://github.com/xibyte/jsketcher
Nextjob
Posts: 8
Joined: Mon Dec 30, 2019 7:43 pm

Re: Controlling FreeCAD from an application program with Python

Post by Nextjob »

I had intended to clean up the code before creating a github repository, but here goes.
https://github.com/nextjob/CodeSharkFC
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Controlling FreeCAD from an application program with Python

Post by Kunda1 »

Nextjob wrote: Fri Jan 17, 2020 11:52 pm I had intended to clean up the code before creating a github repository, but here goes.
https://github.com/nextjob/CodeSharkFC
Awesome. Some favors:
Would you please add the link to this forum thread in to the README.md of that repo?
You could also easily drag drop the above screenshots in to the README.md as well.

If you don't mind, can you tweak the title of this thread to be less generic (for example adding python4delphi instread of just python?)

Offtopic: JFYI to both @Nextjob and @mmiscool have you seen:
https://github.com/dliess/FreeCADFrontPanelSimulation
https://forum.freecadweb.org/viewtopic.php?t=29988
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
frozen
Posts: 1
Joined: Tue Feb 18, 2020 3:42 pm

Re: Controlling FreeCAD with python4Delphi

Post by frozen »

Hello,

I was able to compile and run your example with Delphi 10.3. The freecad GUI opens but when I try to load a dxf or even click on "File->New", I get an exception and the GUI finally closes.

Image

I had the same kind of error during my previous attempts to use FreecadGui with Python4Delphi. Do you have any idea on what could be wrong? Thanks.
Nextjob
Posts: 8
Joined: Mon Dec 30, 2019 7:43 pm

Re: Controlling FreeCAD with python4Delphi

Post by Nextjob »

Hi

I just downloaded FreeCAD_0.19.19635_x64_Conda_Py3QT5-WinVS2015.
I had to update:
PythonEngine1 component on form FreeCad.dfm to specify python 3.7
Property: Value
DLLName: python37.dll
RegVersion: 3.7

..\PythonForDelphi\Components\Sources\Core\Definition.Inc to default to Python 3.7
//{$DEFINE PYTHON36}
{$DEFINE PYTHON37}
//{$DEFINE PYTHON38}

The Error verbiage:
- This application failed to start because it could not find or load the Qt platform plugin "windows"
seems to have been replaced with
- This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem
Same solution: copy contents of ..\FreeCAD19\bin\platforms to ..\CodeSharkFCs\platforms

After making the above changes and copying the platform directory everything seems to work with the latest build.

Question:
What version of Delphi are you using? I could not get this to work with earlier version of Delphi 10 (but never had issues with Lazarus/FPC).
My latest build is with Delphi 10.3 Version 26.0.36039.7899 (10.3.3 community edition) with update 3 installed.
Nextjob
Posts: 8
Joined: Mon Dec 30, 2019 7:43 pm

Re: Controlling FreeCAD with python4Delphi

Post by Nextjob »

Hi again,

I forgot to add:
Assuming the JCL packages are installed on your Delphi IDE, you should be able to click the "continue" button on the Debugger Exception Dialog which will display the JCL debug exception dialog. Clicking on details will give you an exception log with additional debug info.
Post Reply