Compare commits

...

2 Commits

19 changed files with 571 additions and 52 deletions

View File

@ -15,6 +15,11 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Remove="Icons\connect.png" />
<None Remove="Icons\disconnect.png" />
<None Remove="Icons\save.png" />
<None Remove="Icons\start.png" />
<None Remove="Icons\stop.png" />
<None Remove="motor2.png" /> <None Remove="motor2.png" />
<None Remove="tank.png" /> <None Remove="tank.png" />
</ItemGroup> </ItemGroup>
@ -48,6 +53,11 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Resource Include="Icons\connect.png" />
<Resource Include="Icons\disconnect.png" />
<Resource Include="Icons\save.png" />
<Resource Include="Icons\start.png" />
<Resource Include="Icons\stop.png" />
<Resource Include="motor2.png" /> <Resource Include="motor2.png" />
<Resource Include="tank.png" /> <Resource Include="tank.png" />
</ItemGroup> </ItemGroup>

BIN
Icons/connect.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

BIN
Icons/disconnect.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.1 KiB

BIN
Icons/save.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
Icons/start.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
Icons/stop.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

View File

@ -38,8 +38,10 @@ namespace CtrEditor
public ObservableCollection<string> listaImagenes { get; private set; } // Publicación de las claves del diccionario public ObservableCollection<string> listaImagenes { get; private set; } // Publicación de las claves del diccionario
public ObservableCollection<TipoSimulable> ListaOsBase { get; } = new ObservableCollection<TipoSimulable>(); public ObservableCollection<TipoSimulable> ListaOsBase { get; } = new ObservableCollection<TipoSimulable>();
private ObservableCollection<osBase> _objetosSimulables = new ObservableCollection<osBase>(); private ObservableCollection<osBase> _objetosSimulables = new ObservableCollection<osBase>();
public PLCViewModel _plcViewModelData; public PLCViewModel plcViewModelData;
public Stopwatch stopwatch; public Stopwatch stopwatch;
private bool isSimulationRunning;
private bool isConnected;
public SimulationManagerFP simulationManager = new SimulationManagerFP(); public SimulationManagerFP simulationManager = new SimulationManagerFP();
@ -49,6 +51,11 @@ namespace CtrEditor
public ICommand StopSimulationCommand { get; } public ICommand StopSimulationCommand { get; }
public ICommand ItemDoubleClickCommand { get; private set; } public ICommand ItemDoubleClickCommand { get; private set; }
public ICommand TBStartSimulationCommand { get; }
public ICommand TBStopSimulationCommand { get; }
public ICommand TBSaveCommand { get; }
public ICommand TBConnectPLCCommand { get; }
public ICommand TBDisconnectPLCCommand { get; }
// Evento que se dispara cuando se selecciona una nueva imagen // Evento que se dispara cuando se selecciona una nueva imagen
public event EventHandler<string> ImageSelected; public event EventHandler<string> ImageSelected;
@ -61,8 +68,8 @@ namespace CtrEditor
datosDeTrabajo = new DatosDeTrabajo(); datosDeTrabajo = new DatosDeTrabajo();
// Inicializa el PLCViewModel // Inicializa el PLCViewModel
_plcViewModelData = new PLCViewModel(); plcViewModelData = new PLCViewModel();
_plcViewModelData.RefreshEvent += OnRefreshEvent; plcViewModelData.RefreshEvent += OnRefreshEvent;
InitializeTipoSimulableList(); InitializeTipoSimulableList();
@ -75,6 +82,12 @@ namespace CtrEditor
StartSimulationCommand = new RelayCommand(StartSimulation); StartSimulationCommand = new RelayCommand(StartSimulation);
StopSimulationCommand = new RelayCommand(StopSimulation); StopSimulationCommand = new RelayCommand(StopSimulation);
TBStartSimulationCommand = new RelayCommand(StartSimulation, () => !IsSimulationRunning);
TBStopSimulationCommand = new RelayCommand(StopSimulation, () => IsSimulationRunning);
TBSaveCommand = new RelayCommand(Save);
TBConnectPLCCommand = new RelayCommand(ConnectPLC, () => !IsConnected);
TBDisconnectPLCCommand = new RelayCommand(DisconnectPLC, () => IsConnected);
stopwatch = new Stopwatch(); stopwatch = new Stopwatch();
} }
@ -128,6 +141,27 @@ namespace CtrEditor
} }
} }
public bool IsSimulationRunning
{
get => isSimulationRunning;
set
{
isSimulationRunning = value;
OnPropertyChanged(nameof(IsSimulationRunning));
CommandManager.InvalidateRequerySuggested(); // Notificar que el estado de los comandos ha cambiado
}
}
public bool IsConnected
{
get => isConnected;
set
{
isConnected = value;
OnPropertyChanged(nameof(IsConnected));
CommandManager.InvalidateRequerySuggested();
}
}
private void StartSimulation() private void StartSimulation()
{ {
@ -138,12 +172,14 @@ namespace CtrEditor
_timerSimulacion.Start(); _timerSimulacion.Start();
simulationManager.stopwatch.Start(); simulationManager.stopwatch.Start();
IsSimulationRunning = true;
} }
private void StopSimulation() private void StopSimulation()
{ {
_timerSimulacion.Stop(); _timerSimulacion.Stop();
simulationManager.stopwatch.Stop(); simulationManager.stopwatch.Stop();
IsSimulationRunning = false;
} }
private void OnTickSimulacion(object sender, EventArgs e) private void OnTickSimulacion(object sender, EventArgs e)
@ -159,10 +195,23 @@ namespace CtrEditor
} }
private void ConnectPLC()
{
plcViewModelData.Connect();
}
private void DisconnectPLC()
{
plcViewModelData.Disconnect();
IsConnected = false;
}
private void OnRefreshEvent(object sender, EventArgs e) private void OnRefreshEvent(object sender, EventArgs e)
{ {
if (_plcViewModelData.IsConnected) if (plcViewModelData.IsConnected)
{ {
if (!isConnected)
IsConnected = true;
// Detener el cronómetro y obtener el tiempo transcurrido en milisegundos // Detener el cronómetro y obtener el tiempo transcurrido en milisegundos
stopwatch.Stop(); stopwatch.Stop();
float elapsedMilliseconds = (float)stopwatch.Elapsed.TotalMilliseconds; float elapsedMilliseconds = (float)stopwatch.Elapsed.TotalMilliseconds;
@ -171,7 +220,7 @@ namespace CtrEditor
stopwatch.Restart(); stopwatch.Restart();
foreach (var objetoSimulable in ObjetosSimulables) foreach (var objetoSimulable in ObjetosSimulables)
objetoSimulable.UpdatePLC(_plcViewModelData.PLCInterface, (int) elapsedMilliseconds); objetoSimulable.UpdatePLC(plcViewModelData.PLCInterface, (int) elapsedMilliseconds);
} else } else
stopwatch.Stop(); stopwatch.Stop();
@ -206,10 +255,10 @@ namespace CtrEditor
public PLCViewModel PLCViewModel public PLCViewModel PLCViewModel
{ {
get { return _plcViewModelData; } get { return plcViewModelData; }
set set
{ {
_plcViewModelData = value; plcViewModelData = value;
OnPropertyChanged(nameof(PLCViewModel)); OnPropertyChanged(nameof(PLCViewModel));
} }
} }
@ -350,7 +399,7 @@ namespace CtrEditor
PixelToMeter.Instance.calc = simulationData.UnitConverter; PixelToMeter.Instance.calc = simulationData.UnitConverter;
// Re-register to the events // Re-register to the events
_plcViewModelData.RefreshEvent += OnRefreshEvent; plcViewModelData.RefreshEvent += OnRefreshEvent;
// Recorrer la colección de objetos simulables // Recorrer la colección de objetos simulables
foreach (var objetoSimulable in ObjetosSimulables) foreach (var objetoSimulable in ObjetosSimulables)

View File

@ -9,15 +9,35 @@
Height="900" Width="1600" Height="900" Width="1600"
ResizeMode="CanResize" Title="{Binding directorioTrabajo}"> ResizeMode="CanResize" Title="{Binding directorioTrabajo}">
<Window.Resources>
<convert:FloatToFormattedStringConverter x:Key="floatFormatter"/>
<convert:BrushToColorNameConverter x:Key="BrushToColorNameConverter"/>
</Window.Resources>
<Window.DataContext> <Window.DataContext>
<ctreditor:MainViewModel/> <ctreditor:MainViewModel/>
</Window.DataContext> </Window.DataContext>
<Window.Resources>
<convert:FloatToFormattedStringConverter x:Key="floatFormatter"/>
<convert:BrushToColorNameConverter x:Key="BrushToColorNameConverter"/>
<!-- Style for Start/Stop Button -->
<Style x:Key="StartStopButtonStyle" TargetType="Button">
<Setter Property="Background" Value="Transparent"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsSimulationRunning}" Value="True">
<Setter Property="Background" Value="LightGreen"/>
</DataTrigger>
</Style.Triggers>
</Style>
<!-- Style for Connect/Disconnect Button -->
<Style x:Key="ConnectDisconnectButtonStyle" TargetType="Button">
<Setter Property="Background" Value="Transparent"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsConnected}" Value="True">
<Setter Property="Background" Value="LightGreen"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid> <Grid>
<!-- Menú Principal sobre toda la ventana --> <!-- Menú Principal sobre toda la ventana -->
<Menu VerticalAlignment="Top" HorizontalAlignment="Stretch"> <Menu VerticalAlignment="Top" HorizontalAlignment="Stretch">
@ -60,7 +80,47 @@
<!-- Segunda Columna --> <!-- Segunda Columna -->
<Grid Grid.Column="1"> <Grid Grid.Column="1">
<ScrollViewer x:Name="ImagenEnTrabajoScrollViewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" PanningMode="Both"> <Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ToolBarTray Grid.Row="0">
<ToolBar>
<Button Command="{Binding TBStartSimulationCommand}" ToolTip="Iniciar Simulación" Style="{StaticResource StartStopButtonStyle}">
<StackPanel>
<Image Source="Icons/start.png" Width="16" Height="16"/>
<TextBlock Text="Iniciar"/>
</StackPanel>
</Button>
<Button Command="{Binding TBStopSimulationCommand}" ToolTip="Detener Simulación">
<StackPanel>
<Image Source="Icons/stop.png" Width="16" Height="16"/>
<TextBlock Text="Detener"/>
</StackPanel>
</Button>
<Button Command="{Binding TBSaveCommand}" ToolTip="Guardar">
<StackPanel>
<Image Source="Icons/save.png" Width="16" Height="16"/>
<TextBlock Text="Guardar"/>
</StackPanel>
</Button>
<Button Command="{Binding TBConnectPLCCommand}" ToolTip="Conectar PLC" Style="{StaticResource ConnectDisconnectButtonStyle}">
<StackPanel>
<Image Source="Icons/connect.png" Width="16" Height="16"/>
<TextBlock Text="Conectar"/>
</StackPanel>
</Button>
<Button Command="{Binding TBDisconnectPLCCommand}" ToolTip="Desconectar PLC">
<StackPanel>
<Image Source="Icons/disconnect.png" Width="16" Height="16"/>
<TextBlock Text="Desconectar"/>
</StackPanel>
</Button>
</ToolBar>
</ToolBarTray>
<ScrollViewer Grid.Row="1" x:Name="ImagenEnTrabajoScrollViewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" PanningMode="Both">
<Canvas x:Name="ImagenEnTrabajoCanvas" Margin="20"> <Canvas x:Name="ImagenEnTrabajoCanvas" Margin="20">
<!-- El Margin puede ser ajustado según el espacio adicional que quieras proporcionar --> <!-- El Margin puede ser ajustado según el espacio adicional que quieras proporcionar -->
<Canvas.LayoutTransform> <Canvas.LayoutTransform>

