agrobot_base/AgroBase/AgroBase/Forms/Direcional/frmDirPlot.cs

203 lines
8.3 KiB
C#

using AgroBase.Models;
using AgroBase.Models.Modules;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static AgroBase.Models.Enuns;
namespace AgroBase.Forms.Direcional
{
public partial class frmDirPlot : Form
{
public frmDirPlot()
{
InitializeComponent();
}
public void AtualizaGrafico()
{
picET.Invalidate();
picEF.Invalidate();
picDT.Invalidate();
picDF.Invalidate();
}
private void pic_Paint(object sender, PaintEventArgs e)
{
DesenharGraficos(sender, e);
}
private void DesenharGraficos(object sender, PaintEventArgs e)
{
PictureBox pic = (PictureBox)sender;
var Motor = ((DirecionalModel)Variaveis.DispositivosConectados.FirstOrDefault(d => d.Dispositivo == T_Code.Dir).Dados).Motores.FirstOrDefault(m => m.ID == pic.Name.Replace("pic", ""));
Label lbl = Controls.OfType<Label>().FirstOrDefault(ctrl => ctrl.Name == pic.Name.Replace("pic", "lbl"));
lbl.Text = "SP: " + ((Motor.Sentido_SP == Sentido.Antihorario ? -1 : 1) * Motor.Angulo_SP).ToString("00") + "º - Angulo: " + Motor.Angulo.ToString("00") + "º - Sentido: " + Enum.GetName(typeof(Sentido), Motor.Sentido);
// Use o objeto Graphics para desenhar a representação do motor
Graphics g = e.Graphics;
int centerX = pic.Width / 2;
int centerY = pic.Height / 2;
int radius = Math.Min(centerX, centerY) - 30;
// Desenhe o círculo para representar o motor
g.DrawEllipse(Pens.Black, centerX - radius, centerY - radius, 2 * radius, 2 * radius);
// Desenhe a linha externa (set point)
double setPointAngleInRadians = (Motor.Sentido_SP == Sentido.Antihorario ? -1 : 1) * ((Motor.Angulo_SP * Math.PI) / 180);
int setPointX = centerX + (int)((radius + 10) * Math.Sin(setPointAngleInRadians));
int setPointY = centerY - (int)((radius + 10) * Math.Cos(setPointAngleInRadians));
g.DrawLine(Pens.Green, centerX, centerY, setPointX, setPointY);
// Desenhe a seta que indica o sentido
int arrowLength = 15; // Comprimento da seta
if (Motor.Sentido_SP == Sentido.Horario)
{
// Desenhe uma seta para a direita (sentido horário)
Point arrowStart = new Point(setPointX, setPointY);
Point arrowEnd = new Point(setPointX + arrowLength, setPointY);
g.DrawLine(Pens.Green, arrowStart, arrowEnd);
// Desenhe a "ponta" da seta (triângulo) para a direita
Point arrowPoint1 = new Point(setPointX + arrowLength - 5, setPointY - 5);
Point arrowPoint2 = new Point(setPointX + arrowLength - 5, setPointY + 5);
g.DrawLine(Pens.Green, arrowEnd, arrowPoint1);
g.DrawLine(Pens.Green, arrowEnd, arrowPoint2);
}
else
{
// Desenhe uma seta para a esquerda (sentido anti-horário)
Point arrowStart = new Point(setPointX, setPointY);
Point arrowEnd = new Point(setPointX - arrowLength, setPointY);
g.DrawLine(Pens.Green, arrowStart, arrowEnd);
// Desenhe a "ponta" da seta (triângulo) para a esquerda
Point arrowPoint1 = new Point(setPointX - arrowLength + 5, setPointY - 5);
Point arrowPoint2 = new Point(setPointX - arrowLength + 5, setPointY + 5);
g.DrawLine(Pens.Green, arrowEnd, arrowPoint1);
g.DrawLine(Pens.Green, arrowEnd, arrowPoint2);
}
// Desenhe uma linha para representar a posição do eixo do motor
double angleInRadians = (Motor.Angulo * Math.PI) / 180;
int x = centerX + (int)(radius * Math.Sin(angleInRadians));
int y = centerY - (int)(radius * Math.Cos(angleInRadians));
g.DrawLine(Pens.Red, centerX, centerY, x, y);
// Desenhe a seta que indica o sentido
if (Motor.Sentido == Sentido.Horario)
{
// Desenhe uma seta para a direita (sentido horário)
Point arrowStart = new Point(x, y);
Point arrowEnd = new Point(x + arrowLength, y);
g.DrawLine(Pens.Red, arrowStart, arrowEnd);
// Desenhe a "ponta" da seta (triângulo) para a direita
Point arrowPoint1 = new Point(x + arrowLength - 5, y - 5);
Point arrowPoint2 = new Point(x + arrowLength - 5, y + 5);
g.DrawLine(Pens.Red, arrowEnd, arrowPoint1);
g.DrawLine(Pens.Red, arrowEnd, arrowPoint2);
}
else
{
// Desenhe uma seta para a esquerda (sentido anti-horário)
Point arrowStart = new Point(x, y);
Point arrowEnd = new Point(x - arrowLength, y);
g.DrawLine(Pens.Red, arrowStart, arrowEnd);
// Desenhe a "ponta" da seta (triângulo) para a esquerda
Point arrowPoint1 = new Point(x - arrowLength + 5, y - 5);
Point arrowPoint2 = new Point(x - arrowLength + 5, y + 5);
g.DrawLine(Pens.Red, arrowEnd, arrowPoint1);
g.DrawLine(Pens.Red, arrowEnd, arrowPoint2);
}
// Desenhe os pontos nos angulos fixos
int radiusCircle = 5;
for (int i = 0; i < 4; i++)
{
double angle = ((i * 90) * Math.PI) / 180;
int xA = centerX + (int)((radius) * Math.Sin(angle)); // Adicione 20 para afastar do círculo principal
int yA = centerY - (int)((radius) * Math.Cos(angle)); // Adicione 20 para afastar do círculo principal
Rectangle rect = new Rectangle(xA - radiusCircle, yA - radiusCircle, 2 * radiusCircle, 2 * radiusCircle);
bool Acionado =
Motor.Ref000 && i == 0 ||
Motor.Ref090 && i == 1 ||
Motor.Ref180 && i == 2 ||
Motor.Ref270 && i == 3;
using (SolidBrush brush = new SolidBrush(Acionado ? Color.Red : Color.DarkRed))
{
g.FillEllipse(brush, rect);
}
string angleText = ((i * 90).ToString() + "°");
SizeF textSize = g.MeasureString(angleText, Font);
float textX = xA;
float textY = yA;
float textOffset = 12;
// Ajuste as coordenadas do texto com base no ângulo
if (angle == 0)
{
textX -= textSize.Width / 2;
textY -= textSize.Height - (textOffset * 2);
}
else if (angle == Math.PI / 2)
{
textX -= textSize.Width + textOffset;
textY -= textSize.Height / 2;
}
else if (angle == Math.PI)
{
textX -= textSize.Width / 2;
textY -= (textOffset * 2);
}
else if (angle == 3 * Math.PI / 2)
{
textX += textOffset;
textY -= textSize.Height / 2;
}
g.DrawString(angleText, Font, Brushes.Black, textX, textY);
}
}
private void frmDirPlot_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.Hide();
}
private void btnReferenciar_Click(object sender, EventArgs e)
{
string ID = ((Button)sender).Name.Substring(3, 2);
var DispCon = Variaveis.DispositivosConectados.Where(x => x._Porta.IsOpen && x.Dispositivo == T_Code.Dir).ToList();
foreach (var Dispositivo in DispCon)
{
var Motor = ((DirecionalModel)Dispositivo.Dados).Motores.FirstOrDefault(x => x.ID == ID);
string Comando = Motor.ProtocoloComando(Direcao.Parado, true);
Dispositivo.EnviarDadosSerial(Comando);
}
}
}
}