Fixes and enhancements in python scripts relative to BOM generation.

This commit is contained in:
unknown 2015-06-26 19:52:49 +02:00 committed by jean-pierre charras
parent 1c5cde94f0
commit f194905117
4 changed files with 55 additions and 34 deletions

View File

@ -40,7 +40,7 @@ out = csv.writer(f, lineterminator='\n', delimiter=',', quotechar="\"", quoting=
def writerow( acsvwriter, columns ):
utf8row = []
for col in columns:
utf8row.append( str(col).encode('utf8') )
utf8row.append( str(col) )
acsvwriter.writerow( utf8row )
components = net.getInterestingComponents()

View File

@ -9,7 +9,7 @@
Generate a HTML BOM list.
Components are sorted and grouped by ref
Fields are (if exist)
Ref, Quantity, Value, Part, Datasheet, Description, Vendor
Ref, Quantity, Value, Symbol, footprint, Description, Vendor
"""
@ -26,7 +26,7 @@ html = """
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>KiCad BOM Example 5</title>
<title>KiCad BOM grouped by value and footprint </title>
</head>
<body>
<h1><!--SOURCE--></h1>
@ -60,12 +60,12 @@ def myEqu(self, other):
result = False
elif self.getFootprint() != other.getFootprint():
result = False
elif self.getField("Tolerance") != other.getField("Tolerance"):
result = False
elif self.getField("Manufacturer") != other.getField("Manufacturer"):
result = False
elif self.getField("Voltage") != other.getField("Voltage"):
result = False
# elif self.getField("Tolerance") != other.getField("Tolerance"):
# result = False
# elif self.getField("Manufacturer") != other.getField("Manufacturer"):
# result = False
# elif self.getField("Voltage") != other.getField("Voltage"):
# result = False
return result
@ -97,10 +97,12 @@ html = html.replace('<!--COMPCOUNT-->', "<b>Component Count:</b>" + \
str(len(components)))
row = "<tr><th style='width:640px'>Ref</th>" + "<th>Qnty</th>"
row += "<th>Value</th>" + "<th>Part</th>"
row += "<th>Value</th>"
row += "<th>Symbol</th>"
row += "<th>Footprint</th>"
row += "<th>Description</th>"
#row += "<th>Datasheet</th>"
row += "<th>PartNumber</th>" + "<th>Vendor</th></tr>"
row += "<th>PartNumber</th>"
row += "<th>Vendor</th></tr>"
html = html.replace('<!--TABLEROW-->', row + "<!--TABLEROW-->")
@ -120,14 +122,17 @@ for group in grouped:
refs += component.getRef()
c = component
row = "<tr><td>" + refs +"</td><td>" + str(len(group))
row += "</td><td>" + c.getValue() + "</td><td>"
row += c.getLibName() + ":" + c.getPartName() + "</td><td>"
#row += c.getDatasheet() + "</td><td>"
row += c.getDescription() + "</td><td>"
row += c.getField("PartNumber") + "</td><td>"
row += c.getField("Vendor")
row += "</td></tr>"
row = "<tr>"
row += "<td>" + refs +"</td>"
row += "<td align=center>" + str(len(group)) + "</td>"
row += "<td align=center>" + c.getValue() + "</td>"
# row += "<td align=center>" + c.getLibName() + ":" + c.getPartName() + "</td>"
row += "<td align=center>" + c.getPartName() + "</td>"
row += "<td align=center>" + c.getFootprint() + "</td>"
row += "<td align=center>" + c.getDescription() + "</td>"
row += "<td align=center>" + c.getField("PartNumber") + "</td>"
row += "<td align=center>" + c.getField("Vendor") + "</td>"
row += "</tr>"
html = html.replace('<!--TABLEROW-->', row + "<!--TABLEROW-->")

View File

@ -40,7 +40,7 @@ def writerow( acsvwriter, columns ):
utf8row = []
for col in columns:
txt=str(col);
utf8row.append( txt.encode('utf-8') )
utf8row.append( txt )
acsvwriter.writerow( utf8row )
components = net.getInterestingComponents()

View File

@ -165,6 +165,7 @@ class xmlElement():
def addAttribute(self, attr, value):
"""Add an attribute to this element"""
if type(value) != str: value = value.encode('utf-8')
self.attributes[attr] = value
def setAttribute(self, attr, value):
@ -220,20 +221,31 @@ class xmlElement():
try:
if attrmatch != "":
if self.attributes[attribute] == attrmatch:
return self.chars
ret = self.chars
if type(ret) != str: ret = ret.encode('utf-8')
return ret
else:
return self.attributes[attribute]
ret = self.attributes[attribute]
if type(ret) != str: ret = ret.encode('utf-8')
return ret
except AttributeError:
return ""
ret = ""
if type(ret) != str: ret = ret.encode('utf-8')
return ret
else:
return self.chars
ret = self.chars
if type(ret) != str: ret = ret.encode('utf-8')
return ret
for child in self.children:
ret = child.get(elemName, attribute, attrmatch)
if ret != "":
if type(ret) != str: ret = ret.encode('utf-8')
return ret
return ""
ret = ""
if type(ret) != str: ret = ret.encode('utf-8')
return ret
@ -345,7 +357,7 @@ class comp():
v.setChars(value)
def getValue(self):
return self.element.get("value").encode( "utf-8" )
return self.element.get("value")
def getField(self, name, libraryToo=True):
"""Return the value of a field named name. The component is first
@ -361,7 +373,7 @@ class comp():
field = self.element.get("field", "name", name)
if field == "" and libraryToo:
field = self.libpart.getField(name).encode( "utf-8" )
field = self.libpart.getField(name)
return field
def getFieldNames(self):
@ -374,7 +386,7 @@ class comp():
fields = self.element.getChild('fields')
if fields:
for f in fields.getChildren():
fieldNames.append( f.get('field','name').encode( "utf-8" ) )
fieldNames.append( f.get('field','name') )
return fieldNames
def getRef(self):
@ -396,7 +408,7 @@ class comp():
return self.element.get("tstamp")
def getDescription(self):
return self.libpart.getDescription().encode( "utf-8" )
return self.libpart.getDescription()
class netlist():
@ -607,7 +619,9 @@ class netlist():
ret.append(c)
# Sort first by ref as this makes for easier to read BOM's
ret.sort(key=lambda g: g.getRef())
def f(v):
return re.sub(r'([A-z]+)[0-9]+', r'\1', v) + '%08i' % int(re.sub(r'[A-z]+([0-9]+)', r'\1', v))
ret.sort(key=lambda g: f(g.getRef()))
return ret
@ -648,11 +662,13 @@ class netlist():
# Each group is a list of components, we need to sort each list first
# to get them in order as this makes for easier to read BOM's
def f(v):
return re.sub(r'([A-z]+)[0-9]+', r'\1', v) + '%08i' % int(re.sub(r'[A-z]+([0-9]+)', r'\1', v))
for g in groups:
g = sorted(g, key=lambda g: g.getRef())
g = sorted(g, key=lambda g: f(g.getRef()))
# Finally, sort the groups to order the references alphabetically
groups = sorted(groups, key=lambda group: group[0].getRef())
groups = sorted(groups, key=lambda group: f(group[0].getRef()))
return groups