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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
from PyQt4 import QtGui
import os
import sys
import shutil
import TrackWidget
from xml.etree import ElementTree as ET
class Convert:
"""
This class has all the necessary function required to convert kicad netlist to ngspice netlist.
"""
def __init__(self,sourcelisttrack,source_entry_var,schematicInfo):
print "Start Conversion"
self.sourcelisttrack = sourcelisttrack
self.schematicInfo = schematicInfo
self.entry_var = source_entry_var
self.sourcelistvalue = []
def addSourceParameter(self):
"""
This function add the source details to schematicInfo
"""
#print "Adding Source parameter"
#print "SourceListTrack : ",self.sourcelisttrack
#print "Schematic Info ",self.schematicInfo
#print "Entry Var",self.entry_var
self.start = 0
self.end = 0
for compline in self.sourcelisttrack:
self.index = compline[0]
self.addline = self.schematicInfo[self.index]
if compline[1] == 'sine':
try:
self.start = compline[2]
self.end = compline[3]
vo_val = str(self.entry_var[self.start].text()) if len(str(self.entry_var[self.start].text())) > 0 else '0'
va_val = str(self.entry_var[self.start+1].text()) if len(str(self.entry_var[self.start+1].text())) > 0 else '0'
freq_val = str(self.entry_var[self.start+2].text()) if len(str(self.entry_var[self.start+2].text())) > 0 else '0'
td_val = str(self.entry_var[self.start+3].text()) if len(str(self.entry_var[self.start+3].text())) > 0 else '0'
theta_val = str(self.entry_var[self.end].text()) if len(str(self.entry_var[self.end].text())) > 0 else '0'
self.addline = self.addline.partition('(')[0] + "("+vo_val+" "+va_val+" "+freq_val+" "+td_val+" "+theta_val+")"
self.sourcelistvalue.append([self.index,self.addline])
except:
print "Caught an exception in sine voltage source ",self.addline
elif compline[1] == 'pulse':
try:
self.start = compline[2]
self.end = compline[3]
v1_val = str(self.entry_var[self.start].text()) if len(str(self.entry_var[self.start].text())) > 0 else '0'
v2_val = str(self.entry_var[self.start+1].text()) if len(str(self.entry_var[self.start+1].text())) > 0 else '0'
td_val = str(self.entry_var[self.start+2].text()) if len(str(self.entry_var[self.start+2].text())) > 0 else '0'
tr_val = str(self.entry_var[self.start+3].text()) if len(str(self.entry_var[self.start+3].text())) > 0 else '0'
tf_val = str(self.entry_var[self.start+4].text()) if len(str(self.entry_var[self.start+4].text())) > 0 else '0'
pw_val = str(self.entry_var[self.start+5].text()) if len(str(self.entry_var[self.start+5].text())) > 0 else '0'
tp_val = str(self.entry_var[self.end].text()) if len(str(self.entry_var[self.end].text())) > 0 else '0'
self.addline = self.addline.partition('(')[0] + "("+v1_val+" "+v2_val+" "+td_val+" "+tr_val+" "+tf_val+" "+pw_val+" "+tp_val+")"
self.sourcelistvalue.append([self.index,self.addline])
except:
print "Caught an exception in pulse voltage source ",self.addline
elif compline[1] == 'pwl':
try:
self.start = compline[2]
self.end = compline[3]
t_v_val = str(self.entry_var[self.start].text()) if len(str(self.entry_var[self.start].text())) > 0 else '0 0'
self.addline = self.addline.partition('(')[0] + "("+t_v_val+")"
self.sourcelistvalue.append([self.index,self.addline])
except:
print "Caught an exception in pwl voltage source ",self.addline
elif compline[1] == 'ac':
try:
self.start = compline[2]
self.end = compline[3]
va_val=str(self.entry_var[self.start].text()) if len(str(self.entry_var[self.start].text())) > 0 else '0'
self.addline = ' '.join(self.addline.split())
self.addline = self.addline.partition('ac')[0] +" "+'ac'+" "+ va_val
self.sourcelistvalue.append([self.index,self.addline])
except:
print "Caught an exception in ac voltage source ",self.addline
elif compline[1] == 'dc':
try:
self.start = compline[2]
self.end = compline[3]
v1_val = str(self.entry_var[self.start].text()) if len(str(self.entry_var[self.start].text())) > 0 else '0'
self.addline = ' '.join(self.addline.split())
self.addline = self.addline.partition('dc')[0] + " " +'dc'+ " "+v1_val
self.sourcelistvalue.append([self.index,self.addline])
except:
print "Caught an exception in dc voltage source",self.addline
elif compline[1] == 'exp':
try:
self.start = compline[2]
self.end = compline[3]
v1_val = str(self.entry_var[self.start].text()) if len(str(self.entry_var[self.start].text())) > 0 else '0'
v2_val = str(self.entry_var[self.start+1].text()) if len(str(self.entry_var[self.start+1].text())) > 0 else '0'
td1_val = str(self.entry_var[self.start+2].text()) if len(str(self.entry_var[self.start+2].text())) > 0 else '0'
tau1_val = str(self.entry_var[self.start+3].text()) if len(str(self.entry_var[self.start+3].text())) > 0 else '0'
td2_val = str(self.entry_var[self.start+4].text()) if len(str(self.entry_var[self.start+4].text())) > 0 else '0'
tau2_val = str(self.entry_var[self.end].text()) if len(str(self.entry_var[self.end].text())) > 0 else '0'
self.addline = self.addline.partition('(')[0] + "("+v1_val+" "+v2_val+" "+td1_val+" "+tau1_val+" "+td2_val+" "+tau2_val+")"
self.sourcelistvalue.append([self.index,self.addline])
except:
print "Caught an exception in exp voltage source ",self.addline
#Updating Schematic with source value
for item in self.sourcelistvalue:
del self.schematicInfo[item[0]]
self.schematicInfo.insert(item[0],item[1])
return self.schematicInfo
def analysisInsertor(self,ac_entry_var,dc_entry_var, tran_entry_var,set_checkbox,ac_parameter,dc_parameter,tran_parameter,ac_type,op_check):
"""
This function creates an analysis file in current project
"""
self.ac_entry_var = ac_entry_var
self.dc_entry_var = dc_entry_var
self.tran_entry_var = tran_entry_var
self.set_checkbox = set_checkbox
self.ac_parameter= ac_parameter
self.dc_parameter= dc_parameter
self.trans_parameter = tran_parameter
self.ac_type= ac_type
self.op_check = op_check
self.no=0
self.variable=self.set_checkbox
self.direct= sys.argv[1]
(filepath, filemname)= os.path.split(self.direct)
self.Fileopen = os.path.join(filepath, "analysis")
self.writefile= open(self.Fileopen,"w")
if self.variable== 'AC':
self.no=0
self.writefile.write(".ac"+' ' + self.ac_type + ' '+ str(self.defaultvalue(self.ac_entry_var[self.no+2].text()))+' ' + str(self.defaultvalue(self.ac_entry_var[self.no].text())) + self.ac_parameter[self.no]+ ' ' + str(self.defaultvalue(self.ac_entry_var[self.no+1].text())) + self.ac_parameter[self.no+1] )
elif self.variable=='DC':
if self.op_check[-1] == 1:
self.no=0
self.writefile.write(".op")
elif self.op_check[-1] == 0:
self.no=0
self.writefile.write(".dc" +' '+ str(self.dc_entry_var[self.no].text())+ ' '+ str(self.defaultvalue(self.dc_entry_var[self.no+1].text())) + self.converttosciform(self.dc_parameter[self.no]) + ' '+ str(self.defaultvalue(self.dc_entry_var[self.no+3].text()))+ self.converttosciform(self.dc_parameter[self.no+2]) + ' '+ str(self.defaultvalue(self.dc_entry_var[self.no+2].text())) + self.converttosciform(self.dc_parameter[self.no+1]))
elif self.variable == 'TRAN':
self.no= 0
self.writefile.write(".tran" + ' '+ str(self.defaultvalue(self.tran_entry_var[self.no+1].text())) + self.converttosciform(self.trans_parameter[self.no+1]) + ' ' + str(self.defaultvalue(self.tran_entry_var[self.no+2].text())) + self.converttosciform(self.trans_parameter[self.no+2])+' '+ str(self.defaultvalue(self.tran_entry_var[self.no].text()))+ self.converttosciform(self.trans_parameter[self.no]))
else:
pass
self.writefile.close()
def converttosciform(self, string_obj):
"""
This function is used for scientific conversion.
"""
self.string_obj = string_obj
if self.string_obj[0] == 'm':
return "e-03"
elif self.string_obj[0] == 'u':
return "e-06"
elif self.string_obj[0] == 'n':
return "e-09"
elif self.string_obj[0] == 'p':
return "e-12"
else:
return "e-00"
def defaultvalue(self, value):
"""
This function select default value as 0 if Analysis widget do not hold any value.
"""
self.value= value
if self.value == '':
return 0
else:
return self.value
def addModelParameter(self,schematicInfo):
"""
This function add the Ngspice Model details to schematicInfo
"""
#Create object of TrackWidget
self.obj_track = TrackWidget.TrackWidget()
#List to store model line
addmodelLine = []
modelParamValue = []
for line in self.obj_track.modelTrack:
#print "Model Track :",line
if line[2] == 'transfo':
try:
start=line[5]
end=line[6]
num_turns=str(self.obj_track.model_entry_var[start].text())
if num_turns=="": num_turns="310"
h_array= "H_array = [ "
b_array = "B_array = [ "
h1=str(self.obj_track.model_entry_var[start+1].text())
b1=str(self.obj_track.model_entry_var[start+2].text())
if len(h1)!=0 and len(b1)!=0:
h_array=h_array+h1+" "
b_array=b_array+b1+" "
bh_array = h_array+" ] " + b_array+" ]"
else:
bh_array = "H_array = [-1000 -500 -375 -250 -188 -125 -63 0 63 125 188 250 375 500 1000] B_array = [-3.13e-3 -2.63e-3 -2.33e-3 -1.93e-3 -1.5e-3 -6.25e-4 -2.5e-4 0 2.5e-4 6.25e-4 1.5e-3 1.93e-3 2.33e-3 2.63e-3 3.13e-3]"
area=str(self.obj_track.model_entry_var[start+3].text())
length=str(self.obj_track.model_entry_var[start+4].text())
if area=="": area="1"
if length=="":length="0.01"
num_turns2=str(self.obj_track.model_entry_var[start+5].text())
if num_turns2=="": num_turns2="620"
addmodelLine=".model "+line[3]+"_primary lcouple (num_turns= "+num_turns+")"
modelParamValue.append([line[0],addmodelLine,line[4]])
addmodelLine=".model "+line[3]+"_iron_core core ("+bh_array+" area = "+area+" length ="+length +")"
modelParamValue.append([line[0],addmodelLine,line[4]])
addmodelLine=".model "+line[3]+"_secondary lcouple (num_turns ="+num_turns2+ ")"
modelParamValue.append([line[0],addmodelLine,line[4]])
except:
print "Caught an exception in transfo model ",line[1]
else:
try:
start = line[5]
end = line[6]
addmodelLine=".model "+ line[3]+" "+line[2]+"("
for key,value in line[9].iteritems():
#print "Tags: ",key
#print "Value: ",value
#Checking for default value and accordingly assign param and default.
if ':' in key:
key = key.split(':')
param = key[0]
default = key[1]
else:
param = key
default = 0
#Cheking if value is iterable.its for vector
if hasattr(value, '__iter__'):
addmodelLine += param+"=["
for lineVar in value:
if str(self.obj_track.model_entry_var[lineVar].text()) == "":
paramVal = default
else:
paramVal = str(self.obj_track.model_entry_var[lineVar].text())
addmodelLine += paramVal+" "
addmodelLine += "] "
else:
if str(self.obj_track.model_entry_var[value].text()) == "":
paramVal = default
else:
paramVal = str(self.obj_track.model_entry_var[value].text())
addmodelLine += param+"="+paramVal+" "
addmodelLine += ") "
modelParamValue.append([line[0],addmodelLine,line[4]])
except:
print "Caught an exception in gain model ",line[1]
#Adding it to schematic
for item in modelParamValue:
schematicInfo.append(item[2]) #Adding Comment
schematicInfo.append(item[1]) #Adding model line
return schematicInfo
def addDeviceLibrary(self,schematicInfo,kicadFile):
"""
This function add the library details to schematicInfo
"""
print "Adding Device library to Schematic info file"
(projpath,filename) = os.path.split(kicadFile)
print "Project Path",projpath
deviceLibList = self.obj_track.deviceModelTrack
deviceLine = {} #Key:Index, Value:with its updated line in the form of list
includeLine = [] #All .include line list
if not deviceLibList:
print "No Library Added in the schematic"
pass
else:
for eachline in schematicInfo:
words = eachline.split()
if words[0] in deviceLibList:
print "Found Library line"
index = schematicInfo.index(eachline)
completeLibPath = deviceLibList[words[0]]
(libpath,libname) = os.path.split(completeLibPath)
print "Library Path :",libpath
#Copying library from devicemodelLibrary to Project Path
#Special case for MOSFET
if eachline[0] == 'm':
#For mosfet library name come along with MOSFET dimension information
tempStr = libname.split(':')
libname = tempStr[0]
dimension = tempStr[1]
#Replace last word with library name
#words[-1] = libname.split('.')[0]
words[-1] = self.getRefrenceName(libname,libpath)
#Appending Dimension of MOSFET
words.append(dimension)
deviceLine[index] = words
includeLine.append(".include "+libname)
#src = completeLibPath.split(':')[0] # <----- Not working in Windows
(src_path,src_lib) = os.path.split(completeLibPath)
src_lib = src_lib.split(':')[0]
src = os.path.join(src_path,src_lib)
dst = projpath
shutil.copy2(src, dst)
else:
#Replace last word with library name
#words[-1] = libname.split('.')[0]
words[-1] = self.getRefrenceName(libname,libpath)
deviceLine[index] = words
includeLine.append(".include "+libname)
src = completeLibPath
dst = projpath
shutil.copy2(src,dst)
else:
pass
#Adding device line to schematicInfo
for index,value in deviceLine.iteritems():
#Update the device line
strLine = " ".join(str(item) for item in value)
schematicInfo[index] = strLine
#This has to be second i.e after deviceLine details
#Adding .include line to Schematic Info at the start of line
for item in list(set(includeLine)):
schematicInfo.insert(0,item)
return schematicInfo
def addSubcircuit(self,schematicInfo,kicadFile):
"""
This function add the subcircuit to schematicInfo
"""
print "Adding Subcircuit to Schematic info file"
(projpath,filename) = os.path.split(kicadFile)
subList = self.obj_track.subcircuitTrack
subLine = {} #Key:Index, Value:with its updated line in the form of list
includeLine = [] #All .include line list
if len(self.obj_track.subcircuitList) != len(self.obj_track.subcircuitTrack):
self.msg = QtGui.QErrorMessage()
self.msg.showMessage("Conversion failed. Please add all Subcircuits.")
self.msg.setWindowTitle("Error Message")
self.msg.show()
raise Exception('All subcircuit directories need to be specified.')
elif not subList:
print "No Subcircuit Added in the schematic"
pass
else:
for eachline in schematicInfo:
words = eachline.split()
if words[0] in subList:
print "Found Subcircuit line"
index = schematicInfo.index(eachline)
completeSubPath = subList[words[0]]
(subpath,subname) = os.path.split(completeSubPath)
print "Library Path :",subpath
#Copying library from devicemodelLibrary to Project Path
#Replace last word with library name
words[-1] = subname.split('.')[0]
subLine[index] = words
includeLine.append(".include "+subname+".sub")
src = completeSubPath
dst = projpath
print os.listdir(src)
for files in os.listdir(src):
if os.path.isfile(os.path.join(src,files)):
if files != "analysis":
shutil.copy2(os.path.join(src,files),dst)
else:
pass
#Adding subcircuit line to schematicInfo
for index,value in subLine.iteritems():
#Update the subcircuit line
strLine = " ".join(str(item) for item in value)
schematicInfo[index] = strLine
#This has to be second i.e after subcircuitLine details
#Adding .include line to Schematic Info at the start of line
for item in list(set(includeLine)):
schematicInfo.insert(0,item)
return schematicInfo
def getRefrenceName(self,libname,libpath):
libname = libname.replace('.lib','.xml')
library = os.path.join(libpath,libname)
#Extracting Value from XML
libtree = ET.parse(library)
for child in libtree.iter():
if child.tag == 'ref_model':
retVal = child.text
else:
pass
return retVal
|