View File

@ -29,6 +29,9 @@ namespace CtrEditor.ObjetosSim
return new ucBoton(); return new ucBoton();
if (tipoObjeto == typeof(osTanque)) if (tipoObjeto == typeof(osTanque))
return new ucTanque(); return new ucTanque();
if (tipoObjeto == typeof(osSensTemperatura))
return new ucSensTemperatura();
// Puedes añadir más condiciones para otros tipos // Puedes añadir más condiciones para otros tipos
@ -53,6 +56,8 @@ namespace CtrEditor.ObjetosSim
return new osBoton(); return new osBoton();
if (tipoObjeto == typeof(osTanque)) if (tipoObjeto == typeof(osTanque))
return new osTanque(); return new osTanque();
if (tipoObjeto == typeof(osSensTemperatura))
return new osSensTemperatura();
// Puedes añadir más condiciones para otros tipos // Puedes añadir más condiciones para otros tipos

View File

@ -70,6 +70,22 @@ namespace CtrEditor.ObjetosSim
public SimulationManagerFP simulationManager; public SimulationManagerFP simulationManager;
protected osBase ObtenerLink(string NameLink, Type tipoOsBase)
{
if (!string.IsNullOrEmpty(NameLink) && _mainViewModel != null)
{
foreach (var objetoSimulable in _mainViewModel.ObjetosSimulables)
{
if (tipoOsBase.IsInstanceOfType(objetoSimulable) && ((osBase)objetoSimulable).Nombre == NameLink)
{
return (osBase)objetoSimulable;
}
}
}
return null;
}
public void CanvasSetLeftinMeter(float left) public void CanvasSetLeftinMeter(float left)
{ {
if (_visualRepresentation != null) if (_visualRepresentation != null)

View File

@ -0,0 +1,33 @@
<UserControl x:Class="CtrEditor.ObjetosSim.ucSensTemperatura"
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"/>
</UserControl.Resources>
<Grid>
<Border x:Name="BackgroundRectangle"
BorderBrush="Black"
BorderThickness="2"
Height="{Binding Alto, Converter={StaticResource MeterToPixelConverter}, ConverterParameter=1.5}"
VerticalAlignment="Top"
Background="Gray"
HorizontalAlignment="Center"
Width="{Binding Ancho, Converter={StaticResource MeterToPixelConverter}}">
<Grid>
<Slider
HorizontalAlignment="Center"
VerticalAlignment="Center"
Height="{Binding Alto, Converter={StaticResource MeterToPixelConverter}}"
Orientation="Vertical"
Value="{Binding Value}"
Maximum="100"/>
</Grid>
</Border>
</Grid>
</UserControl>

View File

@ -0,0 +1,222 @@
using CtrEditor.Convertidores;
using CtrEditor.Siemens;
using Siemens.Simatic.Simulation.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace CtrEditor.ObjetosSim
{
/// <summary>
/// Interaction logic for ucSensTemperatura.xaml
/// </summary>
public class osSensTemperatura : osBase
{
// Otros datos y métodos relevantes para la simulación
private string _nombre = "Temperatura";
private float _ancho;
private float _alto;
private float _left;
private float _top;
private float _angulo;
private string _tag;
private float _value;
private float _max_OUT_Scaled;
private float _min_OUT_Scaled;
public string Tag
{
get => _tag;
set
{
if (_tag != value)
{
_tag = value;
OnPropertyChanged(nameof(Tag));
}
}
}
public float Min_OUT_Scaled
{
get => _min_OUT_Scaled;
set
{
_min_OUT_Scaled = value;
OnPropertyChanged(nameof(Min_OUT_Scaled));
}
}
public float Max_OUT_Scaled
{
get => _max_OUT_Scaled;
set
{
_max_OUT_Scaled = value;
OnPropertyChanged(nameof(Max_OUT_Scaled));
}
}
public float Value
{
get => _value;
set
{
_value = value;
OnPropertyChanged(nameof(Value));
}
}
public override float Left
{
get => _left;
set
{
_left = value;
CanvasSetLeftinMeter(value);
OnPropertyChanged(nameof(Left));
}
}
public override float Top
{
get => _top;
set
{
_top = value;
CanvasSetTopinMeter(value);
OnPropertyChanged(nameof(Top));
}
}
public float Ancho
{
get => _ancho;
set
{
_ancho = value;
OnPropertyChanged(nameof(Ancho));
}
}
public float Alto
{
get => _alto;
set
{
_alto = value;
OnPropertyChanged(nameof(Alto));
}
}
public float Angulo
{
get => _angulo;
set
{
_angulo = value;
OnPropertyChanged(nameof(Angulo));
}
}
public override string Nombre
{
get => _nombre;
set
{
if (_nombre != value)
{
_nombre = value;
OnPropertyChanged(nameof(Nombre));
}
}
}
public osSensTemperatura()
{
Ancho = 0.4f;
Alto = 1f;
Max_OUT_Scaled = 27648;
Min_OUT_Scaled = 0;
}
public override void UpdateGeometryStart()
{
// Se llama antes de la simulacion
}
public override void UpdateGeometryStep()
{
}
public override void UpdatePLC(PLCModel plc, int elapsedMilliseconds)
{
if (Tag.Length > 0)
{
SDataValue s = new SDataValue();
s.UInt16 = (ushort)((Value / 100.0 * Max_OUT_Scaled) + Min_OUT_Scaled);
plc.EscribirTag(Tag, s);
}
}
public override void UpdateControl()
{
}
public override void ucLoaded()
{
// El UserControl ya se ha cargado y podemos obtener las coordenadas para
// crear el objeto de simulacion
}
}
public partial class ucSensTemperatura : UserControl, IDataContainer
{
public osBase? Datos { get; set; }
public ucSensTemperatura()
{
InitializeComponent();
this.Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
Datos?.ucLoaded();
}
public void Resize(float width, float height)
{
if (Datos is osSensTemperatura datos)
{
datos.Ancho = PixelToMeter.Instance.calc.PixelsToMeters(width);
datos.Alto = 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)
{
if (Datos != null)
if (Datos is osSensTemperatura datos)
datos.Angulo = Angle;
}
public void Highlight(bool State) { }
public int ZIndex()
{
return 10;
}
}
}

View File

@ -18,7 +18,15 @@
Source="/tank.png" Source="/tank.png"
Width="{Binding Alto, Converter={StaticResource MeterToPixelConverter}}" Width="{Binding Alto, Converter={StaticResource MeterToPixelConverter}}"
Height="{Binding Ancho, Converter={StaticResource MeterToPixelConverter}}" Height="{Binding Ancho, Converter={StaticResource MeterToPixelConverter}}"
Stretch="Uniform"/> Stretch="Uniform">
</Image>
<Slider
HorizontalAlignment="Left"
VerticalAlignment="Center"
Height="{Binding Alto, Converter={StaticResource MeterToPixelConverter}}"
Orientation="Vertical"
Value="{Binding Level}"
Maximum="100"/>
<Rectangle x:Name="WaterLevel" <Rectangle x:Name="WaterLevel"
Fill="Blue" Fill="Blue"
@ -34,6 +42,7 @@
</MultiBinding> </MultiBinding>
</Rectangle.Height> </Rectangle.Height>
</Rectangle> </Rectangle>
<Label Content="{Binding Level}" HorizontalAlignment="Center" Margin="10,10,0,0" VerticalAlignment="Top"/> <Label Content="{Binding Level}" HorizontalAlignment="Center" Margin="10,10,0,0" VerticalAlignment="Top"/>
</Grid> </Grid>
</UserControl> </UserControl>

View File

@ -1,5 +1,6 @@
using CtrEditor.Convertidores; using CtrEditor.Convertidores;
using CtrEditor.Siemens; using CtrEditor.Siemens;
using Newtonsoft.Json.Linq;
using OpenCvSharp.Flann; using OpenCvSharp.Flann;
using Siemens.Simatic.Simulation.Runtime; using Siemens.Simatic.Simulation.Runtime;
using System; using System;
@ -34,7 +35,8 @@ namespace CtrEditor.ObjetosSim
private float _angulo; private float _angulo;
private float _level; private float _level;
private string _tag; private string _tag;
private float _max_OUT_Scaled;
private float _min_OUT_Scaled;
public string Tag public string Tag
{ {
@ -48,6 +50,27 @@ namespace CtrEditor.ObjetosSim
} }
} }
} }
public float Min_OUT_Scaled
{
get => _min_OUT_Scaled;
set
{
_min_OUT_Scaled = value;
OnPropertyChanged(nameof(Min_OUT_Scaled));
}
}
public float Max_OUT_Scaled
{
get => _max_OUT_Scaled;
set
{
_max_OUT_Scaled = value;
OnPropertyChanged(nameof(Max_OUT_Scaled));
}
}
public float Level public float Level
{ {
get => _level; get => _level;
@ -125,6 +148,8 @@ namespace CtrEditor.ObjetosSim
{ {
Ancho = 0.30f; Ancho = 0.30f;
Alto = 0.30f; Alto = 0.30f;
Max_OUT_Scaled = 27648;
Min_OUT_Scaled = 0;
} }
public override void UpdateGeometryStart() public override void UpdateGeometryStart()
@ -140,7 +165,7 @@ namespace CtrEditor.ObjetosSim
if (Tag.Length > 0) if (Tag.Length > 0)
{ {
SDataValue s = new SDataValue(); SDataValue s = new SDataValue();
s.UInt16 = (ushort)(Level / 100.0 * 26600); s.UInt16 = (ushort)((Level / 100.0 * Max_OUT_Scaled) + Min_OUT_Scaled);
plc.EscribirTag(Tag, s); plc.EscribirTag(Tag, s);
} }
} }

