From 72692cdf8cfdf12364b5e4f49fb17be837d4b8fa Mon Sep 17 00:00:00 2001 From: Miguel Date: Fri, 10 May 2024 22:17:57 +0200 Subject: [PATCH] Adaptado UserControl del PLCSim Advanced --- CtrEditor.csproj | 6 + MainViewModel.cs | 30 +++- MainWindow.xaml | 9 +- MainWindow.xaml.cs | 1 + ObjetosSim/UserControlFactory.cs | 4 + ObjetosSim/ucGuia.xaml | 2 +- ObjetosSim/ucGuia.xaml.cs | 6 +- ObjetosSim/ucTransporteGuias.xaml | 22 ++- ObjetosSim/ucTransporteGuias.xaml.cs | 133 ++++++++++++++- Siemens/PLCControl.xaml | 42 +++++ Siemens/PLCControl.xaml.cs | 233 +++++++++++++++++++++++++++ Siemens/PLCSim.cs | 52 ++++++ 12 files changed, 526 insertions(+), 14 deletions(-) create mode 100644 Siemens/PLCControl.xaml create mode 100644 Siemens/PLCControl.xaml.cs create mode 100644 Siemens/PLCSim.cs diff --git a/CtrEditor.csproj b/CtrEditor.csproj index 7b6fce2..6d341c5 100644 --- a/CtrEditor.csproj +++ b/CtrEditor.csproj @@ -15,4 +15,10 @@ + + + C:\Program Files (x86)\Common Files\Siemens\PLCSIMADV\API\6.0\Siemens.Simatic.Simulation.Runtime.Api.x64.dll + + + diff --git a/MainViewModel.cs b/MainViewModel.cs index 04b6e78..f74c6c3 100644 --- a/MainViewModel.cs +++ b/MainViewModel.cs @@ -14,6 +14,7 @@ using System.Windows.Media.Imaging; using static System.Runtime.InteropServices.JavaScript.JSType; using System.Windows.Threading; using CtrEditor.ObjetosSim; +using CtrEditor.Siemens; using System.IO; // using System.Windows.Forms; using System.Text.Json.Serialization; @@ -32,6 +33,7 @@ namespace CtrEditor public ObservableCollection listaImagenes { get; private set; } // Publicación de las claves del diccionario public ObservableCollection ListaOsBase { get; } = new ObservableCollection(); private ObservableCollection _objetosSimulables = new ObservableCollection(); + public PLCViewModel _plcViewModelData; private SimulationManager simulationManager = new SimulationManager(); @@ -52,6 +54,9 @@ namespace CtrEditor OpenWorkDirectoryCommand = new RelayCommand(OpenWorkDirectory); datosDeTrabajo = new DatosDeTrabajo(); + // Inicializa el PLCViewModel + _plcViewModelData = new PLCViewModel(); + InitializeTipoSimulableList(); ItemDoubleClickCommand = new ParameterizedRelayCommand(ExecuteDoubleClick); @@ -62,7 +67,6 @@ namespace CtrEditor StartSimulationCommand = new RelayCommand(StartSimulation); StopSimulationCommand = new RelayCommand(StopSimulation); - } public void LoadInitialData() @@ -161,6 +165,17 @@ namespace CtrEditor } } + + public PLCViewModel PLCViewModel + { + get { return _plcViewModelData; } + set + { + _plcViewModelData = value; + OnPropertyChanged(nameof(PLCViewModel)); + } + } + private string _selectedImage = null; public string SelectedImage { @@ -241,7 +256,8 @@ namespace CtrEditor var dataToSerialize = new SimulationData { ObjetosSimulables = ObjetosSimulables, - UnitConverter = PixelToMeter.Instance.calc + UnitConverter = PixelToMeter.Instance.calc, + PLC_ConnectionData = PLCViewModel }; var serializedData = JsonConvert.SerializeObject(dataToSerialize, settings); @@ -271,9 +287,14 @@ namespace CtrEditor var simulationData = JsonConvert.DeserializeObject(jsonString, settings); if (simulationData != null) { - ObjetosSimulables = simulationData.ObjetosSimulables; + if (simulationData.ObjetosSimulables is not null) + ObjetosSimulables = simulationData.ObjetosSimulables; + + if (simulationData.PLC_ConnectionData is not null) + PLCViewModel = simulationData.PLC_ConnectionData; + else + PLCViewModel = new PLCViewModel(); - // Restaura el UnitConverter si es necesario en otra parte de tu código PixelToMeter.Instance.calc = simulationData.UnitConverter; // Recorrer la colección de objetos simulables @@ -322,6 +343,7 @@ namespace CtrEditor { public ObservableCollection ObjetosSimulables { get; set; } public UnitConverter UnitConverter { get; set; } + public PLCViewModel PLC_ConnectionData { get; set; } } public class TipoSimulable diff --git a/MainWindow.xaml b/MainWindow.xaml index 6de6195..08d20ac 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -1,9 +1,10 @@ - @@ -33,8 +34,9 @@ - + + @@ -44,6 +46,7 @@ + diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index affba3d..db1e473 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -10,6 +10,7 @@ using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using CtrEditor.ObjetosSim; +using CtrEditor.Siemens; using static System.Runtime.InteropServices.JavaScript.JSType; using Binding = System.Windows.Data.Binding; using Label = System.Windows.Controls.Label; diff --git a/ObjetosSim/UserControlFactory.cs b/ObjetosSim/UserControlFactory.cs index 173e0ed..1738944 100644 --- a/ObjetosSim/UserControlFactory.cs +++ b/ObjetosSim/UserControlFactory.cs @@ -18,6 +18,8 @@ namespace CtrEditor.ObjetosSim return new ucTransporteTTop(); if (tipoObjeto == typeof(osGuia)) return new ucGuia(); + if (tipoObjeto == typeof(osTransporteGuias)) + return new ucTransporteGuias(); // Puedes añadir más condiciones para otros tipos @@ -32,6 +34,8 @@ namespace CtrEditor.ObjetosSim return new osTransporteTTop(); if (tipoObjeto == typeof(osGuia)) return new osGuia(); + if (tipoObjeto == typeof(osTransporteGuias)) + return new osTransporteGuias(); // Puedes añadir más condiciones para otros tipos diff --git a/ObjetosSim/ucGuia.xaml b/ObjetosSim/ucGuia.xaml index 65e2867..a6adfff 100644 --- a/ObjetosSim/ucGuia.xaml +++ b/ObjetosSim/ucGuia.xaml @@ -11,7 +11,7 @@ - + diff --git a/ObjetosSim/ucGuia.xaml.cs b/ObjetosSim/ucGuia.xaml.cs index e67fa35..9ffe27f 100644 --- a/ObjetosSim/ucGuia.xaml.cs +++ b/ObjetosSim/ucGuia.xaml.cs @@ -54,13 +54,13 @@ namespace CtrEditor.ObjetosSim OnPropertyChanged(nameof(Ancho)); } } - public float Alto + public float AltoGuia { get => Geometria.Width; set { Geometria.Width = value; - OnPropertyChanged(nameof(Alto)); + OnPropertyChanged(nameof(AltoGuia)); } } @@ -90,7 +90,7 @@ namespace CtrEditor.ObjetosSim public osGuia() { Ancho = 1; - Alto = 0.10f; + AltoGuia = 0.03f; } public override void ConnectSimManager(SimulationManager simulationManager) diff --git a/ObjetosSim/ucTransporteGuias.xaml b/ObjetosSim/ucTransporteGuias.xaml index 4385360..63e8b2b 100644 --- a/ObjetosSim/ucTransporteGuias.xaml +++ b/ObjetosSim/ucTransporteGuias.xaml @@ -4,10 +4,28 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:CtrEditor.ObjetosSim" - mc:Ignorable="d" - d:DesignHeight="450" d:DesignWidth="800"> + mc:Ignorable="d" + d:DesignHeight="300" d:DesignWidth="300"> + + + + + + + + + + + + + + + + + + diff --git a/ObjetosSim/ucTransporteGuias.xaml.cs b/ObjetosSim/ucTransporteGuias.xaml.cs index 7f86251..380a594 100644 --- a/ObjetosSim/ucTransporteGuias.xaml.cs +++ b/ObjetosSim/ucTransporteGuias.xaml.cs @@ -18,11 +18,142 @@ namespace CtrEditor.ObjetosSim /// /// Interaction logic for ucTransporteGuias.xaml /// - public partial class ucTransporteGuias : UserControl + public class osTransporteGuias : osBase { + private string _nombre = "Transporte Guias"; + + private float frictionCoefficient; + private float velMax50hz; // en metros por minuto + private float tiempoRampa; + private bool esMarcha; + + private Rectangle Geometria = new Rectangle(); + + public override float Left + { + get => Geometria.Left; + set + { + Geometria.Left = value; + CanvasSetLeftinMeter(value); + OnPropertyChanged(nameof(Left)); + } + } + public override float Top + { + get => Geometria.Top; + set + { + Geometria.Top = value; + CanvasSetTopinMeter(value); + OnPropertyChanged(nameof(Top)); + } + } + + public float Ancho + { + get => Geometria.Length; + set + { + Geometria.Length = value; + OnPropertyChanged(nameof(Ancho)); + } + } + public float Alto + { + get => Geometria.Width; + set + { + Geometria.Width = value; + OnPropertyChanged(nameof(Alto)); + } + } + + public float Angulo + { + get => Geometria.Angle; + set + { + Geometria.Angle = value; + OnPropertyChanged(nameof(Angulo)); + } + } + public float VelocidadActual + { + get => Geometria.Speed; + set + { + Geometria.Speed = value; + OnPropertyChanged(nameof(VelocidadActual)); + } + } + + public override string Nombre + { + get => _nombre; + set + { + if (_nombre != value) + { + _nombre = value; + OnPropertyChanged(nameof(Nombre)); + } + } + } + + public float FrictionCoefficient { get => frictionCoefficient; set => frictionCoefficient = value; } + public float VelMax50hz { get => velMax50hz; set => velMax50hz = value; } + public float TiempoRampa { get => tiempoRampa; set => tiempoRampa = value; } + public bool EsMarcha { get => esMarcha; set => esMarcha = value; } + + public osTransporteGuias() + { + Ancho = 1; + Alto = 0.10f; + } + + public override void ConnectSimManager(SimulationManager simulationManager) + { + simulationManager.rectangles.Add(Geometria); + } + public override void UpdateControl() + { + } + + } + + public partial class ucTransporteGuias : UserControl, IDataContainer + { + public osBase? Datos { get; set; } + public ucTransporteGuias() { InitializeComponent(); } + public void Resize(float width, float height) + { + if (Datos is osTransporteGuias datos) + datos.Ancho = 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 osTransporteGuias datos) + datos.Angulo = Angle; + } + public void Highlight(bool State) { } + public int ZIndex() + { + return 1; + } + } } diff --git a/Siemens/PLCControl.xaml b/Siemens/PLCControl.xaml new file mode 100644 index 0000000..ec948e4 --- /dev/null +++ b/Siemens/PLCControl.xaml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + +