Power System Platform  2026w34a-beta
Loading...
Searching...
No Matches
Limiter.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
18#include "Limiter.h"
20#include <wx/pen.h>
21#include <wx/brush.h>
22
23Limiter::Limiter(int id) : ControlElement(id)
24{
25 m_width = m_height = 36.0;
26 Node* nodeIn = new Node(m_position + wxPoint2DDouble(-18, 0), Node::NodeType::NODE_IN, m_borderSize);
27 nodeIn->StartMove(m_position);
28 Node* nodeOut = new Node(m_position + wxPoint2DDouble(18, 0), Node::NodeType::NODE_OUT, m_borderSize);
29 nodeOut->SetAngle(180.0);
30 nodeOut->StartMove(m_position);
31 m_nodeList.push_back(nodeIn);
32 m_nodeList.push_back(nodeOut);
33}
34
35Limiter::~Limiter()
36{
37 for (auto& node : m_nodeList) if (node) delete node;
38 m_nodeList.clear();
39}
40
41void Limiter::DrawDC(GUIColour* guiColour, wxPoint2DDouble translation, double scale, wxGraphicsContext* gc) const
42{
43 if (m_selected) {
44 gc->SetPen(*wxTRANSPARENT_PEN);
45 gc->SetBrush(guiColour->selection);
46 double borderSize = (m_borderSize * 2.0 + 1.0) / scale;
47 gc->DrawRectangle(m_position.m_x - m_width / 2 - borderSize / 2, m_position.m_y - m_height / 2 - borderSize / 2, m_width + borderSize, m_height + borderSize);
48 }
49 gc->SetPen(wxPen(guiColour->enabled, 1));
50 gc->SetBrush(guiColour->background);
51 gc->DrawRectangle(m_position.m_x - m_width / 2, m_position.m_y - m_height / 2, m_width, m_height);
52
53 // Plot symbol.
54 gc->SetPen(wxPen(guiColour->bus, 2));
55 gc->SetBrush(*wxTRANSPARENT_BRUSH);
56 wxPoint2DDouble limSymbol[4];
57 limSymbol[0] = m_position + wxPoint2DDouble(10, -10);
58 limSymbol[1] = m_position + wxPoint2DDouble(2, -10);
59 limSymbol[2] = m_position + wxPoint2DDouble(-2, 10);
60 limSymbol[3] = m_position + wxPoint2DDouble(-10, 10);
61 gc->StrokeLines(4, limSymbol);
62
63 gc->SetPen(*wxTRANSPARENT_PEN);
64 gc->SetBrush(guiColour->enabled);
65 DrawDCNodes(gc);
66}
67
68bool Limiter::ShowForm(wxWindow* parent, Element* element, wxWindow* workspace)
69{
70 LimiterForm limiter(parent, this);
71 limiter.CenterOnParent();
72 if (limiter.ShowModal() == wxID_OK) {
73 return true;
74 }
75 return false;
76}
77
78void Limiter::Rotate(bool clockwise)
79{
80 if (clockwise)
81 m_angle += 90.0;
82 else
83 m_angle -= 90.0;
84 if (m_angle >= 360.0)
85 m_angle = 0.0;
86 else if (m_angle < 0)
87 m_angle = 270.0;
88
89 UpdatePoints();
90
91 for (auto it = m_nodeList.begin(), itEnd = m_nodeList.end(); it != itEnd; ++it) {
92 Node* node = *it;
93 node->Rotate(clockwise);
94 }
95}
96
97void Limiter::UpdatePoints()
98{
99 if (m_angle == 0.0) {
100 m_nodeList[0]->SetPosition(m_position + wxPoint2DDouble(-18, 0));
101 m_nodeList[1]->SetPosition(m_position + wxPoint2DDouble(18, 0));
102 }
103 else if (m_angle == 90.0) {
104 m_nodeList[0]->SetPosition(m_position + wxPoint2DDouble(0, -18));
105 m_nodeList[1]->SetPosition(m_position + wxPoint2DDouble(0, 18));
106 }
107 else if (m_angle == 180.0) {
108 m_nodeList[0]->SetPosition(m_position + wxPoint2DDouble(18, 0));
109 m_nodeList[1]->SetPosition(m_position + wxPoint2DDouble(-18, 0));
110 }
111 else if (m_angle == 270.0) {
112 m_nodeList[0]->SetPosition(m_position + wxPoint2DDouble(0, 18));
113 m_nodeList[1]->SetPosition(m_position + wxPoint2DDouble(0, -18));
114 }
115}
116
117bool Limiter::Solve(double* input, double timeStep)
118{
119 if (!input) {
120 m_output = 0.0;
121 return true;
122 }
123 m_output = input[0];
124 if (m_output > m_upLimit)
125 m_output = m_upLimit;
126 else if (m_output < m_lowLimit)
127 m_output = m_lowLimit;
128
129 return true;
130}
131
133{
134 Limiter* copy = new Limiter(*this);
135 return copy;
136}
137
138rapidxml::xml_node<>* Limiter::SaveElement(rapidxml::xml_document<>& doc, rapidxml::xml_node<>* elementListNode)
139{
140 auto elementNode = XMLParser::AppendNode(doc, elementListNode, "Limiter");
141 XMLParser::SetNodeAttribute(doc, elementNode, "ID", m_elementID);
142
143 SaveCADProperties(doc, elementNode);
144 SaveControlNodes(doc, elementNode);
145
146 // Element properties
147 auto upLimit = XMLParser::AppendNode(doc, elementNode, "UpperLimit");
148 XMLParser::SetNodeValue(doc, upLimit, m_upLimit);
149 auto lowLimit = XMLParser::AppendNode(doc, elementNode, "LowerLimit");
150 XMLParser::SetNodeValue(doc, lowLimit, m_lowLimit);
151
152 return elementNode;
153}
154
155bool Limiter::OpenElement(rapidxml::xml_node<>* elementNode)
156{
157 if (!OpenCADProperties(elementNode)) return false;
158 if (!OpenControlNodes(elementNode)) return false;
159
160 // Element properties
161 m_upLimit = XMLParser::GetNodeValueDouble(elementNode, "UpperLimit");
162 m_lowLimit = XMLParser::GetNodeValueDouble(elementNode, "LowerLimit");
163
164 StartMove(m_position);
165 UpdatePoints();
166 return true;
167}
virtual void StartMove(wxPoint2DDouble position)
Update the element attributes related to the movement.
Element()
Constructor.
Definition Element.cpp:28
Form to edit the limit control data.
Definition LimiterForm.h:32
virtual bool ShowForm(wxWindow *parent, Element *element, wxWindow *workspace=nullptr)
Show element data form.
Definition Limiter.cpp:68
virtual Element * GetCopy()
Get a the element copy.
Definition Limiter.cpp:132
virtual void Rotate(bool clockwise=true)
Rotate the element.
Definition Limiter.cpp:78
virtual void DrawDC(GUIColour *guiColour, wxPoint2DDouble translation, double scale, wxGraphicsContext *gc) const
Draw the element using GDI+.
Definition Limiter.cpp:41
Node of a control element. This class manages the user interaction with the connection and control el...