Blame view

GUI/SW1/SRC/EXPORT_SPICE.cpp 8.84 KB
886c558b   Steve Greedy   SACAMOS Public Re...
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
/////////////////////////////////////////////////////////////////////////////////
//
// This file is part of SACAMOS, cable models for EMI simulations in SPICE.
// It was developed by the University of Nottingham and the Netherlands Aerospace
// Centre (NLR) for ESA under contract number 4000112765/14/NL/HK.
//
// Copyright (C) 2015 - 2017 University of Nottingham
//
// SACAMOS is free software: you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// SACAMOS is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.
//
// A copy of the GNU General Public License version 3 can be found in the
// file GNU_GPL_v3 in the root or at <http://www.gnu.org/licenses/>.
//
// wxWidgets is currently licenced under the "wxWindows Library Licence".
// A copy of the wxWindows Library Licence, Version 3.1 can be found in the file
// wxWindows_Library_Licence_v3-1 in the root or at:
// <https://www.wxwidgets.org/about/licence/>
//
// The University of Nottingham can be contacted at: ggiemr@nottingham.ac.uk
//
// File Contents:
//
// NAME
//     EXPORT_SPICE.cpp
//
// DESCRIPTION
//     Export spice models from SACAMOS to spice version specific locations
//
// AUTHOR(S)
//     Steve Greedy
//
/////////////////////////////////////////////////////////////////////////////////

#include "EXPORT_SPICE.h"

//(*InternalHeaders(EXPORT_SPICE)
#include <wx/intl.h>
#include <wx/string.h>
//*)

//(*IdInit(EXPORT_SPICE)
const long EXPORT_SPICE::ID_DIRPICKERCTRL1 = wxNewId();
const long EXPORT_SPICE::ID_DIRPICKERCTRL2 = wxNewId();
const long EXPORT_SPICE::ID_BUTTON1 = wxNewId();
const long EXPORT_SPICE::ID_BUTTON2 = wxNewId();
d475f4d6   Steve Greedy   SW1 Update to v1.0.1
54
const long EXPORT_SPICE::ID_TEXTCTRL1 = wxNewId();
886c558b   Steve Greedy   SACAMOS Public Re...
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
//*)

BEGIN_EVENT_TABLE(EXPORT_SPICE,wxDialog)
	//(*EventTable(EXPORT_SPICE)
	//*)
END_EVENT_TABLE()

