/////////////////////////////////////////////////////////////////////////////////
//
// 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
//     SPICEMODEL.cpp
//
// DESCRIPTION
//     Application for the creation of Spice models
//
// AUTHOR(S)
//     Steve Greedy
//
/////////////////////////////////////////////////////////////////////////////////
#include "SPICEMODEL.h"

#include <wx/filename.h>
#include <wx/dcclient.h>
#include <wx/tokenzr.h>

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;


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

//(*IdInit(SPICEMODEL)
const long SPICEMODEL::ID_STATICTEXT1 = wxNewId();
const long SPICEMODEL::ID_TEXTCTRL1 = wxNewId();
const long SPICEMODEL::ID_PANEL2 = wxNewId();
const long SPICEMODEL::ID_STATICTEXT21 = wxNewId();
const long SPICEMODEL::ID_TEXTCTRL15 = wxNewId();
const long SPICEMODEL::ID_STATICTEXT2 = wxNewId();
const long SPICEMODEL::ID_TEXTCTRL2 = wxNewId();
const long SPICEMODEL::ID_STATICTEXT3 = wxNewId();
const long SPICEMODEL::ID_STATICTEXT4 = wxNewId();
const long SPICEMODEL::ID_STATICTEXT5 = wxNewId();
const long SPICEMODEL::ID_TEXTCTRL3 = wxNewId();
const long SPICEMODEL::ID_STATICTEXT6 = wxNewId();
const long SPICEMODEL::ID_TEXTCTRL4 = wxNewId();
const long SPICEMODEL::ID_STATICTEXT7 = wxNewId();
const long SPICEMODEL::ID_TEXTCTRL5 = wxNewId();
const long SPICEMODEL::ID_STATICTEXT8 = wxNewId();
const long SPICEMODEL::ID_TEXTCTRL6 = wxNewId();
const long SPICEMODEL::ID_PANEL3 = wxNewId();
const long SPICEMODEL::ID_STATICTEXT9 = wxNewId();
const long SPICEMODEL::ID_TEXTCTRL7 = wxNewId();
const long SPICEMODEL::ID_STATICTEXT10 = wxNewId();
const long SPICEMODEL::ID_TEXTCTRL8 = wxNewId();
const long SPICEMODEL::ID_STATICTEXT11 = wxNewId();
const long SPICEMODEL::ID_TEXTCTRL9 = wxNewId();
const long SPICEMODEL::ID_PANEL4 = wxNewId();
const long SPICEMODEL::ID_STATICTEXT17 = wxNewId();
const long SPICEMODEL::ID_CHECKBOX1 = wxNewId();
const long SPICEMODEL::ID_STATICTEXT12 = wxNewId();
const long SPICEMODEL::ID_TEXTCTRL10 = wxNewId();
const long SPICEMODEL::ID_STATICTEXT13 = wxNewId();
const long SPICEMODEL::ID_RADIOBUTTON1 = wxNewId();
const long SPICEMODEL::ID_RADIOBUTTON2 = wxNewId();
const long SPICEMODEL::ID_STATICTEXT14 = wxNewId();
const long SPICEMODEL::ID_TEXTCTRL11 = wxNewId();
const long SPICEMODEL::ID_STATICTEXT15 = wxNewId();
const long SPICEMODEL::ID_TEXTCTRL12 = wxNewId();
const long SPICEMODEL::ID_STATICTEXT16 = wxNewId();
const long SPICEMODEL::ID_TEXTCTRL13 = wxNewId();
const long SPICEMODEL::ID_PANEL5 = wxNewId();
const long SPICEMODEL::ID_NOTEBOOK1 = wxNewId();
const long SPICEMODEL::ID_BUTTON1 = wxNewId();
const long SPICEMODEL::ID_BUTTON2 = wxNewId();
const long SPICEMODEL::ID_BUTTON3 = wxNewId();
const long SPICEMODEL::ID_PANEL1 = wxNewId();
//*)

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

