EXPORT_SPICE.cpp 9.4 KB
/////////////////////////////////////////////////////////////////////////////////
//
// 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 - 2018 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();
const long EXPORT_SPICE::ID_TEXTCTRL1 = wxNewId();
//*)

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);
	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);
	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 separator(wxFileName::GetPathSeparator());

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

    wxString file, pattern, export_notification;

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

        wxString ng_sym_dir = DirPickerCtrl1->GetPath();

        pattern = "*.sym";

        file = wxFindFirstFile(pattern);

        TextCtrl1->AppendText("Exporting Ngspice Symbols\n");
        TextCtrl1->AppendText("=========================\n\n");

        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;
            ng_sym_to = ng_sym_dir + separator + file_no_path;

            wxCopyFile(ng_sym_from, ng_sym_to);

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

            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";
                        file_to_edit[i] = "file=" + pathToMOD + separator + "SPICE" + separator + file_name + "_Ngspice.lib";
                    }
                    file_to_edit.Write();
                }
            file_to_edit.Close();
            }
            file = wxFindNextFile();
        }
        TextCtrl1->AppendText("\n");
    }


    if (DirPickerCtrl2->GetPath() != "")
    {

        wxString lt_sym_from, lt_sym_to;

        wxString lt_sym_dir = DirPickerCtrl2->GetPath();

        pattern = "*.asy";

        file = wxFindFirstFile(pattern);

        TextCtrl1->AppendText("Exporting LTspice Symbols\n");
        TextCtrl1->AppendText("=========================\n\n");

        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;
            lt_sym_to = lt_sym_dir + separator + file_no_path;

            wxCopyFile(lt_sym_from, lt_sym_to);

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

            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[i] = "SYMATTR ModelFile " + pathToMOD + separator + "SPICE" + separator + file_name + "_LTspice.lib";
                    }
                file_to_edit.Write();
                }
            file_to_edit.Close();
            }

            file = wxFindNextFile();
        }
    }

    TextCtrl1->AppendText("\nExport Complete");

    wxSetWorkingDirectory(pathToMOD);
}