using CommunityToolkit.Mvvm.ComponentModel; using CtrEditor.FuncionesBase; using System.Windows; using System.Windows.Controls; using CommunityToolkit.Mvvm.ComponentModel; using LibS7Adv; using CtrEditor.Simulacion; using Xceed.Wpf.Toolkit.PropertyGrid.Attributes; using System.ComponentModel; using CtrEditor.FuncionesBase; using System.Text.Json.Serialization; namespace CtrEditor.ObjetosSim { /// /// Interaction logic for ucTransporteTTopDualInverter.xaml /// /// public partial class osTransporteTTopDualInverter : osBase, IosBase { private simTransporte SimGeometria; private osVMmotorSim MotorA; private osVMmotorSim MotorB; public static string NombreClase() { return "Transporte TTOP Doble Inversor"; } private string nombre = NombreClase(); [property: Category("Identificación")] [property: Description("Nombre identificativo del objeto")] [property: Name("Nombre")] public override string Nombre { get => nombre; set => SetProperty(ref nombre, value); } [ObservableProperty] [property: Category("Simulación")] [property: Description("Velocidad actual del transporte")] [property: Name("Velocidad Actual")] public float velocidadActual; partial void OnVelocidadActualChanged(float value) { SetSpeed(); } [ObservableProperty] [property: Category("Configuración")] [property: Description("Invierte el sentido de movimiento")] [property: Name("Invertir Dirección")] bool invertirDireccion; partial void OnInvertirDireccionChanged(bool value) { SetSpeed(); if (_visualRepresentation is ucTransporteTTopDualInverter uc) { CrearAnimacionStoryBoardTrasnporte(uc.Transporte, InvertirDireccion); ActualizarAnimacionStoryBoardTransporte(VelocidadActual); } } void SetSpeed() { if (InvertirDireccion) SimGeometria?.SetSpeed(-VelocidadActual); else SimGeometria?.SetSpeed(VelocidadActual); ActualizarAnimacionStoryBoardTransporte(VelocidadActual); } [ObservableProperty] [property: Category("Enlace PLC")] [property: Description("Tag para activar enlace con inversor A")] [property: Name("Tag Activación Motor A")] string tag_ReleActivatedMotor_A; [ObservableProperty] [property: Category("Enlace PLC")] [property: Description("Tag para activar enlace con inversor B")] [property: Name("Tag Activación Motor B")] string tag_ReleActivatedMotor_B; [ObservableProperty] [property: Category("Enlace PLC")] [property: Description("Enlace a inversor A")] [property: Name("Motor A")] [property: ItemsSource(typeof(osBaseItemsSource))] string id_Motor_A; [ObservableProperty] [property: Category("Enlace PLC")] [property: Description("Enlace a inversor B")] [property: Name("Motor B")] [property: ItemsSource(typeof(osBaseItemsSource))] string id_Motor_B; [JsonIgnore] private PropertyChangedEventHandler motorPropertyChangedHandler; partial void OnId_Motor_AChanged(string value) { if (MotorA != null && motorPropertyChangedHandler != null) MotorA.PropertyChanged -= motorPropertyChangedHandler; if (_mainViewModel != null && !string.IsNullOrEmpty(value)) { MotorA = (osVMmotorSim)_mainViewModel.ObjetosSimulables.FirstOrDefault(s => s is osVMmotorSim motor && motor.Nombre == value); if (MotorA != null) { motorPropertyChangedHandler = (sender, e) => { if (e.PropertyName == nameof(osVMmotorSim.Nombre)) { Id_Motor_A = ((osVMmotorSim)sender).Nombre; } }; MotorA.PropertyChanged += motorPropertyChangedHandler; } } } partial void OnId_Motor_BChanged(string value) { if (MotorB != null && motorPropertyChangedHandler != null) MotorB.PropertyChanged -= motorPropertyChangedHandler; if (_mainViewModel != null && !string.IsNullOrEmpty(value)) { MotorB = (osVMmotorSim)_mainViewModel.ObjetosSimulables.FirstOrDefault(s => s is osVMmotorSim motor && motor.Nombre == value); if (MotorB != null) { motorPropertyChangedHandler = (sender, e) => { if (e.PropertyName == nameof(osVMmotorSim.Nombre)) { Id_Motor_B = ((osVMmotorSim)sender).Nombre; } }; MotorB.PropertyChanged += motorPropertyChangedHandler; } } } [ObservableProperty] [property: Category("Configuración")] [property: Description("Coeficiente de fricción")] [property: Name("Coeficiente Fricción")] public float frictionCoefficient; [ObservableProperty] [property: Category("Configuración")] [property: Description("Velocidad máxima a 50Hz")] [property: Name("Velocidad Max 50Hz")] public float velMax50hz; [ObservableProperty] [property: Category("Configuración")] [property: Description("Tiempo de rampa")] [property: Name("Tiempo Rampa")] public float tiempoRampa; [ObservableProperty] [property: Category("Información")] [property: Description("Estado de marcha")] [property: Name("En Marcha")] public bool esMarcha; private void ActualizarGeometrias() { if (_visualRepresentation is ucTransporteTTopDualInverter uc) { UpdateRectangle(SimGeometria, uc.Transporte, Alto, Ancho, Angulo); SetSpeed(); } ActualizarAnimacionStoryBoardTransporte(VelocidadActual); } public override void OnMoveResizeRotate() { ActualizarGeometrias(); } public osTransporteTTopDualInverter() { Ancho = 1; Alto = 0.10f; Tag_ReleActivatedMotor_A = "1"; Tag_ReleActivatedMotor_B = "1"; } public override void SimulationStop() { // Se llama al detener la simulacion ActualizarAnimacionStoryBoardTransporte(VelocidadActual); } public override void UpdateGeometryStart() { // Se llama antes de la simulacion ActualizarGeometrias(); } public override void UpdatePLC(PLCViewModel plc, int elapsedMilliseconds) { if (LeerBitTag(Tag_ReleActivatedMotor_A)) { if (MotorA != null) if (MotorA is osVMmotorSim motor) VelocidadActual = motor.Velocidad; else VelocidadActual = 0; } else if (LeerBitTag(Tag_ReleActivatedMotor_B)) { if (MotorB != null) if (MotorB is osVMmotorSim motor) VelocidadActual = motor.Velocidad; else VelocidadActual = 0; } else VelocidadActual = 0; } public override void ucLoaded() { // El UserControl ya se ha cargado y podemos obtener las coordenadas para // crear el objeto de simulacion base.ucLoaded(); OnId_Motor_AChanged(Id_Motor_A); // Link Id_Motor = Motor OnId_Motor_BChanged(Id_Motor_B); // Link Id_Motor = Motor if (_visualRepresentation is ucTransporteTTopDualInverter uc) { SimGeometria = AddRectangle(simulationManager, uc.Transporte, Alto, Ancho, Angulo); CrearAnimacionStoryBoardTrasnporte(uc.Transporte, InvertirDireccion); } } public override void ucUnLoaded() { // El UserControl se esta eliminando // eliminar el objeto de simulacion simulationManager.Remove(SimGeometria); } } public partial class ucTransporteTTopDualInverter : UserControl, IDataContainer { public osBase? Datos { get; set; } public int zIndex_fromFrames { get; set; } public ucTransporteTTopDualInverter() { InitializeComponent(); this.Loaded += OnLoaded; this.Unloaded += OnUnloaded; } private void OnLoaded(object sender, RoutedEventArgs e) { Datos?.ucLoaded(); } private void OnUnloaded(object sender, RoutedEventArgs e) { Datos?.ucUnLoaded(); } public void Highlight(bool State) { } public ZIndexEnum ZIndex_Base() { return ZIndexEnum.Estaticos; } } }