Blame view

GUI/SW1/SRC/EXPORT_SPICE.cpp 9.4 KB
886c558b   Steve Greedy   SACAMOS Public Re...
1
2
3
4
5
6
/////////////////////////////////////////////////////////////////////////////////
//
// 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.
//
7ba34678   Steve Greedy   Update Copyright ...
7
// Copyright (C) 2015 - 2018 University of Nottingham
886c558b   Steve Greedy   SACAMOS Public Re...
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
//
// 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
	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)
{
3d24aeb1   Steve Greedy   Modification to s...
134
    wxString separator(wxFileName::GetPathSeparator());
886c558b   Steve Greedy   SACAMOS Public Re...
135

3d24aeb1   Steve Greedy   Modification to s...
136
137
    //wxString sacamos_sym_dir = "..\\" + MODname + "\\SPICE\\SYMBOL\\";
    wxString sacamos_sym_dir = ".." + separator + MODname + separator +"SPICE" + separator +"SYMBOL" + separator;
886c558b   Steve Greedy   SACAMOS Public Re...
138
139
    wxSetWorkingDirectory(sacamos_sym_dir);

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

    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
152
153
154
        TextCtrl1->AppendText("Exporting Ngspice Symbols\n");
        TextCtrl1->AppendText("=========================\n\n");

886c558b   Steve Greedy   SACAMOS Public Re...
155
156
157
158
159
160
161
162
        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;
3d24aeb1   Steve Greedy   Modification to s...
163
164
            //ng_sym_to = ng_sym_dir + "\\" + file_no_path;
            ng_sym_to = ng_sym_dir + separator + file_no_path;
886c558b   Steve Greedy   SACAMOS Public Re...
165
166

            wxCopyFile(ng_sym_from, ng_sym_to);
d475f4d6   Steve Greedy   SW1 Update to v1.0.1
167
168
169
170

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

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

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


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

886c558b   Steve Greedy   SACAMOS Public Re...
196
197
198
199
200
201
202
203
        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
204
205
206
        TextCtrl1->AppendText("Exporting LTspice Symbols\n");
        TextCtrl1->AppendText("=========================\n\n");

886c558b   Steve Greedy   SACAMOS Public Re...
207
208
209
210
211
212
213
214
        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;
3d24aeb1   Steve Greedy   Modification to s...
215
216
            //lt_sym_to = lt_sym_dir + "\\" + file_no_path;
            lt_sym_to = lt_sym_dir + separator + file_no_path;
886c558b   Steve Greedy   SACAMOS Public Re...
217
218

            wxCopyFile(lt_sym_from, lt_sym_to);
d475f4d6   Steve Greedy   SW1 Update to v1.0.1
219
220
221
222

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

886c558b   Steve Greedy   SACAMOS Public Re...
223
224
225
226
227
228
229
230
231
            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"))
                    {
3d24aeb1   Steve Greedy   Modification to s...
232
233
                        //file_to_edit[i] = "SYMATTR ModelFile " + pathToMOD + "\\SPICE\\" + file_name + "_LTspice.lib";
                        file_to_edit[i] = "SYMATTR ModelFile " + pathToMOD + separator + "SPICE" + separator + file_name + "_LTspice.lib";
886c558b   Steve Greedy   SACAMOS Public Re...
234
235
236
237
238
239
240
241
242
243
                    }
                file_to_edit.Write();
                }
            file_to_edit.Close();
            }

            file = wxFindNextFile();
        }
    }

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

886c558b   Steve Greedy   SACAMOS Public Re...
246
247
    wxSetWorkingDirectory(pathToMOD);
}