Power System Platform  2026w10a-beta
Loading...
Searching...
No Matches
Element.h
Go to the documentation of this file.
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#ifndef ELEMENT_H
19#define ELEMENT_H
20
21#include <wx/msgdlg.h>
22#include <wx/geometry.h>
23#include <wx/cursor.h>
24#include <wx/menu.h>
25#include <wx/stdpaths.h>
26#include <wx/filename.h>
27//#include <GL/glew.h>
28#ifdef _MSC_VER
29 //#include <windows.h>
30#endif
31//#include <GL/gl.h>
32#include <wx/graphics.h>
33#include <wx/dc.h>
34
35#include <complex>
36
37#include "../utils/XMLParser.h"
38
39//#include <wx/log.h>
40
41enum ElementType {
42 TYPE_NONE = 0,
43 TYPE_BUS,
44 TYPE_CAPACITOR,
45 TYPE_IND_MOTOR,
46 TYPE_INDUCTOR,
47 TYPE_LINE,
48 TYPE_LOAD,
49 TYPE_SYNC_GENERATOR,
50 TYPE_SYNC_MOTOR,
51 TYPE_TRANSFORMER,
52 TYPE_HARMCURRENT,
53 TYPE_TEXT
54};
55
69
82
83enum TextID {
84 ID_TXT_NAME = 1000,
85 ID_TXT_VOLTAGE,
86 ID_TXT_ANGLE,
87 ID_TXT_FAULTCURRENT,
88 ID_TXT_FAULTVOLTAGE,
89 ID_TXT_SCC,
90 ID_TXT_THD,
91 ID_TXT_ACTIVE_POWER,
92 ID_TXT_REACTIVE_POWER,
93 ID_TXT_BRANCH_ACTIVE_POWER_1_2,
94 ID_TXT_BRANCH_ACTIVE_POWER_2_1,
95 ID_TXT_BRANCH_REACTIVE_POWER_1_2,
96 ID_TXT_BRANCH_REACTIVE_POWER_2_1,
97 ID_TXT_BRANCH_LOSSES,
98 ID_TXT_BRANCH_CURRENT_1_2,
99 ID_TXT_BRANCH_CURRENT_2_1,
100 ID_TXT_BRANCH_FAULT_CURRENT_1_2,
101 ID_TXT_BRANCH_FAULT_CURRENT_2_1
102};
112{
113public:
117 Element();
118
122 virtual ~Element() {}
123
128 void SetDragging(bool dragging = true) { m_dragging = dragging; }
133 void SetHeight(double height) { m_height = height; }
138 void SetPosition(const wxPoint2DDouble position);
139
144 void SetSelected(bool selected = true) { m_selected = selected; }
149 void SetWidth(double width) { m_width = width; }
154 void SetAngle(double angle) { m_angle = angle; }
159 void ShowPickbox(bool showPickbox = true) { m_showPickbox = showPickbox; }
164 void SetBorderSize(double borderSize) { m_borderSize = borderSize; }
169 bool SetOnline(bool online = true);
170
175 virtual void SetPointList(std::vector<wxPoint2DDouble> pointList) { m_pointList = pointList; }
176 ElementType GetElementType() { return m_elementType; }
181 wxRect2DDouble GetRect() const { return m_rect; }
186 wxPoint2DDouble GetPosition() const { return m_position; }
191 bool IsDragging() const { return m_dragging; }
196 double GetHeight() const { return m_height; }
201 bool IsSelected() const { return m_selected; }
206 double GetWidth() const { return m_width; }
211 double GetAngle() const { return m_angle; }
216 double GetRotationAngle() const { return m_rotationAngle; }
221 bool IsPickboxShown() const { return m_showPickbox; }
226 bool IsOnline() const { return m_online; }
231 virtual std::vector<wxPoint2DDouble> GetPointList() const { return m_pointList; }
239 virtual bool AddParent(Element* parent, wxPoint2DDouble position) { return false; }
244 virtual void AddParent(Element* parent) { m_parentList.push_back(parent); }
249 virtual bool Contains(wxPoint2DDouble position) const = 0;
250
255 virtual bool Intersects(wxRect2DDouble rect) const = 0;
256
261 virtual Element* GetCopy() { return nullptr; }
266 virtual void SetID(int id) { m_elementID = id; }
271 virtual int GetID() const { return m_elementID; }
276 virtual void AddChild(Element* child);
277
282 virtual void RemoveChild(Element* child);
283
289 virtual void ReplaceChild(Element* oldChild, Element* newChild);
290
295 virtual wxString GetTipText() const { return wxEmptyString; }
296
297 //virtual void Draw(wxPoint2DDouble translation, double scale) const {}
304 virtual void DrawDC(wxPoint2DDouble translation, double scale, wxGraphicsContext* gc) const {}
311 virtual void DrawDC(wxPoint2DDouble translation, double scale, wxDC& dc) const {}
316 virtual void Rotate(bool clockwise = true) {}
322 virtual bool GetContextMenu(wxMenu& menu) { return false; }
327 virtual void AddPoint(wxPoint2DDouble point) {}
332 virtual void StartMove(wxPoint2DDouble position);
333
339 virtual void Move(wxPoint2DDouble position);
340
346 virtual void MoveNode(Element* parent, wxPoint2DDouble position) {}
352 virtual bool NodeContains(wxPoint2DDouble position) { return false; }
356 virtual void UpdateNodes() {}
357
358 void SetParent(Element* parent, int num) { m_parentList[num] = parent; }
359 void SetChild(Element* child, int num) { m_childList[num] = child; }
366 virtual bool SetNodeParent(Element* parent) { return false; }
371 virtual void RemoveParent(Element* parent) {}
377 virtual void ReplaceParent(Element* oldParent, Element* newParent);
378
384 virtual void RotateNode(Element* parent, bool clockwise = true) {}
391 virtual bool PickboxContains(wxPoint2DDouble position) { return false; }
396 virtual void MovePickbox(wxPoint2DDouble position) {}
401 virtual wxCursor GetBestPickboxCursor() const { return wxCURSOR_ARROW; }
405 virtual void ResetPickboxes() { m_activePickboxID = ID_PB_NONE; }
409 virtual void ResetNodes() { m_activeNodeID = 0; }
417 virtual wxPoint2DDouble WorldToScreen(wxPoint2DDouble translation,
418 double scale,
419 double offsetX = 0.0,
420 double offsetY = 0.0) const;
421
430 virtual wxPoint2DDouble WorldToScreen(wxPoint2DDouble position,
431 wxPoint2DDouble translation,
432 double scale,
433 double offsetX = 0.0,
434 double offsetY = 0.0) const;
435
443 virtual bool RotatedRectanglesIntersects(wxRect2DDouble rect1,
444 wxRect2DDouble rect2,
445 double angle1,
446 double angle2) const;
447
455 //virtual void DrawCircle(wxPoint2DDouble position, double radius, int numSegments, GLenum mode = GL_LINE_LOOP) const;
456
457 virtual void DrawDCRectangle(wxPoint2DDouble position, double width, double height, double angle, wxDC& dc) const;
458
459 virtual void DrawDCRoundedRectRotated(wxDC& dc, const wxPoint2DDouble& center, double width, double height, double radius, double angleDeg, int arcSegments = 8) const;
460
468 virtual void DrawDCCircle(wxPoint2DDouble position, double radius, int numSegments, wxGraphicsContext* gc) const;
469
470 virtual void DrawDCCircle(wxPoint2DDouble position, double radius, wxDC& dc) const;
471
472 //virtual void DrawArc(wxPoint2DDouble position,
473 // double radius,
474 // double initAngle,
475 // double finalAngle,
476 // int numSegments,
477 // GLenum mode = GL_LINE_LOOP) const;
478
479 virtual void DrawDCArc(wxPoint2DDouble position, double radius, double initAngle, double finalAngle, int numSegments, wxGraphicsContext* gc) const;
480
481 virtual void DrawDCArc(wxPoint2DDouble position, double radius, double initAngle, double finalAngle, wxDC& dc) const;
482
490 //virtual void DrawRectangle(wxPoint2DDouble position, double width, double height, GLenum mode = GL_QUADS) const;
491 //virtual void DrawRectangle(wxPoint2DDouble position, double width, double height) const;
492
498 //virtual void DrawRectangle(wxPoint2DDouble* points, GLenum mode = GL_QUADS) const;
499
505 //virtual void DrawTriangle(std::vector<wxPoint2DDouble> points, GLenum mode = GL_TRIANGLES) const;
506
512 virtual void DrawDCTriangle(std::vector<wxPoint2DDouble> points, wxGraphicsContext* gc) const;
513
514 virtual void DrawDCTriangle(std::vector<wxPoint> points, wxDC& dc) const;
515
521 //virtual void DrawPoint(wxPoint2DDouble position, double size) const;
522
528 //virtual void DrawLine(std::vector<wxPoint2DDouble> points, GLenum mode = GL_LINE_STRIP) const;
529
534 //virtual void DrawPickbox(wxPoint2DDouble position) const;
535
541 virtual void DrawDCPickbox(wxPoint2DDouble position, wxGraphicsContext* gc) const;
542
549 virtual wxPoint2DDouble RotateAtPosition(wxPoint2DDouble pointToRotate, double angle, bool degrees = true) const;
550
551 virtual wxPoint2DDouble RotateLocal(wxPoint2DDouble local, double angleDeg) const;
552
553 virtual wxPoint RotateAround(const wxPoint2DDouble& p, const wxPoint2DDouble& center, double angleDeg) const;
554
559 virtual std::vector<Element*> GetParentList() const { return m_parentList; }
564 virtual std::vector<Element*> GetChildList() const { return m_childList; }
565 // virtual wxPoint2DDouble GetMoveStartPosition() const { return m_moveStartPt; }
566 // virtual wxPoint2DDouble GetMovePosition() const { return m_movePos; }
567
573 virtual void CalculateBoundaries(wxPoint2DDouble& leftUp, wxPoint2DDouble& rightBottom) const;
574
579 virtual void GeneralMenuItens(wxMenu& menu);
580
587 virtual bool ShowForm(wxWindow* parent, Element* element) { return false; }
595 static bool DoubleFromString(wxWindow* parent, wxString strValue, double& value, wxString errorMsg);
596
604 static bool IntFromString(wxWindow* parent, wxString strValue, int& value, wxString errorMsg);
605
611 static wxString StringFromDouble(double value, int minDecimal = 1, int maxDecimals = 13);
612
619 virtual double PointToLineDistance(wxPoint2DDouble point, int* segmentNumber = nullptr) const;
620
625 bool IsInserted() const { return m_inserted; }
626
631 void SetInserted(bool inserted = true) { m_inserted = inserted; }
632
633 virtual rapidxml::xml_node<>* SaveElement(rapidxml::xml_document<>& doc, rapidxml::xml_node<>* elementListNode) { return nullptr; }
634 virtual bool OpenElement(rapidxml::xml_node<>* elementNode) { return true; }
635 void SaveCADProperties(rapidxml::xml_document<>& doc, rapidxml::xml_node<>* elementNode);
636 bool OpenCADProperties(rapidxml::xml_node<>* elementNode);
637
638protected:
639 ElementType m_elementType = ElementType::TYPE_NONE;
640 int m_elementID = 0;
641 std::vector<Element*> m_parentList;
642 std::vector<Element*> m_childList;
643
644 wxRect2DDouble m_rect;
645 wxPoint2DDouble m_position;
646 double m_width = 0.0;
647 double m_height = 0.0;
648 double m_angle = 0.0;
649 double m_borderSize = 2.0;
650 double m_rotationAngle = 45.0;
651 double m_switchSize = 10.0;
652
653 std::vector<wxRect2DDouble> m_switchRect;
654
655 bool m_selected = false;
656 bool m_dragging = false;
657 bool m_showPickbox = false;
658 bool m_inserted = false;
659
660 int m_activePickboxID = ID_PB_NONE;
661 int m_activeNodeID = 0;
662
663 std::vector<wxPoint2DDouble> m_pointList;
664 std::vector<wxPoint2DDouble> m_movePts;
665
666 wxPoint2DDouble m_moveStartPt;
667 wxPoint2DDouble m_movePos;
668
669 bool m_online = true;
670
671 //OpenGLColour m_selectionColour;
672 wxColour m_selectionColour;
673};
674
675#endif // ELEMENT_H
ContextMenuID
ID of contex menu itens.
Definition Element.h:74
@ ID_LINE_REMOVE_NODE
Definition Element.h:77
@ ID_DELETE
Definition Element.h:80
@ ID_ROTATE_CLOCK
Definition Element.h:78
@ ID_EDIT_ELEMENT
Definition Element.h:75
@ ID_ROTATE_COUNTERCLOCK
Definition Element.h:79
@ ID_LINE_ADD_NODE
Definition Element.h:76
PickboxID
ID of the pickbox.
Definition Element.h:60
@ ID_PB_NONE
Definition Element.h:61
@ ID_PB_RIGHT_TOP
Definition Element.h:65
@ ID_PB_LEFT_TOP
Definition Element.h:67
@ ID_PB_RIGHT_BOTTOM
Definition Element.h:64
@ ID_PB_LEFT
Definition Element.h:63
@ ID_PB_LEFT_BOTTOM
Definition Element.h:66
@ ID_PB_RIGHT
Definition Element.h:62
Base class of all elements of the program. This class is responsible for manage graphical and his dat...
Definition Element.h:112
virtual bool RotatedRectanglesIntersects(wxRect2DDouble rect1, wxRect2DDouble rect2, double angle1, double angle2) const
Check if two roteted rectangles intersect.
Definition Element.cpp:359
virtual bool Intersects(wxRect2DDouble rect) const =0
Check if the element's rect intersects other rect.
virtual Element * GetCopy()
Get a the element copy.
Definition Element.h:261
virtual std::vector< Element * > GetParentList() const
Get the parent list.
Definition Element.h:559
virtual int GetID() const
Get the element ID.
Definition Element.h:271
double GetWidth() const
Get the element width.
Definition Element.h:206
virtual wxCursor GetBestPickboxCursor() const
Get the best cursor to shown to the user when the mouse is above a pickbox.
Definition Element.h:401
virtual bool GetContextMenu(wxMenu &menu)
Get the element contex menu.
Definition Element.h:322
bool IsDragging() const
Checks if the element is being dragged.
Definition Element.h:191
virtual void GeneralMenuItens(wxMenu &menu)
Insert general itens to context menu.
Definition Element.cpp:457
double GetRotationAngle() const
Get the angle of rotation.
Definition Element.h:216
virtual void ResetPickboxes()
Remove the pickboxes.
Definition Element.h:405
virtual void DrawDC(wxPoint2DDouble translation, double scale, wxDC &dc) const
Draw the element using wxDC.
Definition Element.h:311
virtual void UpdateNodes()
Update the nodes according to the parents. If a parent is removed, use this method.
Definition Element.h:356
virtual std::vector< wxPoint2DDouble > GetPointList() const
Get the list of points that connect the element to bus.
Definition Element.h:231
static bool IntFromString(wxWindow *parent, wxString strValue, int &value, wxString errorMsg)
Convert a string to int. Show a error message if the conversion fail.
Definition Element.cpp:519
virtual double PointToLineDistance(wxPoint2DDouble point, int *segmentNumber=nullptr) const
Calculate the distance between a line (formed by point list) and a point.
Definition Element.cpp:601
void SetSelected(bool selected=true)
Set element selection.
Definition Element.h:144
wxPoint2DDouble GetPosition() const
Get the element position.
Definition Element.h:186
virtual bool AddParent(Element *parent, wxPoint2DDouble position)
Add a parent to the element. This method must be used on power elements that connect to a bus,...
Definition Element.h:239
bool IsOnline() const
Checks if the element is online or offline.
Definition Element.h:226
virtual void CalculateBoundaries(wxPoint2DDouble &leftUp, wxPoint2DDouble &rightBottom) const
Calculate the element boundaries.
Definition Element.cpp:478
virtual std::vector< Element * > GetChildList() const
Get the Child list.
Definition Element.h:564
double GetAngle() const
Get the element angle.
Definition Element.h:211
void SetWidth(double width)
Set element width.
Definition Element.h:149
double GetHeight() const
Get the element height.
Definition Element.h:196
virtual void RemoveChild(Element *child)
Remove a child from the list.
Definition Element.cpp:567
virtual void DrawDC(wxPoint2DDouble translation, double scale, wxGraphicsContext *gc) const
Draw the element using GDI+.
Definition Element.h:304
virtual void MovePickbox(wxPoint2DDouble position)
Move the pickbox.
Definition Element.h:396
virtual void ReplaceParent(Element *oldParent, Element *newParent)
Replace a parent.
Definition Element.cpp:559
virtual void DrawDCRectangle(wxPoint2DDouble position, double width, double height, double angle, wxDC &dc) const
Draw a circle.
Definition Element.cpp:45
virtual void StartMove(wxPoint2DDouble position)
Update the element attributes related to the movement.
Definition Element.cpp:329
virtual void MoveNode(Element *parent, wxPoint2DDouble position)
Move a node. StartMove(wxPoint2DDouble position) before start moving.
Definition Element.h:346
void SetHeight(double height)
Set element height.
Definition Element.h:133
virtual void SetPointList(std::vector< wxPoint2DDouble > pointList)
Set the list of points that connect the element to the bus.
Definition Element.h:175
virtual void ResetNodes()
Remove the active nodes.
Definition Element.h:409
void SetPosition(const wxPoint2DDouble position)
Set the element position and update the rectangle.
Definition Element.cpp:27
virtual void DrawDCTriangle(std::vector< wxPoint2DDouble > points, wxGraphicsContext *gc) const
Draw rectangle.
Definition Element.cpp:237
virtual void RemoveParent(Element *parent)
Remove a parent.
Definition Element.h:371
virtual bool NodeContains(wxPoint2DDouble position)
Check if a node contains a point. If contains, set the attributes related to node movement.
Definition Element.h:352
virtual wxPoint2DDouble RotateAtPosition(wxPoint2DDouble pointToRotate, double angle, bool degrees=true) const
Rotate a point as element position being the origin.
Definition Element.cpp:292
virtual void SetID(int id)
Set the element ID.
Definition Element.h:266
virtual void DrawDCPickbox(wxPoint2DDouble position, wxGraphicsContext *gc) const
Draw a point.
Definition Element.cpp:285
virtual void AddPoint(wxPoint2DDouble point)
Add point to the list of points that connect the element to the bus.
Definition Element.h:327
void SetAngle(double angle)
Set element angle.
Definition Element.h:154
virtual wxString GetTipText() const
Get the tip text.
Definition Element.h:295
Element()
Constructor.
Definition Element.cpp:26
wxRect2DDouble GetRect() const
Get the element rectangle.
Definition Element.h:181
virtual ~Element()
Destructor.
Definition Element.h:122
void ShowPickbox(bool showPickbox=true)
Set if the pickbox is shown.
Definition Element.h:159
bool IsPickboxShown() const
Checks if the pickbox is shown.
Definition Element.h:221
virtual void Move(wxPoint2DDouble position)
Move the element other position.
Definition Element.cpp:335
virtual bool Contains(wxPoint2DDouble position) const =0
Checks if the element contains a position.
virtual wxPoint2DDouble WorldToScreen(wxPoint2DDouble translation, double scale, double offsetX=0.0, double offsetY=0.0) const
Convert the element position to screen position.
Definition Element.cpp:336
virtual void AddChild(Element *child)
Add a child to the child list.
Definition Element.cpp:566
bool IsSelected() const
Checks if the element is selected.
Definition Element.h:201
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 Element.h:366
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
virtual void AddParent(Element *parent)
Add a parent to the element.
Definition Element.h:244
static wxString StringFromDouble(double value, int minDecimal=1, int maxDecimals=13)
Convert a double value to string.
Definition Element.cpp:533
virtual void ReplaceChild(Element *oldChild, Element *newChild)
Replace a child from the list.
Definition Element.cpp:575
void SetDragging(bool dragging=true)
Set if the element are being dragged.
Definition Element.h:128
virtual bool ShowForm(wxWindow *parent, Element *element)
Show element data form.
Definition Element.h:587
bool SetOnline(bool online=true)
Set if the element is online or offline.
Definition Element.cpp:447
bool IsInserted() const
Check if the element is properly inserted in the workspace.
Definition Element.h:625
virtual void DrawDCCircle(wxPoint2DDouble position, double radius, int numSegments, wxGraphicsContext *gc) const
Draw a circle using device context.
Definition Element.cpp:177
virtual void RotateNode(Element *parent, bool clockwise=true)
Rotate a node.
Definition Element.h:384
virtual bool PickboxContains(wxPoint2DDouble position)
Check if a pickbox contains a point. If contains the attributes related to pickbox movement will be c...
Definition Element.h:391
virtual void Rotate(bool clockwise=true)
Rotate the element.
Definition Element.h:316
void SetInserted(bool inserted=true)
Set if the element is properly inserted in the workspace.
Definition Element.h:631
void SetBorderSize(double borderSize)
Set the size of the border (shown in selected elements).
Definition Element.h:164