1#include "ExportCSVForm.h"
6ExportCSVForm::ExportCSVForm(wxWindow* parent,
const std::vector<wxGrid*>& gridList)
7 : ExportCSVFormBase(parent), m_gridList(gridList)
9 m_checkBoxList = { m_checkBoxPFPowerFlow, m_checkBoxPFBuses, m_checkBoxPFBranches, m_checkBoxCCFault, m_checkBoxCCBuses,
10 m_checkBoxCCBranches, m_checkBoxCCGenerators, m_checkBoxHarmSources, m_checkBoxHarmVoltages,
11 m_checkBoxharmCurrents };
14ExportCSVForm::~ExportCSVForm()
18void ExportCSVForm::OnCancelButtonClick(wxCommandEvent& event)
20 EndModal(wxID_CANCEL);
23void ExportCSVForm::OnExportButtonClick(wxCommandEvent& event)
25 wxFileDialog saveFileDialog(
this,
29 _(
"CSV files (*.csv)|*.csv"),
30 wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
32 if (saveFileDialog.ShowModal() == wxID_CANCEL)
35 wxString fileAddName[10] = { _(
"_PowerFlow"), _(
"_PFBuses"), _(
"_PFBranches"), _(
"_Fault"), _(
"_FaultBuses"), _(
"_FaultBranches"),
36 _(
"_FaultGenerators"), _(
"_HarmSources"), _(
"_HarmVoltages"), _(
"_HarmCurrents") };
37 std::vector<wxString> filesSaved;
39 for (
auto& checkBox : m_checkBoxList)
41 if (checkBox->GetValue())
43 wxString path = saveFileDialog.GetPath();
44 path.insert(path.Length() - 4, fileAddName[i]);
45 ExportGridToCSV(m_gridList[i], path);
48 filesSaved.push_back(fn.GetFullName());
53 wxString message = _(
"The following files have been successfully exported:\n");
54 for (
const auto& file : filesSaved)
55 message += file +
"\n";
57 wxMessageDialog msgDialog(
this, message, _(
"Information"), wxOK | wxCENTRE | wxICON_INFORMATION);
58 msgDialog.ShowModal();
63bool ExportCSVForm::ExportGridToCSV(wxGrid* grid,
const wxString& filePath)
68 wxFFile file(filePath,
"wb");
73 file.Write(
"\xEF\xBB\xBF", 3);
75 int rows = grid->GetNumberRows();
76 int cols = grid->GetNumberCols();
78 for (
int row = 0; row < rows; ++row)
81 for (
int col = 0; col < cols; )
87 grid->GetCellSize(row, col, &rowspan, &colspan);
91 if (rowspan <= 0 || colspan <= 0)
93 line << wxT(
"\u200B");
97 wxString value = grid->GetCellValue(row, col);
100 value.Replace(
"\"",
"\"\"");
103 if (value.Contains(
";") || value.Contains(
"\"") || value.Contains(
"\n"))
105 value =
"\"" + value +
"\"";
114 for (
int c = 1; c < colspan; ++c)
125 wxScopedCharBuffer buffer = line.utf8_str();
126 file.Write(buffer.data(), buffer.length());