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:
@@ -30,6 +30,7 @@ import os
|
||||
import FreeCAD
|
||||
|
||||
from materialtools.cardutils import get_material_template
|
||||
import Material
|
||||
|
||||
if FreeCAD.GuiUp:
|
||||
from PySide import QtGui
|
||||
@@ -97,6 +98,10 @@ def decode(name):
|
||||
# as we had and we might will have problems again and again
|
||||
# https://github.com/berndhahnebach/FreeCAD_bhb/commits/materialdev
|
||||
|
||||
def read(filename):
|
||||
materialManager = Material.MaterialManager()
|
||||
material = materialManager.getMaterialByPath(filename)
|
||||
return material.Properties
|
||||
|
||||
# Metainformation
|
||||
# first two lines HAVE, REALLY HAVE to be the same (no comment) in any card file !!!!!
|
||||
@@ -106,7 +111,7 @@ def decode(name):
|
||||
# Line3: information string
|
||||
# Line4: information link
|
||||
# Line5: FreeCAD version info or empty
|
||||
def read(filename):
|
||||
def read_old(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
|
||||
@@ -181,6 +186,105 @@ def read(filename):
|
||||
d[k[0].strip()] = v
|
||||
return d
|
||||
|
||||
def read2(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
|
||||
|
||||
# print(filename)
|
||||
card_name_file = os.path.splitext(os.path.basename(filename))[0]
|
||||
f = pythonopen(filename, encoding="utf8")
|
||||
try:
|
||||
content = f.readlines()
|
||||
# print(len(content))
|
||||
# print(type(content))
|
||||
# print(content)
|
||||
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
|
||||
FreeCAD.Console.PrintError("Error on card loading. File might not utf-8.")
|
||||
error_message = "Error on loading. Material file '{}' might not utf-8.".format(filename)
|
||||
FreeCAD.Console.PrintError("{}\n".format(error_message))
|
||||
if FreeCAD.GuiUp:
|
||||
QtGui.QMessageBox.critical(None, "Error on card reading", 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):
|
||||
# print(line)
|
||||
# enumerate starts with 0
|
||||
|
||||
# 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:
|
||||
# print("Line CardName: {}".format(line))
|
||||
v = line.split(";")[1].strip() # Line 1
|
||||
if hasattr(v, "decode"):
|
||||
v = v.decode('utf-8')
|
||||
card_name_content = v
|
||||
if card_name_content != d["Meta"]["CardName"]:
|
||||
FreeCAD.Console.PrintLog(
|
||||
"File CardName ( {} ) is not content CardName ( {} )\n"
|
||||
.format(card_name_file, card_name_content)
|
||||
)
|
||||
|
||||
# AuthorAndLicense
|
||||
elif line.startswith(';') and ln == 1:
|
||||
# print("Line AuthorAndLicense: {}".format(line))
|
||||
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] == '[':
|
||||
# print("parse section '{0}'".format(line))
|
||||
line = line[1:]
|
||||
# print("\tline '{0}'".format(line))
|
||||
k = line.split("]", 1)
|
||||
if len(k) >= 2:
|
||||
v = k[0].strip()
|
||||
if hasattr(v, "decode"):
|
||||
v = v.decode('utf-8')
|
||||
section = v
|
||||
# print("Section '{0}'".format(section))
|
||||
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')
|
||||
# print("key '{0}', value '{1}'".format(k[0].strip(), v))
|
||||
d[section][k[0].strip()] = v
|
||||
return d
|
||||
|
||||
|
||||
def write(filename, dictionary, write_group_section=True):
|
||||
"writes the given dictionary to the given file"
|
||||
|
||||
Reference in New Issue
Block a user