Hi all,
I've had some progress with my material code, and have some code to share now. Please notice that this is in the very early stages. The code is at
https://github.com/eivindkv/free-cad-code/tree/Material , almost rebased to most recent master.
Things more or less done:
- Added a MaterialDatabase class. This is the collection of various sources described below. Material queries happens from this database.
- Added a DefaultMaterialSource class. This source is read-only and contains the original pre-defined materials that could be selected in FreeCAD. It also contains a DEFAULT material, that all other materials can inherit from, to ensure that visual properties are defined.
- Added a DocumentMaterialSource class. Per-document materials are stored here. If visualization materials are changed, a copy is stored here.
- Added a FileMaterialSource class. FCMat materials cards are provided by this source. Multiple sources may be added (system/user).
- Added a PropertyPartMaterial class. This class and its helper classes encapsulates materials for solids and surfaces.
- Added a PropertyPartMaterial member Material to Part::Feature, along with its Shape.
I've been focusing on the data structure part now; almost nothing is done on the Gui part, except propagating material colors from the Material property to the view provider, so the colors of the parts change.
I have also added a Python interface (this is what I actually use for testing), and a typical use-case could be:
App.ActiveDocument.Box.Material.setSolidMaterial(0, "PLA");
This will assign the PLA material to solid 0 of Box. Properties can then be queried, e.g
App.ActiveDocument.Box.Material.Solid[0].YoungsModulus
Surface materials are accessed as e.g
App.ActiveDocument.Box.Material.Solid[0].setSurfaceMaterial(0, "GOLD")
(provided that Solid[0] has been set first).
The material databases are accessed either at App.getMaterialDatabase() or doc.MaterialDatabase (ideally they should have the same syntax; something for later). The material database in App is the application-wide database, so adding/removing/editing materials should be possible to do without an open document. The material database in the document links to the application-wide one, but also provides a private material source for materials specific for the document.
Please let me know what you think.
Eivind