1#include "BusFormArcFlashProtection.h"
4#include <wx/dcbuffer.h>
5#include <wx/textfile.h>
7#include <wx/numformatter.h>
15 wxString FormatProtectionNumber(
double value)
17 return wxNumberFormatter::ToString(value, 12, wxNumberFormatter::Style_None | wxNumberFormatter::Style_NoTrailingZeroes);
20 wxString FormatOptionalNumber(
double value)
22 return value == 0.0 ? wxString() : FormatProtectionNumber(value);
27BusFormArcFlashProtection::BusFormArcFlashProtection(wxWindow* parent,
Bus* bus,
double basePower)
28 : BusFormArcFlashProtectionBase(parent), m_bus(bus), m_basePower(basePower)
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;
37 if (busData.hasFault) {
38 m_staticTextIbf->SetLabel(_(
"Total Ibf (fault): ") + Bus::StringFromDouble(ibf / 1e3, 0, 4) + wxT(
" kA"));
41 auto addDevice = [&](
auto* device) {
42 const auto& parentList = device->GetParentList();
43 if (parentList.size() != 2)
46 const auto& data = device->GetElectricalDataRef();
47 int busIndex = parentList[0] == m_bus ? 0 : 1;
49 double faultCurrent = std::abs(data.faultCurrent[busIndex][0]) * baseCurrent;
51 if (!device->IsOnline())
54 const bool check = data.arcFlashProtection.configured
55 ? data.arcFlashProtection.consider
56 : faultCurrent > 1e-3 && data.faultCurrent[busIndex][0].real() > 1e-6;
58 wxString deviceName = data.name;
59 wxString faultCurrentString = Bus::StringFromDouble(faultCurrent / 1e3, 0, 2);
61 m_dvListCtrlDevices->AppendItem({
63 wxVariant(deviceName),
64 wxVariant(faultCurrentString)
66 reinterpret_cast<wxUIntPtr
>(device));
68 for (
auto* element : m_bus->GetChildList()) {
69 if (auto* line = dynamic_cast<Line*>(element))
71 else if (auto* transformer = dynamic_cast<Transformer*>(element))
72 addDevice(transformer);
75 m_gridTCC->AppendCols(2);
78 m_gridTCC->SetColLabelValue(0, _(
"Current (A)"));
79 m_gridTCC->SetColLabelValue(1, _(
"Time (s)"));
81 m_gridTCC->SetColFormatFloat(0, -1, 3);
82 m_gridTCC->SetColFormatFloat(1, -1, 3);
84 int gridWidth = m_gridTCC->GetRowLabelSize();
86 for (
int col = 0; col < m_gridTCC->GetNumberCols(); ++col)
87 gridWidth += m_gridTCC->GetColSize(col);
89 m_gridTCC->SetMinSize(wxSize(gridWidth + 100, -1));
90 m_splitterPageRight->SetMinSize(wxSize(gridWidth + 100, -1));
92 int devicesWidth = 30;
94 for (
unsigned int i = 0; i < m_dvListCtrlDevices->GetColumnCount(); ++i) {
96 devicesWidth +=
static_cast<int>(m_dvListCtrlDevices->GetBestColumnWidth(
static_cast<int>(i)));
98 devicesWidth += m_dvListCtrlDevices->GetColumn(i)->GetWidth();
102 m_dvListCtrlDevices->SetMinSize(wxSize(devicesWidth, 100));
104 m_splitterPageLeft->Layout();
105 m_splitterPageRight->Layout();
107 GetSizer()->Fit(
this);
109 m_splitter->SetSashGravity(1.0);
112 const int rightWidth = m_splitterPageRight->GetMinWidth();
113 const int sashPosition = m_splitter->GetClientSize().x - rightWidth - m_splitter->GetSashSize();
115 m_splitter->SetSashPosition(sashPosition);
117 m_gridTCC->Bind(wxEVT_KEY_DOWN, &BusFormArcFlashProtection::OnGridKeyDown,
this);
120 wxWindow* corner = m_gridTCC->GetGridCornerLabelWindow();
121 corner->Bind(wxEVT_PAINT, &BusFormArcFlashProtection::OnGridCornerPaint,
this);
122 corner->Bind(wxEVT_ENTER_WINDOW, [
this](wxMouseEvent& event) {
124 m_tccPopup =
new TCCPopup(
this, m_gridTCC);
126 wxPoint mousePos = wxGetMousePosition();
127 m_tccPopup->Move(mousePos + wxPoint(15, 15));
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);
139 wxPoint pos = mouse + wxPoint(offset, offset);
141 const wxRect screen = wxGetClientDisplayRect();
143 if (pos.x + popupSize.x > screen.GetRight())
144 pos.x -= pos.x + popupSize.x - screen.GetRight();
146 if (pos.y + popupSize.y > screen.GetBottom())
147 pos.y -= pos.y + popupSize.y - screen.GetBottom();
149 if (pos.y < screen.GetTop())
150 pos.y = screen.GetTop();
152 m_tccPopup->SetPosition(pos);
158 corner->Bind(wxEVT_LEAVE_WINDOW, [
this](wxMouseEvent& event) {
160 m_tccPopup->Dismiss();
165 InitializeCBChoices();
166 UpdateRadioI1Selection();
168 SetSize(GetBestSize());
171BusFormArcFlashProtection::~BusFormArcFlashProtection()
173 wxWindow* corner = m_gridTCC->GetGridCornerLabelWindow();
174 corner->Unbind(wxEVT_PAINT, &BusFormArcFlashProtection::OnGridCornerPaint,
this);
177void BusFormArcFlashProtection::EnableFields()
179 switch (m_choiceType->GetSelection())
182 m_choiceMethod->Enable();
183 m_choiceMethod->SetString(1, _(
"Appendix I"));
184 m_checkBoxIsMeltTime->Enable(
false);
185 m_textCtrlDelay->Enable();
188 m_choiceMethod->Enable();
189 m_choiceMethod->SetString(1, _(
"Appendix H"));
190 m_checkBoxIsMeltTime->Enable();
191 m_textCtrlDelay->Enable(
false);
195 switch (m_choiceMethod->GetSelection())
198 m_panelTCC->Enable();
199 m_panelAI->Enable(
false);
200 m_panelAH->Enable(
false);
201 m_notebook->SetSelection(0);
208 m_panelTCC->Enable(
false);
209 if (m_choiceType->GetSelection() == 0)
212 m_panelAH->Enable(
false);
213 m_notebook->SetSelection(1);
217 m_panelAI->Enable(
false);
219 m_notebook->SetSelection(2);
226void BusFormArcFlashProtection::OnAddButtonClick(wxCommandEvent& event)
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");
234void BusFormArcFlashProtection::OnDeviceChanged(wxDataViewEvent& event)
244 if (m_restoringDeviceSelection)
247 const wxDataViewItem newItem =
event.GetItem();
252 if (newItem == m_previousDeviceSelection)
255 auto getElement = [
this](
const wxDataViewItem& item) -> Element* {
256 auto devicePtr = m_dvListCtrlDevices->GetItemData(item);
257 return reinterpret_cast<Element*
>(devicePtr);
260 auto getProtectionData = [](Element* element) -> ArcFlashProtectionData* {
261 if (
auto* line =
dynamic_cast<Line*
>(element))
262 return &line->GetElectricalDataRef().arcFlashProtection;
264 if (
auto* transformer =
dynamic_cast<Transformer*
>(element))
265 return &transformer->GetElectricalDataRef().arcFlashProtection;
271 if (m_previousDeviceSelection.IsOk()) {
272 auto* previousElement = getElement(m_previousDeviceSelection);
273 auto* previousData = getProtectionData(previousElement);
276 ArcFlashProtectionData validatedData = *previousData;
278 const int previousRow =
279 m_dvListCtrlDevices->ItemToRow(m_previousDeviceSelection);
281 if (previousRow != wxNOT_FOUND)
282 validatedData.consider =
283 m_dvListCtrlDevices->GetToggleValue(previousRow, 0);
285 if (!ValidateAndGetProtectionData(validatedData)) {
286 m_restoringDeviceSelection =
true;
287 m_dvListCtrlDevices->Unselect(newItem);
288 m_dvListCtrlDevices->Select(m_previousDeviceSelection);
289 m_restoringDeviceSelection =
false;
293 *previousData = validatedData;
298 m_previousDeviceSelection = newItem;
300 if (
auto* element = getElement(newItem)) {
301 if (
auto* protectionData = getProtectionData(element))
302 LoadProtectionData(*protectionData);
306void BusFormArcFlashProtection::OnImportButtonClick(wxCommandEvent& event)
310 _(
"Import TCC curve"),
313 _(
"CSV files (*.csv)|*.csv|All files (*.*)|*.*"),
314 wxFD_OPEN | wxFD_FILE_MUST_EXIST);
316 if (dialog.ShowModal() != wxID_OK)
321 if (!file.Open(dialog.GetPath()))
323 wxMessageBox(_(
"Unable to open the selected file."), _(
"Import error"), wxOK | wxICON_ERROR,
this);
328 m_gridTCC->ClearGrid();
330 if (m_gridTCC->GetNumberRows() > 0)
331 m_gridTCC->DeleteRows(0, m_gridTCC->GetNumberRows());
335 for (
size_t i = 0; i < file.GetLineCount(); ++i)
337 wxString line = file.GetLine(i).Trim(
true).Trim(
false);
342 wxArrayString columns;
344 if (line.Find(
';') != wxNOT_FOUND)
346 wxStringTokenizer tokenizer(line,
";");
348 while (tokenizer.HasMoreTokens())
349 columns.Add(tokenizer.GetNextToken());
351 else if (line.Find(
'\t') != wxNOT_FOUND)
353 wxStringTokenizer tokenizer(line,
"\t");
355 while (tokenizer.HasMoreTokens())
356 columns.Add(tokenizer.GetNextToken());
360 wxStringTokenizer tokenizer(line,
",");
362 while (tokenizer.HasMoreTokens())
363 columns.Add(tokenizer.GetNextToken());
366 if (columns.size() < 2)
369 wxString currentString = columns[0].Trim(
true).Trim(
false);
370 wxString timeString = columns[1].Trim(
true).Trim(
false);
372 double current = 0.0;
375 if (!ParseDouble(currentString, current) || !ParseDouble(timeString, time))
381 wxString::Format(_(
"Invalid data at line %zu."), i + 1),
390 if (current <= 0.0 || time <= 0.0)
393 wxString::Format(_(
"Invalid data at line %zu. Current and time must be greater than zero."), i + 1),
402 m_gridTCC->AppendRows(1);
403 m_gridTCC->SetCellValue(row, 0, wxString::FromDouble(current));
404 m_gridTCC->SetCellValue(row, 1, wxString::FromDouble(time));
411 m_gridTCC->ForceRefresh();
413 if (m_tccPopup && m_tccPopup->IsShown())
414 m_tccPopup->UpdateGraph();
417void BusFormArcFlashProtection::OnMethodSelected(wxCommandEvent& event)
422void BusFormArcFlashProtection::OnOKButtonClick(wxCommandEvent& event)
424 const unsigned int itemCount = m_dvListCtrlDevices->GetItemCount();
426 if (itemCount == 0) {
431 auto getElement = [
this](
const wxDataViewItem& item) -> Element* {
432 return reinterpret_cast<Element*
>(
433 m_dvListCtrlDevices->GetItemData(item));
436 auto getProtectionData = [](Element* element) -> ArcFlashProtectionData* {
437 if (
auto* line =
dynamic_cast<Line*
>(element))
438 return &line->GetElectricalDataRef().arcFlashProtection;
440 if (
auto* transformer =
dynamic_cast<Transformer*
>(element))
441 return &transformer->GetElectricalDataRef().arcFlashProtection;
446 std::vector<std::pair<ArcFlashProtectionData*,
447 ArcFlashProtectionData>> updates;
449 updates.reserve(itemCount);
451 const wxDataViewItem originalSelection = m_previousDeviceSelection;
454 if (originalSelection.IsOk()) {
455 auto* element = getElement(originalSelection);
456 auto* protectionData = getProtectionData(element);
458 if (protectionData) {
459 ArcFlashProtectionData data = *protectionData;
461 const int row = m_dvListCtrlDevices->ItemToRow(originalSelection);
462 if (row != wxNOT_FOUND)
463 data.consider = m_dvListCtrlDevices->GetToggleValue(row, 0);
465 if (!ValidateAndGetProtectionData(data))
468 updates.emplace_back(protectionData, data);
473 m_restoringDeviceSelection =
true;
475 for (
unsigned int row = 0; row < itemCount; ++row) {
476 const wxDataViewItem item = m_dvListCtrlDevices->RowToItem(row);
478 if (item == originalSelection)
481 auto* element = getElement(item);
482 auto* protectionData = getProtectionData(element);
487 LoadProtectionData(*protectionData);
489 ArcFlashProtectionData data = *protectionData;
490 data.consider = m_dvListCtrlDevices->GetToggleValue(row, 0);
492 if (!ValidateAndGetProtectionData(data)) {
493 m_restoringDeviceSelection =
false;
495 m_dvListCtrlDevices->UnselectAll();
496 m_dvListCtrlDevices->Select(item);
497 m_previousDeviceSelection = item;
499 LoadProtectionData(*protectionData);
503 updates.emplace_back(protectionData, data);
506 m_restoringDeviceSelection =
false;
509 for (
auto& [protectionData, data] : updates)
510 *protectionData = data;
515void BusFormArcFlashProtection::OnRemoveButtonClick(wxCommandEvent& event)
517 wxArrayInt rows = m_gridTCC->GetSelectedRows();
519 for (
int i = rows.size() - 1; i >= 0; --i)
521 m_gridTCC->DeleteRows(rows[i], 1);
525void BusFormArcFlashProtection::OnTypeSelected(wxCommandEvent& event)
530void BusFormArcFlashProtection::OnnCancelButtonClick(wxCommandEvent& event)
532 EndModal(wxID_CANCEL);
535void BusFormArcFlashProtection::OnGridKeyDown(wxKeyEvent& event)
537 if (!event.ControlDown() || event.GetKeyCode() !=
'V') {
542 if (!wxTheClipboard->Open()) {
547 wxTextDataObject data;
549 if (!wxTheClipboard->GetData(data)) {
550 wxTheClipboard->Close();
555 wxTheClipboard->Close();
557 wxString text = data.GetText();
558 text.Replace(
"\r\n",
"\n");
559 text.Replace(
"\r",
"\n");
562 wxStringTokenizer rowTokenizer(text,
"\n");
564 while (rowTokenizer.HasMoreTokens())
565 rows.Add(rowTokenizer.GetNextToken());
567 int startRow = m_gridTCC->GetGridCursorRow();
568 int startCol = m_gridTCC->GetGridCursorCol();
578 for (
const auto& rowText : rows) {
579 if (rowText.IsEmpty())
582 wxArrayString columns;
583 wxStringTokenizer columnTokenizer(rowText,
"\t");
585 while (columnTokenizer.HasMoreTokens())
586 columns.Add(columnTokenizer.GetNextToken());
588 while (row >= m_gridTCC->GetNumberRows())
589 m_gridTCC->AppendRows(1);
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]);
597 m_gridTCC->ForceRefresh();
600void BusFormArcFlashProtection::OnGridCornerPaint(wxPaintEvent& event)
602 wxWindow* corner = m_gridTCC->GetGridCornerLabelWindow();
604 wxAutoBufferedPaintDC dc(corner);
605 dc.SetBackground(wxBrush(corner->GetBackgroundColour()));
608 std::unique_ptr<wxGraphicsContext> gc(wxGraphicsContext::Create(dc));
612 gc->SetAntialiasMode(wxANTIALIAS_DEFAULT);
614 const wxSize size = corner->GetClientSize();
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;
621 if (size.x <= 4 || size.y <= 4)
624 const wxColour axisColour(90, 90, 90);
625 const wxColour curveColour(40, 100, 200);
628 gc->SetPen(wxPen(axisColour, 1.0));
629 gc->StrokeLine(left, top, left, bottom);
630 gc->StrokeLine(left, bottom, right, bottom);
633 const double x0 = left + size.x * 0.23;
634 const double y0 = top + size.y * 0.12;
636 const double x1 = left + size.x * 0.42;
637 const double y1 = top + size.y * 0.50;
639 const double x2 = left + size.x * 0.63;
640 const double y2 = top + size.y * 0.72;
642 const double x3 = right - size.x * 0.08;
643 const double y3 = top + size.y * 0.80;
645 wxGraphicsPath path = gc->CreatePath();
647 path.MoveToPoint(x0, y0);
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,
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,
661 path.AddCurveToPoint(
662 x2 + size.x * 0.10, y2 + size.y * 0.06,
663 x3 - size.x * 0.08, y3,
668 gc->SetPen(wxPen(curveColour, m_gridCornerHover ? 2.0 : 1.5));
669 gc->StrokePath(path);
672bool BusFormArcFlashProtection::ParseDouble(
const wxString& text,
double& value)
675 str.Trim(
true).Trim(
false);
676 if (str.Find(
',') != wxNOT_FOUND)
677 str.Replace(
",",
".");
679 return str.ToCDouble(&value);
682void BusFormArcFlashProtection::InitializeCBChoices()
684 m_choiceCBType->Clear();
685 m_choiceCBType->Append(_(
"MCCB"));
686 m_choiceCBType->Append(_(
"ICCB"));
687 m_choiceCBType->Append(_(
"LVPCB"));
688 m_choiceCBType->SetSelection(0);
690 m_choiceCBVoltage->Clear();
691 m_choiceCBVoltage->Append(_(
"\u2264 480 V"));
692 m_choiceCBVoltage->Append(_(
"575\u2013690 V"));
693 m_choiceCBVoltage->SetSelection(0);
698void BusFormArcFlashProtection::UpdateCBCurrentChoices()
700 const int type = m_choiceCBType->GetSelection();
702 m_choiceCBCurrent->Clear();
703 m_choiceCBCurrent->Enable(
true);
708 m_choiceCBCurrent->Append(_(
"100\u2013400A"));
709 m_choiceCBCurrent->Append(_(
"600\u20131200A"));
710 m_choiceCBCurrent->SetSelection(0);
714 m_choiceCBCurrent->Append(_(
"1600\u20136000A"));
715 m_choiceCBCurrent->SetSelection(0);
716 m_choiceCBCurrent->Enable(
false);
720 m_choiceCBCurrent->Append(_(
"800\u20136300A"));
721 m_choiceCBCurrent->SetSelection(0);
722 m_choiceCBCurrent->Enable(
false);
727void BusFormArcFlashProtection::UpdateCBTripChoices()
729 const int type = m_choiceCBType->GetSelection();
730 const int current = m_choiceCBCurrent->GetSelection();
732 m_choiceCBTrip->Clear();
733 m_choiceCBTrip->Enable(
true);
739 m_choiceCBTrip->Append(_(
"TM or M"));
740 m_choiceCBTrip->SetSelection(0);
741 m_choiceCBTrip->Enable(
false);
745 m_choiceCBTrip->Append(_(
"TM or M"));
746 m_choiceCBTrip->Append(_(
"Electronic (E, LI)"));
747 m_choiceCBTrip->SetSelection(0);
752 m_choiceCBTrip->Append(_(
"TM or M"));
753 m_choiceCBTrip->SetSelection(0);
754 m_choiceCBTrip->Enable(
false);
758 m_choiceCBTrip->Append(_(
"Electronic (E, LI)"));
759 m_choiceCBTrip->Append(_(
"Electronic, short-time delay (E, LS)"));
760 m_choiceCBTrip->SetSelection(0);
764void BusFormArcFlashProtection::UpdateCBChoices()
766 UpdateCBCurrentChoices();
767 UpdateCBTripChoices();
770void BusFormArcFlashProtection::UpdateRadioI1Selection()
772 if (m_radioBoxI1->GetSelection() == 0) {
773 m_textCtrlIn->Enable(
false);
774 m_staticTextI1->SetLabel(_(
"Minimum current (I₁) [A]"));
777 m_staticTextI1->SetLabel(_(
"Instant/Short-Time (Iₜ) [A]"));
778 m_textCtrlIn->Enable();
782void BusFormArcFlashProtection::UpdateFuseCurrentChoices()
784 m_choiceFuseCurrent->Clear();
786 switch (m_choiceFuseClass->GetSelection())
789 m_choiceFuseCurrent->Append(_(
"601–800A"));
790 m_choiceFuseCurrent->Append(_(
"801–1200A"));
791 m_choiceFuseCurrent->Append(_(
"1201–1600A"));
792 m_choiceFuseCurrent->Append(_(
"1601–2000A"));
796 m_choiceFuseCurrent->Append(_(
"≤ 100A"));
797 m_choiceFuseCurrent->Append(_(
"101–200A"));
798 m_choiceFuseCurrent->Append(_(
"201–400A"));
799 m_choiceFuseCurrent->Append(_(
"401–600A"));
806 if (m_choiceFuseCurrent->GetCount() > 0)
807 m_choiceFuseCurrent->SetSelection(0);
812 const bool isBreaker = data.type == ArcFlashProtectionType::CIRCUIT_BREAKER;
813 const bool isFuse = !isBreaker;
816 m_choiceType->SetSelection(isBreaker ? 0 : 1);
817 m_choiceMethod->SetString(1, isBreaker ? _(
"Appendix I") : _(
"Appendix H"));
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;
823 wxASSERT_MSG(data.method == ArcFlashProtectionMethod::TCC_CURVE || validAnnex,
824 "Invalid protection type and method combination");
826 const int methodSelection = data.method == ArcFlashProtectionMethod::TCC_CURVE || !validAnnex ? 0 : 1;
827 m_choiceMethod->SetSelection(methodSelection);
832 const int page = methodSelection == 0 ? 0 : (isBreaker ? 1 : 2);
833 m_notebook->ChangeSelection(page);
836 m_gridTCC->BeginBatch();
838 if (m_gridTCC->GetNumberRows() > 0)
839 m_gridTCC->DeleteRows(0, m_gridTCC->GetNumberRows());
841 if (!data.tccPoints.empty())
843 m_gridTCC->AppendRows(
static_cast<int>(data.tccPoints.size()));
845 for (
size_t i = 0; i < data.tccPoints.size(); ++i)
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));
852 m_gridTCC->EndBatch();
853 m_gridTCC->ForceRefresh();
855 m_checkBoxIsMeltTime->SetValue(data.curveIsMeltTimeOnly);
856 m_textCtrlDelay->ChangeValue(FormatOptionalNumber(data.additionalDelay));
860 int currentRange = 0;
863 switch (data.breakerFamily)
865 case ArcFlashBreakerFamily::MCCB_100_400A:
866 breakerType = 0; currentRange = 0; tripUnit = 0;
869 case ArcFlashBreakerFamily::MCCB_600_1200A_TM_OR_M:
870 breakerType = 0; currentRange = 1; tripUnit = 0;
873 case ArcFlashBreakerFamily::MCCB_600_1200A_E_LI:
874 breakerType = 0; currentRange = 1; tripUnit = 1;
877 case ArcFlashBreakerFamily::MCCB_OR_ICCB_1600_6000A:
878 breakerType = 1; currentRange = 0; tripUnit = 0;
881 case ArcFlashBreakerFamily::LVPCB_800_6300A_E_LI:
882 breakerType = 2; currentRange = 0; tripUnit = 0;
885 case ArcFlashBreakerFamily::LVPCB_800_6300A_E_LS:
886 breakerType = 2; currentRange = 0; tripUnit = 1;
890 m_choiceCBType->SetSelection(breakerType);
891 UpdateCBCurrentChoices();
893 m_choiceCBCurrent->SetSelection(currentRange);
894 UpdateCBTripChoices();
896 m_choiceCBTrip->SetSelection(tripUnit);
897 m_choiceCBVoltage->SetSelection(
static_cast<int>(data.breakerVoltageBand));
899 m_textCtrlI2->ChangeValue(FormatOptionalNumber(data.i2_kA));
902 const int i1Selection = data.i1Source == ArcFlashI1Source::DIRECT ? 0 : 1;
903 m_radioBoxI1->SetSelection(i1Selection);
904 UpdateRadioI1Selection();
907 m_textCtrlIn->ChangeValue(FormatOptionalNumber(data.breakerNominalCurrent_A));
910 if (data.i1Source == ArcFlashI1Source::DIRECT)
911 m_textCtrl1->ChangeValue(FormatOptionalNumber(data.i1_kA * 1000.0));
913 m_textCtrl1->ChangeValue(FormatOptionalNumber(data.it_A));
916 const int fuseFamily =
static_cast<int>(data.fuseFamily);
918 m_choiceFuseClass->SetSelection(fuseFamily < 4 ? 0 : 1);
919 UpdateFuseCurrentChoices();
920 m_choiceFuseCurrent->SetSelection(fuseFamily < 4 ? fuseFamily : fuseFamily - 4);
928 m_notebook->Layout();
929 m_splitterPageRight->Layout();
932 if (m_tccPopup && m_tccPopup->IsShown())
933 m_tccPopup->UpdateGraph();
938 ArcFlashProtectionData result = data;
940 auto showError = [&](
const wxString& message) {
941 wxMessageBox(message, _(
"Invalid data"), wxOK | wxICON_WARNING, m_parent);
945 auto parseDouble = [&](
const wxString& text,
double& value,
const wxString& field) {
946 return m_bus->DoubleFromString(
950 wxString::Format(_(
"Value entered incorrectly in the field \"%s\"."), field));
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));
962 const int typeSelection = m_choiceType->GetSelection();
964 if (typeSelection < 0 || typeSelection > 1)
965 return showError(_(
"Select a valid protection type."));
967 result.type = typeSelection == 0
968 ? ArcFlashProtectionType::CIRCUIT_BREAKER
969 : ArcFlashProtectionType::FUSE;
972 const int methodSelection = m_choiceMethod->GetSelection();
974 if (methodSelection < 0 || methodSelection > 1)
975 return showError(_(
"Select a valid protection method."));
977 if (methodSelection == 0)
978 result.method = ArcFlashProtectionMethod::TCC_CURVE;
979 else if (result.type == ArcFlashProtectionType::CIRCUIT_BREAKER)
980 result.method = ArcFlashProtectionMethod::ANNEX_I;
982 result.method = ArcFlashProtectionMethod::ANNEX_H;
985 wxString delayText = m_textCtrlDelay->GetValue();
986 delayText.Trim(
false).Trim(
true);
988 if (delayText.IsEmpty())
989 result.additionalDelay = 0.0;
990 else if (!parseDouble(delayText, result.additionalDelay, _(
"Additional delay (s)")))
993 if (!std::isfinite(result.additionalDelay) || result.additionalDelay < 0.0)
994 return showError(_(
"The additional delay must be greater than or equal to zero."));
997 if (result.method == ArcFlashProtectionMethod::TCC_CURVE) {
998 const int rows = m_gridTCC->GetNumberRows();
1000 if (m_gridTCC->GetNumberCols() < 2 || rows < 2)
1001 return showError(_(
"The TCC curve must contain at least two points."));
1003 std::vector<ArcFlashTCCPoint> points;
1004 points.reserve(rows);
1006 for (
int row = 0; row < rows; ++row) {
1007 ArcFlashTCCPoint point;
1009 const wxString currentField =
1010 wxString::Format(_(
"Current (A), row %d of the TCC curve"), row + 1);
1012 const wxString timeField =
1013 wxString::Format(_(
"Time (s), row %d of the TCC curve"), row + 1);
1015 if (!parseDouble(m_gridTCC->GetCellValue(row, 0), point.current, currentField))
1018 if (!requirePositive(point.current, currentField))
1021 if (!parseDouble(m_gridTCC->GetCellValue(row, 1), point.time, timeField))
1024 if (!requirePositive(point.time, timeField))
1027 points.push_back(point);
1030 result.tccPoints = points;
1031 result.curveIsMeltTimeOnly = m_checkBoxIsMeltTime->GetValue();
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();
1040 if (cbType < 0 || cbType > 2 || currentRange < 0 || tripUnit < 0)
1041 return showError(_(
"Select valid circuit breaker parameters."));
1046 if (currentRange == 0 && tripUnit == 0) {
1047 result.breakerFamily = ArcFlashBreakerFamily::MCCB_100_400A;
1049 else if (currentRange == 1 && tripUnit == 0) {
1050 result.breakerFamily = ArcFlashBreakerFamily::MCCB_600_1200A_TM_OR_M;
1052 else if (currentRange == 1 && tripUnit == 1) {
1053 result.breakerFamily = ArcFlashBreakerFamily::MCCB_600_1200A_E_LI;
1057 _(
"The selected MCCB current range and trip unit are not compatible."));
1062 if (currentRange != 0 || tripUnit != 0)
1064 _(
"The selected ICCB current range and trip unit are not compatible."));
1066 result.breakerFamily = ArcFlashBreakerFamily::MCCB_OR_ICCB_1600_6000A;
1070 if (currentRange != 0 || (tripUnit != 0 && tripUnit != 1))
1072 _(
"The selected LVPCB current range and trip unit are not compatible."));
1074 result.breakerFamily = tripUnit == 0
1075 ? ArcFlashBreakerFamily::LVPCB_800_6300A_E_LI
1076 : ArcFlashBreakerFamily::LVPCB_800_6300A_E_LS;
1081 const int voltageSelection = m_choiceCBVoltage->GetSelection();
1083 if (voltageSelection < 0 || voltageSelection > 1)
1084 return showError(_(
"Select a valid circuit breaker voltage range."));
1086 result.breakerVoltageBand = voltageSelection == 0
1087 ? ArcFlashBreakerVoltageBand::V480_AND_LOWER
1088 : ArcFlashBreakerVoltageBand::V575_TO_690;
1091 if (!parseDouble(m_textCtrlI2->GetValue(), result.i2_kA,
1092 _(
"Interrupting capacity (I₂) [kA]")))
1095 if (!requirePositive(result.i2_kA, _(
"Interrupting capacity (I₂) [kA]")))
1099 const int i1Selection = m_radioBoxI1->GetSelection();
1101 if (i1Selection < 0 || i1Selection > 1)
1102 return showError(_(
"Select a valid minimum current calculation method."));
1104 if (i1Selection == 0) {
1107 if (!parseDouble(m_textCtrl1->GetValue(), i1_A,
1108 _(
"Minimum current (I₁) [A]")))
1111 if (!requirePositive(i1_A, _(
"Minimum current (I₁) [A]")))
1114 result.i1Source = ArcFlashI1Source::DIRECT;
1115 result.i1_kA = i1_A / 1000.0;
1117 result.breakerNominalCurrent_A = 0.0;
1120 double nominalCurrent_A = 0.0;
1123 if (!parseDouble(m_textCtrlIn->GetValue(), nominalCurrent_A,
1124 _(
"Nominal current [A]")))
1127 if (!requirePositive(nominalCurrent_A, _(
"Nominal current [A]")))
1130 if (!parseDouble(m_textCtrl1->GetValue(), it_A,
1131 _(
"Instant/Short-Time current (Iₜ) [A]")))
1134 if (!requirePositive(it_A, _(
"Instant/Short-Time current (Iₜ) [A]")))
1137 result.i1Source = ArcFlashI1Source::FROM_IT;
1138 result.breakerNominalCurrent_A = nominalCurrent_A;
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());
1150 if (fuseClass < 0 || fuseClass > 1 || currentSelection < 0)
1151 return showError(_(
"Select a valid fuse class and current range."));
1153 int familyIndex = -1;
1155 if (currentCount == 4) {
1157 familyIndex = fuseClass * 4 + currentSelection;
1159 else if (currentCount == 8) {
1161 const int firstIndex = fuseClass * 4;
1163 if (currentSelection < firstIndex || currentSelection >= firstIndex + 4)
1165 _(
"The selected fuse current range does not match the fuse class."));
1167 familyIndex = currentSelection;
1170 return showError(_(
"The fuse current range choices are invalid."));
1173 if (familyIndex < 0 || familyIndex > 7)
1174 return showError(_(
"Select a valid fuse current range."));
1176 result.fuseFamily =
static_cast<ArcFlashFuseFamily
>(familyIndex);
1180 const int selectedRow = m_dvListCtrlDevices->GetSelectedRow();
1186 result.configured =
true;
1192TCCPopup::TCCPopup(wxWindow* parent, wxGrid* grid) : wxPopupTransientWindow(parent, wxBORDER_SIMPLE), m_grid(grid)
1194 SetBackgroundStyle(wxBG_STYLE_PAINT);
1197 Bind(wxEVT_PAINT, &TCCPopup::OnPaint,
this);
1200TCCPopup::~TCCPopup()
1202 Unbind(wxEVT_PAINT, &TCCPopup::OnPaint,
this);
1205void TCCPopup::UpdateGraph()
1211bool TCCPopup::GetCellDouble(
int row,
int col,
double& value)
const
1213 wxString str = m_grid->GetCellValue(row, col);
1214 return str.ToDouble(&value);
1217void TCCPopup::OnPaint(wxPaintEvent& event)
1219 wxAutoBufferedPaintDC dc(
this);
1221 dc.SetBackground(*wxWHITE_BRUSH);
1227 std::unique_ptr<wxGraphicsContext> gc(wxGraphicsContext::Create(dc));
1232 gc->SetAntialiasMode(wxANTIALIAS_DEFAULT);
1234 const wxSize size = GetClientSize();
1236 const int left = 60;
1237 const int right = 20;
1239 const int bottom = 50;
1241 const int graphWidth = size.x - left - right;
1242 const int graphHeight = size.y - top - bottom;
1244 if (graphWidth <= 0 || graphHeight <= 0)
1253 std::vector<Point> data;
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();
1260 for (
int row = 0; row < m_grid->GetNumberRows(); ++row)
1262 double current = 0.0;
1265 if (!GetCellDouble(row, 0, current) || !GetCellDouble(row, 1, time))
1268 if (current <= 0.0 || time <= 0.0)
1271 data.push_back({ current, time });
1273 minCurrent = std::min(minCurrent, current);
1274 maxCurrent = std::max(maxCurrent, current);
1275 minTime = std::min(minTime, time);
1276 maxTime = std::max(maxTime, time);
1279 if (data.size() < 2)
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));
1288 if (logMinCurrent == logMaxCurrent)
1294 if (logMinTime == logMaxTime)
1300 auto mapX = [&](
double current) {
1301 return left + (std::log10(current) - logMinCurrent) / (logMaxCurrent - logMinCurrent) * graphWidth;
1304 auto mapY = [&](
double time) {
1305 return size.y - bottom - (std::log10(time) - logMinTime) / (logMaxTime - logMinTime) * graphHeight;
1308 const int firstCurrentDecade =
static_cast<int>(logMinCurrent);
1309 const int lastCurrentDecade =
static_cast<int>(logMaxCurrent);
1311 const int firstTimeDecade =
static_cast<int>(logMinTime);
1312 const int lastTimeDecade =
static_cast<int>(logMaxTime);
1315 gc->SetPen(wxPen(wxColour(230, 230, 230), 1));
1317 for (
int exponent = firstCurrentDecade; exponent <= lastCurrentDecade; ++exponent)
1319 const double decade = std::pow(10.0, exponent);
1321 for (
int i = 2; i <= 9; ++i)
1323 const double current = i * decade;
1324 const double x = mapX(current);
1326 if (x >= left && x <= size.x - right)
1327 gc->StrokeLine(x, top, x, size.y - bottom);
1332 for (
int exponent = firstTimeDecade; exponent <= lastTimeDecade; ++exponent)
1334 const double decade = std::pow(10.0, exponent);
1336 for (
int i = 2; i <= 9; ++i)
1338 const double time = i * decade;
1339 const double y = mapY(time);
1341 if (y >= top && y <= size.y - bottom)
1342 gc->StrokeLine(left, y, size.x - right, y);
1347 gc->SetPen(wxPen(wxColour(190, 190, 190), 1));
1349 for (
int exponent = firstCurrentDecade; exponent <= lastCurrentDecade; ++exponent)
1351 const double current = std::pow(10.0, exponent);
1352 const double x = mapX(current);
1354 if (x >= left && x <= size.x - right)
1355 gc->StrokeLine(x, top, x, size.y - bottom);
1359 for (
int exponent = firstTimeDecade; exponent <= lastTimeDecade; ++exponent)
1361 const double time = std::pow(10.0, exponent);
1362 const double y = mapY(time);
1364 if (y >= top && y <= size.y - bottom)
1365 gc->StrokeLine(left, y, size.x - right, y);
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);
1374 gc->SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT), *wxBLACK);
1376 for (
int exponent = firstCurrentDecade; exponent <= lastCurrentDecade; ++exponent)
1378 const double current = std::pow(10.0, exponent);
1379 const double x = mapX(current);
1381 if (x < left || x > size.x - right)
1384 const wxString label = wxString::Format(
"1e%d", exponent);
1388 gc->GetTextExtent(label, &textWidth, &textHeight);
1390 gc->DrawText(label, x - textWidth / 2.0, size.y - bottom + 5);
1394 for (
int exponent = firstTimeDecade; exponent <= lastTimeDecade; ++exponent)
1396 const double time = std::pow(10.0, exponent);
1397 const double y = mapY(time);
1399 if (y < top || y > size.y - bottom)
1402 const wxString label = wxString::Format(
"1e%d", exponent);
1406 gc->GetTextExtent(label, &textWidth, &textHeight);
1408 gc->DrawText(label, left - textWidth - 5, y - textHeight / 2.0);
1412 wxGraphicsPath path = gc->CreatePath();
1414 for (
size_t i = 0; i < data.size(); ++i)
1416 const double x = mapX(data[i].current);
1417 const double y = mapY(data[i].time);
1420 path.MoveToPoint(x, y);
1422 path.AddLineToPoint(x, y);
1425 gc->SetPen(wxPen(wxColour(40, 100, 200), 2.0));
1426 gc->StrokePath(path);
1429 const wxString xLabel = _(
"Current (A)");
1432 double xLabelHeight;
1433 gc->GetTextExtent(xLabel, &xLabelWidth, &xLabelHeight);
1435 gc->DrawText(xLabel, left + (graphWidth - xLabelWidth) / 2.0, size.y - 20);
1438 const wxString yLabel = _(
"Time (s)");
1441 double yLabelHeight;
1442 gc->GetTextExtent(yLabel, &yLabelWidth, &yLabelHeight);
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);
1450void BusFormArcFlashProtection::OnChoiceCBCurrent(wxCommandEvent& event)
1452 UpdateCBTripChoices();
1454void BusFormArcFlashProtection::OnChoiceCBTrip(wxCommandEvent& event)
1458void BusFormArcFlashProtection::OnChoiceCBType(wxCommandEvent& event)
1462void BusFormArcFlashProtection::OnRadioI1Selected(wxCommandEvent& event)
1464 UpdateRadioI1Selection();
1466void BusFormArcFlashProtection::OnNominalCurrentUpdated(wxCommandEvent& event)
1469 if (m_textCtrlIn->GetValue().ToDouble(&value))
1471 if (value <= 100.0) {
1472 m_textCtrl1->SetValue(wxString::FromDouble(1300));
1475 m_textCtrl1->SetValue(wxString::FromDouble(value * 10.0));
1479void BusFormArcFlashProtection::OnChoiceFuseClass(wxCommandEvent& event)
1481 UpdateFuseCurrentChoices();
Node for power elements. All others power elements are connected through this.