View File

@ -202,18 +202,11 @@ namespace CtrEditor.ObjetosSim
{ {
if (_osMotor is osVMmotorSim motor) if (_osMotor is osVMmotorSim motor)
VelocidadActual = motor.Velocidad; VelocidadActual = motor.Velocidad;
} else
{
if (Motor.Length > 0)
if (_mainViewModel != null)
foreach (var objetoSimulable in _mainViewModel.ObjetosSimulables)
if (objetoSimulable.Nombre == _motor)
{
_osMotor = objetoSimulable;
break;
}
} }
else
_osMotor = ObtenerLink(_motor, typeof(osVMmotorSim));
} }
public override void ucLoaded() public override void ucLoaded()
{ {
// El UserControl ya se ha cargado y podemos obtener las coordenadas para // El UserControl ya se ha cargado y podemos obtener las coordenadas para

View File

@ -153,16 +153,7 @@ namespace CtrEditor.ObjetosSim
VelocidadActual = motor.Velocidad; VelocidadActual = motor.Velocidad;
} }
else else
{ _osMotor = ObtenerLink(_motor, typeof(osVMmotorSim));
if (Motor.Length > 0)
if (_mainViewModel != null)
foreach (var objetoSimulable in _mainViewModel.ObjetosSimulables)
if (objetoSimulable.Nombre == _motor)
{
_osMotor = objetoSimulable;
break;
}
}
} }
public override void UpdateControl() public override void UpdateControl()

