Mejorado el sistema de SaveStateObjetosSimulables y trabajando en el usercontrol Descarte

This commit is contained in:
Miguel 2024-05-18 14:58:41 +02:00
parent ebe7986142
commit 9ed8a0b7bd
17 changed files with 575 additions and 105 deletions

View File

@ -249,19 +249,23 @@ namespace CtrEditor
{
if (parameter is TipoSimulable tipoSimulable)
{
CrearObjetoSimulable(tipoSimulable.Tipo);
CrearObjetoSimulableEnCentroCanvas(tipoSimulable.Tipo);
}
}
public void CrearObjetoSimulable(Type tipoSimulable)
public void CrearObjetoSimulableEnCentroCanvas(Type tipoSimulable)
{
var CentroCanvas = MainWindow.ObtenerCentroCanvasMeters();
CrearObjetoSimulable(tipoSimulable,CentroCanvas.X,CentroCanvas.Y);
}
public osBase CrearObjetoSimulable(Type tipoSimulable,float Left,float Top)
{
// Crear una nueva instancia del osBase correspondiente
osBase? NuevoOsBase = UserControlFactory.GetInstanceForType(tipoSimulable);
var CentroCanvas = MainWindow.ObtenerCentroCanvasPixels();
NuevoOsBase.Left = CentroCanvas.X;
NuevoOsBase.Top = CentroCanvas.Y;
NuevoOsBase.Left = Left;
NuevoOsBase.Top = Top;
if (NuevoOsBase != null)
{
@ -269,6 +273,7 @@ namespace CtrEditor
// Añadir el nuevo osBase a la colección de objetos simulables
ObjetosSimulables.Add(NuevoOsBase);
}
return NuevoOsBase;
}
// Crear UserControl desde osBase : Nuevo o desde Deserealizacion
@ -292,6 +297,16 @@ namespace CtrEditor
return false;
}
public void RemoverObjetoSimulable(osBase osObjeto)
{
if (osObjeto != null && ObjetosSimulables.Contains(osObjeto))
{
ObjetosSimulables.Remove(osObjeto);
if (osObjeto.VisualRepresentation != null)
MainWindow.EliminarUserControlDelCanvas(osObjeto.VisualRepresentation);
}
}
private void InitializeTipoSimulableList()
{
var baseType = typeof(osBase);
@ -340,8 +355,15 @@ namespace CtrEditor
simulationManager.Step();
foreach (var objetoSimulable in ObjetosSimulables)
var objetosSimulablesCopy = new List<osBase>(ObjetosSimulables);
foreach (var objetoSimulable in objetosSimulablesCopy)
{
if (!objetoSimulable.RemoverDesdeSimulacion)
objetoSimulable.UpdateControl((int)elapsedMilliseconds);
else
RemoverObjetoSimulable(objetoSimulable);
}
}
@ -393,8 +415,6 @@ namespace CtrEditor
public void Save()
{
SaveStateObjetosSimulables();
ImageSelected?.Invoke(this, datosDeTrabajo.Imagenes[_selectedImage]); // Dispara el evento con la nueva ruta de imagen
LoadStateObjetosSimulables();
}
public void SaveStateObjetosSimulables()
@ -403,19 +423,33 @@ namespace CtrEditor
{
StopSimulation();
PLCViewModel.Disconnect();
// Crear copias temporales de las propiedades que serán anuladas
var tempVisualRepresentations = new Dictionary<osBase, UserControl>();
var tempSimulationManagers = new Dictionary<osBase, SimulationManagerFP>();
var tempMainViewModels = new Dictionary<osBase, MainViewModel>();
foreach (var obj in ObjetosSimulables)
{
// Guardar referencias temporales
tempVisualRepresentations[obj] = obj.VisualRepresentation;
tempSimulationManagers[obj] = obj.simulationManager;
tempMainViewModels[obj] = obj._mainViewModel;
// Anular propiedades para la serialización
obj.VisualRepresentation = null;
obj.simulationManager = null;
obj._mainViewModel = null;
}
var settings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore,
TypeNameHandling = TypeNameHandling.Auto
};
foreach (var obj in ObjetosSimulables)
{
obj.VisualRepresentation = null;
obj.simulationManager = null;
obj._mainViewModel = null;
}
// Crear un objeto que incluya tanto los ObjetosSimulables como el UnitConverter
// Crear un objeto que incluya tanto los ObjetosSimulables como el UnitConverter y PLC_ConnectionData
var dataToSerialize = new SimulationData
{
ObjetosSimulables = ObjetosSimulables,
@ -423,10 +457,21 @@ namespace CtrEditor
PLC_ConnectionData = PLCViewModel
};
// Serializar
var serializedData = JsonConvert.SerializeObject(dataToSerialize, settings);
File.WriteAllText(datosDeTrabajo.ObtenerPathImagenConExtension(_selectedImage, ".json"), serializedData);
// Restaurar las propiedades originales de los objetos
foreach (var obj in ObjetosSimulables)
{
obj.VisualRepresentation = tempVisualRepresentations[obj];
obj.simulationManager = tempSimulationManagers[obj];
obj._mainViewModel = tempMainViewModels[obj];
}
}
}
public void LoadStateObjetosSimulables()
{

View File

@ -118,6 +118,14 @@ namespace CtrEditor
}
}
public void EliminarUserControlDelCanvas(UserControl userControl)
{
if (ImagenEnTrabajoCanvas.Children.Contains(userControl))
{
ImagenEnTrabajoCanvas.Children.Remove(userControl);
}
}
private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (!_isDrawingCanvas)

