Internal server for FreeCAD

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
chrisb
Veteran
Posts: 53922
Joined: Tue Mar 17, 2015 9:14 am

Re: Internal server for FreeCAD

Post by chrisb »

dcapeletti wrote: Wed Jun 30, 2021 8:03 pm I think I am on the right track and I have good news.
Have you seen this: https://forum.freecadweb.org/viewtopic. ... 36#p513636 ?
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
User avatar
dcapeletti
Posts: 504
Joined: Wed Jul 23, 2014 2:27 pm

Re: Internal server for FreeCAD

Post by dcapeletti »

I am not looking to create a viewer, but to look for features in FreeCAD that serve to control the hardware from the model. This is aimed more at IOT or Digital Twins industries, home automation, robotics where it is important that the 3D model reflects the state of the hardware and processes...
I aim that FreeCAD can serve as a man-machine interface, receiving and sending data to represent hardware and process states in the 3D view.
ickby
Veteran
Posts: 3116
Joined: Wed Oct 05, 2011 7:36 am

Re: Internal server for FreeCAD

Post by ickby »

To run a server or client directly in freecad you need to make it asyncronous, as otherwise you would block FC with long running or even blocking network operations. This means you need an eventloop. The problem is, that freecad ui already has one, the Qt event loop, and you cannot run a second one. So you have two options:
1. Directly use Qt for all networking stuff, see https://doc.qt.io/qt-5/qtnetwork-index.html

2. Drive a python event loop from the Qt event loop. This can be done for example with the Standart asyncio event loop and this little code: https://github.com/CabbageDevelopment/qasync. With this you could use every server/client implementation which is based on asyncio. Im pretty sure you could also drive the twisted ever loop from Qt, but I never tried it myself.
dead.hau5
Posts: 3
Joined: Sat May 29, 2021 1:56 pm

Re: Internal server for FreeCAD

Post by dead.hau5 »

dcapeletti wrote: Wed Jun 30, 2021 8:03 pm
chrisb wrote: Sun Jun 27, 2021 7:40 pm This makes perfectly sense, and in that way it is indeed like like a chat program which is nothing more than a communication platform. And in FreeCAD it is geometric information that is communicated.
I think I am on the right track and I have good news. I present the progress I have made. I have a FreeCAD non-blocking multithreaded socket server, now several clients can connect and enter and query freecad model information without problems. I have also added a model observer to report when something changes.

Here is the working video in Spanish https://www.youtube.com/watch?v=Ta3nfMsbhDE
The model is a led, 2 clients connect to the freecad server and change or query the status of the model. In turn freecad can also notify if someone changes the model.

In a couple of days I will share the repo. I hope you like it.

Greetings and thanks
The FreeCAD communication you've displayed is amazing. Can you please elaborate on the querying and the model observer methods? What utilities did you use in FreeCAD to accomplish this?
I am working on a project similar to this, which is to accomplish 3D scripting using Smalltalk code. I can send commands to FreeCAD via sockets, but the Python-executing functions in FreeCAD does not return any output except errors. Your methods would greatly help me in extracting model information back.

Thanks in advance.
User avatar
dcapeletti
Posts: 504
Joined: Wed Jul 23, 2014 2:27 pm

Re: Internal server for FreeCAD

Post by dcapeletti »

[/quote]
The FreeCAD communication you've displayed is amazing. Can you please elaborate on the querying and the model observer methods? What utilities did you use in FreeCAD to accomplish this?
I am working on a project similar to this, which is to accomplish 3D scripting using Smalltalk code. I can send commands to FreeCAD via sockets, but the Python-executing functions in FreeCAD does not return any output except errors. Your methods would greatly help me in extracting model information back.

Thanks in advance.
[/quote]

See https://gitlab.com/dcapeletti/freecad-server
There you can find in the demo folder, the sample files and the source code.
The main thing is in the macro. I have created a document observer and a socket-based protocol. When the client or the model changes a value, it changes the state of the model.
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Internal server for FreeCAD

Post by wmayer »

dcapeletti wrote: Sun Jun 27, 2021 5:43 pm What I am trying to do is to have freecad give you answers to customer questions. For example, tell me the width and length of the cube.
The problem is not the web server but the function PyRun_String. When executing a string it either returns NULL to indicate an error or PyNone but it doesn't return the value of a request like

Code: Select all

App.ActiveDocument.ActiveObject.Mesh.Volume
However, there is a trick to also get the value of a request but the command must be slightly changed to

Code: Select all

value = App.ActiveDocument.ActiveObject.Mesh.Volume
value will now be an attribute of the __main__ module and this can be accessed to retrieve the actual value.

When applying this trick to the client/server communication the server needs to know when it should check the __main__ module. So, I decided that a client must add "GET " at the beginning of a request. Then internally the server will pass "GET = ..." to the Python interpreter and afterwards reads in __main__.GET.

The client() function from the linked forum thread must be changed to:

Code: Select all

def client(ip, port, message):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((ip, port))
    try:
        sock.sendall(message)
        response = sock.recv(1024)
        return response
    finally:
        sock.close()
git commit 5387e12e80
User avatar
dcapeletti
Posts: 504
Joined: Wed Jul 23, 2014 2:27 pm

Re: Internal server for FreeCAD

Post by dcapeletti »

Hi, I downloaded the weekly-builds version and I confirm that it works! But apparently it does not work, when I start FreeCAD with -c in console mode the server does not respond to the client.

Regards
freedman
Veteran
Posts: 3440
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: Internal server for FreeCAD

Post by freedman »

I don't know if this makes any difference to your coding but could you please presume that someone will want to remove something like this later. I want someday for FreeCAD to go into the school system and stand alone operation is a big advantage. I plan to remove all internet connection code and anything that connects machines.
Thank you
User avatar
dcapeletti
Posts: 504
Joined: Wed Jul 23, 2014 2:27 pm

Re: Internal server for FreeCAD

Post by dcapeletti »

freedman wrote: Thu Oct 14, 2021 5:27 am I don't know if this makes any difference to your coding but could you please presume that someone will want to remove something like this later. I want someday for FreeCAD to go into the school system and stand alone operation is a big advantage. I plan to remove all internet connection code and anything that connects machines.
Thank you
Hello, you can create your FreeCAD branch and delete what you don't want. Anyway it is less work to block FreeCAD in the firewall so that it cannot create connections or receive requests from outside. That way you will be 100% sure that there is no code out there that can create random connections.

Regards
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Internal server for FreeCAD

Post by wmayer »

dcapeletti wrote: Wed Oct 13, 2021 7:06 pm Hi, I downloaded the weekly-builds version and I confirm that it works! But apparently it does not work, when I start FreeCAD with -c in console mode the server does not respond to the client.

Regards
The client/server communication also requires a running event loop because otherwise Qt's signal/slot mechanism doesn't work. I have to check where to start an event loop...
Post Reply