-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlib_LTspice2Kicad.py
206 lines (181 loc) · 9.98 KB
/
lib_LTspice2Kicad.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#! usr/bin/python
# -*- coding: ISO-8859-1 -*-
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
# Written by : Laurent CHARRIER
# last change: 2017, Oct 30.
#
# usage examples : python lib_LTspice2Kicad.py C:\Program Files\LTC\LTspiceXVII\lib\sym
# python lib_LTspice2Kicad.py C:\Program Files\LTC\LTspiceXVII\lib\sym\Comparators
# Those examples will create the files : LTspice_sym.libs and LTspice_Comparators.libs
#
import sys,re,os,codecs
# function to find locaion of each space character in each line
def find_all(a_str, sub):
start = 0
while True:
start = a_str.find(sub, start)
if start == -1: return
yield start
start += len(sub) # use start += 1 to find overlapping matches
directory = sys.argv[1]
# out_file = sys.argv[2]
if directory=="." : directory = os.getcwd()
dir = os.listdir(directory)
comp = []
for component in dir:
if (component[-4:]==".asy") : comp.append(component)
indir = directory.split("\\")
out_file = "LTspice_" + indir[len(indir)-1] + ".lib"
outfl = codecs.open(out_file,"w",'utf-8');
outfl.write("EESchema-LIBRARY Version 2.3\n#encoding utf-8\n#\n")
for component in comp :
print(component)
in_file = directory + "\\" + component
if (component == "ADA4807.asy" or component == "ADA4895.asy") : # I don't know how to detect automatically the file encoding UTF-16-LE with Python
infl = codecs.open(in_file,"r",'utf-16-le');
else : infl = open(in_file,"r");
lines = infl.readlines()
infl.close()
drw_lin = list()
# pins stuffs
pin_pos = []
pin_orient = []
pin_justif=[]
pin_name = []
pin_order = []
pin_off=[]
Value = "Value"
Value_XY = "0 0"
Value_orient = "H"
Value_justif = "L"
Prefix = ""
Prefix_XY = "0 0"
Prefix_orient = "H"
Prefix_justif = "L"
Description = ""
SpiceModel = ""
# read the LTspice library line by line :
for line1 in lines:
line1 = line1.rstrip('\n')
line1 = line1.rstrip('\r')
# print(line1)
spc = list(find_all(line1," ")) # find all space locations to split the variables of the line
if re.match(r"^SYMATTR Prefix *", line1) is not None:
Prefix = line1[15:]
if re.match(r"^WINDOW 0 *", line1) is not None:
Prefix_XY = str(int(3.125*int(line1[spc[1]:spc[2]]))) + " " + str(int(-3.125*int(line1[spc[2]:spc[3]])))
Prefix_orient = "H"
Prefix_justif = line1[spc[3]+1:spc[3]+2]
if Prefix_justif=="V" :
Prefix_orient = "V"
Prefix_justif = line1[spc[3]+2:spc[3]+3]
if re.match(r"^SYMATTR Value *", line1) is not None:
Value = line1[14:]
if re.match(r"^SYMATTR Value2 *", line1) is not None:
Value = line1[15:]
if re.match(r"^WINDOW 3 *", line1) is not None:
Value_XY = str(int(3.125*int(line1[spc[1]:spc[2]]))) + " " + str(int(-3.125*int(line1[spc[2]:spc[3]])))
Value_orient = "H"
Value_justif = line1[spc[3]+1:spc[3]+2]
if Value_justif=="V" :
Value_orient = "V"
Value_justif = line1[spc[3]+2:spc[3]+3]
if re.match(r"^SYMATTR Description *", line1) is not None:
Description = line1[19:]
if re.match(r"^SYMATTR SpiceModel *", line1) is not None:
SpiceModel = line1[18:]
if re.match(r"^LINE *", line1) is not None:
if len(spc)==5 :
drw_lin.append("P 2 0 0 0 " + str(int(3.125*int(line1[spc[1]:spc[2]]))) + " " + str(int(-3.125*int(line1[spc[2]:spc[3]]))) + " " + str(int(3.125*int(line1[spc[3]:spc[4]]))) + " " + str(int(-3.125*int(line1[spc[4]:]))))
else :
drw_lin.append("P 2 0 0 0 " + str(int(3.125*int(line1[spc[1]:spc[2]]))) + " " + str(int(-3.125*int(line1[spc[2]:spc[3]]))) + " " + str(int(3.125*int(line1[spc[3]:spc[4]]))) + " " + str(int(-3.125*int(line1[spc[4]:spc[5]]))))
if re.match(r"^RECTANGLE *", line1) is not None:
if len(spc)==5 :
drw_lin.append("S " + str(int(3.125*int(line1[spc[1]:spc[2]]))) + " " + str(int(-3.125*int(line1[spc[2]:spc[3]]))) + " " + str(int(3.125*int(line1[spc[3]:spc[4]]))) + " " + str(int(-3.125*int(line1[spc[4]:]))) + " 0 0 0 f")
else :
drw_lin.append("S " + str(int(3.125*int(line1[spc[1]:spc[2]]))) + " " + str(int(-3.125*int(line1[spc[2]:spc[3]]))) + " " + str(int(3.125*int(line1[spc[3]:spc[4]]))) + " " + str(int(-3.125*int(line1[spc[4]:spc[5]]))) + " 0 0 0 f")
if re.match(r"^CIRCLE *", line1) is not None:
if len(spc)==5 :
drw_lin.append("C " + str(int(0.5*3.125*(int(line1[spc[1]:spc[2]]) + int(line1[spc[3]:spc[4]])))) + " " + str(int(0.5*-3.125*(int(line1[spc[2]:spc[3]]) + int(line1[spc[4]:])))) + " " + str(int(0.5*3.125*abs(int(line1[spc[1]:spc[2]]) - int(line1[spc[3]:spc[4]])))) + " 0 0 0 N")
else :
drw_lin.append("C " + str(int(0.5*3.125*(int(line1[spc[1]:spc[2]]) + int(line1[spc[3]:spc[4]])))) + " " + str(int(0.5*-3.125*(int(line1[spc[2]:spc[3]]) + int(line1[spc[4]:spc[5]])))) + " " + str(int(0.5*3.125*abs(int(line1[spc[1]:spc[2]]) - int(line1[spc[3]:spc[4]])))) + " 0 0 0 N")
if re.match(r"^ARC *", line1) is not None:
if len(spc)==9 :
drw_lin.append("A " + str(int(0.5*3.125*(int(line1[spc[1]:spc[2]]) + int(line1[spc[3]:spc[4]])))) + " " + str(int(0.5*-3.125*(int(line1[spc[2]:spc[3]]) + int(line1[spc[4]:spc[5]])))) + " " + str(int(0.5*3.125*abs(int(line1[spc[1]:spc[2]]) - int(line1[spc[3]:spc[4]])))) + " 0 900 0 0 0 N " + str(int(3.125*int(line1[spc[5]:spc[6]]))) + " " + str(int(-3.125*int(line1[spc[6]:spc[7]]))) + " " + str(int(3.125*int(line1[spc[7]:spc[8]]))) + " " + str(int(-3.125*int(line1[spc[8]:]))))
else :
drw_lin.append("A " + str(int(0.5*3.125*(int(line1[spc[1]:spc[2]]) + int(line1[spc[3]:spc[4]])))) + " " + str(int(0.5*-3.125*(int(line1[spc[2]:spc[3]]) + int(line1[spc[4]:spc[5]])))) + " " + str(int(0.5*3.125*abs(int(line1[spc[1]:spc[2]]) - int(line1[spc[3]:spc[4]])))) + " 0 900 0 0 0 N " + str(int(3.125*int(line1[spc[5]:spc[6]]))) + " " + str(int(-3.125*int(line1[spc[6]:spc[7]]))) + " " + str(int(3.125*int(line1[spc[7]:spc[8]]))) + " " + str(int(-3.125*int(line1[spc[8]:spc[9]]))))
if re.match(r"^TEXT *", line1) is not None:
if (line1[spc[3]+1:spc[3]+2]=="V"):
text_orient = "1 "
text_justif = line1[spc[3]+2:spc[3]+3]
else :
text_orient = "0 "
text_justif = line1[spc[3]+1:spc[3]+2]
drw_lin.append("T " + text_orient + str(int(3.125*int(line1[spc[0]:spc[1]]))) + " " + str(int(-3.125*int(line1[spc[1]:spc[2]]))) + " 50 0 0 1 " + line1[spc[4]:])
# drw_lin.append("T " + text_orient + str(int(3.125*int(line1[spc[0]:spc[1]]))) + " " + str(int(-3.125*int(line1[spc[1]:spc[2]]))) + " 0 0 1 " + line1[spc[4]:] + " N N " + text_justif)
if ((re.match(r"^PIN *", line1) is not None) and not(re.match(r"^PINATTR *", line1) is not None)):
pin_pos.append(str(int(3.125*int(line1[spc[0]:spc[1]]))) + " " + str(int(-3.125*int(line1[spc[1]:spc[2]]))))
pin_off.append(str(int(3.125*int(line1[spc[3]:]))))
if (line1[spc[2]+1:spc[2]+2]=="V") :
pin_orient.append("V")
pin_justif.append(line1[spc[2]+2:spc[2]+3])
else :
pin_orient.append("H")
pin_justif.append(line1[spc[2]+1:spc[2]+2])
if re.match(r"^PINATTR SpiceOrder *", line1) is not None:
pin_order.append(line1[spc[1]:])
if re.match(r"^PINATTR PinName *", line1) is not None:
pin_name.append(line1[spc[1]:])
# output the data in Kicad format
outfl.write("# " + component[0:len(component)-4] + "\n")
if Description != "":
outfl.write("# " + Description + "\n")
if SpiceModel != "":
outfl.write("# SpiceModel : " + SpiceModel + "\n")
outfl.write("#\n")
if (Prefix=="B" or Prefix=="E" or Prefix=="F" or Prefix=="G" or Prefix=="H" or Prefix=="I" or Prefix=="V") : Pow="P"
else : Pow="N"
if (Prefix=="X" or Prefix=="U"): ViewPin = "Y"
else : ViewPin = "N"
outfl.write("DEF " + component[0:len(component)-4] + " " + Prefix + " 0 1 N " + ViewPin + " 1 F " + Pow + "\n")
if ((Prefix_justif == "B") or (Prefix_justif == "T")):
outfl.write("F0 \"" + Prefix + "\" " + Prefix_XY + " 50 " + Prefix_orient + " V C " + Prefix_justif + "NN\n")
else :
outfl.write("F0 \"" + Prefix + "\" " + Prefix_XY + " 50 " + Prefix_orient + " V " + Prefix_justif + " CNN\n")
if ((Value_justif == "B") or (Value_justif == "T")):
outfl.write("F1 \"" + component[0:len(component)-4] + "\" " + Value_XY + " 50 " + Value_orient + " V C " + Value_justif + "NN\n")
else :
outfl.write("F1 \"" + component[0:len(component)-4] + "\" " + Value_XY + " 50 " + Value_orient + " V " + Value_justif + " CNN\n")
# the value is transferd to F5 instead of F1 because F1 text should be the component name
if Value != "Value" :
if ((Value_justif == "B") or (Value_justif == "T")):
outfl.write("F5 \"" + Value + "\" " + Value_XY + " 50 " + Value_orient + " I C " + Value_justif + "NN\n")
else :
outfl.write("F5 \"" + Value + "\" " + Value_XY + " 50 " + Value_orient + " I " + Value_justif + " CNN\n")
if (Pow=="N") : outfl.write("$FPLIST\n " + Prefix + "_*\n$ENDFPLIST\n")
#DRAWINGS and PINS
outfl.write("DRAW\n")
for i in range(0,len(drw_lin)) :
outfl.write(drw_lin[i] + "\n")
for i in range(0,len(pin_name)) :
pinjustif = pin_justif[i]
if pin_justif[i] == "L" : pinjustif = "R"
if pin_justif[i] == "R" : pinjustif = "L"
if pin_justif[i] == "T" : pinjustif = "D"
if pin_justif[i] == "B" : pinjustif = "U"
outfl.write("X " + pin_name[i].replace(" ","") + " " + pin_order[i] + " " + pin_pos[i] + " 0 " + pinjustif + " 50 50 1 1 U\n")
outfl.write("ENDDRAW\nENDDEF\n#\n")
outfl.close()