SPICEMODEL::SPICEMODEL(wxWindow* parent,wxWindowID id,const wxPoint& pos,const wxSize& size)
{
	//(*Initialize(SPICEMODEL)
	wxBoxSizer* BoxSizer4;
	wxStaticBoxSizer* StaticBoxSizer2;
	wxFlexGridSizer* FlexGridSizer4;
	wxFlexGridSizer* FlexGridSizer10;
	wxFlexGridSizer* FlexGridSizer3;
	wxFlexGridSizer* FlexGridSizer5;
	wxFlexGridSizer* FlexGridSizer9;
	wxFlexGridSizer* FlexGridSizer2;
	wxBoxSizer* BoxSizer2;
	wxFlexGridSizer* FlexGridSizer7;
	wxStaticBoxSizer* StaticBoxSizer6;
	wxFlexGridSizer* FlexGridSizer8;
	wxBoxSizer* BoxSizer1;
	wxFlexGridSizer* FlexGridSizer6;
	wxStaticBoxSizer* StaticBoxSizer1;
	wxFlexGridSizer* FlexGridSizer1;
	wxBoxSizer* BoxSizer3;

	Create(parent, wxID_ANY, _("SACAMOS: SPICE MODEL BUILDER"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _T("wxID_ANY"));
	FlexGridSizer1 = new wxFlexGridSizer(0, 2, 0, 0);
	FlexGridSizer2 = new wxFlexGridSizer(0, 1, 0, 0);
	StaticBoxSizer2 = new wxStaticBoxSizer(wxVERTICAL, this, _("Spice Model Definition"));
	BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
	StaticText1 = new wxStaticText(this, ID_STATICTEXT1, _("Spice Model Name"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1"));
	BoxSizer1->Add(StaticText1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	TextCtrl1 = new wxTextCtrl(this, ID_TEXTCTRL1, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
	BoxSizer1->Add(TextCtrl1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticBoxSizer2->Add(BoxSizer1, 1, wxALL|wxEXPAND, 5);
	Panel2 = new wxPanel(this, ID_PANEL2, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL2"));
	StaticBoxSizer2->Add(Panel2, 5, wxALL|wxEXPAND, 5);
	FlexGridSizer2->Add(StaticBoxSizer2, 1, wxALL|wxEXPAND, 5);
	StaticBoxSizer6 = new wxStaticBoxSizer(wxHORIZONTAL, this, _("Cable Bundle Length"));
	FlexGridSizer7 = new wxFlexGridSizer(0, 3, 0, 0);
	StaticText21 = new wxStaticText(this, ID_STATICTEXT21, _("Length of Cable Bundle (m)"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT21"));
	FlexGridSizer7->Add(StaticText21, 1, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	TextCtrl15 = new wxTextCtrl(this, ID_TEXTCTRL15, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL15"));
	FlexGridSizer7->Add(TextCtrl15, 1, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	StaticBoxSizer6->Add(FlexGridSizer7, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	FlexGridSizer2->Add(StaticBoxSizer6, 1, wxALL|wxEXPAND, 5);
	BoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
	Notebook1 = new wxNotebook(this, ID_NOTEBOOK1, wxDefaultPosition, wxDefaultSize, 0, _T("ID_NOTEBOOK1"));
	Panel3 = new wxPanel(Notebook1, ID_PANEL3, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL3"));
	FlexGridSizer4 = new wxFlexGridSizer(0, 4, 0, 0);
	StaticText2 = new wxStaticText(Panel3, ID_STATICTEXT2, _("Amp (V/m)"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT2"));
	FlexGridSizer4->Add(StaticText2, 1, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	TextCtrl2 = new wxTextCtrl(Panel3, ID_TEXTCTRL2, _("0"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL2"));
	FlexGridSizer4->Add(TextCtrl2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticText3 = new wxStaticText(Panel3, ID_STATICTEXT3, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT3"));
	FlexGridSizer4->Add(StaticText3, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticText4 = new wxStaticText(Panel3, ID_STATICTEXT4, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT4"));
	FlexGridSizer4->Add(StaticText4, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticText5 = new wxStaticText(Panel3, ID_STATICTEXT5, _("k theta"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT5"));
	FlexGridSizer4->Add(StaticText5, 1, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	TextCtrl3 = new wxTextCtrl(Panel3, ID_TEXTCTRL3, _("0"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL3"));
	FlexGridSizer4->Add(TextCtrl3, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticText6 = new wxStaticText(Panel3, ID_STATICTEXT6, _("k phi"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT6"));
	FlexGridSizer4->Add(StaticText6, 1, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	TextCtrl4 = new wxTextCtrl(Panel3, ID_TEXTCTRL4, _("0"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL4"));
	FlexGridSizer4->Add(TextCtrl4, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticText7 = new wxStaticText(Panel3, ID_STATICTEXT7, _("E theta"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT7"));
	FlexGridSizer4->Add(StaticText7, 1, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	TextCtrl5 = new wxTextCtrl(Panel3, ID_TEXTCTRL5, _("0"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL5"));
	FlexGridSizer4->Add(TextCtrl5, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticText8 = new wxStaticText(Panel3, ID_STATICTEXT8, _("E phi"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT8"));
	FlexGridSizer4->Add(StaticText8, 1, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	TextCtrl6 = new wxTextCtrl(Panel3, ID_TEXTCTRL6, _("0"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL6"));
	FlexGridSizer4->Add(TextCtrl6, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	Panel3->SetSizer(FlexGridSizer4);
	FlexGridSizer4->Fit(Panel3);
	FlexGridSizer4->SetSizeHints(Panel3);
	Panel4 = new wxPanel(Notebook1, ID_PANEL4, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL4"));
	FlexGridSizer5 = new wxFlexGridSizer(0, 2, 0, 0);
	ConductorNumber = new wxStaticText(Panel4, ID_STATICTEXT9, _("Number of Transfer Impedances"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT9"));
	FlexGridSizer5->Add(ConductorNumber, 1, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	TextCtrl7 = new wxTextCtrl(Panel4, ID_TEXTCTRL7, _("0"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL7"));
	FlexGridSizer5->Add(TextCtrl7, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticText10 = new wxStaticText(Panel4, ID_STATICTEXT10, _("Conductor Number List"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT10"));
	FlexGridSizer5->Add(StaticText10, 1, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	TextCtrl8 = new wxTextCtrl(Panel4, ID_TEXTCTRL8, _("0"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL8"));
	FlexGridSizer5->Add(TextCtrl8, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticText11 = new wxStaticText(Panel4, ID_STATICTEXT11, _("Coupling Direction List"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT11"));
	FlexGridSizer5->Add(StaticText11, 1, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	TextCtrl9 = new wxTextCtrl(Panel4, ID_TEXTCTRL9, _("0"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL9"));
	FlexGridSizer5->Add(TextCtrl9, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	Panel4->SetSizer(FlexGridSizer5);
	FlexGridSizer5->Fit(Panel4);
	FlexGridSizer5->SetSizeHints(Panel4);
	Panel5 = new wxPanel(Notebook1, ID_PANEL5, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL5"));
	FlexGridSizer6 = new wxFlexGridSizer(0, 1, 0, 0);
	BoxSizer4 = new wxBoxSizer(wxHORIZONTAL);
	StaticText16 = new wxStaticText(Panel5, ID_STATICTEXT17, _("Use Filter Fitting"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT17"));
	BoxSizer4->Add(StaticText16, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	CheckBox1 = new wxCheckBox(Panel5, ID_CHECKBOX1, _("Label"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX1"));
	CheckBox1->SetValue(false);
	BoxSizer4->Add(CheckBox1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	FlexGridSizer6->Add(BoxSizer4, 1, wxALL|wxEXPAND, 5);
	FlexGridSizer8 = new wxFlexGridSizer(0, 2, 0, 0);
	StaticText9 = new wxStaticText(Panel5, ID_STATICTEXT12, _("Order for Filter Fitting "), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT12"));
	FlexGridSizer8->Add(StaticText9, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	TextCtrl10 = new wxTextCtrl(Panel5, ID_TEXTCTRL10, _("-10"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL10"));
	TextCtrl10->SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT));
	FlexGridSizer8->Add(TextCtrl10, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	FlexGridSizer6->Add(FlexGridSizer8, 1, wxALL, 5);
	FlexGridSizer9 = new wxFlexGridSizer(0, 3, 0, 0);
	StaticText12 = new wxStaticText(Panel5, ID_STATICTEXT13, _("Range Type"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT13"));
	FlexGridSizer9->Add(StaticText12, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	RadioButton1 = new wxRadioButton(Panel5, ID_RADIOBUTTON1, _("Lin"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_RADIOBUTTON1"));
	FlexGridSizer9->Add(RadioButton1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	RadioButton2 = new wxRadioButton(Panel5, ID_RADIOBUTTON2, _("Log"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_RADIOBUTTON2"));
	FlexGridSizer9->Add(RadioButton2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	FlexGridSizer6->Add(FlexGridSizer9, 1, wxALL|wxEXPAND, 5);
	FlexGridSizer10 = new wxFlexGridSizer(0, 2, 0, 0);
	StaticText13 = new wxStaticText(Panel5, ID_STATICTEXT14, _("Frequency min (Hz)"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT14"));
	FlexGridSizer10->Add(StaticText13, 1, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	TextCtrl11 = new wxTextCtrl(Panel5, ID_TEXTCTRL11, _("1e3"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL11"));
	TextCtrl11->SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT));
	FlexGridSizer10->Add(TextCtrl11, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticText14 = new wxStaticText(Panel5, ID_STATICTEXT15, _("Frequecy max (Hz)"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT15"));
	FlexGridSizer10->Add(StaticText14, 1, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	TextCtrl12 = new wxTextCtrl(Panel5, ID_TEXTCTRL12, _("1e9"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL12"));
	TextCtrl12->SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT));
	FlexGridSizer10->Add(TextCtrl12, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticText15 = new wxStaticText(Panel5, ID_STATICTEXT16, _("Number of Frequencies"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT16"));
	FlexGridSizer10->Add(StaticText15, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	TextCtrl13 = new wxTextCtrl(Panel5, ID_TEXTCTRL13, _("12"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL13"));
	TextCtrl13->SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT));
	FlexGridSizer10->Add(TextCtrl13, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	FlexGridSizer6->Add(FlexGridSizer10, 1, wxALL|wxEXPAND, 5);
	Panel5->SetSizer(FlexGridSizer6);
	FlexGridSizer6->Fit(Panel5);
	FlexGridSizer6->SetSizeHints(Panel5);
	Notebook1->AddPage(Panel3, _("Incident Field Excitation"), false);
	Notebook1->AddPage(Panel4, _("Transfer Impedance Model"), false);
	Notebook1->AddPage(Panel5, _("Filter Fitting"), false);
	BoxSizer3->Add(Notebook1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	FlexGridSizer2->Add(BoxSizer3, 1, wxALL|wxEXPAND, 5);
	BoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
	Button1 = new wxButton(this, ID_BUTTON1, _("SAVE"), 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, _("BUILD"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON2"));
	BoxSizer2->Add(Button2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	Button3 = new wxButton(this, ID_BUTTON3, _("CLOSE"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON3"));
	BoxSizer2->Add(Button3, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	FlexGridSizer2->Add(BoxSizer2, 1, wxALL|wxEXPAND, 5);
	FlexGridSizer1->Add(FlexGridSizer2, 1, wxALL|wxALIGN_TOP|wxALIGN_CENTER_HORIZONTAL, 5);
	FlexGridSizer3 = new wxFlexGridSizer(0, 1, 0, 0);
	StaticBoxSizer1 = new wxStaticBoxSizer(wxHORIZONTAL, this, _("2D Cross Section"));
	Panel1 = new wxPanel(this, ID_PANEL1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL1"));
	Panel1->SetMinSize(wxSize(500,500));
	Panel1->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
	StaticBoxSizer1->Add(Panel1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	FlexGridSizer3->Add(StaticBoxSizer1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	FlexGridSizer1->Add(FlexGridSizer3, 1, wxALL|wxALIGN_TOP|wxALIGN_CENTER_HORIZONTAL, 5);
	SetSizer(FlexGridSizer1);
	FlexGridSizer1->Fit(this);
	FlexGridSizer1->SetSizeHints(this);

	Connect(ID_TEXTCTRL15,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&SPICEMODEL::OnTextCtrl15Text);
	Connect(ID_TEXTCTRL2,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&SPICEMODEL::OnTextCtrl2Text);
	Connect(ID_TEXTCTRL3,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&SPICEMODEL::OnTextCtrl3Text);
	Connect(ID_TEXTCTRL4,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&SPICEMODEL::OnTextCtrl4Text);
	Connect(ID_TEXTCTRL5,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&SPICEMODEL::OnTextCtrl5Text);
	Connect(ID_TEXTCTRL6,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&SPICEMODEL::OnTextCtrl6Text);
	Connect(ID_TEXTCTRL7,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&SPICEMODEL::OnTextCtrl7Text);
	Connect(ID_TEXTCTRL8,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&SPICEMODEL::OnTextCtrl8Text);
	Connect(ID_TEXTCTRL9,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&SPICEMODEL::OnTextCtrl9Text);
	Connect(ID_TEXTCTRL13,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&SPICEMODEL::OnTextCtrl13Text1);
	Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&SPICEMODEL::OnButton1Click);
	Connect(ID_BUTTON2,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&SPICEMODEL::OnButton2Click);
	Connect(ID_BUTTON3,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&SPICEMODEL::OnButton3Click);
	Panel1->Connect(wxEVT_PAINT,(wxObjectEventFunction)&SPICEMODEL::OnPanel1Paint,0,this);
	Connect(wxID_ANY,wxEVT_INIT_DIALOG,(wxObjectEventFunction)&SPICEMODEL::OnInit);
	//*)
}

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

void SPICEMODEL::OnInit(wxInitDialogEvent& event)
{
}

void SPICEMODEL::SetPathToMOD(wxString Path, wxString Name)
{
    PathToMOD << Path;
    MODname << Name;
}

void SPICEMODEL::SetPathForDir(wxString Path, wxString Name)
{
    PathToMOD << Path;
    MODname << Name;
    PopulateTheDirectoryTree(PathToMOD);
}

void SPICEMODEL::PopulateTheDirectoryTree (wxString Path)
{
    int size_h, size_v;
    Panel2->GetSize(&size_h, &size_v );
    //wxGenericDirCtrl *tree;
    tree = (wxSpecificDirCtrl *) NULL;
    tree = new wxSpecificDirCtrl (Panel2, -1, Path,
        wxDefaultPosition,
        wxSize(size_h, size_v),
        wxSIMPLE_BORDER,//wxDIRCTRL_3D_INTERNAL|wxSUNKEN_BORDER,
        "*.bundle_spec", 0,
        _T("Directory Listing"));

    Connect(tree->GetTreeCtrl()->GetId(),wxEVT_COMMAND_TREE_ITEM_ACTIVATED, (wxObjectEventFunction) (wxEventFunction)(wxCommandEventFunction) &SPICEMODEL::OnTreeSelectionChanged);

}

void SPICEMODEL::OnTreeSelectionChanged(wxTreeEvent& evt)
{

    wxFileName fname (tree->GetFilePath());
    wxString File_Path         = fname.GetPath();
    wxString File_FullName     = fname.GetFullName();
    wxString File_FullNamePath = fname.GetFullPath();

    size_t Name = File_FullName.find_last_of(".");
    bundleSelected = File_FullName.substr(0,Name);

    if(File_FullName.substr(File_FullName.find_last_of(".") + 1) == "bundle_spec") {

         draw_bundle(Panel1, PathToMOD, File_FullNamePath);
    }

}

void SPICEMODEL::OnPanel1Paint(wxPaintEvent& event)
{
    FormatPanel("temp");
}

void SPICEMODEL::FormatPanel(wxString TheFileNameToDraw)
{
    int size_h, size_v;
    Panel1->GetSize(&size_h, &size_v );

    wxClientDC dc(Panel1);
    dc.Clear();

    // Draw centre lines
    dc.SetPen( wxPen( wxColor(224,224,224),1 , wxDOT_DASH) );
    dc.DrawLine( 0, size_v/2, size_h,size_v/2 );
    dc.DrawLine( size_h/2, 0, size_h/2, size_v);

    dc.SetTextForeground(*wxBLACK);
    dc.DrawText("y=0", 5, (size_v/2)-15);
    dc.DrawText("x=0", (size_h/2)+5, size_v-20);

    // Draw ordinates
    dc.SetPen( wxPen( wxColor(255,0,0),1 ) );
    dc.DrawCircle( wxPoint(5,size_v-10), 5 );

    dc.DrawLine( 5, size_v-10, 30, size_v-10 );
    dc.DrawLine( 5, size_v-10, 5, size_v-35);
    dc.DrawText("y", 10, size_v-45);
    dc.DrawText("x", 30, size_v-25);
}


void SPICEMODEL::OnButton2Click(wxCommandEvent& event)
{
    if (bundleSelected == "")
    {
        wxMessageBox("Please Select a Bundle" , _("Missing Bundle Specification"));

    }
    else
    {
        //SPICE CABLE BUNDLE MODEL
        wxString fileName = TextCtrl1->GetValue();
        wxString command_line;

        wxString separator(wxFileName::GetPathSeparator());

        std::stringstream stream;

        stream  <<"." + separator
                << "spice_cable_bundle_model_builder"
                << " "
                << ".." + separator + MODname + separator + "SPICE" + separator
                << fileName ;



//                << "\"..\\BIN\\spice_cable_bundle_model_builder\""
//                << " "
//                << "..\\" + MODname + "\\SPICE\\"
//                << fileName;

        command_line << stream.str().c_str();

        wxExecute (command_line, wxEXEC_SYNC);

        RUN_STATUS *run_status = new RUN_STATUS(this);
        run_status->ShowModal();

        Button1->Enable();
    }
}

void SPICEMODEL::OnButton1Click(wxCommandEvent& event)
{
    int IsError = 0;

    wxString fileName = TextCtrl1->GetValue();

    TextCtrl1->SetBackgroundColour(wxColor(255,255,255));

    if (fileName == "")
    {
        wxMessageBox("Please Specify a Cable Name" , _("Missing Cable Name"));
        return;
    }

    check_is_empty(TextCtrl15, &IsError);

    if ( IsError == 1)
    {
        wxMessageBox("Please Specify a Cable Length" , _("Missing Cable Length"));
        return;
    }

    if ( IsError == 1)
    {
        wxMessageBox("Please fix highlighted errors" , _("Non numeric entry"));
        return;
    }



    int conductor_number;
    wxString cn = TextCtrl7->GetValue();
    conductor_number = wxAtoi(cn);

    wxArrayString conductor_id;
    wxArrayString conductor_dn;

    wxString conductor_ids = TextCtrl8->GetValue();
    wxString conductor_dns = TextCtrl9->GetValue();

    // First set of values
    wxStringTokenizer tk_IDS( conductor_ids, " ", wxTOKEN_DEFAULT);

    while ( tk_IDS.HasMoreTokens())
    {
        conductor_id.Add (tk_IDS.GetNextToken());
    }


    // Second set
    wxStringTokenizer tk_DNS( conductor_dns, " ", wxTOKEN_DEFAULT);

    while ( tk_DNS.HasMoreTokens())
    {
        conductor_dn.Add (tk_DNS.GetNextToken());
    }

    if (fileName == "" || bundleSelected == "")
    {
        if (fileName == "") wxMessageBox("Please Specify a Cable Name" , _("Missing Cable Name"));

        else
        {
            if (bundleSelected == "") wxMessageBox("Please Select a Bundle" , _("Missing Bundle Specification"));
        }
    }

    else
    {
        ofstream spiceFile;

        wxString fullFileName = PathToMOD + "//SPICE//" + fileName + ".spice_model_spec";

        spiceFile.open (fullFileName);

        if (spiceFile.is_open())
        {
            // minimum requirements for type Cylindrical
            spiceFile << "# MOD_cable_lib_dir \n";
            spiceFile <<"../"<< MODname <<"/CABLE/"<<"\n";
            spiceFile << "# MOD_bundle_lib_dir \n";
            spiceFile <<"../"<< MODname <<"/BUNDLE/"<<"\n";
            spiceFile << "# MOD_spice_bundle_lib_dir \n";
            spiceFile <<"../"<< MODname <<"/SPICE/"<<"\n";
            spiceFile << "# spice_symbol_dir \n";
            spiceFile <<"../"<< MODname <<"/SPICE/SYMBOL/"<<"\n";

            spiceFile << "# Specification for spice model of " + fileName + "\n";
            spiceFile << bundleSelected<<"\n";//"wire_over_ground \n";
            spiceFile << "# cable bundle length (m) \n";
            spiceFile << TextCtrl15->GetValue() <<"\n";

            spiceFile <<"# Incident field specification\n";
            spiceFile <<TextCtrl2->GetValue() <<" amplitude (V/m)\n";

            spiceFile <<TextCtrl3->GetValue()<<" "<<TextCtrl4->GetValue()<<" "<<"ktheta kphi (degrees)\n";
            spiceFile <<TextCtrl5->GetValue()<<" "<<TextCtrl6->GetValue()<<" "<<"Etheta Ephi (degrees)\n";

            spiceFile <<"# Transfer Impedance Model\n";
            spiceFile <<TextCtrl7->GetValue()<<" # number of transfer impedances to include in the model\n";

            if (conductor_number != 0)
            {
                for (size_t i = 0; i < conductor_dn.size(); ++i)
                spiceFile <<conductor_id[i] << " " << conductor_dn[i] <<" # conductor number and coupling direction\n";
            }

            spiceFile <<"No_validation_test\n";

            if (CheckBox1->GetValue())
            {
                spiceFile << TextCtrl10->GetValue() <<"\t # order for filter fitting\n";
                if (RadioButton1)
                {
                    spiceFile << "lin   # frequency range type for filter fitting type (lin or dB)\n";
                }
                else
                {
                    spiceFile << "log   # frequency range type for filter fitting type (lin or dB)\n";
                }
                spiceFile << TextCtrl11->GetValue() <<" "<< TextCtrl12->GetValue() <<" "<< TextCtrl13->GetValue() <<"\t # fmin fmax number_of_frequencies for filter fitting\n";

            }
        }
    spiceFile.close();
    }

    Button1->Disable();
}


void SPICEMODEL::OnButton3Click(wxCommandEvent& event)
{
    Close();
}

void SPICEMODEL::OnTextCtrl15Text(wxCommandEvent& event)
{
    int IsError = 0;
    check_is_numeric(TextCtrl15, &IsError);
    check_is_positive(TextCtrl15, &IsError);
}

void SPICEMODEL::OnTextCtrl2Text(wxCommandEvent& event)
{
    int IsError = 0;
    check_is_numeric(TextCtrl2, &IsError);
}

void SPICEMODEL::OnTextCtrl3Text(wxCommandEvent& event)
{
    int IsError = 0;
    check_is_numeric(TextCtrl3, &IsError);
}

void SPICEMODEL::OnTextCtrl4Text(wxCommandEvent& event)
{
    int IsError = 0;
    check_is_numeric(TextCtrl4, &IsError);
}

void SPICEMODEL::OnTextCtrl5Text(wxCommandEvent& event)
{
    int IsError = 0;
    check_is_numeric(TextCtrl5, &IsError);
}

void SPICEMODEL::OnTextCtrl6Text(wxCommandEvent& event)
{
    int IsError = 0;
    check_is_numeric(TextCtrl6, &IsError);
}

void SPICEMODEL::OnTextCtrl7Text(wxCommandEvent& event)
{
    int IsError = 0;
    check_is_numeric(TextCtrl6, &IsError);
}

void SPICEMODEL::OnTextCtrl8Text(wxCommandEvent& event)
{
    int IsError = 0;
    check_is_numeric_list(TextCtrl8, &IsError);
}

void SPICEMODEL::OnTextCtrl9Text(wxCommandEvent& event)
{
    int IsError = 0;
    check_is_numeric_list(TextCtrl9, &IsError);
}

void SPICEMODEL::OnNotebook1PageChanged(wxNotebookEvent& event)
{
}

void SPICEMODEL::OnNotebook1PageChanged1(wxNotebookEvent& event)
{
}

void SPICEMODEL::OnTextCtrl13Text1(wxCommandEvent& event)
{
}

