Jump to content
Sign in to follow this  

First Python + DelphiVCL Program

Recommended Posts

after a long time stressing this forum, especially the Python4Delphi channel, with lots of rookie requests,

I got to a good point with the development of my first Python program.


Until a few months ago I had always ignored Python and its possibilities as Delphi has always been a tool

with which I create all my works and I have never thought of anything else.


When Python4Delphi and DelphiVCL showed up I wondered if I could do something interesting with both

and I must admit that although Python was completely new land to me, the fact of sticking with Delphi

anyway took away any doubt. .. I had to try.


Basically, the program is pure Python (after being compiled with Cython), an embedded version, with the addition

of DelphiVCL (I've never used FMX so it's better to start from the VCL that I know very well) and some Python modules

made in Delphi where I put the more delicate parts and in use real threads and not "crippled" threads by the GIL.

I anticipate, it is nothing transcendental, but as a first Python project, I am satisfied with it.

Description of video
In this short video, we can see the execution of an external program written in Python for the holding of print markers

necessary to calculate the zero machining, the rotation of the piece on the work table, and all the scaling needed to

compensate for the error of model printing between CAD and plotter printer.


The Python program interacts directly with the CNC that moves the XYZ axes for the final cut through an API Client (cnc_api_client_core in PyPi)

to the CNC control software API Server, retrieving information and sending direct commands to the CNC System.


Image capture is done using a proprietary IP Camera equipped with LED lighting.

The Python program is executed through an embedded version of the language prepared with all the necessary tools and allows two UI,
vertical and horizontal, to adapt to all types of monitors.


The below CNC Control Software is 100% made with Delphi 🙂

Many Thanks to forum people for the support!

  • Like 3

Share this post

Link to post

Thanks for the insight view.

That looks great, but I'm not sure if I understand right how Delphi and Python interact here.
You say the RosettaCNC is written in Delphi, also handling the video-camera, while the Python code is doing the image processing to find the marker, is that what you showing us here ?


On the other hand you wrote that Python is controlling the CNC directly, but I assume you mean only the move from one zero-marker to the other, or do you mean also the full G-Code of the drawing ?

If the latter: What about the saying that Python is about 10+ times slower, I would expect that it should better not be used for realtime CNC control.

Ok, maybe the CNC controller understands G-Code or the like directly and you do not control the motor steps directly, so that also Python could handle it well.


In combination Delphi and Python makes a lot of sense, also I plan to start my first Python project when I have some spare time.

Share this post

Link to post

Actually, I was a little poor in the description because I thought I shouldn't bore you.

Let's see if I can group the ideas.


1] The IP Camera
The IP camera is made with an embedded board + a motorized camera sensor + Linux.

The IP camera, mounted on the Z axis of the CNC, uses a LAN connection to receive commands (eg: manual focus, brightness, resolution, etc)

and return a stream of encoded and compressed frames.

To do this, it uses the TCP / UDP protocols.
The management software was initially made with Python but then I will calmly rewrite it completely in C ++.

Theoretically, the code is already performing even in Python, as the bottleneck remains the acquisition of the frames from the sensor,

their encoding, and compression, which is already done using native code libraries.

In any case, a large part of the Python project was then compiled with Cython, creating python modules .so that a few percentage points of profit made me get.


2] Delphi and its expansion modules for Python.
Initially, I tried to manage TCP/UPD streams directly in Python via sockets and threads, but unfortunately, Python gives the worst,

as threading management is always subject to the rules of the GIL and therefore in fact creating more threads in python does not always

mean being able to optimize the use of cores.

I will not dwell on this question, which I too was not aware of, there are many discussions on the net.

I, therefore, thought of using Delphi to create an extension module for Python in which I entered all the communication work between PC and

Camera using Delphi's TThread (more practically internally I used INDY both for the TCP Client and for the UDP server)

In python, I no longer needed to create threads for managing TCP/UDP packets with the resolution of many problems that I had had in the first tests.

Still in the Python expansion module written in Delphi, I was able to manage other features not present in ready-made libraries for Python,

but which I already had working fine in Delphi, certainly gaining performances.


3] Python program
The Python program is actually made up of Python + DelphiVCL + Skia + OpenCV and other minor libraries, plus an image processing framework

that I built from scratch in pure python language.

To improve performance, the whole package, apart from the main file, was also compiled with Cython, obtaining a series of .pyd modules that make up the final product.


4] CNC
The CNC is actually made up of an embedded board with a REAL TIME industrial operating system that I wrote years ago and that we have been using for years

for CNC and PLC and all the CNC part is done directly on the board.

The CNC board, which controls the motors/inputs/outputs/EtherCAT /etc, communicates with the PC and with the CNC control software via LAN.

The control software is just a UI interface, has a G-code compiler, and takes care of transferring motion instructions or pre-processed blocks to the CNC board's execution buffer.
In the CNC control software, there is an API server (TCP/Server) which allows an external program/process, through an API Client, to access all the functions of the CNC,

including sending programs, MDI programs, JOG, etc.

So for Python, I have created a package that implements the client core API allowing a Python program to have full control of the CNC.


The program can use Themes.
Without a theme, the assignment of a Bitmap to TImage objects (a Window control) can generate flickering (due to the WM_ERASExxx message).
Using Themes the flickering increase a lot.
So I've created a new DelphiVCL branch with Graphics32:TImgView32 which is a TGraphicControl-based object and solved the flickering phase managing the PAINT event.

Now I've been really boring :)

Share this post

Link to post
16 hours ago, shineworld said:

Now I've been really boring 🙂

Not so much, reading good technical success stories is one of my hobbies :classic_biggrin:

  • Like 2

Share this post

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this