You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.2 KiB

  1. #
  2. # Example python script to generate an equivalent XML document from XML input
  3. #
  4. # Example: Round value robin, XML to XML conversion with partial value monging
  5. #
  6. from __future__ import print_function
  7. # Import the KiCad python helper module and the csv formatter
  8. import kicad_netlist_reader
  9. import sys
  10. def checkvalue(self):
  11. """Check values, and replace with preferred/consistent values"""
  12. ref = self.getRef()
  13. r = ref.split("R")
  14. c = ref.split("C")
  15. v = self.getValue()
  16. # Common to all values - convert decimation if necessary
  17. dec = v.split(",")
  18. if (len(dec) == 2):
  19. newval = dec[0] + "." + dec[1]
  20. self.setValue(newval)
  21. v = self.getValue()
  22. if len(r) == 2 and r[1].isdigit():
  23. # This is a resistor - make values consistent
  24. # If the value is a pure value, add R to the end of the value
  25. if v.isdigit():
  26. i = int(v)
  27. if (i > 1000000):
  28. i = i / 1000000
  29. v = str(i) + "M"
  30. if (i > 1000):
  31. i = i / 1000
  32. v = str(i) + "K"
  33. else:
  34. v = str(i) + "R"
  35. self.setValue(v)
  36. else:
  37. # Get the multiplier character
  38. multiplier = v[len(v) - 1]
  39. v = v.strip(multiplier)
  40. v = v.split(".")
  41. if (len(v) == 2):
  42. newval = v[0] + multiplier + v[1]
  43. self.setValue(newval)
  44. v = self.getValue()
  45. # Give components a new method for checking the values (this could easily be a
  46. # Company Part Number generator method instead)
  47. kicad_netlist_reader.comp.checkvalue = checkvalue
  48. # Generate an instance of a generic netlist, and load the netlist tree from
  49. # the command line option. If the file doesn't exist, execution will stop
  50. net = kicad_netlist_reader.netlist(sys.argv[1])
  51. # Open a file to write to, if the file cannot be opened output to stdout
  52. # instead
  53. canOpenFile = True
  54. try:
  55. f = open(sys.argv[2], 'w')
  56. except IOError:
  57. e = "Can't open output file for writing: " + sys.argv[2]
  58. print(__file__, ":", e, file=sys.stderr)
  59. f = sys.stdout
  60. canOpenFile = False
  61. for c in net.components:
  62. c.checkvalue()
  63. print(net.formatXML(), file=f)
  64. if not canOpenFile:
  65. sys.exit(1)