2 from __future__
import print_function
13 file = open(fileName,
"r") 18 line = line.strip(
"\n")
19 if len(line.split(
"\""))==3:
20 contentName = line.split(
"\"")[1]
21 content = line.split(
"\"")[2].
split()
24 result[name] = dens,fractions
28 elif len(content) == 1:
33 result[name] = dens,fractions
35 for material
in result:
37 for fraction
in result[material][1]:
38 sum+= result[material][1][fraction]
39 if math.fabs(sum - 1.0) > maxDist:
40 raise Exception(
"Material Fractions do not add up to 100%: "+ material+
" "+
str(sum))
46 file = open(fileName,
"r") 48 line = line.strip(
"\n")
49 content = line.split()
51 materialMap[content[0]] = content[1]
52 if material
in materialMap:
53 result = materialMap[material]+
":"+material
55 result =
"materials:"+material
60 file = open(fileName,
'r') 61 dom = xml.dom.minidom.parse(file) 68 for node
in rootNode.childNodes:
69 if node.nodeName == name:
72 raise Exception(
"Could not find: \""+name+
"\" in childnodes of the rootNode!")
78 for node
in rootNode.childNodes:
79 if node.nodeName == name
and node.nodeType == node.ELEMENT_NODE:
80 for i
in range(0,node.attributes.length):
81 if node.attributes.item(i).name ==
"name":
82 result[node.attributes.item(i).nodeValue] = node
87 output = dom.toprettyxml()
88 for line
in output.splitlines():
89 if not line.strip(
" \t") ==
"":
97 for i
in range(0,node.attributes.length):
99 result[node.attributes.item(i).name] = node.attributes.item(i).nodeValue
105 for node
in domina.childNodes:
106 print(
"NodeName:", node.nodeName, end=
' ')
107 if node.nodeType == node.ELEMENT_NODE:
108 print(
"Typ ELEMENT_NODE")
110 elif node.nodeType == node.TEXT_NODE:
111 print(
"Typ TEXT_NODE, Content: ", node.nodeValue.strip())
112 elif node.nodeType == node.COMMENT_NODE:
113 print(
"Typ COMMENT_NODE, ")
119 matNodes =
getNodes(rootNode,
"CompositeMaterial")
120 for name
in matNodes:
122 for fractionNode
in matNodes[name].childNodes:
123 if fractionNode.nodeName ==
"MaterialFraction":
125 for materialNode
in fractionNode.childNodes:
126 if materialNode.nodeName ==
"rMaterial":
129 print(
" |-- "+fractionString)
141 newMaterial = doc.createElement(
"CompositeMaterial")
142 newMaterial.setAttribute(
"name",name)
143 newMaterial.setAttribute(
"density",density)
144 newMaterial.setAttribute(
"method",method)
145 newMaterial.setAttribute(
"symbol",symbol)
147 for fracMaterialName
in fractions:
148 fraction = doc.createElement(
"MaterialFraction")
149 fraction.setAttribute(
"fraction",
str(fractions[fracMaterialName]))
150 newMaterial.appendChild(fraction)
151 fracMaterial = doc.createElement(
"rMaterial")
152 fracMaterial.setAttribute(
"name",fracMaterialName)
153 fraction.appendChild(fracMaterial)
155 exMaterials =
getNodes(rootNode,
"CompositeMaterial")
156 if name
in exMaterials:
157 rootNode.replaceChild(newMaterial,exMaterials[name])
159 rootNode.appendChild(newMaterial)
163 optParser = optparse.OptionParser()
164 optParser.add_option(
"-t",
"--titles", dest=
"titlesFile",
165 help=
"the .titles file to parse (as generated by mixture)", metavar=
"TITLES")
166 optParser.add_option(
"-x",
"--xml", dest=
"xmlFile",
167 help=
"the .xml file to parse (must be DDD complient)", metavar=
"XML")
168 optParser.add_option(
"-o",
"--output", dest=
"output",
169 help=
"the file to write the new materials into default: materialOutput.xml", metavar=
"XMLOUT")
170 optParser.add_option(
"-m",
"--materialMap", dest=
"materialMap",
171 help=
"file containing map of materials not defined in materials.xml. default: material.map", metavar=
"MMAP")
173 (options, args) = optParser.parse_args()
175 if options.titlesFile ==
None:
176 raise Exception(
"no .titles File given!")
177 if options.xmlFile ==
None:
179 if options.output ==
None:
180 options.output =
"materialOutput.xml" 181 if options.materialMap ==
None:
182 options.materialMap =
"material.map" 185 materials =
readFractions(options.titlesFile, options.materialMap)
193 for material
in materials:
198 outFile = open(options.output,
"w")
def getNodes(rootNode, name)
def printMaterials(rootNode)
S & print(S &os, JobReport::InputFile const &f)
def getSection(rootNode, name)
def createCompositeMaterial(doc, rootNode, name, density, fractions, method="mixture by weight", symbol=" ")
def getMaterialSection(rootNode)
def readFractions(fileName, materialMap)
def getMaterial(material, fileName)