Root/emqbit-utilities/kicad/ibis2kc.py

1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3"""
4Copyright (C) 2010 Andres Calderon, andres.calderon@emqbit.com
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20"""
21
22import re
23import sys
24import getopt
25from operator import attrgetter
26from kccomp import *
27
28def pin_cmp(X,Y):
29  r = re.compile('([A-Z|a-z]+)?([0-9]+)?([\x20-\x7E]+)?')
30  
31  mo = r.match(X.name)
32  mi = r.match(Y.name)
33  
34  if mo.group(2) and mi.group(2):
35    return cmp((mo.group(1),int(mo.group(2)),mo.group(3)),(mi.group(1),int(mi.group(2)),mi.group(3)))
36  else:
37    return cmp((mo.group(1),mo.group(2),mo.group(3)),(mi.group(1),mi.group(2),mi.group(3)))
38  
39
40def usage(appname):
41    print "usage : " + appname + " --ibis-file=inputfile.pkg --output-file=output.lib --vcc-prefix=prefix --gnd-prefix=prefix"
42
43def main():
44  try:
45    opts, args = getopt.getopt(sys.argv[1:], 'i:o:v:g:', ['ibis-file=', 'output-file=','vcc-prefix=', 'gnd-prefix='])
46  except getopt.GetoptError:
47    usage(sys.argv[0])
48    sys.exit(2)
49    
50  inputfile=None
51  outputfile=None
52  vcc_prefix="VCC"
53  gnd_prefix="GND"
54  
55  for o, a in opts:
56    if o in ("-i", "--ibis-file"):
57      inputfile = a
58    elif o in ("-o", "--output-file"):
59      outputfile = a
60    elif o in ("-v", "--vcc-prefix"):
61      vcc_prefix = a
62    elif o in ("-g", "--gnd-prefix"):
63      gnd_prefix = a
64    
65  if inputfile==None:
66    usage(sys.argv[0])
67    sys.exit(2)
68    
69  if outputfile==None:
70    outputfile = inputfile.split('.')[0] + '.lib'
71
72  f = open(inputfile,'r')
73
74  raw_data = f.read()
75
76  regexp = re.compile('\[Component\]([\x20-\x7E]*?)[\r\n|\n][\x20-\x7E\n\r]*?\[Pin\].+[\r\n|\n]?([\x20-\x7E\n\r^\|]*?)[\r\n|\n]?\[')
77  
78  matches = [m.groups() for m in regexp.finditer(raw_data)]
79  
80  xl = KcLibrary (outputfile)
81
82  for m in matches:
83    part = m[0].strip()
84    print ' Importing: ' + part
85    
86    comp = KcComponent(part);
87    xl.add_part(comp)
88
89    pins = []
90
91    for line in m[1].split('\n'):
92        tags = re.split('\s+',line.strip())
93        if len(tags[0]) and (len(tags)>1) and (tags[0][0]!="|"):
94          pins.append(KcPin(tags[1],tags[0]))
95    
96    un = KcUnit(1)
97    comp.add_unit(un)
98    
99    pins=sorted(pins, cmp=pin_cmp)
100
101    signals = []
102    for p in pins:
103      if re.search(vcc_prefix,p.name):
104        un.top_pins.append(p)
105      elif re.search(gnd_prefix,p.name):
106        un.bot_pins.append(p)
107      else:
108        signals.append(p)
109    
110    for p in signals:
111     if (p.name.find('#')>0):
112       p.pin_type = 'I'
113       p.electrical_type = 'I'
114       
115    sp=len(signals)/2
116    for p in signals[0:sp]:
117      un.left_pins.append(p)
118        
119    for p in signals[sp:]:
120      un.right_pins.append(p)
121    
122  xl.write()
123
124#end-main
125
126if __name__ == "__main__":
127    main()

Archive Download this file

Branches:
master



interactive