forked from lxyppc/kicad_tools
-
Notifications
You must be signed in to change notification settings - Fork 56
/
loadnet.py
90 lines (83 loc) · 2.59 KB
/
loadnet.py
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
from . import kisexp as sexp
import pcbnew as pn
import io
import traceback
import os
def loadNet(brd = None):
if not brd:
brd = pn.GetBoard()
name = brd.GetFileName()
name = name[0:name.rindex('.')] + '.net'
if os.path.exists(name):
return loadNetFile(name)
if hasattr(brd, "GetFootprints"):
print("File not exist, try to get info from footprint", name)
r = {}
for fp in brd.GetFootprints():
c = parseFootprint(fp)
r[c['value'] + "&" + c['footprint']] = c
return r
return {}
def parseFootprint(fp):
r = {}
prop = fp.GetProperties()
r['value'] = fp.GetValue()
r['footprint'] = str(fp.GetFPID().GetLibItemName())
if "Datasheet" in prop:
r['datasheet'] = prop["Datasheet"]
if "SuppliersPartNumber" in prop:
r['partNumber'] = prop["SuppliersPartNumber"]
if "Comment" in prop:
if prop["Comment"] != "":
r['comment'] = prop["Comment"]
if "description" in prop:
if prop["description"] != "":
r['description'] = prop["description"]
return r
def toStr(v):
return v
def parseComp(comp):
r = {}
if comp[0] != "comp":
print("Parse comp error")
return None
for i in range(1, len(comp)):
key = comp[i][0]
if key == "value":
r['value'] = toStr(comp[i][1])
if key == "footprint":
fp = toStr(comp[i][1])
pos = fp.rfind(':')
if pos != -1:
fp = fp[pos+1:]
r['footprint'] = fp
if key == "datasheet":
r['datasheet'] = toStr(comp[i][1])
if key == "fields":
fields = comp[i]
for j in range(1, len(fields)):
field = fields[j]
fkey = toStr(field[1][1])
if fkey == "SuppliersPartNumber":
r['partNumber'] = toStr(field[2])
if fkey == "Comment":
r['comment'] = toStr(field[2])
if fkey == "description":
r['description'] = toStr(field[2])
return r
def loadNetFile(fileName):
try:
nets = sexp.loadKicadNet(fileName)
if nets[3][0] != "components":
return None
comps = nets[3]
r = {}
for i in range(1, len(comps)):
comp = comps[i]
c = parseComp(comp)
r[c['value'] + "&" + c['footprint']] = c
return r
except Exception as e:
print("Fail to load netlist:")
traceback.print_exc()
return None