File size: 15,016 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | # SPDX-License-Identifier: LGPL-2.1-or-later
# ***************************************************************************
# * 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 FreeCAD
from materialtools.cardutils import get_material_template
import Materials
from builtins import open as pyopen
if FreeCAD.GuiUp:
from PySide import QtGui
def open(filename):
"called when freecad wants to open a file"
docname = os.path.splitext(os.path.basename(filename))[0]
doc = FreeCAD.newDocument(docname)
doc.Label = docname
FreeCAD.ActiveDocument = doc
read(filename)
return doc
def insert(filename, docname):
"called when freecad wants to import a file"
try:
doc = FreeCAD.getDocument(docname)
except NameError:
doc = FreeCAD.newDocument(docname)
FreeCAD.ActiveDocument = doc
read(filename)
return doc
def export(exportList, filename):
"called when freecad exports a file"
return
def decode(name):
"decodes encoded strings"
try:
decodedName = (name.decode("utf8"))
except UnicodeDecodeError:
try:
decodedName = (name.decode("latin1"))
except UnicodeDecodeError:
FreeCAD.Console.PrintError("Error: Couldn't determine character encoding")
decodedName = name
return decodedName
# the reader and writer do not use some Library to read and write the ini file format
# they are implemented here
# thus non standard ini files will be read and written too
# in standard ini file format:
# a = in the value without any encapsulation or string quotes is not allowed (AFAIK)
# https://en.wikipedia.org/wiki/INI_file
# http://www.docuxplorer.com/WebHelp/INI_File_Format.htm
# mainly this parser here is used in FreeCAD
# in the module Material.py is another implementation of reading and writing FCMat files
# the implementation in Material.py uses Pythons ConfigParser module
# in ViewProviderFemMaterial in add_cards_from_a_dir() the parser from Material.py is used
# since this mixture seems to have be there for ages it should not be changed for 0.18
# TODO and FIXME:
# get rid of this mixture
# best might be to switch to a more robust file schema like YAML
# as we had and we might will have problems again and again
# https://github.com/berndhahnebach/FreeCAD_bhb/commits/materialdev
def read(filename):
materialManager = Materials.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 !!!!!
# first five lines are the same in any card file
# Line1: card name
# Line2: author and licence
# Line3: information string
# Line4: information link
# Line5: FreeCAD version info or empty
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
# an empty dict is returned in such case
# print(filename)
card_name_file = os.path.splitext(os.path.basename(filename))[0]
f = pyopen(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["CardName"] = card_name_file # CardName is the MatCard file name
for ln, line in enumerate(content):
# print(line)
ln += 1 # enumerate starts with 0, but we would like to have the real line number
# 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 == 1:
# 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["CardName"]:
FreeCAD.Console.PrintLog(
"File CardName ( {} ) is not content CardName ( {} )\n"
.format(card_name_file, card_name_content)
)
# AuthorAndLicense
elif line.startswith(';') and ln == 2:
# print("Line AuthorAndLicense: {}".format(line))
v = line.split(";")[1].strip() # Line 2
if hasattr(v, "decode"):
v = v.decode('utf-8')
d["AuthorAndLicense"] = v
# rest
else:
# ; is a Comment
# [ is a Section
if 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[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 = pyopen(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"
# sort the data into sections
contents = []
user = {}
template_data = get_material_template()
for group in template_data:
groupName = list(group)[0] # group dict has only one key
contents.append({"keyname": groupName})
if groupName == "Meta":
header = contents[-1]
elif groupName == 'UserDefined':
user = contents[-1]
for properName in group[groupName]:
contents[-1][properName] = ''
for k, i in dictionary.items():
found = False
for group in contents:
if not found:
if k in group:
group[k] = i
found = True
if not found:
user[k] = i
# delete empty properties
for group in contents:
# iterating over a dict and changing it is not allowed
# thus it is iterated over a list of the keys
for k in list(group):
if group[k] == '':
del group[k]
# card writer
rev = "{}.{}.{}".format(
FreeCAD.ConfigGet("BuildVersionMajor"),
FreeCAD.ConfigGet("BuildVersionMinor"),
FreeCAD.ConfigGet("BuildRevision")
)
# print(filename)
card_name_file = os.path.splitext(os.path.basename(filename))[0]
# print(card_name_file)
if "CardName" not in header:
print(header)
error_message = "No card name provided. Card could not be written.".format(header)
FreeCAD.Console.PrintError("{}\n".format(error_message))
if FreeCAD.GuiUp:
QtGui.QMessageBox.critical(None, "No card name", error_message)
return
f = pyopen(filename, "w", encoding="utf-8")
# write header
# first five lines are the same in any card file, see comment above read def
if header["CardName"] != card_name_file:
# CardName is the MatCard file name
FreeCAD.Console.PrintWarning(
"The file name {} is not equal to the card name {}. The file name is used."
.format(card_name_file, header["CardName"])
)
f.write("; " + card_name_file + "\n")
# f.write("; " + header["AuthorAndLicense"] + "\n")
f.write("; " + header.get("AuthorAndLicense", "no author") + "\n")
f.write("; information about the content of such cards can be found on the wiki:\n")
f.write("; https://www.freecad.org/wiki/Material\n")
f.write("; file created by FreeCAD " + rev + "\n")
# write sections
# write standard FCMat section if write group section parameter is set to False
if write_group_section is False:
f.write("\n[FCMat]\n")
for s in contents:
if s["keyname"] != "Meta":
# if the section has no contents, we don't write it
if len(s) > 1:
# only write group section if write group section parameter is set to True
if write_group_section is True:
f.write("\n[" + s["keyname"] + "]\n")
for k, i in s.items():
if (k != "keyname" and i != '') or k == "Name":
# use only keys which are not empty and the name, even if empty
f.write(k + " = " + i + "\n")
f.close()
# ***** some code examples ***********************************************************************
'''
from materialtools.cardutils import get_source_path as getsrc
from importFCMat import read, write
readmatfile = getsrc() + '/src/Mod/Material/StandardMaterial/Concrete-Generic.FCMat'
writematfile = '/tmp/Concrete-Generic.FCMat'
matdict = read(readmatfile)
matdict
write(writematfile, matdict)
'''
|