Power System Platform  2026w10a-beta
Loading...
Searching...
No Matches
TransferFunctionForm.cpp
1/*
2 * Copyright (C) 2017 Thales Lima Oliveira <thales@ufu.br>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
19#include "../elements/controlElement/TransferFunction.h"
20
21TransferFunctionForm::TransferFunctionForm(wxWindow* parent, TransferFunction* transferFunction)
22 : TransferFunctionFormBase(parent)
23{
24 SetSize(GetBestSize());
25
26 m_parent = parent;
27 m_tf = transferFunction;
28 LoadTFData();
29}
30
31TransferFunctionForm::~TransferFunctionForm() {}
32void TransferFunctionForm::OnCancelClick(wxCommandEvent& event) { EndModal(wxID_CANCEL); }
33void TransferFunctionForm::OnOKClick(wxCommandEvent& event)
34{
35 if(ValidateData()) EndModal(wxID_OK);
36}
37
38void TransferFunctionForm::LoadTFData()
39{
40 auto num = m_tf->GetNumerator();
41 auto den = m_tf->GetDenominator();
42
43 wxString numStr = "";
44 for(auto it = num.begin(), itEnd = num.end(); it != itEnd; ++it) {
45 double value = *it;
46 if(it == num.begin())
47 numStr = m_tf->StringFromDouble(value, 0);
48 else
49 numStr += " " + m_tf->StringFromDouble(value, 0);
50 }
51 m_textCtrlNumerator->SetValue(numStr);
52
53 wxString denStr = "";
54 for(auto it = den.begin(), itEnd = den.end(); it != itEnd; ++it) {
55 double value = *it;
56 if(it == den.begin())
57 denStr = m_tf->StringFromDouble(value, 0);
58 else
59 denStr += " " + m_tf->StringFromDouble(value, 0);
60 }
61 m_textCtrlDenominator->SetValue(denStr);
62}
63
64bool TransferFunctionForm::ValidateData()
65{
66 wxString num = m_textCtrlNumerator->GetValue();
67 std::vector<double> numerator;
68 while(num != "") {
69 wxString rest;
70 wxString strValue = num.BeforeFirst(' ', &rest);
71 num = rest;
72 double value = 0;
73 if(!m_tf->DoubleFromString(this, strValue, value,
74 _("Value entered incorrectly in the field \"Numerator parameters\".")))
75 return false;
76 numerator.push_back(value);
77 }
78
79 wxString den = m_textCtrlDenominator->GetValue();
80 std::vector<double> denominator;
81 while(den != "") {
82 wxString rest;
83 wxString strValue = den.BeforeFirst(' ', &rest);
84 den = rest;
85 double value = 0;
86 if(!m_tf->DoubleFromString(this, strValue, value,
87 _("Value entered incorrectly in the field \"Denominator parameters\".")))
88 return false;
89 denominator.push_back(value);
90 }
91 m_tf->SetNumerator(numerator);
92 m_tf->SetDenominator(denominator);
93 m_tf->UpdateTFText();
94 return true;
95}
static bool DoubleFromString(wxWindow *parent, wxString strValue, double &value, wxString errorMsg)
Get a double value from a string. Show a error message if the conversion fail.
Definition Element.cpp:505
static wxString StringFromDouble(double value, int minDecimal=1, int maxDecimals=13)
Convert a double value to string.
Definition Element.cpp:533
Calculates the time response by a frequency domain transfer function.