Modificando BuscarCoincidencias

This commit is contained in:
Miguel 2024-06-10 11:07:25 +02:00
parent 77c9f3db5e
commit 0f34e6cdaa
4 changed files with 136 additions and 65 deletions

View File

@ -65,6 +65,7 @@
<ItemGroup>
<PackageReference Include="Aether.Physics2D" Version="2.1.0" />
<PackageReference Include="ClosedXML" Version="0.104.0-preview2" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="Emgu.CV" Version="4.9.0.5494" />
<PackageReference Include="Emgu.CV.runtime.windows" Version="4.9.0.5494" />

View File

@ -273,10 +273,16 @@ 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
@ -291,33 +297,15 @@ namespace CtrEditor
// Serializar
var serializedData = JsonConvert.SerializeObject(objDuplicar, settings);
// Duplicar
var NuevoObjetoDuplicado = JsonConvert.DeserializeObject<osBase>(serializedData, settings);
NuevoObjetoDuplicado = JsonConvert.DeserializeObject<osBase>(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.Id.ObtenerNuevaID();
string nombre = NuevoObjetoDuplicado.Nombre + "_" + NuevoObjetoDuplicado.Id.Value;
NuevoObjetoDuplicado.Nombre = nombre;
NuevoObjetoDuplicado.Left += 0.5f;
NuevoObjetoDuplicado.Top += 0.5f;
NuevoObjetoDuplicado.Left += OffsetX;
NuevoObjetoDuplicado.Top += OffsetY;
ObjetosSimulables.Add(NuevoObjetoDuplicado);
CrearUserControlDesdeObjetoSimulable(NuevoObjetoDuplicado);
}
@ -330,8 +318,9 @@ namespace CtrEditor
{
objDuplicar.RestaurarDatosNoSerializables();
}
return NuevoObjetoDuplicado;
}
}
private void EliminarUserControl()
{

View File

@ -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;
@ -317,12 +330,43 @@ namespace CtrEditor.ObjetosSim.Extraccion_Datos
}
}
}
PopularTagExtraction();
}
}
}
});
}
private void PopularTagExtraction()
{
var objetosSimulablesCopy = new List<osBase>(_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<osBase>(_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)

View File

@ -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<osBuscarCoincidencias>))]
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
}
}