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

@@ -0,0 +1,163 @@
# ***************************************************************************
# * Copyright (c) 2023 David Carter <dcarter@dvidcarter.ca> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * This program is distributed in the hope that it will be useful, *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
# * GNU Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
__title__ = "material model utilities"
__author__ = "David Carter"
__url__ = "http://www.freecad.org"
import os
import io
from pathlib import Path
import yaml
import FreeCAD
unicode = str
__models = {}
__modelsByPath = {}
def _dereference(parent, child):
# Add the child parameters to the parent
parentModel = parent["model"]
parentBase = parent["base"]
childModel = child["model"]
childBase = child["base"]
for name, value in childModel[childBase].items():
if name not in ["Name", "UUID", "URL", "Description", "DOI", "Inherits"] and \
name not in parentModel[parentBase]: # Don't add if it's already there
parentModel[parentBase][name] = value
print("dereferenced:")
print(parentModel)
def _dereferenceInheritance(data):
if not data["dereferenced"]:
data["dereferenced"] = True # Prevent recursion loops
model = data["model"]
base = data["base"]
if "Inherits" in model[base]:
print("Model '{0}' inherits from:".format(data["name"]))
for parent in model[base]["Inherits"]:
print("\t'{0}'".format(parent))
print("\t\t'{0}'".format(parent.keys()))
print("\t\t'{0}'".format(parent["UUID"]))
# This requires that all models have already been loaded undereferenced
child = __models[parent["UUID"]]
if child is not None:
_dereference(data, child)
def _dereferenceAll():
for data in __models.values():
_dereferenceInheritance(data)
def _scanFolder(folder):
print("Scanning folder '{0}'".format(folder.absolute()))
for child in folder.iterdir():
if child.is_dir():
_scanFolder(child)
else:
if child.suffix.lower() == ".yml":
data = getModelFromPath(child)
if data is not None:
__models[data["uuid"]] = data
__modelsByPath[data["path"]] = data
# print(data["model"])
else:
print("Extension '{0}'".format(child.suffix.lower()))
def _scanModels(libraries):
__models = {} # Clear the current library
__modelsByPath = {}
print("_scanModels")
print(libraries)
for library in libraries:
_scanFolder(Path(library))
# Satisfy aany inheritances
_dereferenceAll()
def getPreferredSaveDirectory():
pass
def getModelLibraries():
libraries = []
# TODO: Expand beyond the standard models as we do for material paths
path = Path(FreeCAD.getResourceDir()) / "Mod/Material/Resources/Models"
libraries.append(path)
_scanModels(libraries)
return libraries
def getModel(uuid):
"""
Retrieve the specified model.
"""
if len(__models) < 1:
getModelLibraries()
if uuid not in __models:
return None
return __models[uuid]
def getModelFromPath(filePath):
"""
Retrieve the model at the specified path.
This may not need public exposure?
"""
try:
path = Path(filePath)
stream = open(path.absolute(), "r")
model = yaml.safe_load(stream)
base = "Model"
if "AppearanceModel" in model:
base = "AppearanceModel"
uuid = model[base]["UUID"]
name = model[base]["Name"]
data = {}
data["base"] = base
data["name"] = name
data["path"] = path.absolute()
data["uuid"] = uuid
data["model"] = model
data["dereferenced"] = False
return data
except Exception as ex:
print("Unable to load '{0}'".format(path.absolute()))
print(ex)
return None
def saveModel(model, path):
"""
Write the model to the specified path
"""

View File

