Skip to content

Commit cfcf8df

Browse files
Material: matcard reader, fix some problems
1 parent a9300c2 commit cfcf8df

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

src/Mod/Material/importFCMat.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def decode(name):
100100

101101

102102
# Metainformation
103+
# first two lines HAVE, REALLY HAVE to be the same (no comment) in any card file !!!!!
103104
# first five lines are the same in any card file
104105
# Line1: card name
105106
# Line2: author and licence
@@ -108,30 +109,46 @@ def decode(name):
108109
# Line5: FreeCAD version info or empty
109110
def read(filename):
110111
"reads a FCMat file and returns a dictionary from it"
111-
# the reader should return a dictionary in any case even if the file
112-
# has problems, an empty dict should be returned in such case
112+
113+
# the reader returns a dictionary in any case even if the file has problems
114+
# an empty dict is returned in such case
115+
113116
# print(filename)
114117
card_name_file = os.path.splitext(os.path.basename(filename))[0]
115118
f = pythonopen(filename, encoding="utf8")
116119
try:
117-
content = f.read()
118-
except UnicodeDecodeError:
120+
content = f.readlines()
121+
# print(len(content))
122+
# print(type(content))
123+
# print(content)
124+
except Exception:
119125
# https://forum.freecadweb.org/viewtopic.php?f=18&t=56912#p489721
120126
# older FreeCAD do not write utf-8 for special character on windows
121127
# I have seen "ISO-8859-15" or "windows-1252"
122128
# explicit utf-8 writing, https://github.com/FreeCAD/FreeCAD/commit/9a564dd906f
123129
FreeCAD.Console.PrintError("Error on card loading. File might not utf-8.")
130+
error_message = "Error on loading. Material file '{}' might not utf-8.".format(filename)
131+
FreeCAD.Console.PrintError("{}\n".format(error_message))
132+
if FreeCAD.GuiUp:
133+
QtGui.QMessageBox.critical(None, "Error on card reading", error_message)
124134
return {}
125135
d = {}
126136
d["CardName"] = card_name_file # CardName is the MatCard file name
127-
for ln, line in enumerate(f):
137+
for ln, line in enumerate(content):
138+
# print(line)
128139
ln += 1 # enumerate starts with 0, but we would like to have the real line number
140+
141+
# line numbers are used for CardName and AuthorAndLicense
142+
# the use of line number is not smart for a data model
143+
# a wrong user edit could break the file
144+
145+
# comment
129146
if line.startswith('#'):
130147
# a '#' is assumed to be a comment which is ignored
131148
continue
132-
# the use of line number is not smart for a data model
133-
# a wrong user edit could break the file
134-
if line.startswith(';') and ln == 0:
149+
# CardName
150+
if line.startswith(';') and ln == 1:
151+
# print("Line CardName: {}".format(line))
135152
v = line.split(";")[1].strip() # Line 1
136153
if hasattr(v, "decode"):
137154
v = v.decode('utf-8')
@@ -141,11 +158,16 @@ def read(filename):
141158
"File CardName ( {} ) is not content CardName ( {} )\n"
142159
.format(card_name_file, card_name_content)
143160
)
144-
elif line.startswith(';') and ln == 1:
161+
162+
# AuthorAndLicense
163+
elif line.startswith(';') and ln == 2:
164+
# print("Line AuthorAndLicense: {}".format(line))
145165
v = line.split(";")[1].strip() # Line 2
146166
if hasattr(v, "decode"):
147167
v = v.decode('utf-8')
148168
d["AuthorAndLicense"] = v
169+
170+
# rest
149171
else:
150172
# ; is a Comment
151173
# [ is a Section

0 commit comments

Comments
 (0)