summaryrefslogtreecommitdiff
path: root/src/kicadtoNgspice/KicadtoNgspice.py
blob: 6d654158b9eebc8affd8da4d1eb9929e60372bbd (plain)
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
# =========================================================================
#          FILE: kicadtoNgspice.py
#
#         USAGE: ---
#
#   DESCRIPTION: This define all configuration used in Application.
#
#       OPTIONS: ---
#  REQUIREMENTS: ---
#          BUGS: ---
#         NOTES: ---
#        AUTHOR: Fahim Khan, fahim.elex@gmail.com
#      MODIFIED: Rahul Paknikar, rahulp@iitb.ac.in
#  ORGANIZATION: eSim team at FOSSEE, IIT Bombay.
#       CREATED: Wednesday 04 March 2015
#      REVISION: Friday 14 February 2020
# =========================================================================

import sys
import os
from PyQt4 import QtGui
from .Processing import PrcocessNetlist
from . import Analysis
from . import Source
from . import Model
from . import DeviceModel
from . import SubcircuitTab
from . import Convert
from . import TrackWidget
from xml.etree import ElementTree as ET


class MainWindow(QtGui.QWidget):
    """
    - This class create KicadtoNgspice window.
    - And Call Convert function if convert button is pressed.
    - The convert function takes all the value entered by user and create
      a final netlist "*.cir.out".
    - This final netlist is compatible with Ngspice.
    - clarg1 is the path to the .cir file
    - clarg2 is either None or "sub" depending on the analysis type
    """

    def __init__(self, clarg1, clarg2=None):
        QtGui.QWidget.__init__(self)
        print("==================================")
        print("Kicad to Ngspice netlist converter")
        print("==================================")
        global kicadNetlist, schematicInfo
        global infoline, optionInfo
        self.kicadFile = clarg1
        self.clarg1 = clarg1
        self.clarg2 = clarg2

        # Create object of track widget
        # Track the dynamically created widget of KicadtoNgspice Window
        self.obj_track = TrackWidget.TrackWidget()

        # Clear Dictionary/List item of sub circuit and Ngspice model
        # Dictionary
        self.obj_track.subcircuitList.clear()
        self.obj_track.subcircuitTrack.clear()
        self.obj_track.model_entry_var.clear()
        # List
        self.obj_track.modelTrack[:] = []

        # Object of Processing
        obj_proc = PrcocessNetlist()

        # Read the netlist, ie the .cir file
        kicadNetlist = obj_proc.readNetlist(self.kicadFile)
        # print("=============================================================")
        # print("Given Kicad Schematic Netlist Info :", kicadNetlist)

        # Construct parameter information
        param = obj_proc.readParamInfo(kicadNetlist)

        # Replace parameter with values
        netlist, infoline = obj_proc.preprocessNetlist(kicadNetlist, param)
        # print("=============================================================")
        # print("Schematic Info after processing Kicad Netlist: ", netlist)

        # Separate option and schematic information
        optionInfo, schematicInfo = obj_proc.separateNetlistInfo(netlist)
        # print("=============================================================")
        # print("OPTIONINFO in the Netlist", optionInfo)

        # List for storing source and its value
        global sourcelist, sourcelisttrack
        sourcelist = []
        sourcelisttrack = []
        schematicInfo, sourcelist = obj_proc.insertSpecialSourceParam(
            schematicInfo, sourcelist)

        # List storing model detail
        global modelList, outputOption,\
            unknownModelList, multipleModelList, plotText

        modelList = []
        outputOption = []
        plotText = []
        (
            schematicInfo,
            outputOption,
            modelList,
            unknownModelList,
            multipleModelList,
            plotText
        ) = obj_proc.convertICintoBasicBlocks(
            schematicInfo, outputOption, modelList, plotText
        )
        # print("=======================================")
        # print("Model available in the Schematic :", modelList)

        """
        - Checking if any unknown model is used in schematic which is not
          recognized by Ngspice.
        - Also if the two model of same name is present under
          modelParamXML directory
        """
        if unknownModelList:
            print("Unknown Model List is : ", unknownModelList)
            self.msg = QtGui.QErrorMessage()
            self.msg.setModal(True)
            self.msg.setWindowTitle("Unknown Models")
            self.content = "Your schematic contain unknown model " + \
                ', '.join(unknownModelList)
            self.msg.showMessage(self.content)
            self.msg.exec_()

        elif multipleModelList:
            self.msg = QtGui.QErrorMessage()
            self.msg.setModal(True)
            self.msg.setWindowTitle("Multiple Models")
            self.mcontent = "Look like you have duplicate model in \
            modelParamXML directory " + \
                ', '.join(multipleModelList[0])
            self.msg.showMessage(self.mcontent)
            self.msg.exec_()

        else:
            self.createMainWindow()

    def createMainWindow(self):
        """
        - This function create main window of KiCad to Ngspice converter
        - Two components
            - createcreateConvertWidget
            - Convert button => callConvert
        """
        self.vbox = QtGui.QVBoxLayout()
        self.hbox = QtGui.QHBoxLayout()
        self.hbox.addStretch(1)
        self.convertbtn = QtGui.QPushButton("Convert")
        self.convertbtn.clicked.connect(self.callConvert)
        self.hbox.addWidget(self.convertbtn)
        self.vbox.addWidget(self.createcreateConvertWidget())
        self.vbox.addLayout(self.hbox)

        self.setLayout(self.vbox)
        self.setWindowTitle("Kicad To NgSpice Converter")
        self.show()

    def createcreateConvertWidget(self):
        """
        - Contains the tabs for various convertor elements
            - Analysis            => obj_analysis
            => Analysis.Analysis(`path_to_projFile`)

            - Source Details      => obj_source
            => Source.Source(`sourcelist`,`sourcelisttrack`,`path_to_projFile`)

            - NgSpice Model       => obj_model
            => Model.Model(`schematicInfo`,`modelList`,`path_to_projFile`)

            - Device Modelling    => obj_devicemodel
            => DeviceModel.DeviceModel(`schematicInfo`,`path_to_projFile`)

            - Subcircuits         => obj_subcircuitTab
            => SubcircuitTab.SubcircuitTab(`schematicInfo`,`path_to_projFile`)

        - Finally pass each of these objects, to widgets
        - convertWindow > mainLayout > tabWidgets > AnalysisTab, SourceTab ...
        """
        global obj_analysis
        self.convertWindow = QtGui.QWidget()
        self.analysisTab = QtGui.QScrollArea()
        obj_analysis = Analysis.Analysis(self.clarg1)
        self.analysisTab.setWidget(obj_analysis)
        # self.analysisTabLayout = QtGui.QVBoxLayout(self.analysisTab.widget())
        self.analysisTab.setWidgetResizable(True)
        global obj_source
        self.sourceTab = QtGui.QScrollArea()
        obj_source = Source.Source(sourcelist, sourcelisttrack, self.clarg1)
        self.sourceTab.setWidget(obj_source)
        # self.sourceTabLayout = QtGui.QVBoxLayout(self.sourceTab.widget())
        self.sourceTab.setWidgetResizable(True)
        global obj_model
        self.modelTab = QtGui.QScrollArea()
        obj_model = Model.Model(schematicInfo, modelList, self.clarg1)
        self.modelTab.setWidget(obj_model)
        # self.modelTabLayout = QtGui.QVBoxLayout(self.modelTab.widget())
        self.modelTab.setWidgetResizable(True)
        global obj_devicemodel
        self.deviceModelTab = QtGui.QScrollArea()
        obj_devicemodel = DeviceModel.DeviceModel(schematicInfo, self.clarg1)
        self.deviceModelTab.setWidget(obj_devicemodel)
        self.deviceModelTab.setWidgetResizable(True)
        global obj_subcircuitTab
        self.subcircuitTab = QtGui.QScrollArea()
        obj_subcircuitTab = SubcircuitTab.SubcircuitTab(
            schematicInfo, self.clarg1)
        self.subcircuitTab.setWidget(obj_subcircuitTab)
        self.subcircuitTab.setWidgetResizable(True)

        self.tabWidget = QtGui.QTabWidget()
        # self.tabWidget.TabShape(QtGui.QTabWidget.Rounded)
        self.tabWidget.addTab(self.analysisTab, "Analysis")
        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)
        self.convertWindow.setLayout(self.mainLayout)
        self.convertWindow.show()

        return self.convertWindow

    def callConvert(self):
        """
        - This function called when convert button clicked
        - Extracting data from the objs created above
        - Pushing this data to xml, and writing it finally
        - Written to a ..._Previous_Values.xml file in the projDirectory
        - Finally, call createNetListFile, with the converted schematic
        """
        global schematicInfo
        global analysisoutput
        global kicad
        store_schematicInfo = list(schematicInfo)
        (projpath, filename) = os.path.split(self.kicadFile)
        project_name = os.path.basename(projpath)
        check = 1

        try:
            fr = open(
                    os.path.join(
                        projpath, project_name + "_Previous_Values.xml"), 'r'
                )
            temp_tree = ET.parse(fr)
            temp_root = temp_tree.getroot()
        except BaseException:
            check = 0

        # Opening previous value file pertaining to the selected project
        fw = os.path.join(projpath, project_name + "_Previous_Values.xml")

        if check == 0:
            attr_parent = ET.Element("KicadtoNgspice")
        if check == 1:
            attr_parent = temp_root

        for child in attr_parent:
            if child.tag == "analysis":
                attr_parent.remove(child)

        attr_analysis = ET.SubElement(attr_parent, "analysis")
        attr_ac = ET.SubElement(attr_analysis, "ac")

        if obj_analysis.Lin.isChecked():
            ET.SubElement(attr_ac, "field1", name="Lin").text = "true"
            ET.SubElement(attr_ac, "field2", name="Dec").text = "false"
            ET.SubElement(attr_ac, "field3", name="Oct").text = "false"
        elif obj_analysis.Dec.isChecked():
            ET.SubElement(attr_ac, "field1", name="Lin").text = "false"
            ET.SubElement(attr_ac, "field2", name="Dec").text = "true"
            ET.SubElement(attr_ac, "field3", name="Oct").text = "false"
        if obj_analysis.Oct.isChecked():
            ET.SubElement(attr_ac, "field1", name="Lin").text = "false"
            ET.SubElement(attr_ac, "field2", name="Dec").text = "false"
            ET.SubElement(attr_ac, "field3", name="Oct").text = "true"

        ET.SubElement(
            attr_ac, "field4", name="Start Frequency"
        ).text = str(obj_analysis.ac_entry_var[0].text())
        ET.SubElement(
            attr_ac, "field5", name="Stop Frequency"
        ).text = str(obj_analysis.ac_entry_var[1].text())
        ET.SubElement(
            attr_ac, "field6", name="No. of points"
        ).text = str(obj_analysis.ac_entry_var[2].text())
        ET.SubElement(
            attr_ac, "field7", name="Start Fre Combo"
        ).text = obj_analysis.ac_parameter[0]
        ET.SubElement(
            attr_ac, "field8", name="Stop Fre Combo"
        ).text = obj_analysis.ac_parameter[1]

        attr_dc = ET.SubElement(attr_analysis, "dc")

        ET.SubElement(
            attr_dc, "field1", name="Source 1"
        ).text = str(obj_analysis.dc_entry_var[0].text())
        ET.SubElement(
            attr_dc, "field2", name="Start"
        ).text = str(obj_analysis.dc_entry_var[1].text())
        ET.SubElement(
            attr_dc, "field3", name="Increment"
        ).text = str(obj_analysis.dc_entry_var[2].text())
        ET.SubElement(
            attr_dc, "field4", name="Stop"
        ).text = str(obj_analysis.dc_entry_var[3].text())
        # print("OBJ_ANALYSIS.CHECK -----", self.obj_track.op_check[-1])
        ET.SubElement(
            attr_dc, "field5", name="Operating Point"
        ).text = str(self.obj_track.op_check[-1])
        ET.SubElement(
            attr_dc, "field6", name="Start Combo"
        ).text = obj_analysis.dc_parameter[0]
        ET.SubElement(
            attr_dc, "field7", name="Increment Combo"
        ).text = obj_analysis.dc_parameter[1]
        ET.SubElement(
            attr_dc, "field8", name="Stop Combo"
        ).text = obj_analysis.dc_parameter[2]
        ET.SubElement(
            attr_dc, "field9", name="Source 2"
        ).text = str(obj_analysis.dc_entry_var[4].text())
        ET.SubElement(
            attr_dc, "field10", name="Start"
        ).text = str(obj_analysis.dc_entry_var[5].text())
        ET.SubElement(
            attr_dc, "field11", name="Increment"
        ).text = str(obj_analysis.dc_entry_var[6].text())
        ET.SubElement(
            attr_dc, "field12", name="Stop"
        ).text = str(obj_analysis.dc_entry_var[7].text())
        ET.SubElement(
            attr_dc, "field13", name="Start Combo"
        ).text = obj_analysis.dc_parameter[3]
        ET.SubElement(
            attr_dc, "field14", name="Increment Combo"
        ).text = obj_analysis.dc_parameter[4]
        ET.SubElement(
            attr_dc, "field15", name="Stop Combo"
        ).text = obj_analysis.dc_parameter[5]

        attr_tran = ET.SubElement(attr_analysis, "tran")
        ET.SubElement(
            attr_tran, "field1", name="Start Time"
        ).text = str(obj_analysis.tran_entry_var[0].text())
        ET.SubElement(
            attr_tran, "field2", name="Step Time"
        ).text = str(obj_analysis.tran_entry_var[1].text())
        ET.SubElement(
            attr_tran, "field3", name="Stop Time"
        ).text = str(obj_analysis.tran_entry_var[2].text())
        ET.SubElement(
            attr_tran, "field4", name="Start Combo"
        ).text = obj_analysis.tran_parameter[0]
        ET.SubElement(
            attr_tran, "field5", name="Step Combo"
        ).text = obj_analysis.tran_parameter[1]
        ET.SubElement(
            attr_tran, "field6", name="Stop Combo"
        ).text = obj_analysis.tran_parameter[2]
        # print("TRAN PARAMETER 2-----",obj_analysis.tran_parameter[2])

        if check == 0:
            attr_source = ET.SubElement(attr_parent, "source")
        if check == 1:
            for child in attr_parent:
                if child.tag == "source":
                    attr_source = child

        count = 1
        grand_child_count = 0
        keys = list(obj_source.entry_var.keys())

        for i in store_schematicInfo:
            tmp_check = 0
            words = i.split(' ')
            wordv = words[0]
            for child in attr_source:
                if child.tag == wordv and child.text == words[len(words) - 1]:
                    tmp_check = 1
                    for grand_child in child:
                        grand_child.text = \
                            str(obj_source.entry_var
                                [keys[grand_child_count]].text())
                        grand_child_count += 1
            if tmp_check == 0:
                words = i.split(' ')
                wordv = words[0]
                if wordv[0] == "v" or wordv[0] == "i":
                    attr_var = ET.SubElement(
                        attr_source, words[0], name="Source type"
                    )
                    attr_var.text = words[len(words) - 1]
                    # ET.SubElement(
                    #     attr_ac, "field1", name="Lin").text = "true"
                if words[len(words) - 1] == "ac":
                    # attr_ac = ET.SubElement(attr_var, "ac")
                    ET.SubElement(
                        attr_var, "field1", name="Amplitude"
                    ).text = str(obj_source.entry_var[count].text())
                    count += 1
                    ET.SubElement(
                        attr_var, "field2", name="Phase"
                    ).text = str(obj_source.entry_var[count].text())
                    count += 2
                elif words[len(words) - 1] == "dc":
                    # attr_dc = ET.SubElement(attr_var, "dc")
                    ET.SubElement(
                        attr_var, "field1", name="Value"
                    ).text = str(obj_source.entry_var[count].text())
                    count += 2
                elif words[len(words) - 1] == "sine":
                    # attr_sine = ET.SubElement(attr_var, "sine")
                    ET.SubElement(
                        attr_var, "field1", name="Offset Value"
                    ).text = str(obj_source.entry_var[count].text())
                    count += 1
                    ET.SubElement(
                        attr_var, "field2", name="Amplitude"
                    ).text = str(obj_source.entry_var[count].text())
                    count += 1
                    ET.SubElement(
                        attr_var, "field3", name="Frequency"
                    ).text = str(obj_source.entry_var[count].text())
                    count += 1
                    ET.SubElement(
                        attr_var, "field4", name="Delay Time"
                    ).text = str(obj_source.entry_var[count].text())
                    count += 1
                    ET.SubElement(
                        attr_var, "field5", name="Damping Factor"
                    ).text = str(obj_source.entry_var[count].text())
                    count += 2
                elif words[len(words) - 1] == "pulse":
                    # attr_pulse=ET.SubElement(attr_var,"pulse")
                    ET.SubElement(
                        attr_var, "field1", name="Initial Value"
                    ).text = str(obj_source.entry_var[count].text())
                    count += 1
                    ET.SubElement(
                        attr_var, "field2", name="Pulse Value"
                    ).text = str(obj_source.entry_var[count].text())
                    count += 1
                    ET.SubElement(
                        attr_var, "field3", name="Delay Time"
                    ).text = str(obj_source.entry_var[count].text())
                    count += 1
                    ET.SubElement(
                        attr_var, "field4", name="Rise Time"
                    ).text = str(obj_source.entry_var[count].text())
                    count += 1
                    ET.SubElement(
                        attr_var, "field5", name="Fall Time"
                    ).text = str(obj_source.entry_var[count].text())
                    count += 1
                    ET.SubElement(
                        attr_var, "field5", name="Pulse width"
                    ).text = str(obj_source.entry_var[count].text())
                    count += 1
                    ET.SubElement(
                        attr_var, "field5", name="Period"
                    ).text = str(obj_source.entry_var[count].text())
                    count += 2
                elif words[len(words) - 1] == "pwl":
                    # attr_pwl=ET.SubElement(attr_var,"pwl")
                    ET.SubElement(
                        attr_var, "field1", name="Enter in pwl format"
                    ).text = str(obj_source.entry_var[count].text())
                    count += 2
                elif words[len(words) - 1] == "exp":
                    # attr_exp=ET.SubElement(attr_var,"exp")
                    ET.SubElement(
                        attr_var, "field1", name="Initial Value"
                    ).text = str(obj_source.entry_var[count].text())
                    count += 1
                    ET.SubElement(
                        attr_var, "field2", name="Pulsed Value"
                    ).text = str(obj_source.entry_var[count].text())
                    count += 1
                    ET.SubElement(
                        attr_var, "field3", name="Rise Delay Time"
                    ).text = str(obj_source.entry_var[count].text())
                    count += 1
                    ET.SubElement(
                        attr_var, "field4", name="Rise Time Constant"
                    ).text = str(obj_source.entry_var[count].text())
                    count += 1
                    ET.SubElement(
                        attr_var, "field5", name="Fall TIme"
                    ).text = str(obj_source.entry_var[count].text())
                    count += 1
                    ET.SubElement(
                        attr_var, "field6", name="Fall Time Constant"
                    ).text = str(obj_source.entry_var[count].text())
                    count += 2

        if check == 0:
            attr_model = ET.SubElement(attr_parent, "model")
        if check == 1:
            for child in attr_parent:
                if child.tag == "model":
                    attr_model = child
        i = 0

        # tmp_check is a variable to check for duplicates in the xml file
        tmp_check = 0
        # tmp_i is the iterator in case duplicates are there;
        # then in that case we need to replace only the child node and
        # not create a new parent node

        for line in modelList:
            tmp_check = 0
            for rand_itr in obj_model.obj_trac.modelTrack:
                if rand_itr[2] == line[2] and rand_itr[3] == line[3]:
                    start = rand_itr[7]
                    end = rand_itr[8]

            i = start
            for child in attr_model:
                if child.text == line[2] and child.tag == line[3]:
                    for grand_child in child:
                        if i <= end:
                            grand_child.text = \
                              str(obj_model.obj_trac.model_entry_var[i].text())
                            i = i + 1
                    tmp_check = 1

            if tmp_check == 0:
                attr_ui = ET.SubElement(attr_model, line[3], name="type")
                attr_ui.text = line[2]
                for key, value in line[7].items():
                    if(
                        hasattr(value, '__iter__') and
                        i <= end and not isinstance(value, str)
                    ):
                        for item in value:
                            ET.SubElement(
                                attr_ui, "field" + str(i + 1), name=item
                            ).text = str(
                                  obj_model.obj_trac.model_entry_var[i].text()
                                )
                            i = i + 1

                    else:
                        ET.SubElement(
                            attr_ui, "field" + str(i + 1), name=value
                        ).text = str(
                                obj_model.obj_trac.model_entry_var[i].text()
                            )
                        i = i + 1

        # Writing Device Model values
        if check == 0:
            attr_devicemodel = ET.SubElement(attr_parent, "devicemodel")
        if check == 1:
            for child in attr_parent:
                if child.tag == "devicemodel":
                    del child[:]
                    attr_devicemodel = child

        for device in obj_devicemodel.devicemodel_dict_beg:
            attr_var = ET.SubElement(attr_devicemodel, device)
            it = obj_devicemodel.devicemodel_dict_beg[device]
            end = obj_devicemodel.devicemodel_dict_end[device]

            while it <= end:
                ET.SubElement(attr_var, "field").text = \
                        str(obj_devicemodel.entry_var[it].text())
                it = it + 1

        # Writing Subcircuit values
        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 subckt in obj_subcircuitTab.subcircuit_dict_beg:
            attr_var = ET.SubElement(attr_subcircuit, subckt)
            it = obj_subcircuitTab.subcircuit_dict_beg[subckt]
            end = obj_subcircuitTab.subcircuit_dict_end[subckt]

            while it <= end:
                ET.SubElement(attr_var, "field").text = \
                        str(obj_subcircuitTab.entry_var[it].text())
                it = it + 1

        # xml written to previous value file for the project
        tree = ET.ElementTree(attr_parent)
        tree.write(fw)

        # Create Convert object with the source details & the schematic details
        print("=============================================================")
        print("SOURCE LIST TRACK")
        print(self.obj_track.sourcelisttrack["ITEMS"])
        print("SOURCE ENTRY VAR")
        print(self.obj_track.source_entry_var["ITEMS"])
        print("SCHEMATIC INFO")
        print(store_schematicInfo)
        print("=============================================================")

        self.obj_convert = Convert.Convert(
            self.obj_track.sourcelisttrack["ITEMS"],
            self.obj_track.source_entry_var["ITEMS"],
            store_schematicInfo, self.clarg1
        )

        try:
            # Adding Source Value to Schematic Info
            store_schematicInfo = self.obj_convert.addSourceParameter()
            print("=========================================================")
            print("Netlist After Adding Source details :", store_schematicInfo)

            # Adding Model Value to store_schematicInfo
            store_schematicInfo = self.obj_convert.addModelParameter(
                store_schematicInfo)
            print("=========================================================")
            print("Netlist After Adding Ngspice Model :", store_schematicInfo)

            # Adding Device Library to SchematicInfo
            store_schematicInfo = self.obj_convert.addDeviceLibrary(
                store_schematicInfo, self.kicadFile)
            print("=========================================================")
            print(
                "Netlist After Adding Device Model Library :",
                store_schematicInfo)

            # Adding Subcircuit Library to SchematicInfo
            store_schematicInfo = self.obj_convert.addSubcircuit(
                store_schematicInfo, self.kicadFile)
            print("=========================================================")
            print("Netlist After Adding subcircuits :", store_schematicInfo)

            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"],
                self.obj_track.set_CheckBox["ITEMS"],
                self.obj_track.AC_Parameter["ITEMS"],
                self.obj_track.DC_Parameter["ITEMS"],
                self.obj_track.TRAN_Parameter["ITEMS"],
                self.obj_track.AC_type["ITEMS"],
                self.obj_track.op_check
            )
            print("=========================================================")
            print("Analysis OutPut ", analysisoutput)

            # Calling netlist file generation function
            print("=========================================================")
            print("STORE SCHEMATIC INFO")
            print(store_schematicInfo)
            print("=========================================================")
            self.createNetlistFile(store_schematicInfo, plotText)

            self.msg = "The Kicad to Ngspice Conversion completed "
            self.msg += "successfully!"
            QtGui.QMessageBox.information(
                self, "Information", self.msg, QtGui.QMessageBox.Ok
            )
        except Exception as e:
            print("Exception Message: ", e)
            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(self.kicadFile)[0]

        # If sub argument passed, create subCircuit file as well
        if self.clarg2 == "sub":
            self.createSubFile(subPath)

    def createNetlistFile(self, store_schematicInfo, plotText):
        """
        - Creating .cir.out file
        - If analysis file present uses that and extract
            - Simulator
            - Initial
            - Analysis
        - Finally add the following components to .cir.out file
            - SimulatorOption
            - InitialCondOption
            - Store_SchematicInfo
            - AnalysisOption
        - In the end add control statements and allv, alli, end statements
        """
        print("=============================================================")
        print("Creating Final netlist")

        # To avoid writing optionInfo twice in final netlist
        store_optionInfo = list(optionInfo)

        # checking if analysis files is present
        (projpath, filename) = os.path.split(self.kicadFile)
        analysisFileLoc = os.path.join(projpath, "analysis")

        if os.path.exists(analysisFileLoc):
            try:
                f = open(analysisFileLoc)
                # Read data
                data = f.read()
                # Close the file
                f.close()

            except BaseException:
                print("Error While opening Project Analysis file.\
                 Please check it")
                sys.exit()
        else:
            # print("========================================================")
            print(analysisFileLoc + " does not exist")
            sys.exit()

        # Adding analysis file info to optionInfo
        analysisData = data.splitlines()
        for eachline in analysisData:
            eachline = eachline.strip()
            if len(eachline) > 1:
                if eachline[0] == '.':
                    store_optionInfo.append(eachline)

        analysisOption = []
        initialCondOption = []
        simulatorOption = []
        # includeOption=[]  # Don't know why to use it
        # model = []      # Don't know why to use it

        for eachline in store_optionInfo:
            words = eachline.split()
            option = words[0]
            if (option == '.ac' or option == '.dc' or option ==
                '.disto' or option == '.noise' or
                option == '.op' or option == '.pz' or option ==
                '.sens' or option == '.tf' or
                    option == '.tran'):
                analysisOption.append(eachline + '\n')

            elif (option == '.save' or option == '.print' or option ==
                    '.plot' or option == '.four'):
                eachline = eachline.strip('.')
                outputOption.append(eachline + '\n')
            elif (option == '.nodeset' or option == '.ic'):
                initialCondOption.append(eachline + '\n')
            elif option == '.option':
                simulatorOption.append(eachline + '\n')
            # elif (option=='.include' or option=='.lib'):
            #    includeOption.append(eachline+'\n')
            # elif (option=='.model'):
            #    model.append(eachline+'\n')
            elif option == '.end':
                continue

        # Start creating final netlist cir.out file
        outfile = self.kicadFile + ".out"
        out = open(outfile, "w")
        out.writelines(infoline)
        out.writelines('\n')
        sections = [
            simulatorOption,
            initialCondOption,
            store_schematicInfo,
            analysisOption]

        for section in sections:
            if len(section) == 0:
                continue
            else:
                for line in section:
                    out.writelines('\n')
                    out.writelines(line)

        out.writelines('\n* Control Statements \n')
        out.writelines('.control\n')
        out.writelines('run\n')
        # out.writelines(outputOption)
        out.writelines('print allv > plot_data_v.txt\n')
        out.writelines('print alli > plot_data_i.txt\n')
        for item in plotText:
            out.writelines(item + '\n')
        out.writelines('.endc\n')
        out.writelines('.end\n')
        out.close()

    def createSubFile(self, subPath):
        """
        - To create subcircuit file
        - Extract data from .cir.out file
        """
        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 BaseException:
                print("Error in opening .cir.out file.")
        else:
            # print("=========================================================")
            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 = next(netlist)
                    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("=============================================================")
        print("The subcircuit has been written in " + self.projName + ".sub")