@@ -0,0 +1,292 @@
# ***************************************************************************
# * Copyright (c) 2013 Juergen Riegel <FreeCAD@juergen-riegel.net> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * This program is distributed in the hope that it will be useful, *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
# * GNU Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
__title__ = "FreeCAD material card importer"
__author__ = "Juergen Riegel"
__url__ = "https://www.freecad.org"
import os
import glob
import uuid
from pathlib import Path
import xml.etree.ElementTree as ET
def decode(name):
"decodes encoded strings"
try:
decodedName = (name.decode("utf8"))
except UnicodeDecodeError:
try:
decodedName = (name.decode("latin1"))
except UnicodeDecodeError:
print("Error: Couldn't determine character encoding")
decodedName = name
return decodedName
def read(filename):
"reads a FCMat file and returns a dictionary from it"
# the reader returns a dictionary in any case even if the file has problems
# an empty dict is returned in such case
card_name_file = os.path.splitext(os.path.basename(filename))[0]
f = open(filename, encoding="utf8")
try:
content = f.readlines()
except Exception:
# https://forum.freecad.org/viewtopic.php?f=18&t=56912#p489721
# older FreeCAD do not write utf-8 for special character on windows
# I have seen "ISO-8859-15" or "windows-1252"
# explicit utf-8 writing, https://github.com/FreeCAD/FreeCAD/commit/9a564dd906f
print("Error on card loading. File might not utf-8.")
error_message = "Error on loading. Material file '{}' might not utf-8.".format(filename)
print("{}\n".format(error_message))
return {}
d = {}
d["Meta"] = {}
d["General"] = {}
d["Mechanical"] = {}
d["Fluidic"] = {}
d["Thermal"] = {}
d["Electromagnetic"] = {}
d["Architectural"] = {}
d["Rendering"] = {}
d["VectorRendering"] = {}
d["Cost"] = {}
d["UserDefined"] = {}
d["Meta"]["CardName"] = card_name_file # CardName is the MatCard file name
section = ''
for ln, line in enumerate(content):
# line numbers are used for CardName and AuthorAndLicense
# the use of line number is not smart for a data model
# a wrong user edit could break the file
# comment
if line.startswith('#'):
# a '#' is assumed to be a comment which is ignored
continue
# CardName
if line.startswith(';') and ln == 0:
pass
# AuthorAndLicense
elif line.startswith(';') and ln == 1:
v = line.split(";")[1].strip() # Line 2
if hasattr(v, "decode"):
v = v.decode('utf-8')
d["General"]["AuthorAndLicense"] = v # Move the field to the general group
# rest
else:
# ; is a Comment
# [ is a Section
if line[0] == '[':
line = line[1:]
k = line.split("]", 1)
if len(k) >= 2:
v = k[0].strip()
if hasattr(v, "decode"):
v = v.decode('utf-8')
section = v
elif line[0] not in ";":
# split once on first occurrence
# a link could contain a '=' and thus would be split
k = line.split("=", 1)
if len(k) == 2:
v = k[1].strip()
if hasattr(v, "decode"):
v = v.decode('utf-8')
d[section][k[0].strip()] = v
return d
def yamGeneral(card):
father = ""
materialStandard = ""
yamModels = ""
yam = "# File created by ConvertFCMat.py\n"
yam += "General:\n"
# Add UUIDs
yam += ' UUID: "{0}"\n'.format(uuid.uuid4())
for param in card:
if param in ["Name", "AuthorAndLicense", "Description", "ReferenceSource", "SourceURL"]:
yam += ' {0}: "{1}"\n'.format(param, card[param])
elif param in ["Father"]:
father += ' {0}: "{1}"\n'.format(param, card[param])
elif param in ["KindOfMaterial", "MaterialNumber", "Norm", "StandardCode"]:
if param == "Norm": # Handle the name change
materialStandard += ' {0}: "{1}"\n'.format("StandardCode", card[param])
else:
materialStandard += ' {0}: "{1}"\n'.format(param, card[param])
if len(father) > 0:
yamModels += " {0}:\n".format('Father')
yamModels += " UUID: '{0}'\n".format('9cdda8b6-b606-4778-8f13-3934d8668e67')
yamModels += father
if len(materialStandard) > 0:
yamModels += " {0}:\n".format('MaterialStandard')
yamModels += " UUID: '{0}'\n".format('1e2c0088-904a-4537-925f-64064c07d700')
yamModels += materialStandard
return yam, yamModels
def yamSection(card, header, uuid):
if len(card) > 0:
yam = " {0}:\n".format(header)
yam += " UUID: '{0}'\n".format(uuid)
for param in card:
yam += ' {0}: "{1}"\n'.format(param, card[param])
else:
yam = ""
return yam
def yamMechanical(card):
# Check which model we need
useDensity = False
useIso = False
useLinearElastic = False
for param in card:
if param in ["Density"]:
useDensity = True
elif param in ["BulkModulus", "PoissonRatio", "ShearModulus", "YoungsModulus"]:
useIso = True
elif param in ["AngleOfFriction", "CompressiveStrength", "FractureToughness",
"UltimateStrain", "UltimateTensileStrength", "YieldStrength", "Stiffness", "Hardness"]:
useLinearElastic = True
yam = ""
if useLinearElastic:
return yamSection(card, 'LinearElastic', '7b561d1d-fb9b-44f6-9da9-56a4f74d7536')
if useIso:
yam = yamSection(card, 'IsotropicLinearElastic', 'f6f9e48c-b116-4e82-ad7f-3659a9219c50')
if useDensity:
return yam + yamSection(card, 'Density', '454661e5-265b-4320-8e6f-fcf6223ac3af')
# default mechanical model
return ""
def yamFluid(card):
# Split out density
for param in card:
if param not in ["Density"]:
return yamSection(card, 'Fluid', '1ae66d8c-1ba1-4211-ad12-b9917573b202')
return yamSection(card, 'Density', '454661e5-265b-4320-8e6f-fcf6223ac3af')
def yamThermal(card):
return yamSection(card, 'Thermal', '9959d007-a970-4ea7-bae4-3eb1b8b883c7')
def yamElectromagnetic(card):
return yamSection(card, 'Electromagnetic', 'b2eb5f48-74b3-4193-9fbb-948674f427f3')
def yamArchitectural(card):
return yamSection(card, 'Architectural', '32439c3b-262f-4b7b-99a8-f7f44e5894c8')
def yamCost(card):
return yamSection(card, 'Costs', '881df808-8726-4c2e-be38-688bb6cce466')
def yamRendering(card):
# Check which model we need
useTexture = False
useAdvanced = False
for param in card:
if param in ["TexturePath", "TextureScaling"]:
useTexture = True
elif param in ["FragmentShader", "VertexShader"]:
useAdvanced = True
if useAdvanced:
return yamSection(card, 'AdvancedRendering', 'c880f092-cdae-43d6-a24b-55e884aacbbf')
if useTexture:
return yamSection(card, 'TextureRendering', 'bbdcc65b-67ca-489c-bd5c-a36e33d1c160')
# default rendering model
return yamSection(card, 'BasicRendering', 'f006c7e4-35b7-43d5-bbf9-c5d572309e6e')
def yamVectorRendering(card):
return yamSection(card, 'VectorRendering', 'fdf5a80e-de50-4157-b2e5-b6e5f88b680e')
def saveYaml(card, output):
yam, yamModels = yamGeneral(card["General"])
if len(card["Mechanical"]) > 0 or \
len(card["Fluidic"]) > 0 or \
len(card["Thermal"]) > 0 or \
len(card["Electromagnetic"]) > 0 or \
len(card["Architectural"]) > 0 or \
len(card["Cost"]) > 0 or \
len(yamModels) > 0:
yam += "Models:\n"
yam += yamModels
if "Mechanical" in card:
yam += yamMechanical(card["Mechanical"])
if "Fluidic" in card:
yam += yamFluid(card["Fluidic"])
if "Thermal" in card:
yam += yamThermal(card["Thermal"])
if "Electromagnetic" in card:
yam += yamElectromagnetic(card["Electromagnetic"])
if "Architectural" in card:
yam += yamArchitectural(card["Architectural"])
if "Cost" in card:
yam += yamCost(card["Cost"])
if len(card["Rendering"]) > 0 or len(card["VectorRendering"]) > 0:
yam += "AppearanceModels:\n"
if "Rendering" in card:
yam += yamRendering(card["Rendering"])
if "VectorRendering" in card:
yam += yamVectorRendering(card["VectorRendering"])
file = open(output, "w", encoding="utf-8")
file.write(yam)
file.close()
def convert(infolder, outfolder):
a_path = infolder + '/**/*.FCMat'
dir_path_list = glob.glob(a_path, recursive=True)
for a_path in dir_path_list:
p = Path(a_path)
relative = p.relative_to(infolder)
out = Path(outfolder) / relative
print("('{0}', '{1}') -> {2}".format(infolder, relative, out))
try:
card = read(p)
except Exception:
print("Error converting card '{0}'. Skipped.")
continue
out.parent.mkdir(parents=True, exist_ok=True)
saveYaml(card, out)
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("infolder", help="Input folder containing older material cards")
parser.add_argument("outfolder", help="Output folder to place the converted material cards")
args = parser.parse_args()
print("Input folder '{0}'".format(args.infolder))
print("Output folder '{0}'".format(args.outfolder))
convert(args.infolder, args.outfolder)

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: