19#include <wx/numformatter.h>
23#include "../utils/Path.h"
24#include "BusFormArcFlashProtection.h"
29 wxString FormatArcFlashNumber(
double value)
31 return wxNumberFormatter::ToString(
33 wxNumberFormatter::Style_None |
34 wxNumberFormatter::Style_NoTrailingZeroes);
37 wxString FormatArcFlashInput(
double value)
39 return value == 0.0 ? wxString() : FormatArcFlashNumber(value);
43BusForm::BusForm(wxWindow* parent,
Bus* bus, wxWindow* workspace) : BusFormBase(parent), m_bus(bus), m_parent(parent), m_workspace(workspace)
45 m_choiceFaultType->SetString(0, _(
"Three-phase"));
46 m_choiceFaultType->SetString(1, _(
"Line-to-line"));
47 m_choiceFaultType->SetString(2, _(
"Double line-to-ground"));
48 m_choiceFaultType->SetString(3, _(
"Line-to-ground"));
51 struct ElectrodeConfiguration
57 const std::vector<ElectrodeConfiguration> configurations = {
58 {.code = _(
"VCB - Vertical, Metal Box"), .image =
"VCB.png"},
59 {.code = _(
"VCBB - Vertical, Insulating Barrier"), .image =
"VCBB.png"},
60 {.code = _(
"HCB - Horizontal, Metal Box"), .image =
"HCB.png"},
61 {.code = _(
"VOA - Vertical, Open Air"), .image =
"VOA.png"},
62 {.code = _(
"HOA - Horizontal, Open Air"), .image =
"HOA.png"}
65 const wxSize size = FromDIP(wxSize(32, 32));
66 const wxString imgPath = Paths::GetDataPath() +
"/images/arcflash/";
68 for (
const auto& config : configurations)
70 wxImage image(imgPath + config.image, wxBITMAP_TYPE_PNG);
72 wxMessageBox(_(
"Could not load the electrode configuration image:\n") + imgPath + config.image, _(
"Arc Flash"), wxOK | wxICON_ERROR,
this);
76 image.Rescale(size.x, size.y, wxIMAGE_QUALITY_HIGH);
77 m_bmpComboBoxConfig->Append(config.code, wxBitmap(image));
81 m_electrodePreview->Hide();
83 m_textCtrlName->SetValue(bus->GetElectricalData().name);
84 m_textCtrlNomVoltage->SetValue(bus->
StringFromDouble(bus->GetElectricalData().nominalVoltage));
86 if (bus->GetElectricalData().nominalVoltageUnit == ElectricalUnit::UNIT_V)
87 m_choiceNomVoltage->SetSelection(0);
89 m_choiceNomVoltage->SetSelection(1);
91 m_checkBoxCtrlVoltage->SetValue(bus->GetElectricalData().isVoltageControlled);
92 m_textCtrlCtrlVoltage->SetValue(bus->
StringFromDouble(bus->GetElectricalData().controlledVoltage));
93 m_choiceCtrlVoltage->SetSelection(bus->GetElectricalData().controlledVoltageUnitChoice);
94 m_checkBoxSlackBus->SetValue(bus->GetElectricalData().slackBus);
96 m_checkBoxFault->SetValue(bus->GetElectricalData().hasFault);
97 switch (bus->GetElectricalData().faultType) {
99 m_choiceFaultType->SetSelection(0);
102 m_choiceFaultType->SetSelection(1);
105 m_choiceFaultType->SetSelection(2);
108 m_choiceFaultType->SetSelection(3);
113 switch (bus->GetElectricalData().faultLocation) {
115 m_choiceFaultPlace->SetSelection(0);
118 m_choiceFaultPlace->SetSelection(1);
121 m_choiceFaultPlace->SetSelection(2);
126 m_textCtrlFaultResistance->SetValue(bus->
StringFromDouble(bus->GetElectricalData().faultResistance));
127 m_textCtrlFaultReactance->SetValue(bus->
StringFromDouble(bus->GetElectricalData().faultReactance));
129 m_checkBoxPlotData->SetValue(bus->GetElectricalData().plotBus);
130 m_checkBoxStabFault->SetValue(bus->GetElectricalData().stabHasFault);
131 m_textCtrlStabFaultTime->SetValue(bus->
StringFromDouble(bus->GetElectricalData().stabFaultTime));
132 m_textCtrlStabFaultLength->SetValue(bus->
StringFromDouble(bus->GetElectricalData().stabFaultLength));
133 m_textCtrlStabFaultResistance->SetValue(bus->
StringFromDouble(bus->GetElectricalData().stabFaultResistance));
134 m_textCtrlStabFaultReactance->SetValue(bus->
StringFromDouble(bus->GetElectricalData().stabFaultReactance));
136 EnableCtrlVoltageFields(bus->GetElectricalData().isVoltageControlled);
137 EnableFaultFields(bus->GetElectricalData().hasFault);
138 EnableStabFaultFields(bus->GetElectricalData().stabHasFault);
140 m_checkBoxPlotPQData->SetValue(bus->GetElectricalData().plotPQData);
142 LoadArcFlashData(m_bus->GetElectricalDataRef().arcFlash);
144 m_staticBitmapWarning->Hide();
146 GetSizer()->Layout();
147 GetSizer()->Fit(
this);
148 SetSize(GetBestSize());
151BusForm::~BusForm() {}
152void BusForm::OnButtonCancelClick(wxCommandEvent& event) { EndModal(wxID_CANCEL); }
153void BusForm::OnButtonOKClick(wxCommandEvent& event)
155 BusElectricalData data = m_bus->GetElectricalData();
156 data.name = m_textCtrlName->GetValue();
157 if (!m_bus->DoubleFromString(m_parent, m_textCtrlNomVoltage->GetValue(), data.nominalVoltage,
158 _(
"Value entered incorrectly in the field \"Rated voltage\".")))
160 data.nominalVoltageUnit = m_choiceNomVoltage->GetSelection() == 0 ? ElectricalUnit::UNIT_V : ElectricalUnit::UNIT_kV;
161 data.isVoltageControlled = m_checkBoxCtrlVoltage->GetValue();
162 if (data.isVoltageControlled) {
163 if (!m_bus->DoubleFromString(m_parent, m_textCtrlCtrlVoltage->GetValue(), data.controlledVoltage,
164 _(
"Value entered incorrectly in the field \"Controlled voltage\".")))
166 data.controlledVoltageUnitChoice = m_choiceCtrlVoltage->GetSelection();
168 data.slackBus = m_checkBoxSlackBus->GetValue();
170 data.hasFault = m_checkBoxFault->GetValue();
171 switch (m_choiceFaultType->GetSelection()) {
186 switch (m_choiceFaultPlace->GetSelection()) {
198 if (!m_bus->DoubleFromString(m_parent, m_textCtrlFaultResistance->GetValue(), data.faultResistance,
199 _(
"Value entered incorrectly in the field \"Fault resistance\".")))
202 if (!m_bus->DoubleFromString(m_parent, m_textCtrlFaultReactance->GetValue(), data.faultReactance,
203 _(
"Value entered incorrectly in the field \"Fault reactance\".")))
206 data.plotBus = m_checkBoxPlotData->GetValue();
207 data.stabHasFault = m_checkBoxStabFault->GetValue();
209 if (!m_bus->DoubleFromString(m_parent, m_textCtrlStabFaultTime->GetValue(), data.stabFaultTime,
210 _(
"Value entered incorrectly in the field \"Time\".")))
213 if (!m_bus->DoubleFromString(m_parent, m_textCtrlStabFaultLength->GetValue(), data.stabFaultLength,
214 _(
"Value entered incorrectly in the field \"Fault lenght\".")))
217 if (!m_bus->DoubleFromString(m_parent, m_textCtrlStabFaultResistance->GetValue(), data.stabFaultResistance,
218 _(
"Value entered incorrectly in the field \"Fault resistence (stability)\".")))
221 if (!m_bus->DoubleFromString(m_parent, m_textCtrlStabFaultReactance->GetValue(), data.stabFaultReactance,
222 _(
"Value entered incorrectly in the field \"Fault reactance (stability)\".")))
225 data.plotPQData = m_checkBoxPlotPQData->GetValue();
227 m_bus->SetElectricalData(data);
229 if (data.stabHasFault)
230 m_bus->SetDynamicEvent(
true);
232 m_bus->SetDynamicEvent(
false);
234 auto& arcFlash = m_bus->GetElectricalDataRef().arcFlash;
236 ArcFlashData afData = arcFlash;
238 if (!ValidateAndGetArcFlashData(afData))
241 arcFlash = std::move(afData);
246void BusForm::OnNominalVoltageChoice(wxCommandEvent& event) { UpdateChoiceBoxes(); }
247void BusForm::OnFaultTypeChoice(wxCommandEvent& event) { UpdateChoiceBoxes(); }
248void BusForm::OnControlledVoltageClick(wxCommandEvent& event)
250 EnableCtrlVoltageFields(m_checkBoxCtrlVoltage->GetValue());
252void BusForm::OnInsertFaultClick(wxCommandEvent& event) { EnableFaultFields(m_checkBoxFault->GetValue()); }
253void BusForm::OnInsertStabFaultClick(wxCommandEvent& event) { EnableStabFaultFields(m_checkBoxStabFault->GetValue()); }
254void BusForm::EnableCtrlVoltageFields(
bool enable)
256 m_textCtrlCtrlVoltage->Enable(enable);
257 m_choiceCtrlVoltage->Enable(enable);
262void BusForm::EnableFaultFields(
bool enable)
264 m_choiceFaultType->Enable(enable);
265 m_choiceFaultPlace->Enable(enable);
266 m_textCtrlFaultReactance->Enable(enable);
267 m_textCtrlFaultResistance->Enable(enable);
268 m_staticTextPU_1->Enable(enable);
269 m_staticTextPU_2->Enable(enable);
274void BusForm::EnableStabFaultFields(
bool enable)
276 m_textCtrlStabFaultTime->Enable(enable);
277 m_textCtrlStabFaultLength->Enable(enable);
278 m_staticTextS_1->Enable(enable);
279 m_staticTextS_2->Enable(enable);
280 m_textCtrlStabFaultReactance->Enable(enable);
281 m_textCtrlStabFaultResistance->Enable(enable);
282 m_staticTextPU_3->Enable(enable);
283 m_staticTextPU_4->Enable(enable);
286void BusForm::UpdateChoiceBoxes()
288 switch (m_choiceFaultType->GetSelection()) {
291 m_choiceFaultPlace->Enable(
false);
296 if (m_checkBoxFault->GetValue()) m_choiceFaultPlace->Enable(
true);
297 m_choiceFaultPlace->SetString(0, _(
"Lines AB"));
298 m_choiceFaultPlace->SetString(1, _(
"Lines BC"));
299 m_choiceFaultPlace->SetString(2, _(
"Lines CA"));
303 if (m_checkBoxFault->GetValue()) m_choiceFaultPlace->Enable(
true);
304 m_choiceFaultPlace->SetString(0, _(
"Line A"));
305 m_choiceFaultPlace->SetString(1, _(
"Line B"));
306 m_choiceFaultPlace->SetString(2, _(
"Line C"));
311 switch (m_choiceNomVoltage->GetSelection()) {
313 m_choiceCtrlVoltage->SetString(1, _(
"V"));
316 m_choiceCtrlVoltage->SetString(1, _(
"kV"));
323bool BusForm::ValidateAndGetArcFlashData(
ArcFlashData& data)
325 ArcFlashData result = data;
327 auto showError = [&](
const wxString& message) {
328 wxMessageBox(message, _(
"Invalid data"),
329 wxOK | wxICON_WARNING, m_parent);
333 auto readPositiveValue = [&](wxTextCtrl* control,
double& value,
334 const wxString& field) {
335 if (!m_bus->DoubleFromString(
336 m_parent, control->GetValue(), value,
338 _(
"Value entered incorrectly in the field \"%s.\""),
342 if (!std::isfinite(value) || value <= 0.0)
343 return showError(wxString::Format(
344 _(
"The field \"%s\" must be greater than zero."),
351 const int configSelection = m_bmpComboBoxConfig->GetSelection();
353 if (configSelection < 0 ||
354 configSelection >=
static_cast<int>(m_bmpComboBoxConfig->GetCount()) ||
355 configSelection >
static_cast<int>(ArcFlashElectrodeConfig::HOA))
356 return showError(_(
"Select a valid electrode configuration."));
358 result.electrodeConfig =
359 static_cast<ArcFlashElectrodeConfig
>(configSelection);
362 if (!readPositiveValue(m_textCtrlGap, result.conductorGap,
363 _(
"Electrode gap (mm)")))
366 if (!readPositiveValue(m_textCtrlWorkDist, result.workingDistance,
367 _(
"Working distance (mm)")))
371 if (IsEnclosedConfig(result.electrodeConfig)) {
372 if (!readPositiveValue(m_textCtrlHeight, result.enclosureHeight,
376 if (!readPositiveValue(m_textCtrlWidth, result.enclosureWidth,
380 if (!readPositiveValue(m_textCtrlGap525558, result.enclosureDepth,
385 result.enclosureHeight = 0.0;
386 result.enclosureWidth = 0.0;
387 result.enclosureDepth = 0.0;
391 const bool inputsChanged =
392 result.electrodeConfig != data.electrodeConfig ||
393 result.conductorGap != data.conductorGap ||
394 result.workingDistance != data.workingDistance ||
395 (IsEnclosedConfig(result.electrodeConfig) &&
396 (result.enclosureHeight != data.enclosureHeight ||
397 result.enclosureWidth != data.enclosureWidth ||
398 result.enclosureDepth != data.enclosureDepth));
401 result.calculated =
false;
402 result.iarc_kA = 0.0;
403 result.iarcMin_kA = 0.0;
404 result.incidentEnergy_Jcm2 = 0.0;
405 result.incidentEnergy_calcm2 = 0.0;
406 result.arcFlashBoundary_mm = 0.0;
407 result.usedReducedCurrent =
false;
408 result.warnings.clear();
413 data = std::move(result);
416 m_staticTextEnergy->SetLabel(_(
"E = -- J/cm²"));
417 m_staticTextAFB->SetLabel(_(
"AFB = -- mm"));
418 m_staticBitmapWarning->Hide();
419 m_panelArcFlash->Layout();
428 const int configSelection =
static_cast<int>(data.electrodeConfig);
430 if (configSelection >= 0 &&
431 configSelection <
static_cast<int>(m_bmpComboBoxConfig->GetCount()))
432 m_bmpComboBoxConfig->SetSelection(configSelection);
433 else if (m_bmpComboBoxConfig->GetCount() > 0)
434 m_bmpComboBoxConfig->SetSelection(0);
437 m_textCtrlGap->ChangeValue(FormatArcFlashInput(data.conductorGap));
438 m_textCtrlWorkDist->ChangeValue(FormatArcFlashInput(data.workingDistance));
439 m_textCtrlHeight->ChangeValue(FormatArcFlashInput(data.enclosureHeight));
440 m_textCtrlWidth->ChangeValue(FormatArcFlashInput(data.enclosureWidth));
441 m_textCtrlGap525558->ChangeValue(FormatArcFlashInput(data.enclosureDepth));
443 UpdateArcFlashEnclosureFields();
446 if (data.calculated) {
447 m_staticTextEnergy->SetLabel(wxString::Format(
449 FormatArcFlashNumber(data.incidentEnergy_Jcm2)));
451 m_staticTextAFB->SetLabel(wxString::Format(
453 FormatArcFlashNumber(data.arcFlashBoundary_mm)));
456 m_staticTextEnergy->SetLabel(_(
"E = -- J/cm²"));
457 m_staticTextAFB->SetLabel(_(
"AFB = -- mm"));
461 wxString warningText;
463 if (data.calculated) {
464 for (
const auto& warning : data.warnings) {
465 if (!warningText.IsEmpty())
468 warningText += warning;
471 if (data.usedReducedCurrent) {
472 if (!warningText.IsEmpty())
475 warningText += _(
"Reduced-current calculation was used.");
479 m_staticBitmapWarning->SetToolTip(warningText);
480 m_staticBitmapWarning->Show(!warningText.IsEmpty());
482 m_panelArcFlash->Layout();
483 m_panelArcFlash->Refresh();
486void BusForm::UpdateArcFlashEnclosureFields()
488 const int selection = m_bmpComboBoxConfig->GetSelection();
490 const bool isValidSelection =
492 selection <= static_cast<int>(ArcFlashElectrodeConfig::HOA);
494 const bool isEnclosed = isValidSelection &&
496 static_cast<ArcFlashElectrodeConfig
>(selection));
498 m_staticTextHeight->Enable(isEnclosed);
499 m_textCtrlHeight->Enable(isEnclosed);
501 m_staticTextWidth->Enable(isEnclosed);
502 m_textCtrlWidth->Enable(isEnclosed);
504 m_staticTextDepth->Enable(isEnclosed);
505 m_textCtrlGap525558->Enable(isEnclosed);
508void BusForm::OnMouseEnterElectrodeConfig(wxMouseEvent& event)
510 wxString configStr =
"";
511 if (m_bmpComboBoxConfig->GetSelection() == -1)
518 switch (m_bmpComboBoxConfig->GetSelection())
521 configStr = wxT(
"VCB");
524 configStr = wxT(
"VCBB");
527 configStr = wxT(
"HCB");
530 configStr = wxT(
"VOA");
533 configStr = wxT(
"HOA");
537 const wxString imgPath = Paths::GetDataPath() +
"/images/arcflash/" + configStr +
".png";
538 wxImage image(imgPath, wxBITMAP_TYPE_PNG);
539 const wxSize size = FromDIP(wxSize(300, 300));
540 image.Rescale(size.x, size.y, wxIMAGE_QUALITY_HIGH);
541 m_electrodePreview->SetBitmap(wxBitmap(image));
543 m_electrodePreview->Position(wxGetMousePosition() + wxPoint(10, 10), wxSize(0, 0));
545 m_electrodePreview->Show();
546 m_electrodePreview->Raise();
551void BusForm::OnMouseLeaveElectrodeConfig(wxMouseEvent& event)
553 m_electrodePreview->Hide();
557void BusForm::OnMouseMotionElectrodeConfig(wxMouseEvent& event)
559 const wxPoint mouse = wxGetMousePosition();
560 const wxSize popupSize = m_electrodePreview->GetSize();
562 wxPoint pos = mouse + FromDIP(wxPoint(10, 10));
564 const wxRect screen = wxGetClientDisplayRect();
566 if (pos.x + popupSize.x > screen.GetRight())
567 pos.x = mouse.x - popupSize.x - FromDIP(10);
569 if (pos.y + popupSize.y > screen.GetBottom())
570 pos.y = mouse.y - popupSize.y - FromDIP(10);
572 m_electrodePreview->SetPosition(pos);
575BitmapPopup::BitmapPopup(wxWindow* parent) : wxPopupWindow(parent, wxBORDER_SIMPLE)
577 m_bitmap =
new wxStaticBitmap(
this, wxID_ANY, wxNullBitmap);
580void BitmapPopup::SetBitmap(
const wxBitmap& bitmap)
582 m_bitmap->SetBitmap(bitmap);
583 m_bitmap->SetSize(bitmap.GetSize());
584 SetSize(bitmap.GetSize());
586void BusForm::OnnConfigProtectionButtonClick(wxCommandEvent& event)
588 double basePower = 100e6;
589 if (
auto* workspace =
dynamic_cast<Workspace*
>(m_workspace))
591 basePower = m_bus->GetValueFromUnit(
592 workspace->GetProperties()->GetSimulationPropertiesData().basePower,
593 workspace->GetProperties()->GetSimulationPropertiesData().basePowerUnit);
596 BusFormArcFlashProtection protectionConfig(
this, m_bus, basePower);
597 if (protectionConfig.ShowModal() == wxID_OK)
602void BusForm::OnCBArcFlashConfigSelected(wxCommandEvent& event)
604 UpdateArcFlashEnclosureFields();
Node for power elements. All others power elements are connected through this.
static wxString StringFromDouble(double value, int minDecimal=1, int maxDecimals=13)
Convert a double value to string.