1 Star 0 Fork 2

linbinzl/kicad_tools

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
loadnet.py 2.59 KB
一键复制 编辑 原始数据 按行查看 历史
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
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/null_937_1579/kicad_tools.git
git@gitee.com:null_937_1579/kicad_tools.git
null_937_1579
kicad_tools
kicad_tools
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385