2013-05-26 04:36:44 +00:00
|
|
|
#
|
|
|
|
# Example python script to generate an equivalent XML document from XML input
|
|
|
|
#
|
|
|
|
# Example: Round value robin, XML to XML conversion with partial value monging
|
|
|
|
#
|
|
|
|
|
2013-09-01 17:49:01 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2013-05-26 04:36:44 +00:00
|
|
|
# Import the KiCad python helper module and the csv formatter
|
2013-09-01 17:49:01 +00:00
|
|
|
import kicad_netlist_reader
|
2013-05-26 04:36:44 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
def checkvalue(self):
|
|
|
|
"""Check values, and replace with preferred/consistent values"""
|
|
|
|
ref = self.getRef()
|
|
|
|
r = ref.split("R")
|
|
|
|
c = ref.split("C")
|
|
|
|
v = self.getValue()
|
|
|
|
|
|
|
|
# Common to all values - convert decimation if necessary
|
|
|
|
dec = v.split(",")
|
|
|
|
if (len(dec) == 2):
|
|
|
|
newval = dec[0] + "." + dec[1]
|
|
|
|
self.setValue(newval)
|
|
|
|
v = self.getValue()
|
2013-06-14 14:59:52 +00:00
|
|
|
|
2013-05-26 04:36:44 +00:00
|
|
|
if len(r) == 2 and r[1].isdigit():
|
|
|
|
# This is a resistor - make values consistent
|
|
|
|
# If the value is a pure value, add R to the end of the value
|
|
|
|
if v.isdigit():
|
|
|
|
i = int(v)
|
|
|
|
if (i > 1000000):
|
2014-05-09 06:32:05 +00:00
|
|
|
i = i / 1000000
|
2013-05-26 04:36:44 +00:00
|
|
|
v = str(i) + "M"
|
|
|
|
if (i > 1000):
|
|
|
|
i = i / 1000
|
|
|
|
v = str(i) + "K"
|
|
|
|
else:
|
2013-06-14 14:59:52 +00:00
|
|
|
v = str(i) + "R"
|
|
|
|
|
2013-05-26 04:36:44 +00:00
|
|
|
self.setValue(v)
|
|
|
|
else:
|
|
|
|
# Get the multiplier character
|
|
|
|
multiplier = v[len(v) - 1]
|
|
|
|
v = v.strip(multiplier)
|
|
|
|
v = v.split(".")
|
|
|
|
if (len(v) == 2):
|
|
|
|
newval = v[0] + multiplier + v[1]
|
|
|
|
self.setValue(newval)
|
2013-06-14 14:59:52 +00:00
|
|
|
v = self.getValue()
|
|
|
|
|
|
|
|
|
2013-05-26 04:36:44 +00:00
|
|
|
|
|
|
|
# Give components a new method for checking the values (this could easily be a
|
|
|
|
# Company Part Number generator method instead)
|
2013-09-01 17:49:01 +00:00
|
|
|
kicad_netlist_reader.comp.checkvalue = checkvalue
|
2013-06-14 14:59:52 +00:00
|
|
|
|
2013-05-26 04:36:44 +00:00
|
|
|
# Generate an instance of a generic netlist, and load the netlist tree from
|
|
|
|
# the command line option. If the file doesn't exist, execution will stop
|
2013-09-01 17:49:01 +00:00
|
|
|
net = kicad_netlist_reader.netlist(sys.argv[1])
|
2013-05-26 04:36:44 +00:00
|
|
|
|
|
|
|
# Open a file to write to, if the file cannot be opened output to stdout
|
|
|
|
# instead
|
2023-08-19 21:05:16 +00:00
|
|
|
canOpenFile = True
|
2013-05-26 04:36:44 +00:00
|
|
|
try:
|
|
|
|
f = open(sys.argv[2], 'w')
|
|
|
|
except IOError:
|
2014-12-03 16:22:06 +00:00
|
|
|
e = "Can't open output file for writing: " + sys.argv[2]
|
2013-09-01 17:49:01 +00:00
|
|
|
print(__file__, ":", e, file=sys.stderr)
|
2014-12-03 16:22:06 +00:00
|
|
|
f = sys.stdout
|
2023-08-19 21:05:16 +00:00
|
|
|
canOpenFile = False
|
2013-05-26 04:36:44 +00:00
|
|
|
|
|
|
|
for c in net.components:
|
|
|
|
c.checkvalue()
|
2013-06-14 14:59:52 +00:00
|
|
|
|
2013-09-01 17:49:01 +00:00
|
|
|
print(net.formatXML(), file=f)
|
2023-08-19 21:05:16 +00:00
|
|
|
|
|
|
|
if not canOpenFile:
|
|
|
|
sys.exit(1)
|