2024-05-08 08:41:26 -03:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using System;
|
2024-05-04 06:00:52 -03:00
|
|
|
|
using System.Collections.Generic;
|
2024-05-04 15:35:06 -03:00
|
|
|
|
using System.ComponentModel;
|
2024-05-08 06:04:46 -03:00
|
|
|
|
using System.Globalization;
|
2024-05-04 06:00:52 -03:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
using System.Threading.Tasks;
|
2024-05-10 20:40:51 -03:00
|
|
|
|
using System.Windows;
|
2024-05-04 06:00:52 -03:00
|
|
|
|
using System.Windows.Controls;
|
2024-05-08 06:04:46 -03:00
|
|
|
|
using System.Windows.Data;
|
2024-05-04 15:35:06 -03:00
|
|
|
|
using static System.Runtime.InteropServices.JavaScript.JSType;
|
2024-05-11 11:58:55 -03:00
|
|
|
|
using CtrEditor.Convertidores;
|
2024-05-11 15:55:44 -03:00
|
|
|
|
using CtrEditor.Siemens;
|
2024-05-14 03:15:54 -03:00
|
|
|
|
using CtrEditor.Simulacion;
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using FarseerPhysics.Dynamics;
|
2024-05-04 06:00:52 -03:00
|
|
|
|
|
|
|
|
|
namespace CtrEditor.ObjetosSim
|
|
|
|
|
{
|
|
|
|
|
|
2024-05-08 08:41:26 -03:00
|
|
|
|
|
|
|
|
|
|
2024-05-04 06:00:52 -03:00
|
|
|
|
public interface IosBase
|
|
|
|
|
{
|
|
|
|
|
string Nombre { get; }
|
2024-05-06 12:31:45 -03:00
|
|
|
|
|
|
|
|
|
void UpdateControl();
|
2024-05-04 06:00:52 -03:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-04 15:35:06 -03:00
|
|
|
|
public interface IDataContainer
|
|
|
|
|
{
|
|
|
|
|
osBase? Datos { get; set; }
|
2024-05-06 12:31:45 -03:00
|
|
|
|
void Resize(float width, float height);
|
|
|
|
|
void Move(float Left, float Top);
|
|
|
|
|
void Rotate(float Angle);
|
2024-05-04 16:27:04 -03:00
|
|
|
|
void Highlight(bool State);
|
2024-05-08 03:00:31 -03:00
|
|
|
|
int ZIndex();
|
2024-05-04 15:35:06 -03:00
|
|
|
|
}
|
2024-05-04 06:00:52 -03:00
|
|
|
|
|
2024-05-04 15:35:06 -03:00
|
|
|
|
public abstract class osBase : INotifyPropertyChanged, IosBase
|
2024-05-08 03:00:31 -03:00
|
|
|
|
{
|
2024-05-06 12:31:45 -03:00
|
|
|
|
public abstract float Left { get; set; }
|
|
|
|
|
public abstract float Top { get; set; }
|
2024-05-04 15:35:06 -03:00
|
|
|
|
|
|
|
|
|
public bool Inicializado = false;
|
2024-05-08 03:00:31 -03:00
|
|
|
|
|
2024-05-06 12:31:45 -03:00
|
|
|
|
protected UserControl? _visualRepresentation = null;
|
2024-05-04 06:00:52 -03:00
|
|
|
|
|
2024-05-08 03:00:31 -03:00
|
|
|
|
public abstract string Nombre { get; set; }
|
2024-05-04 15:35:06 -03:00
|
|
|
|
|
2024-05-10 20:40:51 -03:00
|
|
|
|
public abstract void UpdateControl();
|
2024-05-14 03:15:54 -03:00
|
|
|
|
public abstract void UpdateGeometryStart();
|
|
|
|
|
public abstract void UpdateGeometryStep();
|
2024-05-11 15:55:44 -03:00
|
|
|
|
public abstract void UpdatePLC(PLCModel plc);
|
2024-05-14 03:15:54 -03:00
|
|
|
|
public abstract void ucLoaded();
|
2024-05-04 15:35:06 -03:00
|
|
|
|
|
2024-05-04 06:00:52 -03:00
|
|
|
|
[JsonIgnore]
|
|
|
|
|
public UserControl? VisualRepresentation
|
|
|
|
|
{
|
|
|
|
|
get => _visualRepresentation;
|
|
|
|
|
set => _visualRepresentation = value;
|
|
|
|
|
}
|
2024-05-14 03:15:54 -03:00
|
|
|
|
[JsonIgnore]
|
|
|
|
|
public SimulationManagerFP simulationManager;
|
|
|
|
|
|
2024-05-08 03:00:31 -03:00
|
|
|
|
|
2024-05-08 08:41:26 -03:00
|
|
|
|
public void CanvasSetLeftinMeter(float left)
|
|
|
|
|
{
|
|
|
|
|
if (_visualRepresentation != null)
|
2024-05-10 20:40:51 -03:00
|
|
|
|
Canvas.SetLeft(_visualRepresentation, PixelToMeter.Instance.calc.MetersToPixels(left));
|
2024-05-08 08:41:26 -03:00
|
|
|
|
}
|
2024-05-10 20:40:51 -03:00
|
|
|
|
public float CanvasGetLeftinMeter()
|
|
|
|
|
{
|
|
|
|
|
if (_visualRepresentation != null)
|
|
|
|
|
return PixelToMeter.Instance.calc.PixelsToMeters((float)Canvas.GetLeft(_visualRepresentation));
|
|
|
|
|
else return 0f;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-08 08:41:26 -03:00
|
|
|
|
public void CanvasSetTopinMeter(float top)
|
|
|
|
|
{
|
|
|
|
|
if (_visualRepresentation != null)
|
|
|
|
|
Canvas.SetTop(_visualRepresentation, PixelToMeter.Instance.calc.MetersToPixels(top));
|
|
|
|
|
}
|
2024-05-10 20:40:51 -03:00
|
|
|
|
public float CanvasGetTopinMeter()
|
|
|
|
|
{
|
|
|
|
|
if (_visualRepresentation != null)
|
|
|
|
|
return PixelToMeter.Instance.calc.PixelsToMeters((float)Canvas.GetTop(_visualRepresentation));
|
|
|
|
|
else return 0f;
|
|
|
|
|
}
|
2024-05-08 08:41:26 -03:00
|
|
|
|
|
2024-05-14 03:15:54 -03:00
|
|
|
|
public (Vector2 TopLeft, Vector2 BottomRight) GetRectangleCoordinatesInMeter(System.Windows.Shapes.Rectangle rect)
|
|
|
|
|
{
|
|
|
|
|
if (rect != null)
|
|
|
|
|
{
|
|
|
|
|
var _canvasLeft = CanvasGetLeftinMeter();
|
|
|
|
|
var _canvasTop = CanvasGetTopinMeter();
|
|
|
|
|
|
|
|
|
|
// Obtiene la transformada del objeto visual
|
|
|
|
|
GeneralTransform transform = rect.TransformToAncestor(_visualRepresentation);
|
|
|
|
|
|
|
|
|
|
// Obtiene la posición absoluta
|
|
|
|
|
Point topLeft = transform.Transform(new Point(0, 0));
|
|
|
|
|
Point bottomRight = transform.Transform(new Point(rect.ActualWidth, rect.ActualHeight));
|
|
|
|
|
|
|
|
|
|
return (new Vector2(PixelToMeter.Instance.calc.PixelsToMeters((float)topLeft.X) + _canvasLeft, PixelToMeter.Instance.calc.PixelsToMeters((float)topLeft.Y) + _canvasTop),
|
|
|
|
|
new Vector2(PixelToMeter.Instance.calc.PixelsToMeters((float)bottomRight.X) + _canvasLeft, PixelToMeter.Instance.calc.PixelsToMeters((float)bottomRight.Y) + _canvasTop));
|
|
|
|
|
}
|
|
|
|
|
else return (new Vector2(0, 0), new Vector2(0, 0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public (Vector2 Start, Vector2 End) GetCenterLineVectors(System.Windows.Shapes.Rectangle rect)
|
|
|
|
|
{
|
|
|
|
|
if (rect == null)
|
|
|
|
|
return (new Vector2(0, 0), new Vector2(0, 0));
|
|
|
|
|
|
|
|
|
|
var _canvasLeft = CanvasGetLeftinMeter();
|
|
|
|
|
var _canvasTop = CanvasGetTopinMeter();
|
|
|
|
|
|
|
|
|
|
var transform = rect.TransformToAncestor(_visualRepresentation);
|
|
|
|
|
|
|
|
|
|
// Puntos en coordenadas locales del rectángulo no rotado
|
|
|
|
|
Point startLocal = new Point(0, rect.ActualHeight / 2);
|
|
|
|
|
Point endLocal = new Point(rect.ActualWidth, rect.ActualHeight / 2);
|
|
|
|
|
|
|
|
|
|
// Transformar estos puntos al sistema de coordenadas del ancestro
|
|
|
|
|
Point transformedStart = transform.Transform(startLocal);
|
|
|
|
|
Point transformedEnd = transform.Transform(endLocal);
|
|
|
|
|
|
|
|
|
|
// Convierte a unidades de Farseer (metros en este caso)
|
|
|
|
|
Vector2 start = new Vector2(PixelToMeter.Instance.calc.PixelsToMeters((float)transformedStart.X) + _canvasLeft, PixelToMeter.Instance.calc.PixelsToMeters((float)transformedStart.Y) + _canvasTop);
|
|
|
|
|
Vector2 end = new Vector2(PixelToMeter.Instance.calc.PixelsToMeters((float)transformedEnd.X) + _canvasLeft, PixelToMeter.Instance.calc.PixelsToMeters((float)transformedEnd.Y) + _canvasTop);
|
|
|
|
|
|
|
|
|
|
return (start, end);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Vector2 GetRectangleCenter(System.Windows.Shapes.Rectangle wpfRect)
|
|
|
|
|
{
|
|
|
|
|
var coords = GetRectangleCoordinatesInMeter(wpfRect);
|
|
|
|
|
|
|
|
|
|
Vector2 topLeft = coords.TopLeft;
|
|
|
|
|
Vector2 bottomRight = coords.BottomRight;
|
|
|
|
|
|
|
|
|
|
// Calcular el centro, ancho y alto en metros
|
|
|
|
|
return new Vector2((topLeft.X + bottomRight.X) / 2, (topLeft.Y + bottomRight.Y) / 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateRectangle(simRectangle simRect, System.Windows.Shapes.Rectangle wpfRect, float Alto, float Ancho, float Angulo)
|
|
|
|
|
{
|
|
|
|
|
if (simRect != null)
|
|
|
|
|
simRect.Create(Ancho, Alto, GetRectangleCenter(wpfRect), Angulo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public simRectangle AddRectangle(SimulationManagerFP simulationManager, System.Windows.Shapes.Rectangle wpfRect, float Alto, float Ancho, float Angulo)
|
|
|
|
|
{
|
|
|
|
|
return simulationManager.AddRectangle(Ancho, Alto, GetRectangleCenter(wpfRect), Angulo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateOrCreateLine(simLine simGuia, System.Windows.Shapes.Rectangle wpfRect)
|
|
|
|
|
{
|
|
|
|
|
if (simGuia != null)
|
|
|
|
|
{
|
|
|
|
|
var coords = GetCenterLineVectors(wpfRect);
|
|
|
|
|
|
|
|
|
|
// Crear o actualizar simRectangle
|
|
|
|
|
simGuia.Create( coords.Start,coords.End); // asumiendo que el ángulo inicial es 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public simLine AddLine(SimulationManagerFP simulationManager, System.Windows.Shapes.Rectangle wpfRect)
|
|
|
|
|
{
|
|
|
|
|
var coords = GetCenterLineVectors(wpfRect);
|
|
|
|
|
return simulationManager.AddLine(coords.Start, coords.End);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-08 08:41:26 -03:00
|
|
|
|
|
2024-05-04 15:35:06 -03:00
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
2024-05-04 06:00:52 -03:00
|
|
|
|
|
2024-05-04 15:35:06 -03:00
|
|
|
|
protected virtual void OnPropertyChanged(string propertyName)
|
|
|
|
|
{
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
|
}
|
2024-05-04 06:00:52 -03:00
|
|
|
|
}
|
2024-05-08 03:00:31 -03:00
|
|
|
|
|
2024-05-04 06:00:52 -03:00
|
|
|
|
}
|