Power System Platform  2026w10a-beta
Loading...
Searching...
No Matches
ExportCSVForm.cpp
1#include "ExportCSVForm.h"
2#include <wx/filedlg.h>
3#include <wx/msgdlg.h>
4#include <wx/ffile.h>
5
6ExportCSVForm::ExportCSVForm(wxWindow* parent, const std::vector<wxGrid*>& gridList)
7 : ExportCSVFormBase(parent), m_gridList(gridList)
8{
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 };
12}
13
14ExportCSVForm::~ExportCSVForm()
15{
16}
17
18void ExportCSVForm::OnCancelButtonClick(wxCommandEvent& event)
19{
20 EndModal(wxID_CANCEL);
21}
22
23void ExportCSVForm::OnExportButtonClick(wxCommandEvent& event)
24{
25 wxFileDialog saveFileDialog(this,
26 _("Export CSV"),
27 "",
28 _("results.csv"),
29 _("CSV files (*.csv)|*.csv"),
30 wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
31
32 if (saveFileDialog.ShowModal() == wxID_CANCEL)
33 return;
34
35 wxString fileAddName[10] = { _("_PowerFlow"), _("_PFBuses"), _("_PFBranches"), _("_Fault"), _("_FaultBuses"), _("_FaultBranches"),
36 _("_FaultGenerators"), _("_HarmSources"), _("_HarmVoltages"), _("_HarmCurrents") };
37 std::vector<wxString> filesSaved;
38 int i = 0;
39 for (auto& checkBox : m_checkBoxList)
40 {
41 if (checkBox->GetValue())
42 {
43 wxString path = saveFileDialog.GetPath();
44 path.insert(path.Length() - 4, fileAddName[i]);
45 ExportGridToCSV(m_gridList[i], path);
46
47 wxFileName fn(path);
48 filesSaved.push_back(fn.GetFullName());
49 }
50 i++;
51 }
52
53 wxString message = _("The following files have been successfully exported:\n");
54 for (const auto& file : filesSaved)
55 message += file + "\n";
56
57 wxMessageDialog msgDialog(this, message, _("Information"), wxOK | wxCENTRE | wxICON_INFORMATION);
58 msgDialog.ShowModal();
59
60 EndModal(wxID_OK);
61}
62
63bool ExportCSVForm::ExportGridToCSV(wxGrid* grid, const wxString& filePath)
64{
65 if (!grid)
66 return false;
67
68 wxFFile file(filePath, "wb");
69 if (!file.IsOpened())
70 return false;
71
72 // Write UTF-8 BOM so Excel detects encoding properly
73 file.Write("\xEF\xBB\xBF", 3);
74
75 int rows = grid->GetNumberRows();
76 int cols = grid->GetNumberCols();
77
78 for (int row = 0; row < rows; ++row)
79 {
80 wxString line;
81 for (int col = 0; col < cols; )
82 {
83
84 int rowspan = 1;
85 int colspan = 1;
86
87 grid->GetCellSize(row, col, &rowspan, &colspan);
88
89
90 // Cell covered by vertical merge
91 if (rowspan <= 0 || colspan <= 0)
92 {
93 line << wxT("\u200B");
94 ++col;
95 continue;
96 }
97 wxString value = grid->GetCellValue(row, col);
98
99 // Escape quotes for CSV
100 value.Replace("\"", "\"\"");
101
102 // Wrap with quotes if necessary
103 if (value.Contains(";") || value.Contains("\"") || value.Contains("\n"))
104 {
105 value = "\"" + value + "\"";
106 }
107
108 if (!line.IsEmpty())
109 line << ";";
110
111 line << value;
112
113 // Horizontal merge
114 for (int c = 1; c < colspan; ++c)
115 {
116 line << ";";
117 }
118
119 col += colspan;
120 }
121
122 line << "\r\n";
123
124 // Convert once to UTF-8 and write raw bytes
125 wxScopedCharBuffer buffer = line.utf8_str();
126 file.Write(buffer.data(), buffer.length());
127 }
128
129 file.Close();
130 return true;
131}