using CtrEditor.Convertidores; using CtrEditor.Siemens; using Newtonsoft.Json.Linq; using Siemens.Simatic.Simulation.Runtime; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using static System.Windows.Forms.VisualStyles.VisualStyleElement; namespace CtrEditor.ObjetosSim { /// /// Interaction logic for ucTanque.xaml /// public class osTanque : osBase { // Otros datos y métodos relevantes para la simulación private string _nombre = "Tanque"; private float _ancho; private float _alto; private float _left; private float _top; private float _angulo; private float _level; private string _tagNivel_Word; private float _max_OUT_Scaled; private float _min_OUT_Scaled; private float _velocidadIngreso; private float _velocidadSalida; private string _tagIngresoAbierto_Bool; private string _tagSalidaAbierta_Bool; private bool _IngresoAbierto_Bool; private bool _SalidaAbierta_Bool; private float _capacidadLitros; public float Capacidad_Litros { get => _capacidadLitros; set { if (_capacidadLitros != value) { _capacidadLitros = value; OnPropertyChanged(nameof(Capacidad_Litros)); } } } public bool Ingreso_Abierto { get => _IngresoAbierto_Bool; set { if (_IngresoAbierto_Bool != value) { _IngresoAbierto_Bool = value; OnPropertyChanged(nameof(Ingreso_Abierto)); } } } public bool Salida_Abierta { get => _SalidaAbierta_Bool; set { if (_SalidaAbierta_Bool != value) { _SalidaAbierta_Bool = value; OnPropertyChanged(nameof(Salida_Abierta)); } } } public string TagNivel_Word { get => _tagNivel_Word; set { if (_tagNivel_Word != value) { _tagNivel_Word = value; OnPropertyChanged(nameof(TagNivel_Word)); } } } public string TagIngresoAbierto_Bool { get => _tagIngresoAbierto_Bool; set { if (_tagIngresoAbierto_Bool != value) { _tagIngresoAbierto_Bool = value; OnPropertyChanged(nameof(TagIngresoAbierto_Bool)); } } } public string TagSalidaAbierta_Bool { get => _tagSalidaAbierta_Bool; set { if (_tagSalidaAbierta_Bool != value) { _tagSalidaAbierta_Bool = value; OnPropertyChanged(nameof(TagSalidaAbierta_Bool)); } } } public float Velocidad_Ingreso { get => _velocidadIngreso; set { if (_velocidadIngreso != value) { _velocidadIngreso = value; OnPropertyChanged(nameof(Velocidad_Ingreso)); } } } public float Velocidad_Salida { get => _velocidadSalida; set { if (_velocidadSalida != value) { _velocidadSalida = value; OnPropertyChanged(nameof(Velocidad_Salida)); } } } public float Min_OUT_Scaled { get => _min_OUT_Scaled; set { _min_OUT_Scaled = value; OnPropertyChanged(nameof(Min_OUT_Scaled)); } } public float Max_OUT_Scaled { get => _max_OUT_Scaled; set { _max_OUT_Scaled = value; OnPropertyChanged(nameof(Max_OUT_Scaled)); } } public float Level { get => _level; set { _level = value; OnPropertyChanged(nameof(Level)); } } public override float Left { get => _left; set { _left = value; CanvasSetLeftinMeter(value); OnPropertyChanged(nameof(Left)); } } public override float Top { get => _top; set { _top = value; CanvasSetTopinMeter(value); OnPropertyChanged(nameof(Top)); } } public float Ancho { get => _ancho; set { _ancho = value; OnPropertyChanged(nameof(Ancho)); } } public float Alto { get => _alto; set { _alto = value; OnPropertyChanged(nameof(Alto)); } } public float Angulo { get => _angulo; set { _angulo = value; OnPropertyChanged(nameof(Angulo)); } } public override string Nombre { get => _nombre; set { if (_nombre != value) { _nombre = value; OnPropertyChanged(nameof(Nombre)); } } } public osTanque() { Ancho = 0.30f; Alto = 0.30f; Max_OUT_Scaled = 27648; Min_OUT_Scaled = 0; } public override void UpdateGeometryStart() { // Se llama antes de la simulacion } public override void UpdateGeometryStep() { } public override void UpdatePLC(PLCModel plc, int elapsedMilliseconds) { EscribirWordTagScaled(plc, TagNivel_Word, Level, 0, 100, Min_OUT_Scaled, Max_OUT_Scaled); Ingreso_Abierto = LeerBitTag(plc, _tagIngresoAbierto_Bool); Salida_Abierta = LeerBitTag(plc, _tagSalidaAbierta_Bool); } public override void UpdateControl(int elapsedMilliseconds) { if (Salida_Abierta && Level > 0) { var l_por_milisegundo = Velocidad_Salida / 60000.0f; Level -= l_por_milisegundo * elapsedMilliseconds; if (Level < 0) Level = 0; } if (Ingreso_Abierto && Level < 100) { var l_por_milisegundo = Velocidad_Ingreso / 60000.0f; Level += l_por_milisegundo * elapsedMilliseconds; if (Level > 100) Level = 100; } } public override void ucLoaded() { // El UserControl ya se ha cargado y podemos obtener las coordenadas para // crear el objeto de simulacion ActualizarLeftTop(); } } public partial class ucTanque : UserControl, IDataContainer { public osBase? Datos { get; set; } public ucTanque() { InitializeComponent(); this.Loaded += OnLoaded; } private void OnLoaded(object sender, RoutedEventArgs e) { Datos?.ucLoaded(); } public void Resize(float width, float height) { if (Datos is osTanque datos) { datos.Ancho = PixelToMeter.Instance.calc.PixelsToMeters(width); datos.Alto = PixelToMeter.Instance.calc.PixelsToMeters(width); } } public void Move(float LeftPixels, float TopPixels) { if (Datos != null) { Datos.Left = PixelToMeter.Instance.calc.PixelsToMeters(LeftPixels); Datos.Top = PixelToMeter.Instance.calc.PixelsToMeters(TopPixels); } } public void Rotate(float Angle) { if (Datos != null) if (Datos is osTanque datos) datos.Angulo = Angle; } public void Highlight(bool State) { } public int ZIndex() { return 10; } } }