summaryrefslogtreecommitdiff
path: root/src/kicadtoNgspice/KicadtoNgspice.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/kicadtoNgspice/KicadtoNgspice.py')
-rw-r--r--src/kicadtoNgspice/KicadtoNgspice.py101
1 files changed, 93 insertions, 8 deletions
diff --git a/src/kicadtoNgspice/KicadtoNgspice.py b/src/kicadtoNgspice/KicadtoNgspice.py
index cf70eab9..c9b5ac02 100644
--- a/src/kicadtoNgspice/KicadtoNgspice.py
+++ b/src/kicadtoNgspice/KicadtoNgspice.py
@@ -23,6 +23,7 @@ import Analysis
import Source
import Model
import DeviceModel
+import SubcircuitTab
import Convert
import TrackWidget
@@ -107,8 +108,11 @@ class MainWindow(QtGui.QWidget):
obj_devicemodel=DeviceModel.DeviceModel(schematicInfo)
self.deviceModelTab.setWidget(obj_devicemodel)
self.deviceModelTab.setWidgetResizable(True)
-
-
+ global obj_subcircuitTab
+ self.subcircuitTab = QtGui.QScrollArea()
+ obj_subcircuitTab = SubcircuitTab.SubcircuitTab(schematicInfo)
+ self.subcircuitTab.setWidget(obj_subcircuitTab)
+ self.subcircuitTab.setWidgetResizable(True)
self.tabWidget = QtGui.QTabWidget()
#self.tabWidget.TabShape(QtGui.QTabWidget.Rounded)
@@ -116,6 +120,7 @@ class MainWindow(QtGui.QWidget):
self.tabWidget.addTab(self.sourceTab,"Source Details")
self.tabWidget.addTab(self.modelTab,"NgSpice Model")
self.tabWidget.addTab(self.deviceModelTab,"Device Modeling")
+ self.tabWidget.addTab(self.subcircuitTab,"Subcircuits")
self.mainLayout = QtGui.QVBoxLayout()
self.mainLayout.addWidget(self.tabWidget)
#self.mainLayout.addStretch(1)
@@ -343,9 +348,10 @@ class MainWindow(QtGui.QWidget):
if check==1:
for child in attr_parent:
if child.tag=="devicemodel":
+ del child[:]
attr_devicemodel=child
- print "Device model dict",obj_devicemodel.devicemodel_dict_beg
- print "Device model dict end",obj_devicemodel.devicemodel_dict_end
+ #print "Device model dict",obj_devicemodel.devicemodel_dict_beg
+ #print "Device model dict end",obj_devicemodel.devicemodel_dict_end
##########################
for i in obj_devicemodel.devicemodel_dict_beg:
attr_var=ET.SubElement(attr_devicemodel,i)
@@ -369,6 +375,23 @@ class MainWindow(QtGui.QWidget):
###################################################################################################################
+ if check==0:
+ attr_subcircuit=ET.SubElement(attr_parent,"subcircuit")
+ if check==1:
+ for child in attr_parent:
+ if child.tag=="subcircuit":
+ del child[:]
+ attr_subcircuit=child
+ ##########################
+ for i in obj_subcircuitTab.subcircuit_dict_beg:
+ attr_var=ET.SubElement(attr_subcircuit,i)
+ it=obj_subcircuitTab.subcircuit_dict_beg[i]
+ end=obj_subcircuitTab.subcircuit_dict_end[i]
+
+ while it<=end:
+ ET.SubElement(attr_var,"field").text=str(obj_subcircuitTab.entry_var[it].text())
+ it=it+1
+
tree=ET.ElementTree(attr_parent)
tree.write(fw)
@@ -388,7 +411,9 @@ class MainWindow(QtGui.QWidget):
#Adding Device Library to SchematicInfo
schematicInfo = self.obj_convert.addDeviceLibrary(schematicInfo,kicadFile)
-
+ #Adding Subcircuit Library to SchematicInfo
+ schematicInfo = self.obj_convert.addSubcircuit(schematicInfo, kicadFile)
+
analysisoutput = self.obj_convert.analysisInsertor(self.obj_track.AC_entry_var["ITEMS"],
self.obj_track.DC_entry_var["ITEMS"],
self.obj_track.TRAN_entry_var["ITEMS"],
@@ -411,8 +436,12 @@ class MainWindow(QtGui.QWidget):
print "There was error while converting kicad to ngspice"
self.close()
-
-
+ # Generate .sub file from .cir.out file if it is a subcircuit
+ subPath = os.path.splitext(kicadFile)[0]
+
+ if len(sys.argv)>2:
+ if sys.argv[2] == "sub":
+ self.createSubFile(subPath)
def createNetlistFile(self,schematicInfo):
print "Creating Final netlist"
@@ -508,7 +537,63 @@ class MainWindow(QtGui.QWidget):
out.close()
-
+ def createSubFile(self,subPath):
+ self.project = subPath
+ self.projName = os.path.basename(self.project)
+ if os.path.exists(self.project+".cir.out"):
+ try:
+ f = open(self.project+".cir.out")
+ except :
+ print("Error in opening circuit file.")
+ else:
+ print self.projName + ".cir.out does not exist. Please create a spice netlist."
+
+ # Read the data from file
+ data=f.read()
+ # Close the file
+
+ f.close()
+ newNetlist=[]
+ netlist=iter(data.splitlines())
+ for eachline in netlist:
+ eachline=eachline.strip()
+ if len(eachline)<1:
+ continue
+ words=eachline.split()
+ if eachline[2] == 'u':
+ if words[len(words)-1] == "port":
+ subcktInfo = ".subckt "+self.projName+" "
+ for i in range(2,len(words)-1):
+ subcktInfo+=words[i]+" "
+ continue
+ if words[0] == ".end" or words[0] == ".ac" or words[0] == ".dc" or words[0] == ".tran" or words[0] == '.disto' or words[0] == '.noise' or words[0] == '.op' or words[0] == '.pz' or words[0] == '.sens' or words[0] == '.tf':
+ continue
+ elif words[0] == ".control":
+ while words[0] != ".endc":
+ eachline=netlist.next()
+ eachline=eachline.strip()
+ if len(eachline)<1:
+ continue
+ words=eachline.split()
+ else:
+ newNetlist.append(eachline)
+
+ outfile=self.project+".sub"
+ out=open(outfile,"w")
+ out.writelines("* Subcircuit " + self.projName)
+ out.writelines('\n')
+ out.writelines(subcktInfo)
+ out.writelines('\n')
+
+ for i in range(len(newNetlist),0,-1):
+ newNetlist.insert(i,'\n')
+
+ out.writelines(newNetlist)
+ out.writelines('\n')
+
+ out.writelines('.ends ' + self.projName)
+ print "The subcircuit has been written in "+self.projName+".sub"
+
#Main Function