Core Objects: Inheritance vs ECS

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
Wesxdz
Posts: 3
Joined: Mon Jul 12, 2021 4:10 pm

Core Objects: Inheritance vs ECS

Post by Wesxdz »

Hello FreeCAD developers!
I am a game engine programmer and just wanted to start a discussion on FreeCAD's core architecture: mainly considering inheritance vs ECS (entity component system like https://github.com/SanderMertens/flecs or https://github.com/skypjack/entt).

Coming from Unreal Engine, I am well-versed in complex inheritance based organization such as the https://docs.unrealengine.com/4.26/en-U ... Framework/, which introduces a functionally similar hierarchy suited to gameplay programming. Working on a AAA game, I learned how poorly it can scale for large teams or sophisticated requirements.

As FreeCAD grows more complex and integrated with other tools I think it would make sense to use ECS rather than inheritance for core objects.
Has anyone in the past written about or tried ECS based work in FreeCAD, do other developers have opinions on this? I might be open to experiment with developing a branch of this in the future if it makes sense.

Image
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: Core Objects: Inheritance vs ECS

Post by carlopav »

Hello and welcome to the forum!
Maybe you are interested in having a look at the extension system developed by Ickby?
follow my experiments on BIM modelling for architecture design
User avatar
Wesxdz
Posts: 3
Joined: Mon Jul 12, 2021 4:10 pm

Re: Core Objects: Inheritance vs ECS

Post by Wesxdz »

https://forum.freecadweb.org/viewtopic.php?t=16021
Extensions huh? Feels like it's designed to tack on to the objects for workarounds via multiple inheritance rather than addressing the core underlying complexity imo (no shade on Ickby for the effort).

Intriguing that FreeCAD has a custom Type class, seems a useful artifact given the sad state of reflection in C/C++. I've written a bespoke type system in the past but found it was a disaster once I started trying to hot reload the creation of new C++ structs at runtime (led me to considering https://clang.llvm.org/docs/Tooling.html related solutions). I'm sure it's reasonable for Python scripting though.
ickby
Veteran
Posts: 3116
Joined: Wed Oct 05, 2011 7:36 am

Re: Core Objects: Inheritance vs ECS

Post by ickby »

Extensions are designed to have certain functionalities addable to objects. It is deliberately a construction time mechanism, not a runtime one, as this is intended for more versatile type creation. For this is see no difference between multiple inheritance or ECS, just that inheritance fits better in the existing style. (But I'm not really deep into ECS)

Apart from that, there is currently at some places a shift towards duck typing ongoing, which works due to the dynamic property system of freecad, e.g. you can add properties at runtime.

In general you have to consider a massive codebase, and if the benefit of a different architecture really is worth the effort. For freecad the current type system worked well, as it also reflects what users are used to in cad program workflows. Sure it has its limitations and nasty workarounds, but which architecture hasn't :)

What benefit would you expect?
User avatar
Wesxdz
Posts: 3
Joined: Mon Jul 12, 2021 4:10 pm

Re: Core Objects: Inheritance vs ECS

Post by Wesxdz »

The advantage of ECS here is that it solves the programmer needs of dynamic typing while retaining the safety, performance, and pre-compile intellisense of static typing. The cannonical introduction is http://cowboyprogramming.com/2007/01/05 ... -heirachy/
Moving logic from OOP objects to systems that act on tuples of component types is a magical and cathartic feeling. :D
I understand the perpetual legacy of these large codebases: how they become resistant to change as they increase in mass. ECS, I think solves a lot of the modularity issues in a nice way.
I have no doubt that the best CAx applications in the world a decade from now will use ECS or evolutions of that framework.
I'll study the codebase more in depth in future months before embarking on any sort of architecture experimentation. @ickby I saw your frameless UI and really liked that (but more broadly I want multiple documents open for editing simultaneously, ie to visualize git repo linked composition of parts as graph nodes). For example, building a house in FreeCAD at Open Source Ecology it is ambiguous what the 'base unit' should be. Raw material like 2xN" lumber? Multiple 2xN App:Link, each cut to a specific length vs a single parametric cut? A house module composed of the underlying materials? These issues get complex once you consider longevity and collaboration.

Pretty much the most user-friendly precedent for this sort of thing is a nested prefab variant in Unity https://docs.unity3d.com/Manual/PrefabVariants.html (that's not quite ECS yet because it doesn't use systems): well that requires a better external editor integration and powerhouse UI workflow. I for one don't understand why open source projects can be so insistent on application contained scripting (Use Godot editor to write GDScript, use FreeCAD console to write macro) Cmon guys I want my custom fonts and color theme in VSCode... Tree drag and drop functionality is not even merged in .20 yet https://github.com/FreeCAD/FreeCAD/pull/4626 :cry:
User avatar
adrianinsaval
Veteran
Posts: 5551
Joined: Thu Apr 05, 2018 5:15 pm

Re: Core Objects: Inheritance vs ECS

Post by adrianinsaval »

You don't need to edit macros in FreeCAD, you can edit in whatever you want. Recently I had an exchange with drmacro where he mentioned he used VSCode and I asked how he set it up for debugging: https://forum.freecadweb.org/viewtopic. ... 36#p516536
Post Reply