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!
User avatar
dcapeletti
Posts: 504
Joined: Wed Jul 23, 2014 2:27 pm

Internal server for FreeCAD

Post by dcapeletti »

Hi, I am trying to create a kind of chat like this https://learningactors.com/creating-com ... ng-python/ for several clients to communicate with FreeCAD and send commands or for FreeCAD to send information to the connected clients.

But when I create a macro and run the server code, FreeCAD freezes.

Does anyone have any working code to create a server in freecad?

Thanks
User avatar
onekk
Veteran
Posts: 6222
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Internal server for FreeCAD

Post by onekk »

Short answer: FreeCAD is not a chat program.

Long answer: what do you want to achieve?

Some people have in mind a "similar" use like to create a web page that use FreeCAD as a visualizer motor, like similar thing done for OpenSCAD and so on.

There were some other discussion using FreeCAD as a library and maybe a "visualization library", so maybe searching for them will address you in something near your needs.

But using FreeCAD in this manner would mean to have a server that run a proper Linux distribution, as FreeCAD is not a plain program and I doubt that installing a whole distribution on a virtual machine that run a Linux distribution with FreeCAD would be a simple thing.

I'm not an expert of such things, but others maybe could read your post and do some usefult hints.

So some other informations are needed to do a proper answer to your question.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Internal server for FreeCAD

Post by openBrain »

@wmayer published an old post where he did run a server that can handle commands and replies. I can't find it ATM but sure it exists.
User avatar
dcapeletti
Posts: 504
Joined: Wed Jul 23, 2014 2:27 pm

Re: Internal server for FreeCAD

Post by dcapeletti »

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.

I came from this thread trying to get freecad to respond to the client https://forum.freecadweb.org/viewtopic.php?f=22&t=59669 but from what I was researching, the web module server can only receive instructions to create or update the model but does not return model responses. Here https://forum.freecadweb.org/viewtopic. ... 92#p184692 the client only sends commands to FreeCAD but how do I query and receive model information in the client?

Another example is this https://forum.freecadweb.org/viewtopic. ... 98#p382798 allows you to create an external editor, you can execute commands to create something or modify in the model, but the client cannot make any query to FreeCAD about the model, for example to know the width, length or height of a cube.

For this very reason, I was considering creating some sort of server that has the ability to receive instructions to create or modify the model and at the same time and return the response to the client about what they want to query about the model.

Regards
chrisb
Veteran
Posts: 54273
Joined: Tue Mar 17, 2015 9:14 am

Re: Internal server for FreeCAD

Post by chrisb »

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.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: Internal server for FreeCAD

Post by heda »

dcapeletti wrote: Sun Jun 27, 2021 1:00 pm But when I create a macro and run the server code, FreeCAD freezes.
the fc gui (or any gui for that matter) is an endless (blocking) loop, and so is a server, hence if you start a server, i.e. an endless loop, within another endless loop - it is the last started (assuming that the frameworks allow you to start one) that does not allow anything else to happen.

if one takes a step back it is rather natural with these endless loops,
- a gui is expected to react to user input --> it needs some sort of observer that always runs and reacts to input
- an interactive webpage is also expected to react to user input...

a single thread can only do one task at any given time (like an endless loop),
maybe read up on threading and spawned processes - to know a bit better what you are up against if you want to make this happen.
while you are at it, makes sense to at least know the basics of async code as well.

you are peeking down an alley that is quite tricky, setting up something with very simplistic functionality I do not think would be super complicated.
having a bit more functionality, and keeping up with progress of fc, including library updates, version compatibility over time, etc,
is likely to be prohibitive for a one man show, just from a resource perspective, let alone from a knowledge/skill perspective.

with that said, if making an attempt with simplistic functionality I would do that with sockets, and the protocol for the socket would just be something similar to the current "Gui.runCommand('make cube')", but of course headless so 'FC.runCommand('make cube')', so maybe something along the lines of "FCWeb.runCommand('make cube')". The protocol for the socket including actions and response to actions both on the client and server side would basically have to be created from scratch, that includes the code to make the headless fc/web page to do anything as well, and that is a daunting task to get done unless one limits the functionality drastically.

In short, it would be analogue to creating you own flask and SQL-alcemy, or react, but instead of the regular web-things it would handle fc.

there are other approaches as well, https://forum.freecadweb.org/viewtopic. ... 73#p427273,
none of them are easy to get to a useful state - which likely is the reason why none of them really exists.

if one would go for something long-lasting, I would put the efforts into a "neutral" protocol over a socket connection,
at least then one can let the client and server side, as well as fc, live their own lives and have their own dependencies evolve independently,
giving it a greater chance of finding people willing to maintain and develop functionality piece by piece (not much different from exposing c-functionality of fc to python, which is a part of fc architecture as it already is today).
the part of making it more or less on par in functionality (or rather using techniques that allows for theoretical full usage of the applications, with minimal maintenance effort) I think is a must, over time even if the original intention is just to cover a small piece of functionality, the usefulness of that is limited, the wish to extend functionality to close to on par will be there quite quickly and live on forever, so if it is not theoretically possible - the solution will simply not gain traction and end up being a one man show that is only maintained as long as that individual is willing to extend/maintain it.

all the best wishes if you decide to go down this alley.
User avatar
dcapeletti
Posts: 504
Joined: Wed Jul 23, 2014 2:27 pm

Re: Internal server for FreeCAD

Post by dcapeletti »

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
User avatar
dcapeletti
Posts: 504
Joined: Wed Jul 23, 2014 2:27 pm

Re: Internal server for FreeCAD

Post by dcapeletti »

Yes, thanks for the clarification is that I'm a bit rusty with multithreading. Anyway, I have managed to create a multithreaded socket server without GUI locking.

I agree to create socket server with a simplistic protocol and implement it http verb style, I think it is a way. The client is independent and can be implemented as you like.

I am just doing proof of concept to see if this could work for IOT or Digital Twins where the 3D model needs to represent the reality of the hardware and processes. In this case I did something bidirectional, between freecad and clients connected over a socket. FreeCAD has an observer that monitors model status and updates connected clients. See https://youtu.be/Ta3nfMsbhDE

This can be developed much more, in a few days I will publish the repository and I hope it will serve to open doors and add to the development.

Regards
Last edited by dcapeletti on Wed Jun 30, 2021 9:37 pm, edited 1 time in total.
chrisb
Veteran
Posts: 54273
Joined: Tue Mar 17, 2015 9:14 am

Re: Internal server for FreeCAD

Post by chrisb »

Please don't quote whole posts, but only the parts you are directly referring to. Reading such long posts again is tedious.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
Post Reply