Power System Platform  2026w10a-beta
Loading...
Searching...
No Matches
Shunt.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 "Shunt.h"
19
20Shunt::Shunt() : PowerElement() {}
21Shunt::~Shunt() {}
22void Shunt::UpdateSwitchesPosition()
23{
24 if(m_parentList[0]) {
25 m_pointList[1] = GetSwitchPoint(m_parentList[0], m_pointList[0], m_pointList[2]);
26 } else {
27 m_pointList[1] = m_pointList[0];
28 }
30}
31
32void Shunt::Move(wxPoint2DDouble position)
33{
34 SetPosition(m_movePos + position - m_moveStartPt);
35 for(int i = 2; i < (int)m_pointList.size(); i++) {
36 m_pointList[i] = m_movePts[i] + position - m_moveStartPt;
37 }
38 if(!m_parentList[0]) {
39 m_pointList[0] = m_movePts[0] + position - m_moveStartPt;
40 }
41 UpdateSwitchesPosition();
42 UpdatePowerFlowArrowsPosition();
43}
44
45void Shunt::MoveNode(Element* element, wxPoint2DDouble position)
46{
47 if(element) {
48 if(element == m_parentList[0]) {
49 m_pointList[0] = m_movePts[0] + position - m_moveStartPt;
50 }
51 } else {
52 if(m_activeNodeID == 1) {
53 m_pointList[0] = m_movePts[0] + position - m_moveStartPt;
54 if(m_parentList[0]) {
55 m_parentList[0]->RemoveChild(this);
56 m_parentList[0] = nullptr;
57 m_online = false;
58 }
59 }
60 }
61
62 // Recalculate switches positions
63 UpdateSwitchesPosition();
64 UpdatePowerFlowArrowsPosition();
65}
66
67void Shunt::StartMove(wxPoint2DDouble position)
68{
69 m_moveStartPt = position;
70 m_movePts = m_pointList;
71 m_movePos = m_position;
72}
73
75{
76 if(parent == m_parentList[0]) {
77 m_parentList[0] = nullptr;
78 m_online = false;
79 UpdateSwitchesPosition();
80 UpdatePowerFlowArrowsPosition();
81 }
82}
83
84bool Shunt::NodeContains(wxPoint2DDouble position)
85{
86 wxRect2DDouble nodeRect(m_pointList[0].m_x - 5.0 - m_borderSize, m_pointList[0].m_y - 5.0 - m_borderSize,
87 10 + 2.0 * m_borderSize, 10 + 2.0 * m_borderSize);
88
89 if(nodeRect.Contains(position)) {
90 m_activeNodeID = 1;
91 return true;
92 }
93
94 m_activeNodeID = 0;
95 return false;
96}
97
99{
100 if(parent && m_activeNodeID != 0) {
101 wxRect2DDouble nodeRect(m_pointList[0].m_x - 5.0 - m_borderSize, m_pointList[0].m_y - 5.0 - m_borderSize,
102 10 + 2.0 * m_borderSize, 10 + 2.0 * m_borderSize);
103
104 if(parent->Intersects(nodeRect)) {
105 m_parentList[0] = parent;
106
107 // Centralize the node on bus.
108 wxPoint2DDouble parentPt =
109 parent->RotateAtPosition(m_pointList[0], -parent->GetAngle()); // Rotate click to horizontal position.
110 parentPt.m_y = parent->GetPosition().m_y; // Centralize on bus.
111 parentPt = parent->RotateAtPosition(parentPt, parent->GetAngle());
112 m_pointList[0] = parentPt;
113
114 UpdateSwitchesPosition();
115 UpdatePowerFlowArrowsPosition();
116 return true;
117 } else {
118 m_parentList[0] = nullptr;
119 m_online = false;
120 }
121 }
122 return false;
123}
124
126{
127 if(m_parentList[0]) {
128 wxRect2DDouble nodeRect(m_pointList[0].m_x - 5.0 - m_borderSize, m_pointList[0].m_y - 5.0 - m_borderSize,
129 10 + 2.0 * m_borderSize, 10 + 2.0 * m_borderSize);
130
131 if(!m_parentList[0]->Intersects(nodeRect)) {
132 m_parentList[0]->RemoveChild(this);
133 m_parentList[0] = nullptr;
134 m_online = false;
135 UpdateSwitchesPosition();
136 UpdatePowerFlowArrowsPosition();
137 }
138 }
139}
140
141void Shunt::RotateNode(Element* parent, bool clockwise)
142{
143 double rotAngle = m_rotationAngle;
144 if(!clockwise) rotAngle = -m_rotationAngle;
145
146 if(parent == m_parentList[0]) {
147 m_pointList[0] = parent->RotateAtPosition(m_pointList[0], rotAngle);
148 UpdateSwitchesPosition();
149 UpdatePowerFlowArrowsPosition();
150 }
151}
152
153//void Shunt::DrawGround(wxPoint2DDouble position) const
154//{
155// std::vector<wxPoint2DDouble> groundPts;
156// groundPts.push_back(position);
157// groundPts.push_back(position + wxPoint2DDouble(0, 10));
158// groundPts.push_back(position + wxPoint2DDouble(-10, 10));
159// groundPts.push_back(position + wxPoint2DDouble(10, 10));
160// groundPts.push_back(position + wxPoint2DDouble(-6, 15));
161// groundPts.push_back(position + wxPoint2DDouble(6, 15));
162// groundPts.push_back(position + wxPoint2DDouble(-3, 20));
163// groundPts.push_back(position + wxPoint2DDouble(3, 20));
164//
165// DrawLine(groundPts, GL_LINES);
166//}
167
168void Shunt::DrawDCGround(wxPoint2DDouble position, wxGraphicsContext* gc) const
169{
170 std::vector<wxPoint2DDouble> groundPts;
171 groundPts.push_back(position);
172 groundPts.push_back(position + wxPoint2DDouble(0, 10));
173 groundPts.push_back(position + wxPoint2DDouble(-10, 10));
174 groundPts.push_back(position + wxPoint2DDouble(10, 10));
175 groundPts.push_back(position + wxPoint2DDouble(-6, 15));
176 groundPts.push_back(position + wxPoint2DDouble(6, 15));
177 groundPts.push_back(position + wxPoint2DDouble(-3, 20));
178 groundPts.push_back(position + wxPoint2DDouble(3, 20));
179
180 gc->StrokeLines(2, &groundPts[0]);
181 gc->StrokeLines(2, &groundPts[2]);
182 gc->StrokeLines(2, &groundPts[4]);
183 gc->StrokeLines(2, &groundPts[6]);
184}
185
186void Shunt::DrawDCGround(wxPoint2DDouble position, wxDC& dc) const
187{
188 wxPoint groundPts[8];
189 wxPoint2DDouble p;
190
191 p = position;
192 groundPts[0] = RotateAround(p, m_position, m_angle);
193 p = position + wxPoint2DDouble(0, 10);
194 groundPts[1] = RotateAround(p, m_position, m_angle);
195 p = position + wxPoint2DDouble(-10, 10);
196 groundPts[2] = RotateAround(p, m_position, m_angle);
197 p = position + wxPoint2DDouble(10, 10);
198 groundPts[3] = RotateAround(p, m_position, m_angle);
199 p = position + wxPoint2DDouble(-6, 15);
200 groundPts[4] = RotateAround(p, m_position, m_angle);
201 p = position + wxPoint2DDouble(6, 15);
202 groundPts[5] = RotateAround(p, m_position, m_angle);
203 p = position + wxPoint2DDouble(-3, 20);
204 groundPts[6] = RotateAround(p, m_position, m_angle);
205 p = position + wxPoint2DDouble(3, 20);
206 groundPts[7] = RotateAround(p, m_position, m_angle);
207
208 dc.DrawLines(2, &groundPts[0]);
209 dc.DrawLines(2, &groundPts[2]);
210 dc.DrawLines(2, &groundPts[4]);
211 dc.DrawLines(2, &groundPts[6]);
212}
213
214void Shunt::UpdatePowerFlowArrowsPosition()
215{
216 std::vector<wxPoint2DDouble> edges;
217 switch(m_pfDirection) {
219 m_powerFlowArrow.clear();
220 } break;
222 edges.push_back(m_pointList[2]);
223 edges.push_back(m_pointList[1]);
224 } break;
226 edges.push_back(m_pointList[1]);
227 edges.push_back(m_pointList[2]);
228 } break;
229 default:
230 break;
231 }
233}
Base class of all elements of the program. This class is responsible for manage graphical and his dat...
Definition Element.h:112
virtual bool Intersects(wxRect2DDouble rect) const =0
Check if the element's rect intersects other rect.
wxPoint2DDouble GetPosition() const
Get the element position.
Definition Element.h:186
double GetAngle() const
Get the element angle.
Definition Element.h:211
void SetPosition(const wxPoint2DDouble position)
Set the element position and update the rectangle.
Definition Element.cpp:27
virtual wxPoint2DDouble RotateAtPosition(wxPoint2DDouble pointToRotate, double angle, bool degrees=true) const
Rotate a point as element position being the origin.
Definition Element.cpp:292
Abstract class of power elements.
virtual void CalculatePowerFlowPts(std::vector< wxPoint2DDouble > edges)
Calculate the points of the power flow arrows.
virtual void UpdateSwitches()
Update the switch position.
virtual wxPoint2DDouble GetSwitchPoint(Element *parent, wxPoint2DDouble parentPoint, wxPoint2DDouble secondPoint) const
Get the correct switch position.
virtual void MoveNode(Element *element, wxPoint2DDouble position)
Move a node. StartMove(wxPoint2DDouble position) before start moving.
Definition Shunt.cpp:45
virtual void StartMove(wxPoint2DDouble position)
Update the element attributes related to the movement.
Definition Shunt.cpp:67
virtual void RemoveParent(Element *parent)
Remove a parent.
Definition Shunt.cpp:74
virtual void RotateNode(Element *parent, bool clockwise=true)
Rotate a node.
Definition Shunt.cpp:141
virtual void Move(wxPoint2DDouble position)
Move the element other position.
Definition Shunt.cpp:32
virtual bool SetNodeParent(Element *parent)
Set a perent to the node. If all conditions are met, a new parent are added to the element and the po...
Definition Shunt.cpp:98
virtual void UpdateNodes()
Update the nodes according to the parents. If a parent is removed, use this method.
Definition Shunt.cpp:125
virtual bool NodeContains(wxPoint2DDouble position)
Check if a node contains a point. If contains, set the attributes related to node movement.
Definition Shunt.cpp:84
virtual bool Intersects(wxRect2DDouble rect) const
Check if the element's rect intersects other rect.
Definition Shunt.h:38