diff --git a/DatosDeTrabajo.cs b/DatosDeTrabajo.cs index 9aceba4..277aced 100644 --- a/DatosDeTrabajo.cs +++ b/DatosDeTrabajo.cs @@ -28,6 +28,16 @@ namespace CtrEditor return null; } + public string? ObtenerPathAllPages(string extension) + { + string folderPath = EstadoPersistente.Instance.directorio; // Usar directamente desde el Singleton + + if (Directory.Exists(folderPath)) + return Path.ChangeExtension(folderPath + "\\allpages", extension); + else return null; + } + + public void CargarImagenes() { Imagenes.Clear(); diff --git a/MainViewModel.cs b/MainViewModel.cs index c6fd2a0..7f2a57c 100644 --- a/MainViewModel.cs +++ b/MainViewModel.cs @@ -16,6 +16,7 @@ using System.Reflection; using CommunityToolkit.Mvvm.ComponentModel; using Xceed.Wpf.Toolkit.PropertyGrid; using System.Text.RegularExpressions; +using System.Windows.Data; namespace CtrEditor @@ -147,9 +148,13 @@ namespace CtrEditor [ObservableProperty] private TipoSimulable selectedItem; + public ICollectionView ObjetosSimulablesFiltered { get; } + public ICollectionView ObjetosSimulablesAllPages { get; } + [ObservableProperty] public ObservableCollection objetosSimulables; + // // Constructor // @@ -160,6 +165,7 @@ namespace CtrEditor datosDeTrabajo = new DatosDeTrabajo(); ObjetosSimulables = new ObservableCollection(); + ListaOsBase = new ObservableCollection(); // Inicializa el PLCViewModel @@ -463,38 +469,40 @@ namespace CtrEditor StopSimulation(); DisconnectPLC(); - // Ruta del archivo a ser guardado - var path = DatosDeTrabajo.ObtenerPathImagenConExtension(SelectedImage, ".json"); - - // Verificar si el archivo ya existe y crear un respaldo - if (File.Exists(path)) - { - var backupPath = Path.ChangeExtension(path, ".bak"); - File.Copy(path, backupPath, true); // Copia el archivo existente a un nuevo archivo .bak, sobrescribiendo si es necesario - } + ObservableCollection _objetosSimulables = new ObservableCollection(); + ObservableCollection _objetosSimulablesAllPages = new ObservableCollection(); foreach (var obj in ObjetosSimulables) + { // Guardar referencias temporales obj.SalvarDatosNoSerializables(); + if (!obj.Enable_on_all_pages) + _objetosSimulables.Add(obj); + else + _objetosSimulablesAllPages.Add(obj); + } - var settings = new JsonSerializerSettings - { - Formatting = Formatting.Indented, - NullValueHandling = NullValueHandling.Ignore, - TypeNameHandling = TypeNameHandling.Auto - }; + // Salvar los objetos de la pagina actual // Crear un objeto que incluya tanto los ObjetosSimulables como el UnitConverter y PLC_ConnectionData var dataToSerialize = new SimulationData { - ObjetosSimulables = ObjetosSimulables, + ObjetosSimulables = _objetosSimulables, UnitConverter = PixelToMeter.Instance.calc, PLC_ConnectionData = PLCViewModel }; - // Serializar - var serializedData = JsonConvert.SerializeObject(dataToSerialize, settings); - File.WriteAllText(path, serializedData); // Escribir el nuevo archivo JSON + // Ruta del archivo a ser guardado + var path = DatosDeTrabajo.ObtenerPathImagenConExtension(SelectedImage, ".json"); + if (path != null) + SerializarYSalvar(dataToSerialize, path); + + // Salvar los objetos de todas las paginas + + // Ruta del archivo a ser guardado + path = DatosDeTrabajo.ObtenerPathAllPages(".json"); + if (path!=null) + SerializarYSalvar(_objetosSimulablesAllPages, path); // Restaurar las propiedades originales de los objetos foreach (var obj in ObjetosSimulables) @@ -502,6 +510,29 @@ namespace CtrEditor } } + private void SerializarYSalvar(object listaObjetos, string path) + { + + // Verificar si el archivo ya existe y crear un respaldo + if (File.Exists(path)) + { + var backupPath = Path.ChangeExtension(path, ".bak"); + File.Copy(path, backupPath, true); // Copia el archivo existente a un nuevo archivo .bak, sobrescribiendo si es necesario + } + + + var settings = new JsonSerializerSettings + { + Formatting = Formatting.Indented, + NullValueHandling = NullValueHandling.Ignore, + TypeNameHandling = TypeNameHandling.Auto + }; + + // Serializar + var serializedData = JsonConvert.SerializeObject(listaObjetos, settings); + File.WriteAllText(path, serializedData); // Escribir el nuevo archivo JSON + } + public void LoadStateObjetosSimulables() { try @@ -511,18 +542,20 @@ namespace CtrEditor ObjetosSimulables.Clear(); simulationManager.Clear(); if (SelectedImage != null) - { + { + var settings = new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.Auto, + ObjectCreationHandling = ObjectCreationHandling.Replace, + PreserveReferencesHandling = PreserveReferencesHandling.Objects, + ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor + }; + string jsonPath = datosDeTrabajo.ObtenerPathImagenConExtension(SelectedImage, ".json"); if (File.Exists(jsonPath)) { string jsonString = File.ReadAllText(jsonPath); - var settings = new JsonSerializerSettings - { - TypeNameHandling = TypeNameHandling.Auto, - ObjectCreationHandling = ObjectCreationHandling.Replace, - PreserveReferencesHandling = PreserveReferencesHandling.Objects, - ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor - }; + var simulationData = JsonConvert.DeserializeObject(jsonString, settings); if (simulationData != null) @@ -540,11 +573,24 @@ namespace CtrEditor // Re-register to the events PLCViewModel.RefreshEvent += OnRefreshEvent; - // Recorrer la colección de objetos simulables - foreach (var objetoSimulable in ObjetosSimulables) - CrearUserControlDesdeObjetoSimulable(objetoSimulable); } } + + jsonPath = DatosDeTrabajo.ObtenerPathAllPages(".json"); + if (File.Exists(jsonPath)) + { + string jsonString = File.ReadAllText(jsonPath); + + ObservableCollection _objetosSimulablesAllPages = new ObservableCollection(); + + _objetosSimulablesAllPages = JsonConvert.DeserializeObject>(jsonString, settings); + if (_objetosSimulablesAllPages != null) + foreach(var obj in _objetosSimulablesAllPages) + ObjetosSimulables.Add(obj); + } + // Recorrer la colección de objetos simulables + foreach (var objetoSimulable in ObjetosSimulables) + CrearUserControlDesdeObjetoSimulable(objetoSimulable); } } catch { /* Consider logging the error or handling it appropriately */ } diff --git a/MainWindow.xaml b/MainWindow.xaml index a7780c7..d9b4ef6 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -1,43 +1,38 @@ - + xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" xmlns:sys="clr-namespace:System;assembly=mscorlib" + xmlns:ObjetosSim="clr-namespace:CtrEditor.ObjetosSim" x:Class="CtrEditor.MainWindow" Height="900" Width="1600" + WindowState="Maximized" ResizeMode="CanResize" Title="{Binding directorioTrabajo}" Icon="/app2.png"> - + - + + @@ -55,27 +50,30 @@ - - - + + + - - - + + + - - + + - + - + @@ -89,44 +87,47 @@ - - - + - + @@ -138,37 +139,49 @@ - - + + - - - + + + + - + + + + + + + + + + + - + - + - @@ -176,7 +189,8 @@ - + @@ -185,7 +199,9 @@ - + @@ -197,11 +213,12 @@ - + - + @@ -209,7 +226,7 @@ - + @@ -217,19 +234,18 @@ - diff --git a/ObjetosSim/osBase.cs b/ObjetosSim/osBase.cs index 1d19550..7307731 100644 --- a/ObjetosSim/osBase.cs +++ b/ObjetosSim/osBase.cs @@ -86,6 +86,11 @@ namespace CtrEditor.ObjetosSim [ObservableProperty] private string group_Panel; + [ObservableProperty] + private bool enable_on_all_pages; + + + partial void OnTopChanged(float value) { CanvasSetTopinMeter(value); @@ -108,7 +113,13 @@ namespace CtrEditor.ObjetosSim OnTimerAfterMovement(); }); } - + /// + /// Este timer se usa para llamar a OnTimerAfterMovement luego de que han pasado 500ms sin que se + /// llame a ResetTimer. + /// Esto es util para no actualizar las simulaciones o controles mientras se esta moviendo aun el control. + /// Se debe hacer overrride de OnTimerAfterMovement y dentro de esta funcion llamar a la actualizacion que se + /// requiere hacer una vez terminado el movimiento del control por parte del usuario. + /// public void ResetTimer() { if (timer == null)