Developer Feature: Extensions
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
Developer Feature: Extensions
Hello developers,
Just now the new Extension mechanism has been merged into FreeCAD. It has been discussed here, documentation for developer can be found in the source code of ExtensionContaienr and Extension
It opens up new possibilities for coding in FreeCAD and hence I like to introduce the new concept here. An extension is a FreeCAD Object much like every other, it has properties and methods. The difference: An extension is not used itself as object, but it is "added" to annother FreeCAD object. Than all properties and methods of the Extension become part of the extended object in a natural and intuitive manner.
What does that mean? For example now Groups are an extension. So everthing can become a group. If you want to make a Part::Feature also behave like a group just add the Group Extension to it, and than your new Type exposes a "Shape" property as well as a "Group" property. This works from c++ and python.
How does that change FreeCAD coding?
1. This allows to not only use linear inheritance as of now. With extensions Objects can implement multiple interfaces. So if you like to provide a interface of some kind which may be usefull for many objects just make it an extension!
2. It allows to create interfaces that can be used and overridden in other workbenches you do know nothing about. For example one could think that the Drawing workbench implements an Extension "Drawable" which does the Object specific drawing. Than Part::Feature would use that extensions and does the specific drawing, Mesh would do it also, and everyone that wants to have its objects on a drawing just uses the extension and overrides the methods. You see where this is going to: It reverses the Workbench dependencies: Now your workbench muust not include Part and Mesh, but Part and Mesh can include yours. That gives much flexibility.
Extensions can only be created in c++, but are usable and overridable from Python and c++. Most likely we will find missing things in the code when implementing new extensions, but we are free to extend the Extension implementation, just start a discussion about it!
I hope you all find it usefull
Just now the new Extension mechanism has been merged into FreeCAD. It has been discussed here, documentation for developer can be found in the source code of ExtensionContaienr and Extension
It opens up new possibilities for coding in FreeCAD and hence I like to introduce the new concept here. An extension is a FreeCAD Object much like every other, it has properties and methods. The difference: An extension is not used itself as object, but it is "added" to annother FreeCAD object. Than all properties and methods of the Extension become part of the extended object in a natural and intuitive manner.
What does that mean? For example now Groups are an extension. So everthing can become a group. If you want to make a Part::Feature also behave like a group just add the Group Extension to it, and than your new Type exposes a "Shape" property as well as a "Group" property. This works from c++ and python.
How does that change FreeCAD coding?
1. This allows to not only use linear inheritance as of now. With extensions Objects can implement multiple interfaces. So if you like to provide a interface of some kind which may be usefull for many objects just make it an extension!
2. It allows to create interfaces that can be used and overridden in other workbenches you do know nothing about. For example one could think that the Drawing workbench implements an Extension "Drawable" which does the Object specific drawing. Than Part::Feature would use that extensions and does the specific drawing, Mesh would do it also, and everyone that wants to have its objects on a drawing just uses the extension and overrides the methods. You see where this is going to: It reverses the Workbench dependencies: Now your workbench muust not include Part and Mesh, but Part and Mesh can include yours. That gives much flexibility.
Extensions can only be created in c++, but are usable and overridable from Python and c++. Most likely we will find missing things in the code when implementing new extensions, but we are free to extend the Extension implementation, just start a discussion about it!
I hope you all find it usefull
Re: Developer Feature: Extensions
This is a great... Still a bit nebulous for me to fully understand the full thing, specially the "now it's Part and Mesh that can include your WB" but cases like "A Part that behaves like a Group" is a scenario that we meet more and more. Thanks ickby!
Re: Developer Feature: Extensions
Imagine you build a measuring workbench which should calculate the weight of all objects in the document. The problem arises that different objects need to be handled differently. Up to now this meant your workbench would need to import for example Part and Mesh workbench and to check if a object is of given type and hande it accordingly. This means you need to know all objects you are going to handle.yorik wrote:Still a bit nebulous for me to fully understand the full thing, specially the "now it's Part and Mesh that can include your WB" but cases like
Now you could now create a Extension "MeasureExtension" in your workbench which has a method "calculateWeight()". Now when calculating the overall weight you just iterate all objects and check if they have the "MeasureExtension", and if so retrieve the extension and use "calculateWeight". You do not need which object this really is, you only need to know that it is a DocmentObject. It could be from any workbench, even if it is one that you do not know as it is a user workbench.
This would mae sense for very central workbenches that provide basic functionality like drawing or measuring. Those central workbenches can't know all existing types and handle them, hence it makes sense to let the other workbenches extend the basic ones via extensions, as they know that "drawing" exists and can import it for using the extension.
Re: Developer Feature: Extensions
Sounds interesting and i guess C++ developers will be able to achieve things currently not possible. What you might want to do in the future is provide simple working example on the Wiki.
Maybe once you will use it yourself somewhere in the code or if you already have simple example available it could be documented?
Maybe once you will use it yourself somewhere in the code or if you already have simple example available it could be documented?
Re: Developer Feature: Extensions
It sounds like a very general and yet simple and very useful approach. Still trying to understand ...
Are these extensions for Python what are interfaces in Java? I.e. kind of a contract with the outside world to offer certain function calls?
Are these extensions for Python what are interfaces in Java? I.e. kind of a contract with the outside world to offer certain function calls?
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
Re: Developer Feature: Extensions
Will be then the actual implementation of the MeasureExtension of a Part object in the Part module and of a Mesh object in the Mesh module, respectively?Now you could now create a Extension "MeasureExtension" in your workbench which has a method "calculateWeight()".
Re: Developer Feature: Extensions
The Part and Mesh Workbench would import the measure workbenches. Their objects would use the provided Extension and override the "calculateWeight" function. Hence the implementation Details would be in those workbenches.wmayer wrote:Will be then the actual implementation of the MeasureExtension of a Part object in the Part module and of a Mesh object in the Mesh module, respectively?Now you could now create a Extension "MeasureExtension" in your workbench which has a method "calculateWeight()".
-
- Veteran
- Posts: 2190
- Joined: Tue Jan 03, 2017 10:55 am
Re: Developer Feature: Extensions
Hi, some suggestion for the extension. When I am playing around with my new C++ FC feature, I find myself in need of something that is dynamically pluggable into the existing ViewProvider. I find that the new extension architecture is a reusable concept. But right now it is not very dynamic on the C++ side. Can we improve on that? I am already able to kind of achieve what I need, by declaring my extension as 'isPythonExtension', so it gets auto deleted when the container is destroyed. I think the easiest things we can do are 1) add a flag to extension to ask for auto delete, 2) add function(s) in container to remove an extension (by name, by type, by pointer value?).ickby wrote:Just now the new Extension mechanism has been merged into FreeCAD.
Re: Developer Feature: Extensions
It should be possible to make it dynamic. It came to my mind too, but never tried it as I don't needed it. I think the main issue you will face is the python interface. Currently an added extension exposes all its methods to the extended object. You need to ensure they get removed again when the extension is removed. This will most likely need some thorough testing.
Re: Developer Feature: Extensions
I have read it and don't understand, but the good thing is I am not the only one who don't understand this
Some examples and tutorials are welcome!
Some examples and tutorials are welcome!