View File

@ -104,17 +104,24 @@ namespace CtrEditor.Siemens
public void Connect() public void Connect()
{ {
// Implementa la conexión utilizando PLCModel try
PLCInterface.Instance = SimulationRuntimeManager.CreateInterface(Name);
PLCInterface.Instance.OnSoftwareConfigurationChanged += Instance_OnSoftwareConfigurationChanged;
//_plcModel.Instance.CommunicationInterface = ECommunicationInterface.Softbus;
if (PLCInterface.Instance != null)
{ {
PLCInterface.UpdateTagList(); // Implementa la conexión utilizando PLCModel
_timer.Start(); PLCInterface.Instance = SimulationRuntimeManager.CreateInterface(Name);
ConnectionStatus = "connected"; PLCInterface.Instance.OnSoftwareConfigurationChanged += Instance_OnSoftwareConfigurationChanged;
IsConnected = true; //_plcModel.Instance.CommunicationInterface = ECommunicationInterface.Softbus;
if (PLCInterface.Instance != null)
{
PLCInterface.UpdateTagList();
_timer.Start();
ConnectionStatus = "connected";
IsConnected = true;
}
} catch (Exception ex)
{
LastError = ex.Message;
ConnectionStatus = "offline";
} }
} }

View File

@ -12,6 +12,7 @@ using CtrEditor.Convertidores;
using FarseerPhysics.Common; using FarseerPhysics.Common;
using System.Windows; using System.Windows;
using System.Diagnostics; using System.Diagnostics;
using System.Windows.Documents;
namespace CtrEditor.Simulacion namespace CtrEditor.Simulacion
{ {
@ -182,7 +183,32 @@ namespace CtrEditor.Simulacion
if (fixtureB.Body.UserData is simRectangle) if (fixtureB.Body.UserData is simRectangle)
{ {
simRectangle conveyor = fixtureB.Body.UserData as simRectangle; simRectangle conveyor = fixtureB.Body.UserData as simRectangle;
ApplyConveyorEffect(conveyor, fixtureA); CircleShape circleShape = fixtureA.Shape as CircleShape;
PolygonShape polygonShape = fixtureB.Shape as PolygonShape;
// Obtener centro y radio del círculo
Vector2 centroCirculo = fixtureA.Body.Position;
float radio = circleShape.Radius;
// Obtener los vértices del polígono (rectángulo)
Vector2[] vertices = new Vector2[polygonShape.Vertices.Count];
float cos = (float)Math.Cos(fixtureB.Body.Rotation);
float sin = (float)Math.Sin(fixtureB.Body.Rotation);
for (int i = 0; i < polygonShape.Vertices.Count; i++)
{
Vector2 vertex = polygonShape.Vertices[i];
float rotatedX = vertex.X * cos - vertex.Y * sin + fixtureB.Body.Position.X;
float rotatedY = vertex.X * sin + vertex.Y * cos + fixtureB.Body.Position.Y;
vertices[i] = new Vector2(rotatedX, rotatedY);
}
// Calcular el porcentaje de la superficie compartida
float porcentajeCompartido = InterseccionCirculoRectangulo.CalcularSuperficieCompartida(vertices, centroCirculo, radio);
// Aplicar el efecto del transportador usando el porcentaje calculado
ApplyConveyorEffect(conveyor, fixtureA, porcentajeCompartido);
return true; // No aplicar respuestas físicas return true; // No aplicar respuestas físicas
} }
return true; // No aplicar respuestas físicas return true; // No aplicar respuestas físicas
@ -193,11 +219,11 @@ namespace CtrEditor.Simulacion
// Aquí puedes restablecer cualquier estado si es necesario al separarse de un simRectangle // Aquí puedes restablecer cualquier estado si es necesario al separarse de un simRectangle
} }
private void ApplyConveyorEffect(simRectangle conveyor, Fixture circleFixture) private void ApplyConveyorEffect(simRectangle conveyor, Fixture circleFixture, float porcentajeCompartido)
{ {
float speedMetersPerSecond = conveyor.Speed / 60.0f; float speedMetersPerSecond = conveyor.Speed / 60.0f;
Vector2 desiredVelocity = new Vector2((float)Math.Cos(conveyor.Body.Rotation), (float)Math.Sin(conveyor.Body.Rotation)) * speedMetersPerSecond; Vector2 desiredVelocity = new Vector2((float)Math.Cos(conveyor.Body.Rotation), (float)Math.Sin(conveyor.Body.Rotation)) * speedMetersPerSecond;
circleFixture.Body.LinearVelocity = desiredVelocity; circleFixture.Body.LinearVelocity += desiredVelocity * porcentajeCompartido;
} }
} }

