File: netlist_form_OrcadPcb2.py

package info (click to toggle)
kicad 9.0.2%2Bdfsg-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 769,136 kB
  • sloc: cpp: 960,330; ansic: 121,001; xml: 66,428; python: 18,382; sh: 1,010; awk: 301; asm: 292; makefile: 227; javascript: 167; perl: 10
file content (115 lines) | stat: -rw-r--r-- 2,881 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#
# Example python script to generate a BOM from a KiCad generic netlist
#

"""
    @package
    Output: old OrcadPcb2 netlist format
    Sorted By: Ref

    Command line:
    python "pathToFile/netlist_form_OrcadPcb2.py" "%I" "%O.net"
"""

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
canOpenFile = True
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
    canOpenFile = False

components = netlist.getInterestingComponents( excludeBoard=True )

row =""

''' Netlist header:
( { EESchema Netlist Version 1.1  02/09/2021 11:10:00
Eeschema (5.99.0-12178-ge03257b55f-dirty)}
'''
''' Generate line ( { EESchema Netlist Version 1.1 <time> '''
row += '( { EESchema Netlist Version 1.1 ' + netlist.getDate() + '\n'
''' Generate line Eeschema <eeschema version> '''
row += 'Eeschema ' + netlist.getTool() + ' }\n\n'


'''
    This template read each component and creates lines:
     ( 3EBF7DBD $noname U1 74LS125
      ... pin list ...
      )
    and calls "create_pin_list" template to build the pin list
'''

for c in components:
    fp_name = c.getFootprint( False )

    if fp_name == "":
        fp_name = "$noname"

    row += "( " + c.getTimestamp() + ' ' + fp_name
    row += ' ' + c.getRef()
    row += ' ' + c.getValue()
    row += '\n'

    '''
    generate pin list
    The pin list from library description is something like
          <pins>
            <pin num="1" type="passive"/>
            <pin num="2" type="passive"/>
          </pins>
    Output pin list is ( <pin num> <net name> )
    something like
            ( 1 VCC )
            ( 2 GND )
    for not connected pins:
            ( 3 ? )
    '''
    lib = c.getLibPart()
    pinlist = lib.getPinList()

    for pin in pinlist:
        pin_num = pin.get('pin','num')
        row += "   ( " + pin_num + " "
        netname = c.getPinNetname( pin_num, netlist, True )

        if netname == "":
            netname = "?"

        row += netname
        row += " )\n"

    row += ')\n'

row += '\n)\n'

f.write(row.encode('utf-8'))
f.close()

if not canOpenFile:
    sys.exit(1)