How to separate GUI and Core in FreeCAD

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
sourcedigger
Posts: 5
Joined: Mon Jul 21, 2014 6:45 am

How to separate GUI and Core in FreeCAD

Postby sourcedigger » Mon Jul 21, 2014 8:30 am

Dear All,

Can anybody tell me how FreeCAD managed to separate GUI and Core? That Core can do anything independently and GUI can update itself automatically? Or any architecture of FreeCAD would be appreciated.

Thanks,
SD
wmayer
Site Admin
Posts: 14645
Joined: Thu Feb 19, 2009 10:32 am

Re: How to separate GUI and Core in FreeCAD

Postby wmayer » Mon Jul 21, 2014 10:19 am

The kernel system of FreeCAD is split into three parts:

FreeCADBase: This mainly consists of mathematical foundation classes (for vectors, matrices, ...), the unit system and the integration of the Python interpreter
FreeCADApp: This includes the handling of documents and the objects it manages (DocumentObject is the base class) on application logic
FreeCADGui: This includes the graphical representation of the document in FreeCADApp and the objects of it (ViewProvider is the base class)

Extension modules:
FreeCAD has many extension modules (written in C++ or Python or mixed) and they all follow the idea that application logic is split from GUI logic

Your 2nd question is targeted on how does the update of the visual representation in GUI work. This is done by implementing the observer pattern. Technically we use the signal concept of the boost C++ library.

Modifying an object in FreeCADApp in the first place means that a property (see App::Property and subclasses) has been changed. This directly informs the parent object (DocumentObject) which forwards the change to the document (App::Document). Now if the Gui of FreeCAD is loaded the App::Document directly informs its counterpart on GUI-side (Gui::Document) about which object and which property of the object has changed. The Gui::Document now informs the appropriate ViewProvider about that change. The view provider now is able to update the visual representation by updating the nodes of the scene graph.

That's the basic design.
sourcedigger
Posts: 5
Joined: Mon Jul 21, 2014 6:45 am

Re: How to separate GUI and Core in FreeCAD

Postby sourcedigger » Tue Jul 22, 2014 11:26 pm

Great, very detail.

Now I have new questions. Why are two documents needed? App::Document and Gui::Document? The advantage of using boost::signal is that you don't have to inherit from Observer/Observable, right?

A much further question is if it is possible to replace GUI side with another GUI toolkit quickly? say wxWidgets? If it is possible, what is necessary to do during the transmission.

Thanks a lot.
SD
jmaustpc
Posts: 9558
Joined: Tue Jul 26, 2011 6:28 am
Location: Australia

Re: How to separate GUI and Core in FreeCAD

Postby jmaustpc » Wed Jul 23, 2014 4:54 am

sourcedigger wrote:is possible to replace GUI side with another GUI toolkit quickly?
It is theoretically possible, but really it would be a huge job....have a look at the code and see how much relates to QT. You would want a very good reason to change GUI tool kits, QT is appropriately licensed, mature, cross platform.....

I think most of us would think there are far more productive things to be spending time on in FreeCAD than wasting the huge effort required in changing the GUI tool kit, for no (or little) benefit.

Jim
sourcedigger
Posts: 5
Joined: Mon Jul 21, 2014 6:45 am

Re: How to separate GUI and Core in FreeCAD

Postby sourcedigger » Wed Jul 23, 2014 5:54 am

Dear wmayer and jim,

Recently I want to start a new project which should have a similar GUI design. I am very worried about GUI part. Since the GUI part in our old code is a nightmare. That's why I am thinking if it is possible to make every module independent and that's why I am looking into FreeCAD. However I found FreeCAD architecture is quite complicated. are there any documents about architecture of FreeCAD besides source code?

A minor question, why using boost::signal not observable/observer pattern directly?

Any design thoughts would be extremely appreciated.

Thanks,
SD
jmaustpc
Posts: 9558
Joined: Tue Jul 26, 2011 6:28 am
Location: Australia

Re: How to separate GUI and Core in FreeCAD

Postby jmaustpc » Wed Jul 23, 2014 6:27 am

sourcedigger wrote:Recently I want to start a new project which should have a similar GUI design. I am very worried about GUI part. Since the GUI part in our old code is a nightmare.
Oh, I see, now I can understand why you asked your questions.
sourcedigger wrote:why using boost::signal not observable/observer pattern directly?


That's a question for someone else my knowledge of FreeCAD code is only skin deep at best ..........so if FreeCAD were a person perhaps I would be a dermatologist (or maybe only a dermatologist's receptionist :) ) conversely Werner and others would be the brain surgeon etc.. :)

Out of curiosity, what is your "other" project and is it open source?

Jim
wmayer
Site Admin
Posts: 14645
Joined: Thu Feb 19, 2009 10:32 am

Re: How to separate GUI and Core in FreeCAD

Postby wmayer » Wed Jul 23, 2014 8:25 am

Now I have new questions. Why are two documents needed? App::Document and Gui::Document?
The Gui::Document doesn't provide that much functionality. It's more like a container to manage the ViewProviders. All the important logic like adding and deleting objects, undo/redo, recompute of the objects is done in the App::Document. And again, Gui::Document is needed to split the visual representation from application logic.
The advantage of using boost::signal is that you don't have to inherit from Observer/Observable, right?
Yes. With boost::signal it's sufficient to just add the (signal) methods you need and you are able to temporarily block a signal if needed. From functionality point of view boost::signal is similar to Qt's signal/slot system (but technically they are totally different).
A much further question is if it is possible to replace GUI side with another GUI toolkit quickly? say wxWidgets?
Quickly? For sure not. And we do not only use Qt in the GUI but also its non-GUI libraries (QtCore, QtXml, ...) in some of our App modules.
If it is possible, what is necessary to do during the transmission.
It is possible but not realistic. Trying to replace Qt with wxWidgets more or less means to rewrite FreeCAD from ground up.
Since the GUI part in our old code is a nightmare.
In this case it might be better to port your application to the FreeCAD platform.
However I found FreeCAD architecture is quite complicated.
Complicated? IMO, the architecture of FreeCAD is quite simple. At the first view it seems complicated for somebody who was never involved in the project but when looking deeper into the source code it should become obvious that's kept rather simple. And again, the iron rule in our architecture is to separate App from Gui logic.

Btw, if you ever try to understand the OCAF framework of OpenCascade (that's the application logic of the CAD kernel we use) then you know what "complicated" means.

Anyway, if you try to port your old application to FreeCAD then have a look at the developer section of our Wiki how to start a new application module. There you should find information about writing your own sub-classes of DocumentObject and their ViewProviders, possibly your own Property sub-classes and a custom workbench with all the toolbars and menus. For convenience you can run the Python script fcbt.py to create the skeleton of a new module.
A minor question, why using boost::signal not observable/observer pattern directly?
See above.