View File

@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FarseerPhysics.Collision;
using Microsoft.VisualBasic.Devices;
using Microsoft.Xna.Framework;
namespace CtrEditor.Simulacion
{
internal class InterseccionCirculoRectangulo
{
// Definición de la función CalcularSuperficieCompartida
public static float CalcularSuperficieCompartida(Vector2[] vertices, Vector2 center, float r)
{
float totalCircleArea = (float)Math.PI * r * r;
// Distancia a líneas ajustado
float[] distances = new float[4];
for (int i = 0; i < 4; i++)
{
distances[i] = DistanceFromLine(center, vertices[i], vertices[(i + 1) % 4]);
}
float minDistance = float.MaxValue;
foreach (var dist in distances)
{
if (Math.Abs(dist) < Math.Abs(minDistance))
minDistance = dist;
}
float d = Math.Abs(minDistance);
float sharedArea = 0;
if (Array.TrueForAll(distances, dist => Math.Abs(dist) > r))
{
sharedArea = totalCircleArea;
}
else if (d < r)
{
float cosTheta = Math.Min(1, d / r);
float sinTheta = (float)Math.Sqrt(Math.Max(0, r * r - d * d));
if (minDistance < 0) // El centro está dentro del rectángulo
{
float areaOutside = r * r * (float)Math.Acos(cosTheta) - d * sinTheta;
sharedArea = totalCircleArea - areaOutside;
}
else // El centro está fuera del rectángulo
{
sharedArea = r * r * (float)Math.Acos(cosTheta) - d * sinTheta;
}
}
else
{
sharedArea = 0;
}
return sharedArea / totalCircleArea;
}
public static float DistanceFromLine(Vector2 point, Vector2 start, Vector2 end)
{
float A = end.Y - start.Y;
float B = start.X - end.X;
float C = end.X * start.Y - start.X * end.Y;
float distance = (A * point.X + B * point.Y + C) / (float)Math.Sqrt(A * A + B * B);
return distance;
}
}
}