Compare commits

..

2 Commits

Author SHA1 Message Date
Miguel 6155f8475e UserControl bajo carpeta de UserControl 2024-05-18 23:14:46 +02:00
Miguel f458a031c5 Creada la clase simBase 2024-05-18 17:42:27 +02:00
28 changed files with 125 additions and 115 deletions

View File

@ -11,6 +11,8 @@
<ItemGroup>
<Compile Remove="ObjetosSim\ucBasicExample.xaml.cs" />
<Compile Remove="ObjetosSim\ucTransporteCurva.xaml.cs" />
<Compile Remove="ObjetosSim\UserControls\ucBasicExample.xaml.cs" />
<Compile Remove="ObjetosSim\UserControls\ucTransporteCurva.xaml.cs" />
<Compile Remove="Simulacion\GeometrySimulator.cs" />
</ItemGroup>
@ -30,13 +32,15 @@
<ItemGroup>
<Page Remove="ObjetosSim\ucBasicExample.xaml" />
<Page Remove="ObjetosSim\ucTransporteCurva.xaml" />
<Page Remove="ObjetosSim\UserControls\ucBasicExample.xaml" />
<Page Remove="ObjetosSim\UserControls\ucTransporteCurva.xaml" />
</ItemGroup>
<ItemGroup>
<None Include="ObjetosSim\ucBasicExample.xaml" />
<None Include="ObjetosSim\ucBasicExample.xaml.cs" />
<None Include="ObjetosSim\ucTransporteCurva.xaml" />
<None Include="ObjetosSim\ucTransporteCurva.xaml.cs" />
<None Include="ObjetosSim\UserControls\ucBasicExample.xaml" />
<None Include="ObjetosSim\UserControls\ucBasicExample.xaml.cs" />
<None Include="ObjetosSim\UserControls\ucTransporteCurva.xaml" />
<None Include="ObjetosSim\UserControls\ucTransporteCurva.xaml.cs" />
<None Include="Simulacion\GeometrySimulator.cs" />
</ItemGroup>

View File

@ -10,7 +10,6 @@ using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CtrEditor.ObjetosSim;
using CtrEditor.Convertidores;
using CtrEditor.Siemens;
using static System.Runtime.InteropServices.JavaScript.JSType;
@ -22,6 +21,7 @@ using UserControl = System.Windows.Controls.UserControl;
using CheckBox = System.Windows.Controls.CheckBox;
using Orientation = System.Windows.Controls.Orientation;
using ListBox = System.Windows.Controls.ListBox;
using CtrEditor.ObjetosSim;
namespace CtrEditor

View File

