Material: Material handling enhancements

Rework of the material handling system.

This first part concntrates on a rework of the material cards.
Rather than use a fixed list of possible properties, properties can
be defined separately in their own files and mixed to provide a
complete list of possible properties. Properties can be inherited.

The cards then provide values for the properties. These can also
be inherited allowing for small changes in cards as required.

The new property definitions are more extensive than previously.
2 and 3 dimensional arrays of properties can be defined. Values
are obtained by calling an API instead of reading from a dictionary.

For compatibility, a Python dictionary of values can be obtained
similar to how it was done previously, but this is considered a
deprecated API and won't support the newer advanced features.

The editor is completely reworked. It will be able to edit older format
material cards, but can only save them in the new format.

For testing during the development phase, a system preference can
specifiy wether the old or new material editors are to be used. This
option will be removed before release.
This commit is contained in:
David Carter
2023-09-14 22:52:48 -04:00
parent 563dc0b0cc
commit 6624fa3775
397 changed files with 19222 additions and 4312 deletions

View File

@@ -25,19 +25,15 @@ __url__ = "http://www.freecad.org"
import os
from os.path import join
from pathlib import Path
import FreeCAD
import Material
unicode = str
# TODO:
# move material GUI preferences from FEM to an own preference tab in Material
# move preference GUI code to material module
# https://forum.freecad.org/viewtopic.php?f=10&t=35515
# TODO:
# implement method check_material_keys from FEM material task panel for material editor
# may be move out of the FEM material task panel to here
@@ -62,36 +58,127 @@ this has been done already by eivind see
https://forum.freecad.org/viewtopic.php?f=38&t=16714
'''
def get_material_preferred_directory(category=None):
"""
Return the preferred material directory. In priority order they are:
1. user specified
2. user modules folder
3. system folder
"""
mat_prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Material/Resources")
use_built_in_materials = mat_prefs.GetBool("UseBuiltInMaterials", True)
use_mat_from_config_dir = mat_prefs.GetBool("UseMaterialsFromConfigDir", True)
use_mat_from_custom_dir = mat_prefs.GetBool("UseMaterialsFromCustomDir", True)
preferred = None
if use_built_in_materials:
if category == 'Fluid':
preferred = join(
FreeCAD.getResourceDir(), "Mod", "Material", "Resources", "Materials", "FluidMaterial"
)
elif category == 'Solid':
preferred = join(
FreeCAD.getResourceDir(), "Mod", "Material", "Resources", "Materials", "StandardMaterial"
)
else:
preferred = join(
FreeCAD.getResourceDir(), "Mod", "Material"
)
if use_mat_from_config_dir:
user = join(
FreeCAD.ConfigGet("UserAppData"), "Material"
)
if os.path.isdir(user):
preferred = user
if use_mat_from_custom_dir:
custom = mat_prefs.GetString("CustomMaterialsDir", "")
if len(custom.strip()) > 0:
preferred = custom
return preferred
def get_material_preferred_save_directory():
"""
Return the preferred directory for saving materials. In priority order they are:
1. user specified
2. user modules folder
"""
mat_prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Material/Resources")
use_mat_from_config_dir = mat_prefs.GetBool("UseMaterialsFromConfigDir", True)
use_mat_from_custom_dir = mat_prefs.GetBool("UseMaterialsFromCustomDir", True)
if use_mat_from_custom_dir:
custom = mat_prefs.GetString("CustomMaterialsDir", "")
if len(custom.strip()) > 0:
# Create the directory if it doesn't exist
try:
if not os.path.isdir(custom):
os.makedirs(custom)
return custom
except Exception as ex:
print(ex)
pass
if use_mat_from_config_dir:
user = join(
FreeCAD.ConfigGet("UserAppData"), "Material"
)
try:
if not os.path.isdir(user):
os.makedirs(user)
return user
except Exception as ex:
print(ex)
pass
return ""
# ***** get resources for cards ******************************************************************
def get_material_resources(category='Solid'):
resources = {} # { resource_path: icon_path, ... }
# TODO: move GUI preferences from FEM to a new side tab Material
# https://forum.freecad.org/viewtopic.php?f=10&t=35515
mat_prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Material/Resources")
use_built_in_materials = mat_prefs.GetBool("UseBuiltInMaterials", True)
use_mat_from_modules = mat_prefs.GetBool("UseMaterialsFromWorkbenches", True)
use_mat_from_config_dir = mat_prefs.GetBool("UseMaterialsFromConfigDir", True)
use_mat_from_custom_dir = mat_prefs.GetBool("UseMaterialsFromCustomDir", True)
if use_built_in_materials:
if category == 'Fluid':
builtin_mat_dir = join(
FreeCAD.getResourceDir(), "Mod", "Material", "FluidMaterial"
FreeCAD.getResourceDir(), "Mod", "Material", "Resources", "Materials", "FluidMaterial"
)
else:
builtin_mat_dir = join(
FreeCAD.getResourceDir(), "Mod", "Material", "StandardMaterial"
FreeCAD.getResourceDir(), "Mod", "Material", "Resources", "Materials", "StandardMaterial"
)
resources[builtin_mat_dir] = ":/icons/freecad.svg"
if use_mat_from_modules:
module_prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Material/Resources/Modules")
module_groups = module_prefs.GetGroups()
for group in module_groups:
module = module_prefs.GetGroup(group)
module_mat_dir = module.GetString("ModuleDir", "")
module_icon_dir = module.GetString("ModuleIcon", "")
if len(module_mat_dir) > 0:
resources[module_mat_dir] = module_icon_dir
if use_mat_from_config_dir:
config_mat_dir = join(
FreeCAD.ConfigGet("UserAppData"), "Material"
)
resources[config_mat_dir] = ":/icons/preferences-general.svg"
if os.path.exists(config_mat_dir):
resources[config_mat_dir] = ":/icons/preferences-general.svg"
if use_mat_from_custom_dir:
custom_mat_dir = mat_prefs.GetString("CustomMaterialsDir", "")
@@ -106,6 +193,62 @@ def get_material_resources(category='Solid'):
return resources
def get_material_libraries():
resources = {} # { resource_path: icon_path, ... }
mat_prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Material/Resources")
use_built_in_materials = mat_prefs.GetBool("UseBuiltInMaterials", True)
use_mat_from_modules = mat_prefs.GetBool("UseMaterialsFromWorkbenches", True)
use_mat_from_config_dir = mat_prefs.GetBool("UseMaterialsFromConfigDir", True)
use_mat_from_custom_dir = mat_prefs.GetBool("UseMaterialsFromCustomDir", True)
if use_built_in_materials:
builtin_mat_dir = join(
FreeCAD.getResourceDir(), "Mod", "Material", "Resources", "Materials"
)
resources["System"] = (builtin_mat_dir, ":/icons/freecad.svg")
if use_mat_from_modules:
module_prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Material/Resources/Modules")
module_groups = module_prefs.GetGroups()
for group in module_groups:
print("\tGroup - {0}".format(group))
module = module_prefs.GetGroup(group)
module_mat_dir = module.GetString("ModuleDir", "")
module_icon = module.GetString("ModuleIcon", "")
if len(module_mat_dir) > 0:
resources[group] = (module_mat_dir, module_icon)
if use_mat_from_config_dir:
config_mat_dir = join(
FreeCAD.ConfigGet("UserAppData"), "Material"
)
if os.path.exists(config_mat_dir):
resources["User"] = (config_mat_dir, ":/icons/preferences-general.svg")
if use_mat_from_custom_dir:
custom_mat_dir = mat_prefs.GetString("CustomMaterialsDir", "")
if os.path.exists(custom_mat_dir):
resources["Custom"] = (custom_mat_dir, ":/icons/user.svg")
return resources
def list_cards(mat_dir, icon):
import glob
a_path = mat_dir + '/**/*.FCMat'
print("path = '{0}'".format(a_path))
dir_path_list = glob.glob(a_path, recursive=True)
# Need to handle duplicates
cards = []
for a_path in dir_path_list:
p = Path(a_path)
relative = p.relative_to(mat_dir)
cards.append(relative)
return cards
def output_resources(resources):
FreeCAD.Console.PrintMessage('Directories in which we will look for material cards:\n')
@@ -117,24 +260,27 @@ def output_resources(resources):
# used in material editor and FEM material task panels
def import_materials(category='Solid', template=False):
resources = get_material_resources(category)
materialManager = Material.MaterialManager()
mats = materialManager.Materials
materials = {}
cards = {}
icons = {}
for path in resources.keys():
materials, cards, icons = add_cards_from_a_dir(
materials,
cards,
icons,
path,
resources[path]
)
for matUUID in mats:
mat = materialManager.getMaterial(matUUID)
physicalModels = mat.PhysicalModels
fluid = ('1ae66d8c-1ba1-4211-ad12-b9917573b202' in physicalModels)
if not fluid:
path = mat.LibraryRoot + "/" + mat.Directory
print(path)
materials[path] = mat.Properties
cards[path] = mat.Name
icons[path] = mat.LibraryIcon
print(path)
print(mat.Properties)
return (materials, cards, icons)
def add_cards_from_a_dir(materials, cards, icons, mat_dir, icon, template=False):
# fill materials and icons
import glob
@@ -220,6 +366,8 @@ def get_material_template(withSpaces=False):
# https://www.freecad.org/wiki/Material_data_model
# https://www.freecad.org/wiki/Material
print("Call to get_material_template() successful")
import yaml
template_data = yaml.safe_load(
open(join(FreeCAD.ConfigGet('AppHomePath'), 'Mod/Material/Templatematerial.yml'))
@@ -247,7 +395,7 @@ def get_material_template(withSpaces=False):
def create_mat_tools_header():
headers = join(get_source_path(), 'src/Mod/Material/StandardMaterial/Tools/headers')
print(headers)
# print(headers)
if not os.path.isfile(headers):
FreeCAD.Console.PrintError(
'file not found: {}'.format(headers)
@@ -431,7 +579,7 @@ def write_cards_to_path(cards_path, cards_data, write_group_section=True, write_
continue
else:
card_path = join(cards_path, (card_data['CardName'] + '.FCMat'))
print(card_path)
# print(card_path)
if write_group_section is True:
write(card_path, card_data, True)
else: