Power System Platform  2026w34a-beta
Loading...
Searching...
No Matches
BusFormArcFlashProtection.cpp
1#include "BusFormArcFlashProtection.h"
2
3#include <wx/clipbrd.h>
4#include <wx/dcbuffer.h>
5#include <wx/textfile.h>
6#include <wx/filedlg.h>
7#include <wx/numformatter.h>
8
12
13namespace
14{
15 wxString FormatProtectionNumber(double value)
16 {
17 return wxNumberFormatter::ToString(value, 12, wxNumberFormatter::Style_None | wxNumberFormatter::Style_NoTrailingZeroes);
18 }
19
20 wxString FormatOptionalNumber(double value)
21 {
22 return value == 0.0 ? wxString() : FormatProtectionNumber(value);
23 }
24}
25
26
27BusFormArcFlashProtection::BusFormArcFlashProtection(wxWindow* parent, Bus* bus, double basePower)
28 : BusFormArcFlashProtectionBase(parent), m_bus(bus), m_basePower(basePower)
29{
30 const auto& busData = m_bus->GetElectricalDataRef();
31 double baseVoltage = m_bus->GetValueFromUnit(
32 m_bus->GetElectricalDataRef().nominalVoltage,
33 m_bus->GetElectricalDataRef().nominalVoltageUnit);
34 double baseCurrent = m_basePower / (std::sqrt(3.0) * baseVoltage);
35 double ibf = std::abs(busData.faultCurrent[0]) * baseCurrent;
36
37 if (busData.hasFault) {
38 m_staticTextIbf->SetLabel(_("Total Ibf (fault): ") + Bus::StringFromDouble(ibf / 1e3, 0, 4) + wxT(" kA"));
39 }
40
41 auto addDevice = [&](auto* device) {
42 const auto& parentList = device->GetParentList();
43 if (parentList.size() != 2)
44 return;
45
46 const auto& data = device->GetElectricalDataRef();
47 int busIndex = parentList[0] == m_bus ? 0 : 1;
48
49 double faultCurrent = std::abs(data.faultCurrent[busIndex][0]) * baseCurrent;
50
51 if (!device->IsOnline())
52 faultCurrent = 0.0;
53
54 const bool check = data.arcFlashProtection.configured
55 ? data.arcFlashProtection.consider
56 : faultCurrent > 1e-3 && data.faultCurrent[busIndex][0].real() > 1e-6;
57
58 wxString deviceName = data.name;
59 wxString faultCurrentString = Bus::StringFromDouble(faultCurrent / 1e3, 0, 2);
60
61 m_dvListCtrlDevices->AppendItem({
62 wxVariant(check),
63 wxVariant(deviceName),
64 wxVariant(faultCurrentString)
65 },
66 reinterpret_cast<wxUIntPtr>(device));
67 };
68 for (auto* element : m_bus->GetChildList()) {
69 if (auto* line = dynamic_cast<Line*>(element))
70 addDevice(line);
71 else if (auto* transformer = dynamic_cast<Transformer*>(element))
72 addDevice(transformer);
73 }
74
75 m_gridTCC->AppendCols(2);
76
77
78 m_gridTCC->SetColLabelValue(0, _("Current (A)"));
79 m_gridTCC->SetColLabelValue(1, _("Time (s)"));
80
81 m_gridTCC->SetColFormatFloat(0, -1, 3);
82 m_gridTCC->SetColFormatFloat(1, -1, 3);
83
84 int gridWidth = m_gridTCC->GetRowLabelSize();
85
86 for (int col = 0; col < m_gridTCC->GetNumberCols(); ++col)
87 gridWidth += m_gridTCC->GetColSize(col);
88
89 m_gridTCC->SetMinSize(wxSize(gridWidth + 100, -1));
90 m_splitterPageRight->SetMinSize(wxSize(gridWidth + 100, -1));
91
92 int devicesWidth = 30;
93
94 for (unsigned int i = 0; i < m_dvListCtrlDevices->GetColumnCount(); ++i) {
95#ifdef __WXMSW__
96 devicesWidth += static_cast<int>(m_dvListCtrlDevices->GetBestColumnWidth(static_cast<int>(i)));
97#else
98 devicesWidth += m_dvListCtrlDevices->GetColumn(i)->GetWidth();
99#endif
100 }
101
102 m_dvListCtrlDevices->SetMinSize(wxSize(devicesWidth, 100));
103
104 m_splitterPageLeft->Layout();
105 m_splitterPageRight->Layout();
106
107 GetSizer()->Fit(this);
108
109 m_splitter->SetSashGravity(1.0);
110
111 //const int rightWidth = m_splitterPageRight->GetSizer()->CalcMin().x;
112 const int rightWidth = m_splitterPageRight->GetMinWidth();
113 const int sashPosition = m_splitter->GetClientSize().x - rightWidth - m_splitter->GetSashSize();
114
115 m_splitter->SetSashPosition(sashPosition);
116
117 m_gridTCC->Bind(wxEVT_KEY_DOWN, &BusFormArcFlashProtection::OnGridKeyDown, this);
118
119 // Chart popup
120 wxWindow* corner = m_gridTCC->GetGridCornerLabelWindow();
121 corner->Bind(wxEVT_PAINT, &BusFormArcFlashProtection::OnGridCornerPaint, this);
122 corner->Bind(wxEVT_ENTER_WINDOW, [this](wxMouseEvent& event) {
123 if (!m_tccPopup)
124 m_tccPopup = new TCCPopup(this, m_gridTCC);
125
126 wxPoint mousePos = wxGetMousePosition();
127 m_tccPopup->Move(mousePos + wxPoint(15, 15));
128 m_tccPopup->Popup();
129
130 event.Skip();
131 });
132
133 corner->Bind(wxEVT_MOTION, [this](wxMouseEvent& event) {
134 if (m_tccPopup && m_tccPopup->IsShown()) {
135 const wxPoint mouse = wxGetMousePosition();
136 const wxSize popupSize = m_tccPopup->GetSize();
137 const int offset = FromDIP(10);
138
139 wxPoint pos = mouse + wxPoint(offset, offset);
140
141 const wxRect screen = wxGetClientDisplayRect();
142
143 if (pos.x + popupSize.x > screen.GetRight())
144 pos.x -= pos.x + popupSize.x - screen.GetRight();
145
146 if (pos.y + popupSize.y > screen.GetBottom())
147 pos.y -= pos.y + popupSize.y - screen.GetBottom();
148
149 if (pos.y < screen.GetTop())
150 pos.y = screen.GetTop();
151
152 m_tccPopup->SetPosition(pos);
153 }
154
155 event.Skip();
156 });
157
158 corner->Bind(wxEVT_LEAVE_WINDOW, [this](wxMouseEvent& event) {
159 if (m_tccPopup)
160 m_tccPopup->Dismiss();
161
162 event.Skip();
163 });
164
165 InitializeCBChoices();
166 UpdateRadioI1Selection();
167 EnableFields();
168 SetSize(GetBestSize());
169}
170
171BusFormArcFlashProtection::~BusFormArcFlashProtection()
172{
173 wxWindow* corner = m_gridTCC->GetGridCornerLabelWindow();
174 corner->Unbind(wxEVT_PAINT, &BusFormArcFlashProtection::OnGridCornerPaint, this);
175}
176
177void BusFormArcFlashProtection::EnableFields()
178{
179 switch (m_choiceType->GetSelection())
180 {
181 case 0:
182 m_choiceMethod->Enable();
183 m_choiceMethod->SetString(1, _("Appendix I"));
184 m_checkBoxIsMeltTime->Enable(false);
185 m_textCtrlDelay->Enable();
186 break;
187 case 1:
188 m_choiceMethod->Enable();
189 m_choiceMethod->SetString(1, _("Appendix H"));
190 m_checkBoxIsMeltTime->Enable();
191 m_textCtrlDelay->Enable(false);
192 break;
193 default: break;
194 }
195 switch (m_choiceMethod->GetSelection())
196 {
197 case 0:
198 m_panelTCC->Enable();
199 m_panelAI->Enable(false);
200 m_panelAH->Enable(false);
201 m_notebook->SetSelection(0);
202 break;
203 case 1:
204 //m_gridTCC->Enable(false);
205 //m_bmpButtonAdd->Enable(false);
206 //m_bmpButtonRemove->Enable(false);
207 //m_buttonImport->Enable(false);
208 m_panelTCC->Enable(false);
209 if (m_choiceType->GetSelection() == 0)
210 {
211 m_panelAI->Enable();
212 m_panelAH->Enable(false);
213 m_notebook->SetSelection(1);
214 }
215 else
216 {
217 m_panelAI->Enable(false);
218 m_panelAH->Enable();
219 m_notebook->SetSelection(2);
220 }
221 break;
222 default: break;
223 }
224}
225
226void BusFormArcFlashProtection::OnAddButtonClick(wxCommandEvent& event)
227{
228 const int row = m_gridTCC->GetNumberRows();
229 m_gridTCC->AppendRows(1);
230 m_gridTCC->SetCellValue(row, 0, "0.0");
231 m_gridTCC->SetCellValue(row, 1, "0.0");
232}
233
234void BusFormArcFlashProtection::OnDeviceChanged(wxDataViewEvent& event)
235{
236 //auto devicePtr = m_dvListCtrlDevices->GetItemData(event.GetItem());
237 //auto* element = reinterpret_cast<Element*>(devicePtr);
238 //ArcFlashProtectionData afpData;
239 //if (auto* line = dynamic_cast<Line*>(element))
240 // afpData = line->GetElectricalDataRef().arcFlashProtection;
241 //else if (auto* transformer = dynamic_cast<Transformer*>(element))
242 // afpData = transformer->GetElectricalDataRef().arcFlashProtection;
243 //LoadProtectionData(afpData);
244 if (m_restoringDeviceSelection)
245 return;
246
247 const wxDataViewItem newItem = event.GetItem();
248
249 if (!newItem.IsOk())
250 return;
251
252 if (newItem == m_previousDeviceSelection)
253 return;
254
255 auto getElement = [this](const wxDataViewItem& item) -> Element* {
256 auto devicePtr = m_dvListCtrlDevices->GetItemData(item);
257 return reinterpret_cast<Element*>(devicePtr);
258 };
259
260 auto getProtectionData = [](Element* element) -> ArcFlashProtectionData* {
261 if (auto* line = dynamic_cast<Line*>(element))
262 return &line->GetElectricalDataRef().arcFlashProtection;
263
264 if (auto* transformer = dynamic_cast<Transformer*>(element))
265 return &transformer->GetElectricalDataRef().arcFlashProtection;
266
267 return nullptr;
268 };
269
270 // Validate and save the previously selected device.
271 if (m_previousDeviceSelection.IsOk()) {
272 auto* previousElement = getElement(m_previousDeviceSelection);
273 auto* previousData = getProtectionData(previousElement);
274
275 if (previousData) {
276 ArcFlashProtectionData validatedData = *previousData;
277
278 const int previousRow =
279 m_dvListCtrlDevices->ItemToRow(m_previousDeviceSelection);
280
281 if (previousRow != wxNOT_FOUND)
282 validatedData.consider =
283 m_dvListCtrlDevices->GetToggleValue(previousRow, 0);
284
285 if (!ValidateAndGetProtectionData(validatedData)) {
286 m_restoringDeviceSelection = true;
287 m_dvListCtrlDevices->Unselect(newItem);
288 m_dvListCtrlDevices->Select(m_previousDeviceSelection);
289 m_restoringDeviceSelection = false;
290 return;
291 }
292
293 *previousData = validatedData;
294 }
295 }
296
297 // Load the newly selected device.
298 m_previousDeviceSelection = newItem;
299
300 if (auto* element = getElement(newItem)) {
301 if (auto* protectionData = getProtectionData(element))
302 LoadProtectionData(*protectionData);
303 }
304}
305
306void BusFormArcFlashProtection::OnImportButtonClick(wxCommandEvent& event)
307{
308 wxFileDialog dialog(
309 this,
310 _("Import TCC curve"),
311 "",
312 "",
313 _("CSV files (*.csv)|*.csv|All files (*.*)|*.*"),
314 wxFD_OPEN | wxFD_FILE_MUST_EXIST);
315
316 if (dialog.ShowModal() != wxID_OK)
317 return;
318
319 wxTextFile file;
320
321 if (!file.Open(dialog.GetPath()))
322 {
323 wxMessageBox(_("Unable to open the selected file."), _("Import error"), wxOK | wxICON_ERROR, this);
324 return;
325 }
326
327
328 m_gridTCC->ClearGrid();
329
330 if (m_gridTCC->GetNumberRows() > 0)
331 m_gridTCC->DeleteRows(0, m_gridTCC->GetNumberRows());
332
333 int row = 0;
334
335 for (size_t i = 0; i < file.GetLineCount(); ++i)
336 {
337 wxString line = file.GetLine(i).Trim(true).Trim(false);
338
339 if (line.IsEmpty())
340 continue;
341
342 wxArrayString columns;
343
344 if (line.Find(';') != wxNOT_FOUND)
345 {
346 wxStringTokenizer tokenizer(line, ";");
347
348 while (tokenizer.HasMoreTokens())
349 columns.Add(tokenizer.GetNextToken());
350 }
351 else if (line.Find('\t') != wxNOT_FOUND)
352 {
353 wxStringTokenizer tokenizer(line, "\t");
354
355 while (tokenizer.HasMoreTokens())
356 columns.Add(tokenizer.GetNextToken());
357 }
358 else
359 {
360 wxStringTokenizer tokenizer(line, ",");
361
362 while (tokenizer.HasMoreTokens())
363 columns.Add(tokenizer.GetNextToken());
364 }
365
366 if (columns.size() < 2)
367 continue;
368
369 wxString currentString = columns[0].Trim(true).Trim(false);
370 wxString timeString = columns[1].Trim(true).Trim(false);
371
372 double current = 0.0;
373 double time = 0.0;
374
375 if (!ParseDouble(currentString, current) || !ParseDouble(timeString, time))
376 {
377 if (row == 0)
378 continue;
379
380 wxMessageBox(
381 wxString::Format(_("Invalid data at line %zu."), i + 1),
382 _("Import error"),
383 wxOK | wxICON_ERROR,
384 this);
385
386 file.Close();
387 return;
388 }
389
390 if (current <= 0.0 || time <= 0.0)
391 {
392 wxMessageBox(
393 wxString::Format(_("Invalid data at line %zu. Current and time must be greater than zero."), i + 1),
394 _("Import error"),
395 wxOK | wxICON_ERROR,
396 this);
397
398 file.Close();
399 return;
400 }
401
402 m_gridTCC->AppendRows(1);
403 m_gridTCC->SetCellValue(row, 0, wxString::FromDouble(current));
404 m_gridTCC->SetCellValue(row, 1, wxString::FromDouble(time));
405
406 ++row;
407 }
408
409 file.Close();
410
411 m_gridTCC->ForceRefresh();
412
413 if (m_tccPopup && m_tccPopup->IsShown())
414 m_tccPopup->UpdateGraph();
415}
416
417void BusFormArcFlashProtection::OnMethodSelected(wxCommandEvent& event)
418{
419 EnableFields();
420}
421
422void BusFormArcFlashProtection::OnOKButtonClick(wxCommandEvent& event)
423{
424 const unsigned int itemCount = m_dvListCtrlDevices->GetItemCount();
425
426 if (itemCount == 0) {
427 EndModal(wxID_OK);
428 return;
429 }
430
431 auto getElement = [this](const wxDataViewItem& item) -> Element* {
432 return reinterpret_cast<Element*>(
433 m_dvListCtrlDevices->GetItemData(item));
434 };
435
436 auto getProtectionData = [](Element* element) -> ArcFlashProtectionData* {
437 if (auto* line = dynamic_cast<Line*>(element))
438 return &line->GetElectricalDataRef().arcFlashProtection;
439
440 if (auto* transformer = dynamic_cast<Transformer*>(element))
441 return &transformer->GetElectricalDataRef().arcFlashProtection;
442
443 return nullptr;
444 };
445
446 std::vector<std::pair<ArcFlashProtectionData*,
447 ArcFlashProtectionData>> updates;
448
449 updates.reserve(itemCount);
450
451 const wxDataViewItem originalSelection = m_previousDeviceSelection;
452
453 // Validate the currently displayed device first.
454 if (originalSelection.IsOk()) {
455 auto* element = getElement(originalSelection);
456 auto* protectionData = getProtectionData(element);
457
458 if (protectionData) {
459 ArcFlashProtectionData data = *protectionData;
460
461 const int row = m_dvListCtrlDevices->ItemToRow(originalSelection);
462 if (row != wxNOT_FOUND)
463 data.consider = m_dvListCtrlDevices->GetToggleValue(row, 0);
464
465 if (!ValidateAndGetProtectionData(data))
466 return;
467
468 updates.emplace_back(protectionData, data);
469 }
470 }
471
472 // Validate all remaining devices.
473 m_restoringDeviceSelection = true;
474
475 for (unsigned int row = 0; row < itemCount; ++row) {
476 const wxDataViewItem item = m_dvListCtrlDevices->RowToItem(row);
477
478 if (item == originalSelection)
479 continue;
480
481 auto* element = getElement(item);
482 auto* protectionData = getProtectionData(element);
483
484 if (!protectionData)
485 continue;
486
487 LoadProtectionData(*protectionData);
488
489 ArcFlashProtectionData data = *protectionData;
490 data.consider = m_dvListCtrlDevices->GetToggleValue(row, 0);
491
492 if (!ValidateAndGetProtectionData(data)) {
493 m_restoringDeviceSelection = false;
494
495 m_dvListCtrlDevices->UnselectAll();
496 m_dvListCtrlDevices->Select(item);
497 m_previousDeviceSelection = item;
498
499 LoadProtectionData(*protectionData);
500 return;
501 }
502
503 updates.emplace_back(protectionData, data);
504 }
505
506 m_restoringDeviceSelection = false;
507
508 // Commit only after every device has been validated.
509 for (auto& [protectionData, data] : updates)
510 *protectionData = data;
511
512 EndModal(wxID_OK);
513}
514
515void BusFormArcFlashProtection::OnRemoveButtonClick(wxCommandEvent& event)
516{
517 wxArrayInt rows = m_gridTCC->GetSelectedRows();
518
519 for (int i = rows.size() - 1; i >= 0; --i)
520 {
521 m_gridTCC->DeleteRows(rows[i], 1);
522 }
523}
524
525void BusFormArcFlashProtection::OnTypeSelected(wxCommandEvent& event)
526{
527 EnableFields();
528}
529
530void BusFormArcFlashProtection::OnnCancelButtonClick(wxCommandEvent& event)
531{
532 EndModal(wxID_CANCEL);
533}
534
535void BusFormArcFlashProtection::OnGridKeyDown(wxKeyEvent& event)
536{
537 if (!event.ControlDown() || event.GetKeyCode() != 'V') {
538 event.Skip();
539 return;
540 }
541
542 if (!wxTheClipboard->Open()) {
543 event.Skip();
544 return;
545 }
546
547 wxTextDataObject data;
548
549 if (!wxTheClipboard->GetData(data)) {
550 wxTheClipboard->Close();
551 event.Skip();
552 return;
553 }
554
555 wxTheClipboard->Close();
556
557 wxString text = data.GetText();
558 text.Replace("\r\n", "\n");
559 text.Replace("\r", "\n");
560
561 wxArrayString rows;
562 wxStringTokenizer rowTokenizer(text, "\n");
563
564 while (rowTokenizer.HasMoreTokens())
565 rows.Add(rowTokenizer.GetNextToken());
566
567 int startRow = m_gridTCC->GetGridCursorRow();
568 int startCol = m_gridTCC->GetGridCursorCol();
569
570 if (startRow < 0)
571 startRow = 0;
572
573 if (startCol < 0)
574 startCol = 0;
575
576 int row = startRow;
577
578 for (const auto& rowText : rows) {
579 if (rowText.IsEmpty())
580 continue;
581
582 wxArrayString columns;
583 wxStringTokenizer columnTokenizer(rowText, "\t");
584
585 while (columnTokenizer.HasMoreTokens())
586 columns.Add(columnTokenizer.GetNextToken());
587
588 while (row >= m_gridTCC->GetNumberRows())
589 m_gridTCC->AppendRows(1);
590
591 for (size_t i = 0; i < columns.size() && startCol + static_cast<int>(i) < m_gridTCC->GetNumberCols(); ++i)
592 m_gridTCC->SetCellValue(row, startCol + static_cast<int>(i), columns[i]);
593
594 ++row;
595 }
596
597 m_gridTCC->ForceRefresh();
598}
599
600void BusFormArcFlashProtection::OnGridCornerPaint(wxPaintEvent& event)
601{
602 wxWindow* corner = m_gridTCC->GetGridCornerLabelWindow();
603
604 wxAutoBufferedPaintDC dc(corner);
605 dc.SetBackground(wxBrush(corner->GetBackgroundColour()));
606 dc.Clear();
607
608 std::unique_ptr<wxGraphicsContext> gc(wxGraphicsContext::Create(dc));
609 if (!gc)
610 return;
611
612 gc->SetAntialiasMode(wxANTIALIAS_DEFAULT);
613
614 const wxSize size = corner->GetClientSize();
615
616 const double left = 2.0;
617 const double right = size.x - 2.0;
618 const double top = 2.0;
619 const double bottom = size.y - 2.0;
620
621 if (size.x <= 4 || size.y <= 4)
622 return;
623
624 const wxColour axisColour(90, 90, 90);
625 const wxColour curveColour(40, 100, 200);
626
627 // Draw axes.
628 gc->SetPen(wxPen(axisColour, 1.0));
629 gc->StrokeLine(left, top, left, bottom);
630 gc->StrokeLine(left, bottom, right, bottom);
631
632 // Define a TCC-like curve.
633 const double x0 = left + size.x * 0.23;
634 const double y0 = top + size.y * 0.12;
635
636 const double x1 = left + size.x * 0.42;
637 const double y1 = top + size.y * 0.50;
638
639 const double x2 = left + size.x * 0.63;
640 const double y2 = top + size.y * 0.72;
641
642 const double x3 = right - size.x * 0.08;
643 const double y3 = top + size.y * 0.80;
644
645 wxGraphicsPath path = gc->CreatePath();
646
647 path.MoveToPoint(x0, y0);
648
649 path.AddCurveToPoint(
650 x0 + size.x * 0.01, y0 + size.y * 0.12,
651 x1 - size.x * 0.10, y1 - size.y * 0.12,
652 x1, y1
653 );
654
655 path.AddCurveToPoint(
656 x1 + size.x * 0.08, y1 + size.y * 0.12,
657 x2 - size.x * 0.05, y2 - size.y * 0.03,
658 x2, y2
659 );
660
661 path.AddCurveToPoint(
662 x2 + size.x * 0.10, y2 + size.y * 0.06,
663 x3 - size.x * 0.08, y3,
664 x3, y3
665 );
666
667 // Draw the TCC curve.
668 gc->SetPen(wxPen(curveColour, m_gridCornerHover ? 2.0 : 1.5));
669 gc->StrokePath(path);
670}
671
672bool BusFormArcFlashProtection::ParseDouble(const wxString& text, double& value)
673{
674 wxString str = text;
675 str.Trim(true).Trim(false);
676 if (str.Find(',') != wxNOT_FOUND)
677 str.Replace(",", ".");
678
679 return str.ToCDouble(&value);
680}
681
682void BusFormArcFlashProtection::InitializeCBChoices()
683{
684 m_choiceCBType->Clear();
685 m_choiceCBType->Append(_("MCCB"));
686 m_choiceCBType->Append(_("ICCB"));
687 m_choiceCBType->Append(_("LVPCB"));
688 m_choiceCBType->SetSelection(0);
689
690 m_choiceCBVoltage->Clear();
691 m_choiceCBVoltage->Append(_("\u2264 480 V"));
692 m_choiceCBVoltage->Append(_("575\u2013690 V"));
693 m_choiceCBVoltage->SetSelection(0);
694
695 UpdateCBChoices();
696}
697
698void BusFormArcFlashProtection::UpdateCBCurrentChoices()
699{
700 const int type = m_choiceCBType->GetSelection();
701
702 m_choiceCBCurrent->Clear();
703 m_choiceCBCurrent->Enable(true);
704
705 switch (type)
706 {
707 case 0: // MCCB
708 m_choiceCBCurrent->Append(_("100\u2013400A"));
709 m_choiceCBCurrent->Append(_("600\u20131200A"));
710 m_choiceCBCurrent->SetSelection(0);
711 break;
712
713 case 1: // ICCB
714 m_choiceCBCurrent->Append(_("1600\u20136000A"));
715 m_choiceCBCurrent->SetSelection(0);
716 m_choiceCBCurrent->Enable(false);
717 break;
718
719 case 2: // LVPCB
720 m_choiceCBCurrent->Append(_("800\u20136300A"));
721 m_choiceCBCurrent->SetSelection(0);
722 m_choiceCBCurrent->Enable(false);
723 break;
724 }
725}
726
727void BusFormArcFlashProtection::UpdateCBTripChoices()
728{
729 const int type = m_choiceCBType->GetSelection();
730 const int current = m_choiceCBCurrent->GetSelection();
731
732 m_choiceCBTrip->Clear();
733 m_choiceCBTrip->Enable(true);
734
735 if (type == 0) // MCCB
736 {
737 if (current == 0) // 100–400 A
738 {
739 m_choiceCBTrip->Append(_("TM or M"));
740 m_choiceCBTrip->SetSelection(0);
741 m_choiceCBTrip->Enable(false);
742 }
743 else // 600–1200 A
744 {
745 m_choiceCBTrip->Append(_("TM or M"));
746 m_choiceCBTrip->Append(_("Electronic (E, LI)"));
747 m_choiceCBTrip->SetSelection(0);
748 }
749 }
750 else if (type == 1) // ICCB
751 {
752 m_choiceCBTrip->Append(_("TM or M"));
753 m_choiceCBTrip->SetSelection(0);
754 m_choiceCBTrip->Enable(false);
755 }
756 else if (type == 2) // LVPCB
757 {
758 m_choiceCBTrip->Append(_("Electronic (E, LI)"));
759 m_choiceCBTrip->Append(_("Electronic, short-time delay (E, LS)"));
760 m_choiceCBTrip->SetSelection(0);
761 }
762}
763
764void BusFormArcFlashProtection::UpdateCBChoices()
765{
766 UpdateCBCurrentChoices();
767 UpdateCBTripChoices();
768}
769
770void BusFormArcFlashProtection::UpdateRadioI1Selection()
771{
772 if (m_radioBoxI1->GetSelection() == 0) {
773 m_textCtrlIn->Enable(false);
774 m_staticTextI1->SetLabel(_("Minimum current (I₁) [A]"));
775 }
776 else {
777 m_staticTextI1->SetLabel(_("Instant/Short-Time (Iₜ) [A]"));
778 m_textCtrlIn->Enable();
779 }
780}
781
782void BusFormArcFlashProtection::UpdateFuseCurrentChoices()
783{
784 m_choiceFuseCurrent->Clear();
785
786 switch (m_choiceFuseClass->GetSelection())
787 {
788 case 0: // Class L
789 m_choiceFuseCurrent->Append(_("601–800A"));
790 m_choiceFuseCurrent->Append(_("801–1200A"));
791 m_choiceFuseCurrent->Append(_("1201–1600A"));
792 m_choiceFuseCurrent->Append(_("1601–2000A"));
793 break;
794
795 case 1: // Class RK1
796 m_choiceFuseCurrent->Append(_("≤ 100A"));
797 m_choiceFuseCurrent->Append(_("101–200A"));
798 m_choiceFuseCurrent->Append(_("201–400A"));
799 m_choiceFuseCurrent->Append(_("401–600A"));
800 break;
801
802 default:
803 break;
804 }
805
806 if (m_choiceFuseCurrent->GetCount() > 0)
807 m_choiceFuseCurrent->SetSelection(0);
808}
809
810void BusFormArcFlashProtection::LoadProtectionData(const ArcFlashProtectionData& data)
811{
812 const bool isBreaker = data.type == ArcFlashProtectionType::CIRCUIT_BREAKER;
813 const bool isFuse = !isBreaker;
814
815 // Protection type and method.
816 m_choiceType->SetSelection(isBreaker ? 0 : 1);
817 m_choiceMethod->SetString(1, isBreaker ? _("Appendix I") : _("Appendix H"));
818
819 const bool isAppendixI = isBreaker && data.method == ArcFlashProtectionMethod::ANNEX_I;
820 const bool isAppendixH = isFuse && data.method == ArcFlashProtectionMethod::ANNEX_H;
821 const bool validAnnex = isAppendixI || isAppendixH;
822
823 wxASSERT_MSG(data.method == ArcFlashProtectionMethod::TCC_CURVE || validAnnex,
824 "Invalid protection type and method combination");
825
826 const int methodSelection = data.method == ArcFlashProtectionMethod::TCC_CURVE || !validAnnex ? 0 : 1;
827 m_choiceMethod->SetSelection(methodSelection);
828
829 EnableFields();
830
831 // Show the corresponding notebook page.
832 const int page = methodSelection == 0 ? 0 : (isBreaker ? 1 : 2);
833 m_notebook->ChangeSelection(page);
834
835 // TCC curve.
836 m_gridTCC->BeginBatch();
837
838 if (m_gridTCC->GetNumberRows() > 0)
839 m_gridTCC->DeleteRows(0, m_gridTCC->GetNumberRows());
840
841 if (!data.tccPoints.empty())
842 {
843 m_gridTCC->AppendRows(static_cast<int>(data.tccPoints.size()));
844
845 for (size_t i = 0; i < data.tccPoints.size(); ++i)
846 {
847 m_gridTCC->SetCellValue(static_cast<int>(i), 0, FormatProtectionNumber(data.tccPoints[i].current));
848 m_gridTCC->SetCellValue(static_cast<int>(i), 1, FormatProtectionNumber(data.tccPoints[i].time));
849 }
850 }
851
852 m_gridTCC->EndBatch();
853 m_gridTCC->ForceRefresh();
854
855 m_checkBoxIsMeltTime->SetValue(data.curveIsMeltTimeOnly);
856 m_textCtrlDelay->ChangeValue(FormatOptionalNumber(data.additionalDelay));
857
858 // Appendix I: map BreakerFamily to the three cascading choices.
859 int breakerType = 0;
860 int currentRange = 0;
861 int tripUnit = 0;
862
863 switch (data.breakerFamily)
864 {
865 case ArcFlashBreakerFamily::MCCB_100_400A:
866 breakerType = 0; currentRange = 0; tripUnit = 0;
867 break;
868
869 case ArcFlashBreakerFamily::MCCB_600_1200A_TM_OR_M:
870 breakerType = 0; currentRange = 1; tripUnit = 0;
871 break;
872
873 case ArcFlashBreakerFamily::MCCB_600_1200A_E_LI:
874 breakerType = 0; currentRange = 1; tripUnit = 1;
875 break;
876
877 case ArcFlashBreakerFamily::MCCB_OR_ICCB_1600_6000A:
878 breakerType = 1; currentRange = 0; tripUnit = 0;
879 break;
880
881 case ArcFlashBreakerFamily::LVPCB_800_6300A_E_LI:
882 breakerType = 2; currentRange = 0; tripUnit = 0;
883 break;
884
885 case ArcFlashBreakerFamily::LVPCB_800_6300A_E_LS:
886 breakerType = 2; currentRange = 0; tripUnit = 1;
887 break;
888 }
889
890 m_choiceCBType->SetSelection(breakerType);
891 UpdateCBCurrentChoices();
892
893 m_choiceCBCurrent->SetSelection(currentRange);
894 UpdateCBTripChoices();
895
896 m_choiceCBTrip->SetSelection(tripUnit);
897 m_choiceCBVoltage->SetSelection(static_cast<int>(data.breakerVoltageBand));
898
899 m_textCtrlI2->ChangeValue(FormatOptionalNumber(data.i2_kA));
900
901 // Load I1 source and update the I1/It label and field state.
902 const int i1Selection = data.i1Source == ArcFlashI1Source::DIRECT ? 0 : 1;
903 m_radioBoxI1->SetSelection(i1Selection);
904 UpdateRadioI1Selection();
905
906 // The nominal current is optional and is not the same as It.
907 m_textCtrlIn->ChangeValue(FormatOptionalNumber(data.breakerNominalCurrent_A));
908
909 // The input field is I1 in amperes, or It in amperes, depending on the radio selection.
910 if (data.i1Source == ArcFlashI1Source::DIRECT)
911 m_textCtrl1->ChangeValue(FormatOptionalNumber(data.i1_kA * 1000.0));
912 else
913 m_textCtrl1->ChangeValue(FormatOptionalNumber(data.it_A));
914
915 // Appendix H: map FuseFamily to class and current range.
916 const int fuseFamily = static_cast<int>(data.fuseFamily);
917
918 m_choiceFuseClass->SetSelection(fuseFamily < 4 ? 0 : 1);
919 UpdateFuseCurrentChoices();
920 m_choiceFuseCurrent->SetSelection(fuseFamily < 4 ? fuseFamily : fuseFamily - 4);
921
922 // Update the "Use" checkbox for the currently selected device.
923 //const int selectedRow = m_dvListCtrlDevices->GetSelectedRow();
924 //
925 //if (selectedRow != wxNOT_FOUND)
926 // m_dvListCtrlDevices->SetToggleValue(data.consider, selectedRow, 0);
927
928 m_notebook->Layout();
929 m_splitterPageRight->Layout();
930 Layout();
931
932 if (m_tccPopup && m_tccPopup->IsShown())
933 m_tccPopup->UpdateGraph();
934}
935
936bool BusFormArcFlashProtection::ValidateAndGetProtectionData(ArcFlashProtectionData& data)
937{
938 ArcFlashProtectionData result = data;
939
940 auto showError = [&](const wxString& message) {
941 wxMessageBox(message, _("Invalid data"), wxOK | wxICON_WARNING, m_parent);
942 return false;
943 };
944
945 auto parseDouble = [&](const wxString& text, double& value, const wxString& field) {
946 return m_bus->DoubleFromString(
947 m_parent,
948 text,
949 value,
950 wxString::Format(_("Value entered incorrectly in the field \"%s\"."), field));
951 };
952
953 auto requirePositive = [&](double value, const wxString& field) {
954 if (!std::isfinite(value) || value <= 0.0)
955 return showError(wxString::Format(
956 _("The field \"%s\" must be greater than zero."), field));
957
958 return true;
959 };
960
961 // Protection type.
962 const int typeSelection = m_choiceType->GetSelection();
963
964 if (typeSelection < 0 || typeSelection > 1)
965 return showError(_("Select a valid protection type."));
966
967 result.type = typeSelection == 0
968 ? ArcFlashProtectionType::CIRCUIT_BREAKER
969 : ArcFlashProtectionType::FUSE;
970
971 // Protection method.
972 const int methodSelection = m_choiceMethod->GetSelection();
973
974 if (methodSelection < 0 || methodSelection > 1)
975 return showError(_("Select a valid protection method."));
976
977 if (methodSelection == 0)
978 result.method = ArcFlashProtectionMethod::TCC_CURVE;
979 else if (result.type == ArcFlashProtectionType::CIRCUIT_BREAKER)
980 result.method = ArcFlashProtectionMethod::ANNEX_I;
981 else
982 result.method = ArcFlashProtectionMethod::ANNEX_H;
983
984 // Additional delay is optional; an empty field means zero.
985 wxString delayText = m_textCtrlDelay->GetValue();
986 delayText.Trim(false).Trim(true);
987
988 if (delayText.IsEmpty())
989 result.additionalDelay = 0.0;
990 else if (!parseDouble(delayText, result.additionalDelay, _("Additional delay (s)")))
991 return false;
992
993 if (!std::isfinite(result.additionalDelay) || result.additionalDelay < 0.0)
994 return showError(_("The additional delay must be greater than or equal to zero."));
995
996 // TCC curve.
997 if (result.method == ArcFlashProtectionMethod::TCC_CURVE) {
998 const int rows = m_gridTCC->GetNumberRows();
999
1000 if (m_gridTCC->GetNumberCols() < 2 || rows < 2)
1001 return showError(_("The TCC curve must contain at least two points."));
1002
1003 std::vector<ArcFlashTCCPoint> points;
1004 points.reserve(rows);
1005
1006 for (int row = 0; row < rows; ++row) {
1007 ArcFlashTCCPoint point;
1008
1009 const wxString currentField =
1010 wxString::Format(_("Current (A), row %d of the TCC curve"), row + 1);
1011
1012 const wxString timeField =
1013 wxString::Format(_("Time (s), row %d of the TCC curve"), row + 1);
1014
1015 if (!parseDouble(m_gridTCC->GetCellValue(row, 0), point.current, currentField))
1016 return false;
1017
1018 if (!requirePositive(point.current, currentField))
1019 return false;
1020
1021 if (!parseDouble(m_gridTCC->GetCellValue(row, 1), point.time, timeField))
1022 return false;
1023
1024 if (!requirePositive(point.time, timeField))
1025 return false;
1026
1027 points.push_back(point);
1028 }
1029
1030 result.tccPoints = points;
1031 result.curveIsMeltTimeOnly = m_checkBoxIsMeltTime->GetValue();
1032 }
1033
1034 // Appendix I: circuit breaker.
1035 if (result.method == ArcFlashProtectionMethod::ANNEX_I) {
1036 const int cbType = m_choiceCBType->GetSelection();
1037 const int currentRange = m_choiceCBCurrent->GetSelection();
1038 const int tripUnit = m_choiceCBTrip->GetSelection();
1039
1040 if (cbType < 0 || cbType > 2 || currentRange < 0 || tripUnit < 0)
1041 return showError(_("Select valid circuit breaker parameters."));
1042
1043 // Map the type, current range and trip unit to the breaker family.
1044 switch (cbType) {
1045 case 0: // MCCB
1046 if (currentRange == 0 && tripUnit == 0) {
1047 result.breakerFamily = ArcFlashBreakerFamily::MCCB_100_400A;
1048 }
1049 else if (currentRange == 1 && tripUnit == 0) {
1050 result.breakerFamily = ArcFlashBreakerFamily::MCCB_600_1200A_TM_OR_M;
1051 }
1052 else if (currentRange == 1 && tripUnit == 1) {
1053 result.breakerFamily = ArcFlashBreakerFamily::MCCB_600_1200A_E_LI;
1054 }
1055 else {
1056 return showError(
1057 _("The selected MCCB current range and trip unit are not compatible."));
1058 }
1059 break;
1060
1061 case 1: // ICCB
1062 if (currentRange != 0 || tripUnit != 0)
1063 return showError(
1064 _("The selected ICCB current range and trip unit are not compatible."));
1065
1066 result.breakerFamily = ArcFlashBreakerFamily::MCCB_OR_ICCB_1600_6000A;
1067 break;
1068
1069 case 2: // LVPCB
1070 if (currentRange != 0 || (tripUnit != 0 && tripUnit != 1))
1071 return showError(
1072 _("The selected LVPCB current range and trip unit are not compatible."));
1073
1074 result.breakerFamily = tripUnit == 0
1075 ? ArcFlashBreakerFamily::LVPCB_800_6300A_E_LI
1076 : ArcFlashBreakerFamily::LVPCB_800_6300A_E_LS;
1077 break;
1078 }
1079
1080 // Voltage range.
1081 const int voltageSelection = m_choiceCBVoltage->GetSelection();
1082
1083 if (voltageSelection < 0 || voltageSelection > 1)
1084 return showError(_("Select a valid circuit breaker voltage range."));
1085
1086 result.breakerVoltageBand = voltageSelection == 0
1087 ? ArcFlashBreakerVoltageBand::V480_AND_LOWER
1088 : ArcFlashBreakerVoltageBand::V575_TO_690;
1089
1090 // Interrupting capacity I2, in kA.
1091 if (!parseDouble(m_textCtrlI2->GetValue(), result.i2_kA,
1092 _("Interrupting capacity (I₂) [kA]")))
1093 return false;
1094
1095 if (!requirePositive(result.i2_kA, _("Interrupting capacity (I₂) [kA]")))
1096 return false;
1097
1098 // I1 entered directly, or calculated from It.
1099 const int i1Selection = m_radioBoxI1->GetSelection();
1100
1101 if (i1Selection < 0 || i1Selection > 1)
1102 return showError(_("Select a valid minimum current calculation method."));
1103
1104 if (i1Selection == 0) {
1105 double i1_A = 0.0;
1106
1107 if (!parseDouble(m_textCtrl1->GetValue(), i1_A,
1108 _("Minimum current (I₁) [A]")))
1109 return false;
1110
1111 if (!requirePositive(i1_A, _("Minimum current (I₁) [A]")))
1112 return false;
1113
1114 result.i1Source = ArcFlashI1Source::DIRECT;
1115 result.i1_kA = i1_A / 1000.0;
1116 result.it_A = 0.0;
1117 result.breakerNominalCurrent_A = 0.0;
1118 }
1119 else {
1120 double nominalCurrent_A = 0.0;
1121 double it_A = 0.0;
1122
1123 if (!parseDouble(m_textCtrlIn->GetValue(), nominalCurrent_A,
1124 _("Nominal current [A]")))
1125 return false;
1126
1127 if (!requirePositive(nominalCurrent_A, _("Nominal current [A]")))
1128 return false;
1129
1130 if (!parseDouble(m_textCtrl1->GetValue(), it_A,
1131 _("Instant/Short-Time current (Iₜ) [A]")))
1132 return false;
1133
1134 if (!requirePositive(it_A, _("Instant/Short-Time current (Iₜ) [A]")))
1135 return false;
1136
1137 result.i1Source = ArcFlashI1Source::FROM_IT;
1138 result.breakerNominalCurrent_A = nominalCurrent_A;
1139 result.it_A = it_A;
1140 result.i1_kA = 0.0;
1141 }
1142 }
1143
1144 // Appendix H: fuse.
1145 if (result.method == ArcFlashProtectionMethod::ANNEX_H) {
1146 const int fuseClass = m_choiceFuseClass->GetSelection();
1147 const int currentSelection = m_choiceFuseCurrent->GetSelection();
1148 const int currentCount = static_cast<int>(m_choiceFuseCurrent->GetCount());
1149
1150 if (fuseClass < 0 || fuseClass > 1 || currentSelection < 0)
1151 return showError(_("Select a valid fuse class and current range."));
1152
1153 int familyIndex = -1;
1154
1155 if (currentCount == 4) {
1156 // The current-range choices contain only the ranges for the selected class.
1157 familyIndex = fuseClass * 4 + currentSelection;
1158 }
1159 else if (currentCount == 8) {
1160 // Support the original list containing both fuse classes.
1161 const int firstIndex = fuseClass * 4;
1162
1163 if (currentSelection < firstIndex || currentSelection >= firstIndex + 4)
1164 return showError(
1165 _("The selected fuse current range does not match the fuse class."));
1166
1167 familyIndex = currentSelection;
1168 }
1169 else {
1170 return showError(_("The fuse current range choices are invalid."));
1171 }
1172
1173 if (familyIndex < 0 || familyIndex > 7)
1174 return showError(_("Select a valid fuse current range."));
1175
1176 result.fuseFamily = static_cast<ArcFlashFuseFamily>(familyIndex);
1177 }
1178
1179 // Use flag associated with the currently selected device.
1180 const int selectedRow = m_dvListCtrlDevices->GetSelectedRow();
1181
1182 //if (selectedRow >= 0)
1183 // result.consider = m_dvListCtrlDevices->GetToggleValue(selectedRow, 0);
1184
1185 // The remaining fields that have no corresponding controls are preserved from the original data structure.
1186 result.configured = true;
1187
1188 data = result;
1189 return true;
1190}
1191
1192TCCPopup::TCCPopup(wxWindow* parent, wxGrid* grid) : wxPopupTransientWindow(parent, wxBORDER_SIMPLE), m_grid(grid)
1193{
1194 SetBackgroundStyle(wxBG_STYLE_PAINT);
1195 SetSize(500, 600);
1196
1197 Bind(wxEVT_PAINT, &TCCPopup::OnPaint, this);
1198}
1199
1200TCCPopup::~TCCPopup()
1201{
1202 Unbind(wxEVT_PAINT, &TCCPopup::OnPaint, this);
1203}
1204
1205void TCCPopup::UpdateGraph()
1206{
1207 Refresh();
1208 Update();
1209}
1210
1211bool TCCPopup::GetCellDouble(int row, int col, double& value) const
1212{
1213 wxString str = m_grid->GetCellValue(row, col);
1214 return str.ToDouble(&value);
1215}
1216
1217void TCCPopup::OnPaint(wxPaintEvent& event)
1218{
1219 wxAutoBufferedPaintDC dc(this);
1220
1221 dc.SetBackground(*wxWHITE_BRUSH);
1222 dc.Clear();
1223
1224 if (!m_grid)
1225 return;
1226
1227 std::unique_ptr<wxGraphicsContext> gc(wxGraphicsContext::Create(dc));
1228
1229 if (!gc)
1230 return;
1231
1232 gc->SetAntialiasMode(wxANTIALIAS_DEFAULT);
1233
1234 const wxSize size = GetClientSize();
1235
1236 const int left = 60;
1237 const int right = 20;
1238 const int top = 20;
1239 const int bottom = 50;
1240
1241 const int graphWidth = size.x - left - right;
1242 const int graphHeight = size.y - top - bottom;
1243
1244 if (graphWidth <= 0 || graphHeight <= 0)
1245 return;
1246
1247 struct Point
1248 {
1249 double current;
1250 double time;
1251 };
1252
1253 std::vector<Point> data;
1254
1255 double minCurrent = std::numeric_limits<double>::max();
1256 double maxCurrent = std::numeric_limits<double>::lowest();
1257 double minTime = std::numeric_limits<double>::max();
1258 double maxTime = std::numeric_limits<double>::lowest();
1259
1260 for (int row = 0; row < m_grid->GetNumberRows(); ++row)
1261 {
1262 double current = 0.0;
1263 double time = 0.0;
1264
1265 if (!GetCellDouble(row, 0, current) || !GetCellDouble(row, 1, time))
1266 continue;
1267
1268 if (current <= 0.0 || time <= 0.0)
1269 continue;
1270
1271 data.push_back({ current, time });
1272
1273 minCurrent = std::min(minCurrent, current);
1274 maxCurrent = std::max(maxCurrent, current);
1275 minTime = std::min(minTime, time);
1276 maxTime = std::max(maxTime, time);
1277 }
1278
1279 if (data.size() < 2)
1280 return;
1281
1282 // Use complete decades for both axes.
1283 double logMinCurrent = std::floor(std::log10(minCurrent));
1284 double logMaxCurrent = std::ceil(std::log10(maxCurrent));
1285 double logMinTime = std::floor(std::log10(minTime));
1286 double logMaxTime = std::ceil(std::log10(maxTime));
1287
1288 if (logMinCurrent == logMaxCurrent)
1289 {
1290 --logMinCurrent;
1291 ++logMaxCurrent;
1292 }
1293
1294 if (logMinTime == logMaxTime)
1295 {
1296 --logMinTime;
1297 ++logMaxTime;
1298 }
1299
1300 auto mapX = [&](double current) {
1301 return left + (std::log10(current) - logMinCurrent) / (logMaxCurrent - logMinCurrent) * graphWidth;
1302 };
1303
1304 auto mapY = [&](double time) {
1305 return size.y - bottom - (std::log10(time) - logMinTime) / (logMaxTime - logMinTime) * graphHeight;
1306 };
1307
1308 const int firstCurrentDecade = static_cast<int>(logMinCurrent);
1309 const int lastCurrentDecade = static_cast<int>(logMaxCurrent);
1310
1311 const int firstTimeDecade = static_cast<int>(logMinTime);
1312 const int lastTimeDecade = static_cast<int>(logMaxTime);
1313
1314 // Minor vertical grid lines: current.
1315 gc->SetPen(wxPen(wxColour(230, 230, 230), 1));
1316
1317 for (int exponent = firstCurrentDecade; exponent <= lastCurrentDecade; ++exponent)
1318 {
1319 const double decade = std::pow(10.0, exponent);
1320
1321 for (int i = 2; i <= 9; ++i)
1322 {
1323 const double current = i * decade;
1324 const double x = mapX(current);
1325
1326 if (x >= left && x <= size.x - right)
1327 gc->StrokeLine(x, top, x, size.y - bottom);
1328 }
1329 }
1330
1331 // Minor horizontal grid lines: time.
1332 for (int exponent = firstTimeDecade; exponent <= lastTimeDecade; ++exponent)
1333 {
1334 const double decade = std::pow(10.0, exponent);
1335
1336 for (int i = 2; i <= 9; ++i)
1337 {
1338 const double time = i * decade;
1339 const double y = mapY(time);
1340
1341 if (y >= top && y <= size.y - bottom)
1342 gc->StrokeLine(left, y, size.x - right, y);
1343 }
1344 }
1345
1346 // Major vertical grid lines: current.
1347 gc->SetPen(wxPen(wxColour(190, 190, 190), 1));
1348
1349 for (int exponent = firstCurrentDecade; exponent <= lastCurrentDecade; ++exponent)
1350 {
1351 const double current = std::pow(10.0, exponent);
1352 const double x = mapX(current);
1353
1354 if (x >= left && x <= size.x - right)
1355 gc->StrokeLine(x, top, x, size.y - bottom);
1356 }
1357
1358 // Major horizontal grid lines: time.
1359 for (int exponent = firstTimeDecade; exponent <= lastTimeDecade; ++exponent)
1360 {
1361 const double time = std::pow(10.0, exponent);
1362 const double y = mapY(time);
1363
1364 if (y >= top && y <= size.y - bottom)
1365 gc->StrokeLine(left, y, size.x - right, y);
1366 }
1367
1368 // Axes.
1369 gc->SetPen(wxPen(*wxBLACK, 1.5));
1370 gc->StrokeLine(left, top, left, size.y - bottom);
1371 gc->StrokeLine(left, size.y - bottom, size.x - right, size.y - bottom);
1372
1373 // X-axis labels: current.
1374 gc->SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT), *wxBLACK);
1375
1376 for (int exponent = firstCurrentDecade; exponent <= lastCurrentDecade; ++exponent)
1377 {
1378 const double current = std::pow(10.0, exponent);
1379 const double x = mapX(current);
1380
1381 if (x < left || x > size.x - right)
1382 continue;
1383
1384 const wxString label = wxString::Format("1e%d", exponent);
1385
1386 double textWidth;
1387 double textHeight;
1388 gc->GetTextExtent(label, &textWidth, &textHeight);
1389
1390 gc->DrawText(label, x - textWidth / 2.0, size.y - bottom + 5);
1391 }
1392
1393 // Y-axis labels: time.
1394 for (int exponent = firstTimeDecade; exponent <= lastTimeDecade; ++exponent)
1395 {
1396 const double time = std::pow(10.0, exponent);
1397 const double y = mapY(time);
1398
1399 if (y < top || y > size.y - bottom)
1400 continue;
1401
1402 const wxString label = wxString::Format("1e%d", exponent);
1403
1404 double textWidth;
1405 double textHeight;
1406 gc->GetTextExtent(label, &textWidth, &textHeight);
1407
1408 gc->DrawText(label, left - textWidth - 5, y - textHeight / 2.0);
1409 }
1410
1411 // TCC curve.
1412 wxGraphicsPath path = gc->CreatePath();
1413
1414 for (size_t i = 0; i < data.size(); ++i)
1415 {
1416 const double x = mapX(data[i].current);
1417 const double y = mapY(data[i].time);
1418
1419 if (i == 0)
1420 path.MoveToPoint(x, y);
1421 else
1422 path.AddLineToPoint(x, y);
1423 }
1424
1425 gc->SetPen(wxPen(wxColour(40, 100, 200), 2.0));
1426 gc->StrokePath(path);
1427
1428 // X-axis title.
1429 const wxString xLabel = _("Current (A)");
1430
1431 double xLabelWidth;
1432 double xLabelHeight;
1433 gc->GetTextExtent(xLabel, &xLabelWidth, &xLabelHeight);
1434
1435 gc->DrawText(xLabel, left + (graphWidth - xLabelWidth) / 2.0, size.y - 20);
1436
1437 // Y-axis title.
1438 const wxString yLabel = _("Time (s)");
1439
1440 double yLabelWidth;
1441 double yLabelHeight;
1442 gc->GetTextExtent(yLabel, &yLabelWidth, &yLabelHeight);
1443
1444 gc->PushState();
1445 gc->Translate(15, top + graphHeight / 2.0);
1446 gc->Rotate(-M_PI / 2.0);
1447 gc->DrawText(yLabel, -yLabelWidth / 2.0, -yLabelHeight / 2.0);
1448 gc->PopState();
1449}
1450void BusFormArcFlashProtection::OnChoiceCBCurrent(wxCommandEvent& event)
1451{
1452 UpdateCBTripChoices();
1453}
1454void BusFormArcFlashProtection::OnChoiceCBTrip(wxCommandEvent& event)
1455{
1456 //UpdateCBChoices();
1457}
1458void BusFormArcFlashProtection::OnChoiceCBType(wxCommandEvent& event)
1459{
1460 UpdateCBChoices();
1461}
1462void BusFormArcFlashProtection::OnRadioI1Selected(wxCommandEvent& event)
1463{
1464 UpdateRadioI1Selection();
1465}
1466void BusFormArcFlashProtection::OnNominalCurrentUpdated(wxCommandEvent& event)
1467{
1468 double value = 0.0;
1469 if (m_textCtrlIn->GetValue().ToDouble(&value))
1470 {
1471 if (value <= 100.0) {
1472 m_textCtrl1->SetValue(wxString::FromDouble(1300));
1473 }
1474 else {
1475 m_textCtrl1->SetValue(wxString::FromDouble(value * 10.0));
1476 }
1477 }
1478}
1479void BusFormArcFlashProtection::OnChoiceFuseClass(wxCommandEvent& event)
1480{
1481 UpdateFuseCurrentChoices();
1482}
Node for power elements. All others power elements are connected through this.
Definition Bus.h:135