JUK1
CircuitSimulator.cpp
Go to the documentation of this file.
1 #ifdef WITH_PYTHON
2 #include "matplotlibcpp.h"
3 #endif
4 #include <iostream>
8 #include <thread>
9 #include <complex>
10 #include <chrono>
11 
12 #ifdef _WIN32
13 #include <windows.h>
14 #include <direct.h>
15 #include <shobjidl.h>
16 #include <ShObjIdl_core.h>
17 #endif
18 
19 
28 int
29 main(int argc, char * argv[]) {
30 #ifdef _WIN32
31  // credit for this way to get exe path:
32  // https://gist.github.com/karolisjan/f9b8ac3ae2d41ec0ce70f2feac6bdfaf
33  char buffer[MAX_PATH];
34  GetModuleFileNameA(NULL, buffer, MAX_PATH);
35  std::string::size_type pos = std::string(buffer).find_last_of("\\/");
36  _chdir(std::string(buffer).substr(0, pos).c_str());
37 
38  // modification to get it as a wstring
39  wchar_t bufferW[MAX_PATH];
40  GetModuleFileNameW(NULL, bufferW, MAX_PATH);
41  std::string::size_type posW = std::wstring(bufferW).find_last_of(L"\\/");
42  std::wstring exePathW = std::wstring(bufferW).substr(0, posW);
43 #endif
44 
45 
46  std::string filePath = "Netlists/Diode Test.netlist";
47  if (argc > 1) {
48  filePath = argv[1];
49  std::cout << "Using netlist: " << filePath;
50  } else {
51 #ifdef _WIN32
52  // example code from:
53  // https://docs.microsoft.com/en-us/windows/win32/learnwin32/example--the-open-dialog-box
54  // modified to use ascii
55 
56  HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED |
57  COINIT_DISABLE_OLE1DDE);
58  if (SUCCEEDED(hr)) {
59  IFileOpenDialog * pFileOpen;
60  IShellItem * defaultFolder;
61 
62  // Create the FileOpenDialog object.
63  hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
64  IID_IFileOpenDialog,
65  reinterpret_cast<void **>(&pFileOpen));
66  std::wstring defaultNetlistPath = exePathW + L"\\Netlists";
67  SHCreateItemFromParsingName(defaultNetlistPath.c_str(), NULL,
68  IID_IShellItem,
69  reinterpret_cast<void **>(&defaultFolder));
70  pFileOpen->SetDefaultFolder(defaultFolder);
71  pFileOpen->SetFolder(defaultFolder);
72 
73  if (SUCCEEDED(hr)) {
74  // Show the Open dialog box.
75  hr = pFileOpen->Show(NULL);
76 
77  // Get the file name from the dialog box.
78  if (SUCCEEDED(hr)) {
79  IShellItem * pItem;
80  hr = pFileOpen->GetResult(&pItem);
81  if (SUCCEEDED(hr)) {
82  PWSTR pszFilePath;
83  hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
84 
85  // Display the file name to the user.
86  if (SUCCEEDED(hr)) {
87  // MessageBoxW(NULL, pszFilePath, L"File Path", MB_OK);
88  WideCharToMultiByte(CP_UTF8, 0, pszFilePath, -1, buffer,
89  MAX_PATH, NULL, NULL);
90  filePath = std::string(buffer);
91  CoTaskMemFree(pszFilePath);
92  }
93  pItem->Release();
94  }
95  }
96  pFileOpen->Release();
97  }
98  CoUninitialize();
99  }
100  std::cout << "Using netlist: " << filePath;
101 #else
102  std::cout << "No path given. Defaulting to using netlist: " << filePath;
103 #endif
104  }
105  std::cout << std::endl;
106 
107  SimulationEnvironment<double> env(filePath);
108 
109  env.simulate();
110 
111  return 0;
112 }
int main(int argc, char *argv[])
main function to launch the circuit simulator
The main class to hold all of the relevant simulation data.
Definition: Simulator.hpp:46
void simulate()
Where a lot of the magic starts. This is what runs the simulation.
Definition: Simulator.hpp:265