summaryrefslogtreecommitdiff
path: root/src/ngspiceSimulation/NgspiceWidget.py
blob: 13a5ece913cbc45ebb18506eec203cc5b934a6bd (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
from PyQt5 import QtWidgets, QtCore
from configuration.Appconfig import Appconfig
from configparser import ConfigParser
from progressBar import progressBar
import os
import time


# This Class creates NgSpice Window
class NgspiceWidget(QtWidgets.QWidget):

    def __init__(self, command, projPath, simulationEssentials):
        """
        - Creates constructor for NgspiceWidget class.
        - Checks whether OS is Linux or Windows and
          creates Ngspice window accordingly.
        """
        QtWidgets.QWidget.__init__(self)
        self.obj_appconfig = Appconfig()
        self.process = QtCore.QProcess(self)
        self.terminal = QtWidgets.QWidget(self)
        self.simulationEssentials = simulationEssentials
        self.checkChangeInPlotFile = simulationEssentials['checkChangeInPlotFile']
        self.progressBarUi = progressBar.Ui_Form(self.process, self.simulationEssentials)
        self.progressBarUi.setupUi(self.terminal)
        self.layout = QtWidgets.QVBoxLayout(self)
        self.layout.addWidget(self.terminal)
        self.errorFlag = False

        print("Argument to ngspice command : ", command)

        if os.name == 'nt':     # For Windows OS
            parser_nghdl = ConfigParser()
            parser_nghdl.read(
                os.path.join('library', 'config', '.nghdl', 'config.ini')
            )

            msys_home = parser_nghdl.get('COMPILER', 'MSYS_HOME')

            tempdir = os.getcwd()
            projPath = self.obj_appconfig.current_project["ProjectName"]
            os.chdir(projPath)
            self.command = 'cmd /c '+'"start /min ' + \
                msys_home + "/usr/bin/mintty.exe ngspice -p " + command + '"'
            self.process.start(self.command)
            os.chdir(tempdir)

        else:                   # For Linux OS
            # self.command = "cd " + projPath + \
            #     ";ngspice -r " + command.replace(".cir.out", ".raw") + \
            #     " " + command
            # Creating argument for process
            self.currTime = time.time()
            self.args = ['-b', '-r', command.replace(".cir.out", ".raw"), command]
            self.process.setWorkingDirectory(projPath)
            self.progressBarUi.setNgspiceArgs(self.args)
            self.process.start('ngspice', self.args)
            self.process.readyReadStandardOutput.connect(lambda: self.readyReadAll())
            self.process.finished.connect(self.finishSimulation)
            self.obj_appconfig.process_obj.append(self.process)
            print(self.obj_appconfig.proc_dict)
            (
                self.obj_appconfig.proc_dict
                [self.obj_appconfig.current_project['ProjectName']].append(
                    self.process.pid())
            )
            self.gawProcess = QtCore.QProcess(self)
            self.gawCommand = "gaw " + command.replace(".cir.out", ".raw")
            self.gawProcess.start('sh', ['-c', self.gawCommand])
            print(self.gawCommand)

    def finishSimulation(self, exitCode, exitStatus):
        if exitStatus == QtCore.QProcess.NormalExit:
            self.checkChangeInPlotFile(self.currTime)
        self.readyToPrintSimulationStatus = True
        self.enableButtons = self.simulationEssentials['enableButtons']

        self.enableButtons(True)
        self.progressBarUi.showProgressCompleted()

        self.writeSimulationStatus()

    def writeSimulationStatus(self):
        if self.readyToPrintSimulationStatus is False:
            return
        self.isSimulationSuccess = self.simulationEssentials['isSimulationSuccess']

        if self.isSimulationSuccess():
            self.progressBarUi.writeSimulationStatusToConsole(isSuccess=True)
        else:
            self.progressBarUi.writeSimulationStatusToConsole(isSuccess=False)

        self.progressBarUi.scrollConsoleToBottom()
        self.readyToPrintSimulationStatus = False

    @QtCore.pyqtSlot()
    def readyReadAll(self):
        self.progressBarUi.writeIntoConsole(
            str(self.process.readAllStandardOutput().data(), encoding='utf-8')
        )
        stderror = self.process.readAllStandardError()
        if stderror.toUpper().contains(b"ERROR"):
            self.errorFlag = True
        self.progressBarUi.writeIntoConsole(str(stderror.data(), encoding='utf-8'))

    # def launchProgressBar(self):
    #     self.progressBar = QtWidgets.QWidget()
    #     self.progressBarUi = progressBar.Ui_Dialog()
    #     self.progressBarUi.setupUi(self.progressBar)
    #     self.progressBar.show()