From 0f34e6cdaa2db7ac37ab6ad983b7a8366bcc6dcc Mon Sep 17 00:00:00 2001 From: Miguel Date: Mon, 10 Jun 2024 11:07:25 +0200 Subject: [PATCH] Modificando BuscarCoincidencias --- CtrEditor.csproj | 1 + MainViewModel.cs | 97 ++++++++----------- .../ucBuscarCoincidencias.xaml.cs | 50 +++++++++- .../Extraccion Datos/ucExtraccionTag.xaml.cs | 53 ++++++++-- 4 files changed, 136 insertions(+), 65 deletions(-) diff --git a/CtrEditor.csproj b/CtrEditor.csproj index 9226bf9..8dd0289 100644 --- a/CtrEditor.csproj +++ b/CtrEditor.csproj @@ -65,6 +65,7 @@ + diff --git a/MainViewModel.cs b/MainViewModel.cs index 560451d..809332a 100644 --- a/MainViewModel.cs +++ b/MainViewModel.cs @@ -109,7 +109,7 @@ namespace CtrEditor DatosDeTrabajo.CargarImagenes(); ListaImagenes = new ObservableCollection(DatosDeTrabajo.Imagenes.Keys); // Actualizar claves SelectedImage = null; - if (EstadoPersistente.Instance.imagen != null && EstadoPersistente.Instance.imagen.Length>0) + if (EstadoPersistente.Instance.imagen != null && EstadoPersistente.Instance.imagen.Length > 0) SelectedImage = EstadoPersistente.Instance.imagen; else if (ListaImagenes.FirstOrDefault() != null) SelectedImage = ListaImagenes.FirstOrDefault(); @@ -273,66 +273,55 @@ namespace CtrEditor private void DuplicarUserControl() { if (SelectedItemOsList is osBase objDuplicar) + DuplicarObjeto(objDuplicar, 0.5f, 0.5f); + } + + public osBase DuplicarObjeto(osBase objDuplicar, float OffsetX, float OffsetY) + { + StopSimulation(); + DisconnectPLC(); + + osBase? NuevoObjetoDuplicado = null; + + objDuplicar.SalvarDatosNoSerializables(); + + var settings = new JsonSerializerSettings { - StopSimulation(); - DisconnectPLC(); + Formatting = Formatting.Indented, + NullValueHandling = NullValueHandling.Ignore, + TypeNameHandling = TypeNameHandling.All + }; - objDuplicar.SalvarDatosNoSerializables(); - - var settings = new JsonSerializerSettings + try + { + // Serializar + var serializedData = JsonConvert.SerializeObject(objDuplicar, settings); + // Duplicar + NuevoObjetoDuplicado = JsonConvert.DeserializeObject(serializedData, settings); + if (NuevoObjetoDuplicado != null) { - Formatting = Formatting.Indented, - NullValueHandling = NullValueHandling.Ignore, - TypeNameHandling = TypeNameHandling.All - }; + NuevoObjetoDuplicado.Id.ObtenerNuevaID(); + string nombre = NuevoObjetoDuplicado.Nombre + "_" + NuevoObjetoDuplicado.Id.Value; - try - { - // Serializar - var serializedData = JsonConvert.SerializeObject(objDuplicar, settings); - // Duplicar - var NuevoObjetoDuplicado = JsonConvert.DeserializeObject(serializedData, settings); - if (NuevoObjetoDuplicado != null) - { - string nombre = NuevoObjetoDuplicado.Nombre; - - // Expresión regular para identificar un nombre que termina con _número - Regex regex = new Regex(@"_(\d+)$"); - - if (regex.IsMatch(nombre)) - { - // Extraer el número actual y sumarle 1 - var match = regex.Match(nombre); - int numeroActual = int.Parse(match.Groups[1].Value); - int nuevoNumero = numeroActual + 1; - - // Reemplazar el número en el nombre - nombre = regex.Replace(nombre, $"_{nuevoNumero}"); - } - else - { - // Si no termina con _número, añadir _1 - nombre += "_1"; - } - - NuevoObjetoDuplicado.Nombre = nombre; - NuevoObjetoDuplicado.Left += 0.5f; - NuevoObjetoDuplicado.Top += 0.5f; - ObjetosSimulables.Add(NuevoObjetoDuplicado); - CrearUserControlDesdeObjetoSimulable(NuevoObjetoDuplicado); - } - } - catch - { - // Log error or handle it accordingly - } - finally - { - objDuplicar.RestaurarDatosNoSerializables(); + NuevoObjetoDuplicado.Nombre = nombre; + NuevoObjetoDuplicado.Left += OffsetX; + NuevoObjetoDuplicado.Top += OffsetY; + ObjetosSimulables.Add(NuevoObjetoDuplicado); + CrearUserControlDesdeObjetoSimulable(NuevoObjetoDuplicado); } } + catch + { + // Log error or handle it accordingly + } + finally + { + objDuplicar.RestaurarDatosNoSerializables(); + } + return NuevoObjetoDuplicado; } + private void EliminarUserControl() { if (SelectedItemOsList is osBase objEliminar) @@ -532,7 +521,7 @@ namespace CtrEditor var settings = new JsonSerializerSettings { Formatting = Formatting.Indented, - // PreserveReferencesHandling = PreserveReferencesHandling.Objects, + // PreserveReferencesHandling = PreserveReferencesHandling.Objects, NullValueHandling = NullValueHandling.Ignore, TypeNameHandling = TypeNameHandling.Auto }; diff --git a/ObjetosSim/Extraccion Datos/ucBuscarCoincidencias.xaml.cs b/ObjetosSim/Extraccion Datos/ucBuscarCoincidencias.xaml.cs index 92c5f94..92cb42b 100644 --- a/ObjetosSim/Extraccion Datos/ucBuscarCoincidencias.xaml.cs +++ b/ObjetosSim/Extraccion Datos/ucBuscarCoincidencias.xaml.cs @@ -18,6 +18,8 @@ using Ookii.Dialogs.Wpf; using Rect = System.Windows.Rect; using System.ComponentModel; using Newtonsoft.Json; +using Xceed.Wpf.Toolkit.PropertyGrid.Attributes; +using System.ComponentModel; namespace CtrEditor.ObjetosSim.Extraccion_Datos { @@ -49,6 +51,7 @@ namespace CtrEditor.ObjetosSim.Extraccion_Datos } [ObservableProperty] + [property: Category("Tag Extraction:")] bool search_templates; partial void OnSearch_templatesChanged(bool oldValue, bool newValue) @@ -59,9 +62,11 @@ namespace CtrEditor.ObjetosSim.Extraccion_Datos } [ObservableProperty] + [property: Category("Tag Extraction:")] bool export_ocr; [ObservableProperty] + [property: Category("Tag Extraction:")] string text_export_ocr; partial void OnExport_ocrChanged(bool value) @@ -98,36 +103,44 @@ namespace CtrEditor.ObjetosSim.Extraccion_Datos [ObservableProperty] [property: Description("Width of the object.")] - [property: Category("Position:")] + [property: Category("Layout:")] public float ancho; [ObservableProperty] [property: Description("Height of the object.")] - [property: Category("Position:")] + [property: Category("Layout:")] public float alto; [ObservableProperty] + [property: Category("Layout:")] public float angulo; [ObservableProperty] + [property: Category("Tag Extraction:")] string tag_extract; [ObservableProperty] + [property: Category("Tag Extraction:")] string clase; [ObservableProperty] + [property: Category("Tag Extraction:")] string tag_name; [ObservableProperty] float opacity_oculto; [ObservableProperty] + [property: Category("Tag Extraction:")] bool show_debug_ocr; [ObservableProperty] + [property: Category("Tag Extraction:")] float threshold; [ObservableProperty] + [property: Category("Tag Extraction:")] + [property: ReadOnly(true)] float coincidencias; @@ -258,7 +271,7 @@ namespace CtrEditor.ObjetosSim.Extraccion_Datos int ConteoPositivos = 0; // Lista para mantener áreas ya aceptadas - List acceptedRectangles = new List(); + List acceptedRectangles = new List(); if (search_rectangles != null) search_rectangles.Clear(); @@ -317,12 +330,43 @@ namespace CtrEditor.ObjetosSim.Extraccion_Datos } } } + PopularTagExtraction(); } } } }); } + private void PopularTagExtraction() + { + var objetosSimulablesCopy = new List(_mainViewModel.ObjetosSimulables); + foreach (var obj in objetosSimulablesCopy) + if (obj is osExtraccionTag objExtraccionTag) + if (objExtraccionTag.Id_Search_Templates == this.Nombre && objExtraccionTag.Cloned) + _mainViewModel.ObjetosSimulables.Remove(objExtraccionTag); + + var objetosSimulables2Copy = new List(_mainViewModel.ObjetosSimulables); + foreach (var rectangle in search_rectangles) + { + float offsetX = PixelsToMeters((float)rectangle.X) - Left; + float offsetY = PixelsToMeters((float)rectangle.Y) - Top; + + foreach (var eTag in objetosSimulables2Copy) + { + if (eTag is osExtraccionTag objExtraccionTag) + { + if (objExtraccionTag.Id_Search_Templates == this.Nombre) + { + osExtraccionTag newObj = (osExtraccionTag)_mainViewModel.DuplicarObjeto(objExtraccionTag, offsetX, offsetY); + if (newObj != null) + newObj.Cloned = true; + } + } + } + + } + } + // Método para convertir BitmapSource a Mat private Mat BitmapSourceToMat(BitmapSource bitmapSource) diff --git a/ObjetosSim/Extraccion Datos/ucExtraccionTag.xaml.cs b/ObjetosSim/Extraccion Datos/ucExtraccionTag.xaml.cs index 96ad9ed..1b2e37e 100644 --- a/ObjetosSim/Extraccion Datos/ucExtraccionTag.xaml.cs +++ b/ObjetosSim/Extraccion Datos/ucExtraccionTag.xaml.cs @@ -4,7 +4,8 @@ using System.Windows.Navigation; using CommunityToolkit.Mvvm.ComponentModel; using CtrEditor.Simulacion; using Newtonsoft.Json; - +using Xceed.Wpf.Toolkit.PropertyGrid.Attributes; +using System.ComponentModel; namespace CtrEditor.ObjetosSim.Extraccion_Datos { @@ -15,10 +16,6 @@ namespace CtrEditor.ObjetosSim.Extraccion_Datos public partial class osExtraccionTag : osBase, IosBase { - private osBase _osMotor = null; - - private simTransporte SimGeometria; - public static string NombreClase() { return "Extraccion Tags"; @@ -31,8 +28,16 @@ namespace CtrEditor.ObjetosSim.Extraccion_Datos } [ObservableProperty] + [property: Category("Tag Extraction:")] bool extraer; + + [ObservableProperty] + [property: Description("Autocreated and cloned with Search Templates")] + [property: Category("Tag Extraction:")] + bool cloned; + + public override void TopChanged(float value) { base.TopChanged(value); @@ -51,6 +56,7 @@ namespace CtrEditor.ObjetosSim.Extraccion_Datos } [ObservableProperty] + [property: Category("Layout:")] public float ancho; partial void OnAnchoChanged(float value) @@ -59,6 +65,7 @@ namespace CtrEditor.ObjetosSim.Extraccion_Datos } [ObservableProperty] + [property: Category("Layout:")] public float alto; partial void OnAltoChanged(float value) @@ -71,19 +78,49 @@ namespace CtrEditor.ObjetosSim.Extraccion_Datos CaptureImageAreaAndDoOCR(); } - [ObservableProperty] - osBuscarCoincidencias search_Templates; + + private osBuscarCoincidencias Search_Templates; [ObservableProperty] + [property: Description("Link to Search Templates")] + [property: Category("Tag Extraction:")] + [property: ItemsSource(typeof(osBaseItemsSource))] + string id_Search_Templates; + + partial void OnId_Search_TemplatesChanged(string value) + { + if (Search_Templates != null) + Search_Templates.PropertyChanged -= OnMotorPropertyChanged; + if (_mainViewModel != null && value != null && value.Length > 0) + { + Search_Templates = (osBuscarCoincidencias)_mainViewModel.ObjetosSimulables.FirstOrDefault(s => (s is osBuscarCoincidencias && s.Nombre == value), null); + if (Search_Templates != null) + Search_Templates.PropertyChanged += OnMotorPropertyChanged; + } + } + + private void OnMotorPropertyChanged(object sender, PropertyChangedEventArgs e) + { + if (e.PropertyName == nameof(osBuscarCoincidencias.Nombre)) + { + Id_Search_Templates = ((osBuscarCoincidencias)sender).Nombre; + } + } + + [ObservableProperty] + [property: Category("Layout:")] public float angulo; [ObservableProperty] + [property: Category("Tag Extraction:")] string tag_extract; [ObservableProperty] + [property: Category("Tag Extraction:")] string clase; [ObservableProperty] + [property: Category("Tag Extraction:")] string tag_name; [ObservableProperty] @@ -107,7 +144,7 @@ namespace CtrEditor.ObjetosSim.Extraccion_Datos // El UserControl ya se ha cargado y podemos obtener las coordenadas para // crear el objeto de simulacion base.ucLoaded(); - + OnId_Search_TemplatesChanged(Id_Search_Templates); // Actualizar Link } }