From 130958e48e7a3499da59325a9cfe51dd589ee257 Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Wed, 1 Sep 2021 20:49:59 +0200 Subject: [PATCH] Eeschema: add python script netlist_form_cadstar.py to replace netlist_form_cadstar.xsl --- .../python_scripts/kicad_netlist_reader.py | 4 + .../python_scripts/netlist_form_cadstar.py | 115 ++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 eeschema/plugins/python_scripts/netlist_form_cadstar.py diff --git a/eeschema/plugins/python_scripts/kicad_netlist_reader.py b/eeschema/plugins/python_scripts/kicad_netlist_reader.py index 5011b091fe..ccb451a759 100644 --- a/eeschema/plugins/python_scripts/kicad_netlist_reader.py +++ b/eeschema/plugins/python_scripts/kicad_netlist_reader.py @@ -531,6 +531,10 @@ class netlist(): """Return the tool string which was used to create the netlist tree""" return self.design.get("tool") + def getNets(self): + """Return the nets """ + return self.nets + def gatherComponentFieldUnion(self, components=None): """Gather the complete 'set' of unique component fields, fields found in any component. """ diff --git a/eeschema/plugins/python_scripts/netlist_form_cadstar.py b/eeschema/plugins/python_scripts/netlist_form_cadstar.py new file mode 100644 index 0000000000..c5e71ab438 --- /dev/null +++ b/eeschema/plugins/python_scripts/netlist_form_cadstar.py @@ -0,0 +1,115 @@ +# +# Example python script to generate a BOM from a KiCad generic netlist +# + +""" + @package + Output: Cadstar RINF netlist + Sorted By: Ref + + Command line: + python "pathToFile/bom_txt_sorted_by_ref.py" "%I" "%O.rinf" +""" + +from __future__ import print_function + +# Import the KiCad python helper module +import kicad_netlist_reader +import sys + +# A helper function to convert a UTF8/Unicode/locale string read in netlist +# for python2 or python3 (Windows/unix) +def fromNetlistText( aText ): + if sys.platform.startswith('win32'): + try: + return aText.encode('utf-8').decode('cp1252') + except UnicodeDecodeError: + return aText + else: + return aText + +# 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 +netlist = kicad_netlist_reader.netlist(sys.argv[1]) + +# Open a file to write to, if the file cannot be opened output to stdout +# instead +try: + f = open(sys.argv[2], 'wb') +except IOError: + e = "Can't open output file for writing: " + sys.argv[2] + print(__file__, ":", e, sys.stderr) + f = sys.stdout + +components = netlist.getInterestingComponents() + +row ="" + +''' Netlist header ''' +row += ".HEA" + '\n' +''' Generate line .TIM