EXPORT_SPICE::EXPORT_SPICE(wxWindow* parent,wxWindowID id,const wxPoint& pos,const wxSize& size)
{
	//(*Initialize(EXPORT_SPICE)
	wxStaticBoxSizer* StaticBoxSizer2;
	wxBoxSizer* BoxSizer2;
	wxBoxSizer* BoxSizer1;
	wxStaticBoxSizer* StaticBoxSizer1;
	wxFlexGridSizer* FlexGridSizer1;

	Create(parent, wxID_ANY, _("SACAMOS: Export Spice Models"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _T("wxID_ANY"));
	FlexGridSizer1 = new wxFlexGridSizer(0, 3, 0, 0);
	BoxSizer1 = new wxBoxSizer(wxVERTICAL);
	StaticBoxSizer1 = new wxStaticBoxSizer(wxHORIZONTAL, this, _("Ngspice: Path to symbol library"));
	DirPickerCtrl1 = new wxDirPickerCtrl(this, ID_DIRPICKERCTRL1, wxEmptyString, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDIRP_DIR_MUST_EXIST|wxDIRP_USE_TEXTCTRL, wxDefaultValidator, _T("ID_DIRPICKERCTRL1"));
	StaticBoxSizer1->Add(DirPickerCtrl1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	BoxSizer1->Add(StaticBoxSizer1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticBoxSizer2 = new wxStaticBoxSizer(wxHORIZONTAL, this, _("LTspice: Path to symbol library"));
	DirPickerCtrl2 = new wxDirPickerCtrl(this, ID_DIRPICKERCTRL2, wxEmptyString, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDIRP_DIR_MUST_EXIST|wxDIRP_USE_TEXTCTRL, wxDefaultValidator, _T("ID_DIRPICKERCTRL2"));
	StaticBoxSizer2->Add(DirPickerCtrl2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	BoxSizer1->Add(StaticBoxSizer2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	BoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
	Button1 = new wxButton(this, ID_BUTTON1, _("EXPORT"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
	BoxSizer2->Add(Button1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	Button2 = new wxButton(this, ID_BUTTON2, _("CLOSE"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON2"));
	BoxSizer2->Add(Button2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	BoxSizer1->Add(BoxSizer2, 1, wxALL|wxEXPAND, 5);
d475f4d6   Steve Greedy   SW1 Update to v1.0.1
88
89
	TextCtrl1 = new wxTextCtrl(this, ID_TEXTCTRL1, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_AUTO_SCROLL|wxTE_MULTILINE|wxTE_READONLY|wxTE_DONTWRAP|wxVSCROLL|wxHSCROLL|wxALWAYS_SHOW_SB, wxDefaultValidator, _T("ID_TEXTCTRL1"));
	BoxSizer1->Add(TextCtrl1, 2, wxALL|wxEXPAND, 5);
886c558b   Steve Greedy   SACAMOS Public Re...
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
	FlexGridSizer1->Add(BoxSizer1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	SetSizer(FlexGridSizer1);
	FlexGridSizer1->Fit(this);
	FlexGridSizer1->SetSizeHints(this);

	Connect(ID_DIRPICKERCTRL1,wxEVT_COMMAND_DIRPICKER_CHANGED,(wxObjectEventFunction)&EXPORT_SPICE::OnDirPickerCtrl1DirChanged);
	Connect(ID_DIRPICKERCTRL2,wxEVT_COMMAND_DIRPICKER_CHANGED,(wxObjectEventFunction)&EXPORT_SPICE::OnDirPickerCtrl2DirChanged);
	Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&EXPORT_SPICE::OnButton1Click);
	Connect(ID_BUTTON2,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&EXPORT_SPICE::OnButton2Click);
	//*)
}

EXPORT_SPICE::~EXPORT_SPICE()
{
	//(*Destroy(EXPORT_SPICE)
	//*)
}

void EXPORT_SPICE::SetPathToMOD(wxString Path, wxString Name)
{
    pathToMOD << Path;
    MODname << Name;
}


void EXPORT_SPICE::OnButton2Click(wxCommandEvent& event)
{
    Close();
}

void EXPORT_SPICE::OnDirPickerCtrl1DirChanged(wxFileDirPickerEvent& event)
{
}

void EXPORT_SPICE::OnDirPickerCtrl2DirChanged(wxFileDirPickerEvent& event)
{
    wxString lt_sym_dir = DirPickerCtrl2->GetPath();

    wxString sacamos_sym_dir = wxGetCwd() + MODname + "\\SPICE\\SYMBOL\\";

}

void EXPORT_SPICE::OnButton1Click(wxCommandEvent& event)
{

    wxString sacamos_sym_dir = "..\\" + MODname + "\\SPICE\\SYMBOL\\";
    wxSetWorkingDirectory(sacamos_sym_dir);

d475f4d6   Steve Greedy   SW1 Update to v1.0.1
138
    wxString file, pattern, export_notification;
886c558b   Steve Greedy   SACAMOS Public Re...
139
140
141
142
143
144
145
146
147
148
149

    if (DirPickerCtrl1->GetPath() != "")
    {
        wxString ng_sym_from, ng_sym_to;

        wxString ng_sym_dir = DirPickerCtrl1->GetPath();

        pattern = "*.sym";

        file = wxFindFirstFile(pattern);

d475f4d6   Steve Greedy   SW1 Update to v1.0.1
150
151
152
        TextCtrl1->AppendText("Exporting Ngspice Symbols\n");
        TextCtrl1->AppendText("=========================\n\n");

886c558b   Steve Greedy   SACAMOS Public Re...
153
154
155
156
157
158
159
160
161
162
163
        while ( !file.empty())
        {
            wxString file_no_path = wxFileNameFromPath(file);

            size_t lastindex = file_no_path.find_last_of(".");
            wxString file_name = file_no_path.substr(0, lastindex);

            ng_sym_from = file_no_path;
            ng_sym_to = ng_sym_dir + "\\" + file_no_path;

            wxCopyFile(ng_sym_from, ng_sym_to);
d475f4d6   Steve Greedy   SW1 Update to v1.0.1
164
165
166
167

            export_notification = "Exported: " + file_no_path + "\n";
            TextCtrl1->AppendText(export_notification);

886c558b   Steve Greedy   SACAMOS Public Re...
168
169
170
171
172
173
174
175
176
177
178
            wxTextFile file_to_edit(ng_sym_to);

            if (file_to_edit.Open())
            {
                size_t i;
                for (i = 0; i <file_to_edit.GetLineCount(); i++)
                {
                    if (file_to_edit[i] == ("file=PATH_TO_TRANSMISSION_LINE_SUB_CIRCUITS/" + file_name + "_NGspice.lib"))
                    {
                        file_to_edit[i] = "file=" + pathToMOD + "\\SPICE\\" + file_name + "_NGspice.lib";
                    }
d475f4d6   Steve Greedy   SW1 Update to v1.0.1
179
                    file_to_edit.Write();
886c558b   Steve Greedy   SACAMOS Public Re...
180
181
182
                }
            file_to_edit.Close();
            }
886c558b   Steve Greedy   SACAMOS Public Re...
183
184
            file = wxFindNextFile();
        }
d475f4d6   Steve Greedy   SW1 Update to v1.0.1
185
        TextCtrl1->AppendText("\n");
886c558b   Steve Greedy   SACAMOS Public Re...
186
187
188
189
190
    }


    if (DirPickerCtrl2->GetPath() != "")
    {
d475f4d6   Steve Greedy   SW1 Update to v1.0.1
191

886c558b   Steve Greedy   SACAMOS Public Re...
192
193
194
195
196
197
198
199
        wxString lt_sym_from, lt_sym_to;

        wxString lt_sym_dir = DirPickerCtrl2->GetPath();

        pattern = "*.asy";

        file = wxFindFirstFile(pattern);

d475f4d6   Steve Greedy   SW1 Update to v1.0.1
200
201
202
        TextCtrl1->AppendText("Exporting LTspice Symbols\n");
        TextCtrl1->AppendText("=========================\n\n");

886c558b   Steve Greedy   SACAMOS Public Re...
203
204
205
206
207
208
209
210
211
212
213
        while ( !file.empty())
        {
            wxString file_no_path = wxFileNameFromPath(file);

            size_t lastindex = file_no_path.find_last_of(".");
            wxString file_name = file_no_path.substr(0, lastindex);

            lt_sym_from = file_no_path;
            lt_sym_to = lt_sym_dir + "\\" + file_no_path;

            wxCopyFile(lt_sym_from, lt_sym_to);
d475f4d6   Steve Greedy   SW1 Update to v1.0.1
214
215
216
217

            export_notification = "Exported:\n" + file_no_path + "\n";
            TextCtrl1->AppendText(export_notification);

886c558b   Steve Greedy   SACAMOS Public Re...
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
            wxTextFile file_to_edit(lt_sym_to);

            if (file_to_edit.Open())
            {
                size_t i;
                for (i = 0; i <file_to_edit.GetLineCount(); i++)
                {
                    if (file_to_edit[i] == ("SYMATTR ModelFile PATH_TO_TRANSMISSION_LINE_SUB_CIRCUITS/" + file_name + "_LTspice.lib"))
                    {
                        file_to_edit[i] = "SYMATTR ModelFile " + pathToMOD + "\\SPICE\\" + file_name + "_LTspice.lib";
                    }
                file_to_edit.Write();
                }
            file_to_edit.Close();
            }

            file = wxFindNextFile();
        }
    }

d475f4d6   Steve Greedy   SW1 Update to v1.0.1
238
239
    TextCtrl1->AppendText("\nExport Complete");

886c558b   Steve Greedy   SACAMOS Public Re...
240
241
    wxSetWorkingDirectory(pathToMOD);
}