@@ -100,6 +100,7 @@ def decode(name):
100
100
101
101
102
102
# Metainformation
103
+ # first two lines HAVE, REALLY HAVE to be the same (no comment) in any card file !!!!!
103
104
# first five lines are the same in any card file
104
105
# Line1: card name
105
106
# Line2: author and licence
@@ -108,30 +109,46 @@ def decode(name):
108
109
# Line5: FreeCAD version info or empty
109
110
def read (filename ):
110
111
"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
+
113
116
# print(filename)
114
117
card_name_file = os .path .splitext (os .path .basename (filename ))[0 ]
115
118
f = pythonopen (filename , encoding = "utf8" )
116
119
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 :
119
125
# https://forum.freecadweb.org/viewtopic.php?f=18&t=56912#p489721
120
126
# older FreeCAD do not write utf-8 for special character on windows
121
127
# I have seen "ISO-8859-15" or "windows-1252"
122
128
# explicit utf-8 writing, https://github.com/FreeCAD/FreeCAD/commit/9a564dd906f
123
129
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 )
124
134
return {}
125
135
d = {}
126
136
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)
128
139
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
129
146
if line .startswith ('#' ):
130
147
# a '#' is assumed to be a comment which is ignored
131
148
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))
135
152
v = line .split (";" )[1 ].strip () # Line 1
136
153
if hasattr (v , "decode" ):
137
154
v = v .decode ('utf-8' )
@@ -141,11 +158,16 @@ def read(filename):
141
158
"File CardName ( {} ) is not content CardName ( {} )\n "
142
159
.format (card_name_file , card_name_content )
143
160
)
144
- elif line .startswith (';' ) and ln == 1 :
161
+
162
+ # AuthorAndLicense
163
+ elif line .startswith (';' ) and ln == 2 :
164
+ # print("Line AuthorAndLicense: {}".format(line))
145
165
v = line .split (";" )[1 ].strip () # Line 2
146
166
if hasattr (v , "decode" ):
147
167
v = v .decode ('utf-8' )
148
168
d ["AuthorAndLicense" ] = v
169
+
170
+ # rest
149
171
else :
150
172
# ; is a Comment
151
173
# [ is a Section
0 commit comments