/
importFCMat.py
151 lines (141 loc) · 6.4 KB
/
importFCMat.py
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
#***************************************************************************
#* *
#* 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 *
#* *
#***************************************************************************
import FreeCAD, Material
import os
__title__="FreeCAD material card importer"
__author__ = "Juergen Riegel"
__url__ = "http://www.freecadweb.org"
# file structure - this affects how files are saved
FileStructure = [
[ "Meta", ["CardName","AuthorAndLicense","Source"] ],
[ "General", ["Name","Father","Description","Density","Vendor","ProductURL","SpecificPrice"] ],
[ "Mechanical", ["YoungsModulus","UltimateTensileStrength","CompressiveStrength","Elasticity","FractureToughness"] ],
[ "FEM", ["PoissonRatio"] ],
[ "Architectural", ["Model","ExecutionInstructions","FireResistanceClass","StandardCode","ThermalConductivity","SoundTransmissionClass","Color","Finish","UnitsPerQuantity","EnvironmentalEfficiencyClass"] ],
[ "Rendering", ["DiffuseColor","AmbientColor","SpecularColor","Shininess","EmissiveColor","Transparency","VertexShader","FragmentShader","TexturePath","TextureScaling"] ],
[ "Vector rendering",["ViewColor","ViewFillPattern","SectionFillPattern","ViewLinewidth","SectionLinewidth"] ],
[ "User defined", [] ]
]
# to distinguish python built-in open function from the one declared below
if open.__module__ == '__builtin__':
pythonopen = open
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
def read(filename):
"reads a FCMat file and returns a dictionary from it"
if isinstance(filename,unicode):
import sys
filename = filename.encode(sys.getfilesystemencoding())
f = pythonopen(filename)
d = {}
l = 0
for line in f:
if l == 0:
d["CardName"] = line.split(";")[1].strip()
elif l == 1:
d["AuthorAndLicense"] = line.split(";")[1].strip()
else:
if not line[0] in ";#[":
k = line.split("=")
if len(k) == 2:
d[k[0].strip()] = k[1].strip().decode('utf-8')
l += 1
return d
def write(filename,dictionary):
"writes the given dictionary to the given file"
# sort the data into sections
contents = []
for key in FileStructure:
contents.append({"keyname":key[0]})
if key[0] == "Meta":
header = contents[-1]
elif key[0] == "User defined":
user = contents[-1]
for p in key[1]:
contents[-1][p] = ""
for k,i in dictionary.iteritems():
found = False
for group in contents:
if not found:
if k in group.keys():
group[k] = i
found = True
if not found:
user[k] = i
# write header
rev = FreeCAD.ConfigGet("BuildVersionMajor")+"."+FreeCAD.ConfigGet("BuildVersionMinor")+" "+FreeCAD.ConfigGet("BuildRevision")
if isinstance(filename,unicode):
import sys
filename = filename.encode(sys.getfilesystemencoding())
print(filename)
f = pythonopen(filename,"wb")
f.write("; " + header["CardName"].encode("utf8") + "\n")
f.write("; " + header["AuthorAndLicense"].encode("utf8") + "\n")
f.write("; file produced by FreeCAD " + rev + "\n")
f.write("; information about the content of this card can be found here:\n")
f.write("; http://www.freecadweb.org/wiki/index.php?title=Material\n")
f.write("\n")
if header["Source"]:
f.write("; source of the data provided in this card:\n")
f.write("; " + header["Source"].encode("utf8") + "\n")
f.write("\n")
# write sections
for s in contents:
if s["keyname"] != "Meta":
if len(s) > 1:
# if the section has no contents, we don't write it
f.write("[" + s["keyname"] + "]\n")
for k,i in s.iteritems():
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.encode('utf-8') + "\n")
f.write("\n")
f.close()