@ -24,7 +24,7 @@ namespace CtrEditor.ObjetosSim
/// Interaction logic for ucBotella.xaml
/// </summary>
///
public class osBotella : osBase
{
private float _diametro;

View File

@ -15,7 +15,8 @@ using System.Windows.Shapes;
using CtrEditor.Convertidores;
using CtrEditor.Siemens;
namespace CtrEditor.ObjetosSim
namespace CtrEditor.ObjetosSim.UserControls
{
/// <summary>
/// Interaction logic for ucTransporteCurva.xaml

View File

@ -213,9 +213,9 @@ namespace CtrEditor.ObjetosSim
// crear el objeto de simulacion
ActualizarLeftTop();
simulationManager.rectangles.Add(TransporteCentral);
simulationManager.lines.Add(Guia_Superior);
simulationManager.lines.Add(Guia_Inferior);
//simulationManager.rectangles.Add(TransporteCentral);
//simulationManager.lines.Add(Guia_Superior);
//simulationManager.lines.Add(Guia_Inferior);
// El UserControl ya se ha cargado y podemos obtener las coordenadas para
// crear el objeto de simulacion

View File

@ -11,7 +11,7 @@ namespace CtrEditor.ObjetosSim
/// Interaction logic for ucTransporteTTop.xaml
/// </summary>
///
public class osTransporteTTop : osBase
{
private string _nombre = "Transporte TTOP";

View File

@ -27,7 +27,7 @@ namespace CtrEditor.ObjetosSim
public interface IosBase
{
string Nombre { get; }
string Nombre { get; }
void UpdateControl(int elapsedMilliseconds);
}
@ -44,7 +44,7 @@ namespace CtrEditor.ObjetosSim
}
public abstract class osBase : INotifyPropertyChanged, IosBase
{
{
public abstract float Left { get; set; }
public abstract float Top { get; set; }
@ -83,9 +83,9 @@ namespace CtrEditor.ObjetosSim
{
foreach (var objetoSimulable in _mainViewModel.ObjetosSimulables)
{
if (tipoOsBase.IsInstanceOfType(objetoSimulable) && ((osBase)objetoSimulable).Nombre == NameLink)
if (tipoOsBase.IsInstanceOfType(objetoSimulable) && objetoSimulable.Nombre == NameLink)
{
return (osBase)objetoSimulable;
return objetoSimulable;
}
}
}
@ -102,8 +102,8 @@ namespace CtrEditor.ObjetosSim
{
if (!string.IsNullOrEmpty(Tag))
{
if (Tag=="1") return true;
else if (Tag=="0") return false;
if (Tag == "1") return true;
else if (Tag == "0") return false;
if (plc != null)
return plc.LeerTagBool(Tag);
}
@ -123,7 +123,7 @@ namespace CtrEditor.ObjetosSim
if (!string.IsNullOrEmpty(Tag))
{
SDataValue plcData = new SDataValue();
plcData.UInt16 = (ushort)(((Value - IN_scale_Min) / (IN_scale_Max - IN_scale_Min) * (OUT_scale_Max - OUT_scale_Min)) + OUT_scale_Min);
plcData.UInt16 = (ushort)((Value - IN_scale_Min) / (IN_scale_Max - IN_scale_Min) * (OUT_scale_Max - OUT_scale_Min) + OUT_scale_Min);
plc.EscribirTag(Tag, plcData);
}
}
@ -138,7 +138,7 @@ namespace CtrEditor.ObjetosSim
{
SDataValue plcData = plc.LeerTag(Tag);
float Value = plcData.UInt16; // WORD
return (((Value - OUT_scale_Min) / (OUT_scale_Max - OUT_scale_Min) * (IN_scale_Max - IN_scale_Min)) + IN_scale_Min);
return (Value - OUT_scale_Min) / (OUT_scale_Max - OUT_scale_Min) * (IN_scale_Max - IN_scale_Min) + IN_scale_Min;
}
}
return 0;
@ -147,7 +147,7 @@ namespace CtrEditor.ObjetosSim
public void CanvasSetLeftinMeter(float left)
{
if (_visualRepresentation != null)
Canvas.SetLeft(_visualRepresentation, PixelToMeter.Instance.calc.MetersToPixels(left));
Canvas.SetLeft(_visualRepresentation, PixelToMeter.Instance.calc.MetersToPixels(left));
}
public float CanvasGetLeftinMeter()
{
@ -243,7 +243,7 @@ namespace CtrEditor.ObjetosSim
var coords = GetCenterLineVectors(wpfRect);
// Crear o actualizar simRectangle
simGuia.Create( coords.Start,coords.End); // asumiendo que el ángulo inicial es 0
simGuia.Create(coords.Start, coords.End); // asumiendo que el ángulo inicial es 0
}
}

View File

@ -18,13 +18,29 @@ using static System.Runtime.InteropServices.JavaScript.JSType;
namespace CtrEditor.Simulacion
{
public class simDescarte
public class simBase
{
public Body Body { get; private set; }
private float _radius;
public Body Body { get; protected set; }
public World _world;
public void RemoverBody()
{
if (Body != null)
{
_world.RemoveBody(Body);
}
}
public void SetPosition(float x, float y)
{
Body.SetTransform(new Vector2(x, y), Body.Rotation);
}
}
public class simDescarte : simBase
{
private float _radius;
public simDescarte(World world, float diameter, Vector2 position)
{
_world = world;
@ -32,23 +48,12 @@ namespace CtrEditor.Simulacion
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 RemoverBody()
{
if (Body != null)
{
_world.RemoveBody(Body);
}
}
public void Create(Vector2 position)
{
RemoverBody();
@ -60,11 +65,10 @@ namespace CtrEditor.Simulacion
}
}
public class simTransporte
public class simTransporte : simBase
{
public Body Body { get; private set; }
public float Speed { get; set; } // Velocidad para efectos de cinta transportadora
public World _world;
public simTransporte(World world, float width, float height, Vector2 position, float angle = 0)
{
@ -78,7 +82,7 @@ namespace CtrEditor.Simulacion
set { Body.Rotation = MathHelper.ToRadians(value); }
}
public void SetPosition(float x, float y)
public new void SetPosition(float x, float y)
{
Body.Position = new Vector2(x, y);
}
@ -95,13 +99,6 @@ namespace CtrEditor.Simulacion
var newShape = new PolygonShape(PolygonTools.CreateRectangle(width / 2, height / 2), 1f);
Body.CreateFixture(newShape);
}
public void RemoverBody()
{
if (Body != null)
{
_world.RemoveBody(Body);
}
}
public void Create(float width, float height, Vector2 position, float angle = 0)
{
RemoverBody();
@ -113,23 +110,55 @@ namespace CtrEditor.Simulacion
}
}
public class simGuia
public class simBarrera : simBase
{
public Body Body { get; private set; }
public World _world;
public bool LuzCortada = false;
public simBarrera(World world, float width, float height, Vector2 position, float angle = 0)
{
_world = world;
Create(width, height, position, angle);
}
public float Angle
{
get { return MathHelper.ToDegrees(Body.Rotation); }
set { Body.Rotation = MathHelper.ToRadians(value); }
}
public new void SetPosition(float x, float y)
{
Body.Position = new Vector2(x, y);
}
public void SetDimensions(float width, float height)
{
Body.DestroyFixture(Body.FixtureList[0]);
var newShape = new PolygonShape(PolygonTools.CreateRectangle(width / 2, height / 2), 1f);
Body.CreateFixture(newShape);
}
public void Create(float width, float height, Vector2 position, float angle = 0)
{
RemoverBody();
Body = BodyFactory.CreateRectangle(_world, width, height, 1f, position);
Body.FixtureList[0].IsSensor = true;
Body.BodyType = BodyType.Static;
Body.Rotation = MathHelper.ToRadians(angle);
Body.UserData = this; // Importante para la identificación durante la colisión
LuzCortada = false;
}
}
public class simGuia : simBase
{
public simGuia(World world, Vector2 start, Vector2 end)
{
_world = world;
Create(start, end);
}
public void RemoverBody()
{
if (Body != null)
{
_world.RemoveBody(Body);
}
}
public void Create(Vector2 start, Vector2 end)
{
RemoverBody();
@ -144,10 +173,8 @@ namespace CtrEditor.Simulacion
}
}
public class simBotella
public class simBotella : simBase
{
public Body Body { get; private set; }
public World _world;
private float _radius;
private float _mass;
public bool Descartar = false;
@ -187,13 +214,7 @@ namespace CtrEditor.Simulacion
}
set { _mass = value; }
}
public void RemoverBody()
{
if (Body != null)
{
_world.RemoveBody(Body);
}
}
private void Create(Vector2 position)
{
RemoverBody();
@ -202,7 +223,7 @@ namespace CtrEditor.Simulacion
// Restablecer manejador de eventos de colisión
Body.OnCollision += HandleCollision;
//Body.OnSeparation += HandleOnSeparation;
Body.OnSeparation += HandleOnSeparation;
Body.UserData = this; // Importante para la identificación durante la colisión
@ -213,12 +234,7 @@ namespace CtrEditor.Simulacion
Body.LinearDamping = 0f; // Ajustar para controlar la reducción de la velocidad lineal
Body.AngularDamping = 0f; // Ajustar para controlar la reducción de la velocidad angular
Body.Restitution = 0.2f; // Baja restitución para menos rebote
Body.IsBullet = true;
}
public void SetPosition(float x, float y)
{
Body.SetTransform(new Vector2(x, y), Body.Rotation);
// Body.IsBullet = true;
}
public void SetDiameter(float diameter)
@ -234,7 +250,12 @@ namespace CtrEditor.Simulacion
private bool HandleCollision(Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact)
{
if (fixtureB.Body.UserData is simDescarte)
if (fixtureB.Body.UserData is simBarrera Sensor)
{
Sensor.LuzCortada = true;
return true;
}
else if (fixtureB.Body.UserData is simDescarte)
{
Descartar = true;
return true;
@ -274,7 +295,8 @@ namespace CtrEditor.Simulacion
private void HandleOnSeparation(Fixture fixtureA, Fixture fixtureB)
{
// Aquí puedes restablecer cualquier estado si es necesario al separarse de un simRectangle
if (fixtureB.Body.UserData is simBarrera Sensor)
Sensor.LuzCortada = false;
}
private void ApplyConveyorEffect(simTransporte conveyor, Fixture circleFixture, float porcentajeCompartido)
@ -289,10 +311,8 @@ namespace CtrEditor.Simulacion
{
private World world;
private Canvas simulationCanvas;
public List<simBotella> circles;
public List<simTransporte> rectangles;
public List<simGuia> lines;
public List<simDescarte> descartes;
public List<simBase> Cuerpos;
public Stopwatch stopwatch;
public Canvas DebugCanvas { get => simulationCanvas; set => simulationCanvas = value; }
@ -300,19 +320,16 @@ namespace CtrEditor.Simulacion
public SimulationManagerFP()
{
world = new World(new Vector2(0, 0)); // Vector2.Zero
circles = new List<simBotella>();
rectangles = new List<simTransporte>();
lines = new List<simGuia>();
descartes = new List<simDescarte>();
Cuerpos = new List<simBase>();
stopwatch = new Stopwatch();
}
public void Clear()
{
circles.Clear();
rectangles.Clear();
lines.Clear();
world.Clear();
if (world.BodyList.Count > 0)
world.Clear();
if (Cuerpos.Count > 0)
Cuerpos.Clear();
}
public void Step()
@ -328,56 +345,44 @@ namespace CtrEditor.Simulacion
world.Step(elapsedMilliseconds / 1000.0f);
}
public void Remove(object Objeto)
public void Remove(simBase Objeto)
{
switch (Objeto)
{
case simBotella obj:
obj.RemoverBody();
circles.Remove(obj);
break;
case simTransporte obj:
obj.RemoverBody();
rectangles.Remove(obj);
break;
case simGuia obj:
obj.RemoverBody();
lines.Remove(obj);
break;
case simDescarte obj:
obj.RemoverBody();
descartes.Remove(obj);
break;
default:
throw new InvalidOperationException("Tipo no soportado");
}
Objeto.RemoverBody();
Cuerpos.Remove(Objeto);
}
public simBotella AddCircle(float diameter, Vector2 position, float mass)
{
simBotella circle = new simBotella(world, diameter, position, mass);
circles.Add(circle);
Cuerpos.Add(circle);
return circle;
}
public simTransporte AddRectangle(float width, float height, Vector2 position, float angle)
{
simTransporte rectangle = new simTransporte(world, width, height, position, angle);
rectangles.Add(rectangle);
Cuerpos.Add(rectangle);
return rectangle;
}
public simTransporte AddBarrera(float width, float height, Vector2 position, float angle)
{
simTransporte rectangle = new simTransporte(world, width, height, position, angle);
Cuerpos.Add(rectangle);
return rectangle;
}
public simGuia AddLine(Vector2 start, Vector2 end)
{
simGuia line = new simGuia(world, start, end);
lines.Add(line);
Cuerpos.Add(line);
return line;
}
public simDescarte AddDescarte(float diameter, Vector2 position)
{
simDescarte descarte = new simDescarte(world, diameter, position);
descartes.Add(descarte);
Cuerpos.Add(descarte);
return descarte;
}