View File

@ -6,6 +6,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using CtrEditor.Simulacion;
using System.Reflection;
namespace CtrEditor.ObjetosSim
{
@ -13,60 +14,42 @@ namespace CtrEditor.ObjetosSim
{
public static UserControl? GetControlForType(Type tipoObjeto)
{
if (tipoObjeto == typeof(osBotella))
return new ucBotella();
if (tipoObjeto == typeof(osTransporteTTop))
return new ucTransporteTTop();
if (tipoObjeto == typeof(osGuia))
return new ucGuia();
if (tipoObjeto == typeof(osTransporteGuias))
return new ucTransporteGuias();
//if (tipoObjeto == typeof(osTransporteCurva))
// return new ucTransporteCurva();
if (tipoObjeto == typeof(osVMmotorSim ))
return new ucVMmotorSim();
if (tipoObjeto == typeof(osBoton))
return new ucBoton();
if (tipoObjeto == typeof(osTanque))
return new ucTanque();
if (tipoObjeto == typeof(osSensTemperatura))
return new ucSensTemperatura();
if (tipoObjeto == typeof(osFiller))
return new ucFiller();
// Obtener el nombre del tipo de objeto
string typeName = tipoObjeto.Name;
// Cambiar las primeras dos letras de 'os' a 'uc'
if (typeName.StartsWith("os"))
{
string newTypeName = "uc" + typeName.Substring(2);
// Puedes añadir más condiciones para otros tipos
// Obtener el ensamblado donde se encuentra el tipo UserControl
Assembly assembly = Assembly.GetExecutingAssembly();
// Buscar el tipo en los ensamblados cargados
Type? controlType = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(a => a.GetTypes())
.FirstOrDefault(t => t.Name == newTypeName);
if (controlType != null && typeof(UserControl).IsAssignableFrom(controlType))
{
// Crear una instancia del tipo encontrado
return (UserControl?)Activator.CreateInstance(controlType);
}
}
return null;
}
public static osBase? GetInstanceForType(Type tipoObjeto)
{
if (tipoObjeto == typeof(osBotella))
return new osBotella();
if (tipoObjeto == typeof(osTransporteTTop))
return new osTransporteTTop();
if (tipoObjeto == typeof(osGuia))
return new osGuia();
if (tipoObjeto == typeof(osTransporteGuias))
return new osTransporteGuias();
//if (tipoObjeto == typeof(osTransporteCurva))
// return new osTransporteCurva();
if (tipoObjeto == typeof(osVMmotorSim))
return new osVMmotorSim();
if (tipoObjeto == typeof(osBoton))
return new osBoton();
if (tipoObjeto == typeof(osTanque))
return new osTanque();
if (tipoObjeto == typeof(osSensTemperatura))
return new osSensTemperatura();
if (tipoObjeto == typeof(osFiller))
return new osFiller();
// Verifica si el tipo pasado es un subtipo de osBase
if (!typeof(osBase).IsAssignableFrom(tipoObjeto))
{
throw new ArgumentException("El tipo pasado no es un subtipo de osBase", nameof(tipoObjeto));
}
// Puedes añadir más condiciones para otros tipos
return null;
// Crear una instancia del tipo especificado
return (osBase?)Activator.CreateInstance(tipoObjeto);
}
public static osBase? CreateInstanceAndPopulate(Type tipoObjeto, string jsonString)

View File

@ -49,6 +49,8 @@ namespace CtrEditor.ObjetosSim
public abstract float Top { get; set; }
public bool Inicializado = false;
public bool AutoCreated = false;
public bool RemoverDesdeSimulacion = false; // La simulacion indica que se debe remover
[JsonIgnore]
protected UserControl? _visualRepresentation = null;
@ -60,6 +62,7 @@ namespace CtrEditor.ObjetosSim
public abstract void UpdateGeometryStep();
public abstract void UpdatePLC(PLCModel plc, int elapsedMilliseconds);
public abstract void ucLoaded();
public abstract void ucUnLoaded();
[JsonIgnore]
public MainViewModel _mainViewModel;
@ -222,18 +225,18 @@ namespace CtrEditor.ObjetosSim
return new Vector2((topLeft.X + bottomRight.X) / 2, (topLeft.Y + bottomRight.Y) / 2);
}
public void UpdateRectangle(simRectangle simRect, System.Windows.Shapes.Rectangle wpfRect, float Alto, float Ancho, float Angulo)
public void UpdateRectangle(simTransporte simRect, System.Windows.Shapes.Rectangle wpfRect, float Alto, float Ancho, float Angulo)
{
if (simRect != null)
simRect.Create(Ancho, Alto, GetRectangleCenter(wpfRect), Angulo);
}
public simRectangle AddRectangle(SimulationManagerFP simulationManager, System.Windows.Shapes.Rectangle wpfRect, float Alto, float Ancho, float Angulo)
public simTransporte AddRectangle(SimulationManagerFP simulationManager, System.Windows.Shapes.Rectangle wpfRect, float Alto, float Ancho, float Angulo)
{
return simulationManager.AddRectangle(Ancho, Alto, GetRectangleCenter(wpfRect), Angulo);
}
public void UpdateOrCreateLine(simLine simGuia, System.Windows.Shapes.Rectangle wpfRect)
public void UpdateOrCreateLine(simGuia simGuia, System.Windows.Shapes.Rectangle wpfRect)
{
if (simGuia != null)
{
@ -244,7 +247,7 @@ namespace CtrEditor.ObjetosSim
}
}
public simLine AddLine(SimulationManagerFP simulationManager, System.Windows.Shapes.Rectangle wpfRect)
public simGuia AddLine(SimulationManagerFP simulationManager, System.Windows.Shapes.Rectangle wpfRect)
{
var coords = GetCenterLineVectors(wpfRect);
return simulationManager.AddLine(coords.Start, coords.End);

View File

@ -122,6 +122,12 @@ namespace CtrEditor.ObjetosSim
// crear el objeto de simulacion
ActualizarLeftTop();
}
public override void ucUnLoaded()
{
// El UserControl se esta eliminando
// eliminar el objeto de simulacion
}
}
@ -133,11 +139,16 @@ namespace CtrEditor.ObjetosSim
{
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 Resize(float width, float height) {
if (Datos is osBasicExample datos)
{

View File

@ -31,7 +31,7 @@ namespace CtrEditor.ObjetosSim
private float _mass;
private Vector2 _centro = new Vector2(); // Centro
private string _nombre = "Botella";
private simCircle Simulacion_Botella;
private simBotella Simulacion_Botella;
// Otros datos y métodos relevantes para la simulación
@ -63,6 +63,7 @@ namespace CtrEditor.ObjetosSim
{
_centro.X = value+Diametro/2;
CanvasSetLeftinMeter(value);
OnPropertyChanged(nameof(CenterX));
OnPropertyChanged(nameof(Left));
}
}
@ -73,6 +74,7 @@ namespace CtrEditor.ObjetosSim
{
_centro.Y = value + Diametro / 2;
CanvasSetTopinMeter(value);
OnPropertyChanged(nameof(CenterY));
OnPropertyChanged(nameof(Top));
}
}
@ -145,7 +147,11 @@ namespace CtrEditor.ObjetosSim
{
CenterX = Simulacion_Botella.CenterX;
CenterY = Simulacion_Botella.CenterY;
if (Simulacion_Botella.Descartar) // Ha sido marcada para remover
RemoverDesdeSimulacion = true;
}
public override void ucLoaded()
{
// El UserControl ya se ha cargado y podemos obtener las coordenadas para
@ -153,6 +159,12 @@ namespace CtrEditor.ObjetosSim
ActualizarLeftTop();
Simulacion_Botella = simulationManager.AddCircle(Diametro, _centro, Mass);
}
public override void ucUnLoaded()
{
// El UserControl se esta eliminando
// eliminar el objeto de simulacion
simulationManager.Remove(Simulacion_Botella);
}
}
@ -164,11 +176,17 @@ namespace CtrEditor.ObjetosSim
{
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 Resize(float width, float height) { }
public void Move(float LeftPixels, float TopPixels)
{

View File

@ -164,6 +164,11 @@ namespace CtrEditor.ObjetosSim
// crear el objeto de simulacion
ActualizarLeftTop();
}
public override void ucUnLoaded()
{
// El UserControl se esta eliminando
// eliminar el objeto de simulacion
}
}
@ -174,14 +179,18 @@ namespace CtrEditor.ObjetosSim
public ucBoton()
{
InitializeComponent();
this.Loaded += OnLoaded;
this.DataContextChanged += OnDataContextChanged;
this.Loaded += OnLoaded;
this.Unloaded += OnUnloaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
Datos?.ucLoaded();
}
private void OnUnloaded(object sender, RoutedEventArgs e)
{
Datos?.ucUnLoaded();
}
private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{

View File

@ -0,0 +1,37 @@
<UserControl x:Class="CtrEditor.ObjetosSim.ucDescarte"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
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"
xmlns:convert="clr-namespace:CtrEditor.Convertidores">
<UserControl.Resources>
<convert:MeterToPixelConverter x:Key="MeterToPixelConverter"/>
<Storyboard x:Key="PulsingStoryboard" RepeatBehavior="Forever">
<DoubleAnimation
Storyboard.TargetName="PulsingEllipse"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
From="1" To="0.5" Duration="0:0:1" AutoReverse="True" />
<DoubleAnimation
Storyboard.TargetName="PulsingEllipse"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"
From="1" To="0.5" Duration="0:0:1" AutoReverse="True" />
</Storyboard>
</UserControl.Resources>
<Grid>
<Ellipse x:Name="PulsingEllipse"
Height="{Binding Diametro, Converter={StaticResource MeterToPixelConverter}}"
Stroke="Yellow"
Fill="Black"
Opacity="0.5"
Width="{Binding Diametro, Converter={StaticResource MeterToPixelConverter}}">
<Ellipse.RenderTransform>
<TransformGroup>
<ScaleTransform />
</TransformGroup>
</Ellipse.RenderTransform>
</Ellipse>
</Grid>
</UserControl>

View File

@ -0,0 +1,180 @@
using CtrEditor.Convertidores;
using CtrEditor.Siemens;
using CtrEditor.Simulacion;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Xna.Framework;
namespace CtrEditor.ObjetosSim
{
/// <summary>
/// Interaction logic for ucDescarte.xaml
/// </summary>
public class osDescarte : osBase
{
// Otros datos y métodos relevantes para la simulación
private string _nombre = "Descarte";
private float _diametro;
private Vector2 _centro = new Vector2(); // Centro
private simDescarte AreaDeDescarte;
public override float Left
{
get => _centro.X - Diametro / 2;
set
{
_centro.X = value + Diametro / 2;
CanvasSetLeftinMeter(value);
OnPropertyChanged(nameof(CenterX));
OnPropertyChanged(nameof(Left));
}
}
public override float Top
{
get => _centro.Y - Diametro / 2;
set
{
_centro.Y = value + Diametro / 2;
CanvasSetTopinMeter(value);
OnPropertyChanged(nameof(CenterY));
OnPropertyChanged(nameof(Top));
}
}
public float CenterX
{
get => _centro.X;
set
{
_centro.X = value;
CanvasSetLeftinMeter(Left);
OnPropertyChanged(nameof(CenterX));
OnPropertyChanged(nameof(Left));
}
}
public float CenterY
{
get => _centro.Y;
set
{
_centro.Y = value;
CanvasSetTopinMeter(Top);
OnPropertyChanged(nameof(CenterY));
OnPropertyChanged(nameof(Top));
}
}
public float Diametro
{
get => _diametro;
set
{
_diametro = value;
AreaDeDescarte?.SetDiameter(Diametro);
OnPropertyChanged(nameof(Diametro));
}
}
public override string Nombre
{
get => _nombre;
set
{
if (_nombre != value)
{
_nombre = value;
OnPropertyChanged(nameof(Nombre));
}
}
}
private void ActualizarGeometrias()
{
if (AreaDeDescarte != null)
{
AreaDeDescarte.SetDiameter(Diametro);
AreaDeDescarte.SetPosition(CenterX, CenterY);
}
}
public osDescarte()
{
Diametro = 1f;
}
public override void UpdateGeometryStart()
{
// Se llama antes de la simulacion
ActualizarGeometrias();
}
public override void UpdateGeometryStep()
{
}
public override void UpdatePLC(PLCModel plc, int elapsedMilliseconds)
{
}
public override void UpdateControl(int elapsedMilliseconds)
{
}
public override void ucLoaded()
{
// El UserControl ya se ha cargado y podemos obtener las coordenadas para
// crear el objeto de simulacion
ActualizarLeftTop();
AreaDeDescarte = simulationManager.AddDescarte(Diametro, _centro);
}
public override void ucUnLoaded()
{
// El UserControl se esta eliminando
// eliminar el objeto de simulacion
simulationManager.Remove(AreaDeDescarte);
}
}
public partial class ucDescarte : UserControl, IDataContainer
{
public osBase? Datos { get; set; }
public ucDescarte()
{
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 Resize(float width, float height)
{
if (Datos is osDescarte datos)
{
datos.Diametro = 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)
{
}
public void Highlight(bool State) { }
public int ZIndex()
{
return 10;
}
}
}

View File

@ -38,7 +38,28 @@ namespace CtrEditor.ObjetosSim
private string _tag_consenso;
private bool _consenso;
private float TiempoRestante;
private List<simCircle> Botellas = new List<simCircle>();
private float _leftSalida;
private float _topSalida;
private List<osBotella> Botellas = new List<osBotella>();
public float OffsetLeftSalida
{
get => _leftSalida;
set
{
_leftSalida = value;
OnPropertyChanged(nameof(OffsetLeftSalida));
}
}
public float OffsetTopSalida
{
get => _topSalida;
set
{
_topSalida = value;
OnPropertyChanged(nameof(OffsetTopSalida));
}
}
public bool Consenso
{
@ -175,26 +196,36 @@ namespace CtrEditor.ObjetosSim
public override void UpdateControl(int elapsedMilliseconds)
{
if (Consenso && Velocidad_actual_percentual>0)
if (Consenso && Velocidad_actual_percentual > 0)
{
TiempoRestante -= elapsedMilliseconds / 1000.0f;
if (TiempoRestante <= 0)
{
TiempoRestante = Botellas_hora * (Velocidad_actual_percentual / 100.0f) / 3600.0f;
var PosSalida = new Vector2(Left, Top);
TiempoRestante = 3600 / (Botellas_hora * (Velocidad_actual_percentual / 100.0f));
var UltimaBotellla = GetLastElement<simCircle>(Botellas);
if (UltimaBotellla != null && UltimaBotellla.Center != PosSalida)
Botellas.Add(simulationManager.AddCircle(Diametro_botella, PosSalida, 1));
var UltimaBotellla = GetLastElement<osBotella>(Botellas);
if (UltimaBotellla == null || (UltimaBotellla != null && !(UltimaBotellla.Left == Left && UltimaBotellla.Top == Top)))
{
var Botella = _mainViewModel.CrearObjetoSimulable(typeof(osBotella), Left + OffsetLeftSalida, Top + OffsetTopSalida);
Botella.AutoCreated = true;
Botellas.Add((osBotella)Botella);
}
}
}
else
TiempoRestante = 0;
}
public override void ucLoaded()
{
// El UserControl ya se ha cargado y podemos obtener las coordenadas para
// crear el objeto de simulacion
ActualizarLeftTop();
}
public override void ucUnLoaded()
{
// El UserControl se esta eliminando
// eliminar el objeto de simulacion
}
}
@ -206,11 +237,16 @@ namespace CtrEditor.ObjetosSim
{
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 Resize(float width, float height)
{
if (Datos is osFiller datos)

View File

@ -33,7 +33,7 @@ namespace CtrEditor.ObjetosSim
private float _angulo;
private string _nombre = "Guia";
private simLine Simulation_Guia;
private simGuia Simulation_Guia;
public override float Left
{
@ -131,6 +131,12 @@ namespace CtrEditor.ObjetosSim
if (_visualRepresentation is ucGuia uc)
Simulation_Guia = AddLine(simulationManager, uc.Guia);
}
public override void ucUnLoaded()
{
// El UserControl se esta eliminando
// eliminar el objeto de simulacion
simulationManager.Remove(Simulation_Guia);
}
}
@ -143,11 +149,16 @@ namespace CtrEditor.ObjetosSim
{
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 Resize(float width, float height)
{
if (Datos is osGuia datos)

View File

@ -172,6 +172,11 @@ namespace CtrEditor.ObjetosSim
ActualizarLeftTop();
}
public override void ucUnLoaded()
{
// El UserControl se esta eliminando
// eliminar el objeto de simulacion
}
}
@ -183,11 +188,16 @@ namespace CtrEditor.ObjetosSim
{
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 Resize(float width, float height)
{
if (Datos is osSensTemperatura datos)

View File

@ -290,6 +290,11 @@ namespace CtrEditor.ObjetosSim
ActualizarLeftTop();
}
public override void ucUnLoaded()
{
// El UserControl se esta eliminando
// eliminar el objeto de simulacion
}
}
@ -301,11 +306,16 @@ namespace CtrEditor.ObjetosSim
{
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 Resize(float width, float height)
{
if (Datos is osTanque datos)

View File

@ -43,9 +43,9 @@ namespace CtrEditor.ObjetosSim
private osBase _osMotor = null;
private string _motor;
private simRectangle? TransporteCentral;
private simLine? Guia_Superior;
private simLine? Guia_Inferior;
private simTransporte? TransporteCentral;
private simGuia? Guia_Superior;
private simGuia? Guia_Inferior;
@ -227,6 +227,14 @@ namespace CtrEditor.ObjetosSim
}
Motor = Motor; // Forzar la busqueda
}
public override void ucUnLoaded()
{
// El UserControl se esta eliminando
// eliminar el objeto de simulacion
simulationManager.Remove(TransporteCentral);
simulationManager.Remove(Guia_Superior);
simulationManager.Remove(Guia_Inferior);
}
}
@ -239,11 +247,16 @@ namespace CtrEditor.ObjetosSim
{
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 Resize(float width, float height)
{
if (Datos is osTransporteGuias datos)

View File

@ -30,7 +30,7 @@ namespace CtrEditor.ObjetosSim
private osBase _osMotor = null;
private string _motor;
private simRectangle Simulation_Transporte;
private simTransporte Simulation_Transporte;
public string Motor
{
@ -168,6 +168,12 @@ namespace CtrEditor.ObjetosSim
if (_visualRepresentation is ucTransporteTTop uc)
Simulation_Transporte = AddRectangle(simulationManager, uc.Transporte, Alto, Ancho, Angulo);
}
public override void ucUnLoaded()
{
// El UserControl se esta eliminando
// eliminar el objeto de simulacion
simulationManager.Remove(Simulation_Transporte);
}
}
@ -179,11 +185,16 @@ namespace CtrEditor.ObjetosSim
{
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 Resize(float width, float height)
{
if (Datos is osTransporteTTop datos)

View File

@ -201,7 +201,11 @@ namespace CtrEditor.ObjetosSim
ActualizarLeftTop();
}
public override void ucUnLoaded()
{
// El UserControl se esta eliminando
// eliminar el objeto de simulacion
}
}
public partial class ucVMmotorSim : UserControl, IDataContainer
@ -212,11 +216,16 @@ namespace CtrEditor.ObjetosSim
{
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 Resize(float width, float height) { }
public void Move(float LeftPixels, float TopPixels)
{

View File

@ -13,16 +13,57 @@ using FarseerPhysics.Common;
using System.Windows;
using System.Diagnostics;
using System.Windows.Documents;
using CtrEditor.ObjetosSim;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace CtrEditor.Simulacion
{
public class simRectangle
public class simDescarte
{
public Body Body { get; private set; }
private float _radius;
public World _world;
public simDescarte(World world, float diameter, Vector2 position)
{
_world = world;
_radius = diameter / 2;
Create(position);
}
public void SetPosition(float x, float y)
{
Body.SetTransform(new Vector2(x, y), Body.Rotation);
}
public void SetDiameter(float diameter)
{
_radius = diameter / 2;
Create(Body.Position); // Recrear el círculo con el nuevo tamaño
}
public void Create(Vector2 position)
{
if (Body != null)
{
_world.RemoveBody(Body);
}
Body = BodyFactory.CreateCircle(_world, _radius, 1f, position);
Body.FixtureList[0].IsSensor = true;
Body.BodyType = BodyType.Static;
Body.UserData = this; // Importante para la identificación durante la colisión
}
}
public class simTransporte
{
public Body Body { get; private set; }
public float Speed { get; set; } // Velocidad para efectos de cinta transportadora
public World _world;
public simRectangle(World world, float width, float height, Vector2 position, float angle = 0)
public simTransporte(World world, float width, float height, Vector2 position, float angle = 0)
{
_world = world;
Create(width, height, position, angle);
@ -66,12 +107,12 @@ namespace CtrEditor.Simulacion
}
}
public class simLine
public class simGuia
{
public Body Body { get; private set; }
public World _world;
public simLine(World world, Vector2 start, Vector2 end)
public simGuia(World world, Vector2 start, Vector2 end)
{
_world = world;
Create(start, end);
@ -94,14 +135,15 @@ namespace CtrEditor.Simulacion
}
}
public class simCircle
public class simBotella
{
public Body Body { get; private set; }
public World _world;
private float _radius;
private float _mass;
public bool Descartar = false;
public simCircle(World world, float diameter, Vector2 position, float mass)
public simBotella(World world, float diameter, Vector2 position, float mass)
{
_world = world;
_radius = diameter / 2;
@ -180,9 +222,13 @@ namespace CtrEditor.Simulacion
private bool HandleCollision(Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact)
{
if (fixtureB.Body.UserData is simRectangle)
if (fixtureB.Body.UserData is simDescarte)
{
simRectangle conveyor = fixtureB.Body.UserData as simRectangle;
Descartar = true;
return true;
} else if (fixtureB.Body.UserData is simTransporte)
{
simTransporte conveyor = fixtureB.Body.UserData as simTransporte;
CircleShape circleShape = fixtureA.Shape as CircleShape;
PolygonShape polygonShape = fixtureB.Shape as PolygonShape;
@ -219,7 +265,7 @@ namespace CtrEditor.Simulacion
// Aquí puedes restablecer cualquier estado si es necesario al separarse de un simRectangle
}
private void ApplyConveyorEffect(simRectangle conveyor, Fixture circleFixture, float porcentajeCompartido)
private void ApplyConveyorEffect(simTransporte conveyor, Fixture circleFixture, float porcentajeCompartido)
{
float speedMetersPerSecond = conveyor.Speed / 60.0f;
Vector2 desiredVelocity = new Vector2((float)Math.Cos(conveyor.Body.Rotation), (float)Math.Sin(conveyor.Body.Rotation)) * speedMetersPerSecond;
@ -231,9 +277,10 @@ namespace CtrEditor.Simulacion
{
private World world;
private Canvas simulationCanvas;
public List<simCircle> circles;
public List<simRectangle> rectangles;
public List<simLine> lines;
public List<simBotella> circles;
public List<simTransporte> rectangles;
public List<simGuia> lines;
public List<simDescarte> descartes;
public Stopwatch stopwatch;
public Canvas DebugCanvas { get => simulationCanvas; set => simulationCanvas = value; }
@ -241,9 +288,10 @@ namespace CtrEditor.Simulacion
public SimulationManagerFP()
{
world = new World(new Vector2(0, 0)); // Vector2.Zero
circles = new List<simCircle>();
rectangles = new List<simRectangle>();
lines = new List<simLine>();
circles = new List<simBotella>();
rectangles = new List<simTransporte>();
lines = new List<simGuia>();
descartes = new List<simDescarte>();
stopwatch = new Stopwatch();
}
@ -268,27 +316,55 @@ namespace CtrEditor.Simulacion
world.Step(elapsedMilliseconds / 1000.0f);
}
public simCircle AddCircle(float diameter, Vector2 position, float mass)
public void Remove(object Objeto)
{
simCircle circle = new simCircle(world, diameter, position, mass);
switch (Objeto)
{
case simBotella obj:
circles.Remove(obj);
break;
case simTransporte obj:
rectangles.Remove(obj);
break;
case simGuia obj:
lines.Remove(obj);
break;
case simDescarte obj:
descartes.Remove(obj);
break;
default:
throw new InvalidOperationException("Tipo no soportado");
}
}
public simBotella AddCircle(float diameter, Vector2 position, float mass)
{
simBotella circle = new simBotella(world, diameter, position, mass);
circles.Add(circle);
return circle;
}
public simRectangle AddRectangle(float width, float height, Vector2 position, float angle)
public simTransporte AddRectangle(float width, float height, Vector2 position, float angle)
{
simRectangle rectangle = new simRectangle(world, width, height, position, angle);
simTransporte rectangle = new simTransporte(world, width, height, position, angle);
rectangles.Add(rectangle);
return rectangle;
}
public simLine AddLine(Vector2 start, Vector2 end)
public simGuia AddLine(Vector2 start, Vector2 end)
{
simLine line = new simLine(world, start, end);
simGuia line = new simGuia(world, start, end);
lines.Add(line);
return line;
}
public simDescarte AddDescarte(float diameter, Vector2 position)
{
simDescarte descarte = new simDescarte(world, diameter, position);
descartes.Add(descarte);
return descarte;
}
public void Debug_DrawInitialBodies()
{
ClearSimulationShapes();