using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using CommunityToolkit.Mvvm.ComponentModel; using LibS7Adv; using CtrEditor.Simulacion; using Xceed.Wpf.Toolkit.PropertyGrid.Attributes; using CtrEditor.FuncionesBase; using System.Text.Json.Serialization; namespace CtrEditor.ObjetosSim { /// /// Interaction logic for ucTransporteGuias.xaml /// public partial class osTransporteGuias : osBase, IosBase { private osBase Motor = null; private simTransporte? SimGeometria; private simGuia? Guia_Superior; private simGuia? Guia_Inferior; public static string NombreClase() { return "Transporte con Guías"; } 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 ucTransporteGuias uc) { CrearAnimacionStoryBoardTrasnporte(uc.Transporte, InvertirDireccion); ActualizarAnimacionStoryBoardTransporte(VelocidadActual); } } void SetSpeed() { if (InvertirDireccion) SimGeometria?.SetSpeed(-VelocidadActual); else SimGeometria?.SetSpeed(VelocidadActual); ActualizarAnimacionStoryBoardTransporte(VelocidadActual); } [ObservableProperty] [property: Category("Apariencia")] [property: Description("Color del transporte")] [property: Name("Color")] Color color = Colors.Blue; [ObservableProperty] [property: Category("Enlace PLC")] [property: Description("Tag para activar enlace con motor")] [property: Name("Tag Activación Motor")] string tag_ReleActivatedMotor; [ObservableProperty] [property: Category("Enlace PLC")] [property: Description("Motor enlazado al transporte")] [property: Name("Motor Enlazado")] [property: ItemsSource(typeof(osBaseItemsSource))] string id_Motor; [JsonIgnore] private PropertyChangedEventHandler motorPropertyChangedHandler; partial void OnId_MotorChanged(string value) { if (Motor != null && motorPropertyChangedHandler != null) Motor.PropertyChanged -= motorPropertyChangedHandler; if (_mainViewModel != null && !string.IsNullOrEmpty(value)) { Motor = (osVMmotorSim)_mainViewModel.ObjetosSimulables.FirstOrDefault(s => s is osVMmotorSim motor && motor.Nombre == value); if (Motor != null) { motorPropertyChangedHandler = (sender, e) => { if (e.PropertyName == nameof(osVMmotorSim.Nombre)) { Id_Motor = ((osVMmotorSim)sender).Nombre; } }; Motor.PropertyChanged += motorPropertyChangedHandler; } } } public override void AltoChanged(float value) { ActualizarGeometrias(); } [ObservableProperty] [property: Category("Configuración")] [property: Description("Actuar como freno")] [property: Name("Es Freno")] public bool esFreno; partial void OnEsFrenoChanged(bool value) { if (SimGeometria != null) SimGeometria.isBrake = value; } [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; [ObservableProperty] [property: Category("Configuración")] [property: Description("Distancia entre guías")] [property: Name("Distancia")] private float distance; [ObservableProperty] [property: Category("Configuración")] [property: Description("Alto de las guías")] [property: Name("Alto Guía")] private float altoGuia; private void ActualizarGeometrias() { if (_visualRepresentation is ucTransporteGuias uc) { UpdateRectangle(SimGeometria, uc.Transporte, Alto, Ancho, Angulo); UpdateOrCreateLine(Guia_Superior, uc.GuiaSuperior); UpdateOrCreateLine(Guia_Inferior, uc.GuiaInferior); SimGeometria.DistanceGuide2Guide = Alto; SimGeometria.isBrake = esFreno; SetSpeed(); } } public override void OnMoveResizeRotate() { ActualizarGeometrias(); } public osTransporteGuias() { Ancho = 1; Alto = 0.10f; AltoGuia = 0.03f; Distance = 0.01f; Tag_ReleActivatedMotor = "1"; } public override void UpdateGeometryStart() { // Se llama antes de la simulacion ActualizarGeometrias(); } public override void SimulationStop() { // Se llama al detener la simulacion ActualizarAnimacionStoryBoardTransporte(VelocidadActual); } public override void UpdatePLC(PLCViewModel plc, int elapsedMilliseconds) { if (Motor != null) if (Motor is osVMmotorSim id_motor) if (LeerBitTag(Tag_ReleActivatedMotor)) VelocidadActual = id_motor.Velocidad; 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(); // El UserControl ya se ha cargado y podemos obtener las coordenadas para // crear el objeto de simulacion if (_visualRepresentation is ucTransporteGuias uc) { SimGeometria = AddRectangle(simulationManager, uc.Transporte, Alto, Ancho, Angulo); SimGeometria.TransportWithGuides = true; SimGeometria.DistanceGuide2Guide = Alto; Guia_Superior = AddLine(simulationManager, uc.GuiaSuperior); Guia_Inferior = AddLine(simulationManager, uc.GuiaInferior); CrearAnimacionStoryBoardTrasnporte(uc.Transporte, InvertirDireccion); } OnId_MotorChanged(Id_Motor); // Link Id_Motor = Motor } public override void ucUnLoaded() { // El UserControl se esta eliminando // eliminar el objeto de simulacion simulationManager.Remove(SimGeometria); simulationManager.Remove(Guia_Superior); simulationManager.Remove(Guia_Inferior); } } public partial class ucTransporteGuias : UserControl, IDataContainer { public osBase? Datos { get; set; } public int zIndex_fromFrames { get; set; } public ucTransporteGuias() { 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; } } }