diff options
author | Tanay Mathur | 2015-06-23 11:50:52 +0530 |
---|---|---|
committer | Tanay Mathur | 2015-06-23 11:50:52 +0530 |
commit | 39fecda9f1be8b1007552437d53c06bdc02f4b47 (patch) | |
tree | 3f4bffd09e28240509a215a4b00b3f8b4dda712a /src/kicadtoNgspice | |
parent | f47a744451fe634efb487023f073321ceed4664c (diff) | |
download | eSim-39fecda9f1be8b1007552437d53c06bdc02f4b47.tar.gz eSim-39fecda9f1be8b1007552437d53c06bdc02f4b47.tar.bz2 eSim-39fecda9f1be8b1007552437d53c06bdc02f4b47.zip |
Added subcircuit functionality
Diffstat (limited to 'src/kicadtoNgspice')
-rw-r--r-- | src/kicadtoNgspice/Convert.py | 66 | ||||
-rw-r--r-- | src/kicadtoNgspice/KicadtoNgspice.py | 79 | ||||
-rw-r--r-- | src/kicadtoNgspice/Processing.py | 214 | ||||
-rw-r--r-- | src/kicadtoNgspice/SubcircuitTab.py | 103 | ||||
-rw-r--r-- | src/kicadtoNgspice/TrackWidget.py | 7 |
5 files changed, 353 insertions, 116 deletions
diff --git a/src/kicadtoNgspice/Convert.py b/src/kicadtoNgspice/Convert.py index 739963ba..baf842d9 100644 --- a/src/kicadtoNgspice/Convert.py +++ b/src/kicadtoNgspice/Convert.py @@ -1,3 +1,5 @@ +from PyQt4 import QtGui + import os import sys import shutil @@ -352,11 +354,69 @@ class Convert: #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) diff --git a/src/kicadtoNgspice/KicadtoNgspice.py b/src/kicadtoNgspice/KicadtoNgspice.py index cf70eab9..7e8617ae 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) @@ -388,7 +393,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 +418,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 +519,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 diff --git a/src/kicadtoNgspice/Processing.py b/src/kicadtoNgspice/Processing.py index 9295a058..fa75320f 100644 --- a/src/kicadtoNgspice/Processing.py +++ b/src/kicadtoNgspice/Processing.py @@ -174,116 +174,118 @@ class PrcocessNetlist: print "Words",words print "compName",compName #Looking if model file is present - xmlfile = compType+".xml" #XML Model File - count = 0 #Check if model of same name is present - modelPath = [] - all_dir = [x[0] for x in os.walk(PrcocessNetlist.modelxmlDIR)] - for each_dir in all_dir: - all_file = os.listdir(each_dir) - if xmlfile in all_file: - count += 1 - modelPath.append(os.path.join(each_dir,xmlfile)) - - if count > 1: - multipleModelList.append(modelPath) - elif count == 0: - unknownModelList.append(compType) - elif count == 1: - try: - print "Start Parsing :",modelPath - tree = ET.parse(modelPath[0]) - - root = tree.getroot() - #Getting number of nodes for model and title - for child in tree.iter(): - if child.tag == 'node_number': - num_of_nodes = int(child.text) - elif child.tag == 'title': - title = child.text+" "+compName - elif child.tag == 'name': - modelname = child.text - elif child.tag == 'type': - #Checking for Analog and Digital - type = child.text - elif child.tag == 'split': - splitDetail = child.text - + if compType != "port": + xmlfile = compType+".xml" #XML Model File + count = 0 #Check if model of same name is present + modelPath = [] + all_dir = [x[0] for x in os.walk(PrcocessNetlist.modelxmlDIR)] + for each_dir in all_dir: + all_file = os.listdir(each_dir) + if xmlfile in all_file: + count += 1 + modelPath.append(os.path.join(each_dir,xmlfile)) + + if count > 1: + multipleModelList.append(modelPath) + elif count == 0: + unknownModelList.append(compType) + elif count == 1: + try: + print "Start Parsing :",modelPath + tree = ET.parse(modelPath[0]) - for param in tree.findall('param'): - for item in param: - #print "Tags ",item.tag - #print "Value",item.text - if 'vector'in item.attrib: - print "Tag having vector attribute",item.tag,item.attrib['vector'] - temp_count = 1 - temp_list = [] - for i in range(0,int(item.attrib['vector'])): - temp_list.append(item.text+" "+str(temp_count)) - temp_count += 1 - if 'default' in item.attrib: - paramDict[item.tag+":"+item.attrib['default']] = temp_list - else: - paramDict[item.tag] = item.text - - else: - if 'default' in item.attrib: - paramDict[item.tag+":"+item.attrib['default']] = item.text + root = tree.getroot() + #Getting number of nodes for model and title + for child in tree.iter(): + if child.tag == 'node_number': + num_of_nodes = int(child.text) + elif child.tag == 'title': + title = child.text+" "+compName + elif child.tag == 'name': + modelname = child.text + elif child.tag == 'type': + #Checking for Analog and Digital + type = child.text + elif child.tag == 'split': + splitDetail = child.text + + + for param in tree.findall('param'): + for item in param: + #print "Tags ",item.tag + #print "Value",item.text + if 'vector'in item.attrib: + print "Tag having vector attribute",item.tag,item.attrib['vector'] + temp_count = 1 + temp_list = [] + for i in range(0,int(item.attrib['vector'])): + temp_list.append(item.text+" "+str(temp_count)) + temp_count += 1 + if 'default' in item.attrib: + paramDict[item.tag+":"+item.attrib['default']] = temp_list + else: + paramDict[item.tag] = item.text + else: - paramDict[item.tag] = item.text + if 'default' in item.attrib: + paramDict[item.tag+":"+item.attrib['default']] = item.text + else: + paramDict[item.tag] = item.text + - - print "Number of Nodes : ",num_of_nodes - print "Title : ",title - print "Parameters",paramDict - #Creating line for adding model line in schematic - if splitDetail == 'None': - modelLine = "a"+str(k)+" " - for i in range(1,num_of_nodes+1): - modelLine += words[i]+" " - modelLine += compName - - else: - print "Split Details :",splitDetail - modelLine = "a"+str(k)+" " - vectorDetail = splitDetail.split(':') - print "Vector Details",vectorDetail - pos = 1 #Node position - for item in vectorDetail: - try: - if item.split("-")[1] == 'V': - print "Vector" - modelLine += "[" - for i in range(0,int(item.split("-")[0])): - modelLine += words[pos]+" " - pos += 1 - modelLine += "] " - elif item.split("-")[1] == 'NV': - print "Non Vector" - for i in range(0,int(item.split("-")[0])): - modelLine += words[pos]+" " - pos += 1 + print "Number of Nodes : ",num_of_nodes + print "Title : ",title + print "Parameters",paramDict + #Creating line for adding model line in schematic + if splitDetail == 'None': + modelLine = "a"+str(k)+" " + for i in range(1,num_of_nodes+1): + modelLine += words[i]+" " + modelLine += compName - except: - print "There is error while processing Vector Details" - sys.exit(2) - modelLine += compName - - print "Final Model Line :",modelLine - try: - schematicInfo.append(modelLine) - k=k+1 - except Exception as e: - print "Error while appending ModelLine ",modelLine - print "Exception Message : ",str(e) - #Insert comment at remove line - schematicInfo.insert(index,"* "+compline) - comment = "* Schematic Name: "+compType+", NgSpice Name: "+modelname - #Here instead of adding compType(use for XML), added modelName(Unique Model Name) - modelList.append([index,compline,modelname,compName,comment,title,type,paramDict]) - except: - print "Unable to parse the model, Please check your your XML file" - sys.exit(2) - + else: + print "Split Details :",splitDetail + modelLine = "a"+str(k)+" " + vectorDetail = splitDetail.split(':') + print "Vector Details",vectorDetail + pos = 1 #Node position + for item in vectorDetail: + try: + if item.split("-")[1] == 'V': + print "Vector" + modelLine += "[" + for i in range(0,int(item.split("-")[0])): + modelLine += words[pos]+" " + pos += 1 + modelLine += "] " + elif item.split("-")[1] == 'NV': + print "Non Vector" + for i in range(0,int(item.split("-")[0])): + modelLine += words[pos]+" " + pos += 1 + + except: + print "There is error while processing Vector Details" + sys.exit(2) + modelLine += compName + + print "Final Model Line :",modelLine + try: + schematicInfo.append(modelLine) + k=k+1 + except Exception as e: + print "Error while appending ModelLine ",modelLine + print "Exception Message : ",str(e) + #Insert comment at remove line + schematicInfo.insert(index,"* "+compline) + comment = "* Schematic Name: "+compType+", NgSpice Name: "+modelname + #Here instead of adding compType(use for XML), added modelName(Unique Model Name) + modelList.append([index,compline,modelname,compName,comment,title,type,paramDict]) + except: + print "Unable to parse the model, Please check your your XML file" + sys.exit(2) + else: + schematicInfo.insert(index,"* "+compline) #print "Count",count #print "UnknownModelList",unknownModelList #print "MultipleModelList",multipleModelList diff --git a/src/kicadtoNgspice/SubcircuitTab.py b/src/kicadtoNgspice/SubcircuitTab.py new file mode 100644 index 00000000..2614d75a --- /dev/null +++ b/src/kicadtoNgspice/SubcircuitTab.py @@ -0,0 +1,103 @@ +from PyQt4 import QtGui + +import TrackWidget +from projManagement import Validation + + +class SubcircuitTab(QtGui.QWidget): + """ + This class creates Subcircuit Tab in KicadtoNgspice Window + It dynamically creates the widget for subcircuits. + """ + + def __init__(self,schematicInfo): + QtGui.QWidget.__init__(self) + + #Creating track widget object + self.obj_trac = TrackWidget.TrackWidget() + + #Creating validation object + self.obj_validation = Validation.Validation() + #Row and column count + self.row = 0 + self.count = 1 #Entry count + self.entry_var = {} + + #List to hold information about device + self.subDetail = {} + + #Stores the number of ports in each subcircuit + self.numPorts = [] + + #Set Layout + self.grid = QtGui.QGridLayout() + self.setLayout(self.grid) + + for eachline in schematicInfo: + words = eachline.split() + if eachline[0] == 'x': + print "Words",words[0] + self.obj_trac.subcircuitList.append(words) + subbox=QtGui.QGroupBox() + subgrid=QtGui.QGridLayout() + subbox.setTitle("Add subcircuit for "+words[len(words)-1]) + self.entry_var[self.count] = QtGui.QLineEdit() + self.entry_var[self.count].setText("") + subgrid.addWidget(self.entry_var[self.count],self.row,1) + self.addbtn = QtGui.QPushButton("Add") + self.addbtn.setObjectName("%d" %self.count) + #Send the number of ports specified with the given subcircuit for verification. + #eg. If the line is 'x1 4 0 3 ua741', there are 3 ports(4, 0 and 3). + self.numPorts.append(len(words)-2) + print "NUMPORTS",self.numPorts + self.addbtn.clicked.connect(self.trackSubcircuit) + subgrid.addWidget(self.addbtn,self.row,2) + subbox.setLayout(subgrid) + + #CSS + subbox.setStyleSheet(" \ + QGroupBox { border: 1px solid gray; border-radius: 9px; margin-top: 0.5em; } \ + QGroupBox::title { subcontrol-origin: margin; left: 10px; padding: 0 3px 0 3px; } \ + ") + + self.grid.addWidget(subbox) + + #Adding Device Details + self.subDetail[self.count] = words[0] + + #Increment row and widget count + self.row = self.row+1 + self.count = self.count+1 + + self.show() + + + def trackSubcircuit(self): + """ + This function is use to keep track of all Device Model widget + """ + print "Calling Track Subcircuit function" + sending_btn = self.sender() + #print "Object Called is ",sending_btn.objectName() + self.widgetObjCount = int(sending_btn.objectName()) + + self.subfile = str(QtGui.QFileDialog.getExistingDirectory(self,"Open Subcircuit","../SubcircuitLibrary")) + self.reply = self.obj_validation.validateSub(self.subfile,self.numPorts[self.widgetObjCount - 1]) + if self.reply == "True": + #Setting Library to Text Edit Line + self.entry_var[self.widgetObjCount].setText(self.subfile) + self.subName = self.subDetail[self.widgetObjCount] + + #Storing to track it during conversion + + self.obj_trac.subcircuitTrack[self.subName] = self.subfile + elif self.reply == "PORT": + self.msg = QtGui.QErrorMessage(self) + self.msg.showMessage("Please select a Subcircuit with correct number of ports.") + self.msg.setWindowTitle("Error Message") + self.msg.show() + elif self.reply == "DIREC": + self.msg = QtGui.QErrorMessage(self) + self.msg.showMessage("Please select a valid Subcircuit directory (Containing '.sub' file).") + self.msg.setWindowTitle("Error Message") + self.msg.show()
\ No newline at end of file diff --git a/src/kicadtoNgspice/TrackWidget.py b/src/kicadtoNgspice/TrackWidget.py index 23991a37..e97b317d 100644 --- a/src/kicadtoNgspice/TrackWidget.py +++ b/src/kicadtoNgspice/TrackWidget.py @@ -21,4 +21,9 @@ class TrackWidget: model_entry_var = {} #Track Widget for Device Model detail - deviceModelTrack = {}
\ No newline at end of file + deviceModelTrack = {} + + #Track Widget for Subcircuits where directory has been selected + subcircuitTrack = {} + #Track subcircuits which are specified in .cir file + subcircuitList = []
\ No newline at end of file |