Agregado de toolbar a MainWindow

This commit is contained in:
Miguel 2024-05-15 11:20:09 +02:00
parent 794a5898c5
commit f3e5992433
12 changed files with 171 additions and 45 deletions

View File

@ -15,6 +15,11 @@
</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="tank.png" />
</ItemGroup>
@ -48,6 +53,11 @@
</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="tank.png" />
</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<TipoSimulable> ListaOsBase { get; } = new ObservableCollection<TipoSimulable>();
private ObservableCollection<osBase> _objetosSimulables = new ObservableCollection<osBase>();
public PLCViewModel _plcViewModelData;
public PLCViewModel plcViewModelData;
public Stopwatch stopwatch;
private bool isSimulationRunning;
private bool isConnected;
public SimulationManagerFP simulationManager = new SimulationManagerFP();
@ -49,6 +51,11 @@ namespace CtrEditor
public ICommand StopSimulationCommand { get; }
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
public event EventHandler<string> ImageSelected;
@ -61,8 +68,8 @@ namespace CtrEditor
datosDeTrabajo = new DatosDeTrabajo();
// Inicializa el PLCViewModel
_plcViewModelData = new PLCViewModel();
_plcViewModelData.RefreshEvent += OnRefreshEvent;
plcViewModelData = new PLCViewModel();
plcViewModelData.RefreshEvent += OnRefreshEvent;
InitializeTipoSimulableList();
@ -75,6 +82,12 @@ namespace CtrEditor
StartSimulationCommand = new RelayCommand(StartSimulation);
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();
}
@ -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()
{
@ -138,12 +172,14 @@ namespace CtrEditor
_timerSimulacion.Start();
simulationManager.stopwatch.Start();
IsSimulationRunning = true;
}
private void StopSimulation()
{
_timerSimulacion.Stop();
simulationManager.stopwatch.Stop();
IsSimulationRunning = false;
}
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)
{
if (_plcViewModelData.IsConnected)
if (plcViewModelData.IsConnected)
{
if (!isConnected)
IsConnected = true;
// Detener el cronómetro y obtener el tiempo transcurrido en milisegundos
stopwatch.Stop();
float elapsedMilliseconds = (float)stopwatch.Elapsed.TotalMilliseconds;
@ -171,7 +220,7 @@ namespace CtrEditor
stopwatch.Restart();
foreach (var objetoSimulable in ObjetosSimulables)
objetoSimulable.UpdatePLC(_plcViewModelData.PLCInterface, (int) elapsedMilliseconds);
objetoSimulable.UpdatePLC(plcViewModelData.PLCInterface, (int) elapsedMilliseconds);
} else
stopwatch.Stop();
@ -206,10 +255,10 @@ namespace CtrEditor
public PLCViewModel PLCViewModel
{
get { return _plcViewModelData; }
get { return plcViewModelData; }
set
{
_plcViewModelData = value;
plcViewModelData = value;
OnPropertyChanged(nameof(PLCViewModel));
}
}
@ -350,7 +399,7 @@ namespace CtrEditor
PixelToMeter.Instance.calc = simulationData.UnitConverter;
// Re-register to the events
_plcViewModelData.RefreshEvent += OnRefreshEvent;
plcViewModelData.RefreshEvent += OnRefreshEvent;
// Recorrer la colección de objetos simulables
foreach (var objetoSimulable in ObjetosSimulables)

View File

@ -9,15 +9,35 @@
Height="900" Width="1600"
ResizeMode="CanResize" Title="{Binding directorioTrabajo}">
<Window.Resources>
<convert:FloatToFormattedStringConverter x:Key="floatFormatter"/>
<convert:BrushToColorNameConverter x:Key="BrushToColorNameConverter"/>
</Window.Resources>
<Window.DataContext>
<ctreditor:MainViewModel/>
</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>
<!-- Menú Principal sobre toda la ventana -->
<Menu VerticalAlignment="Top" HorizontalAlignment="Stretch">
@ -60,7 +80,47 @@
<!-- Segunda Columna -->
<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">
<!-- El Margin puede ser ajustado según el espacio adicional que quieras proporcionar -->
<Canvas.LayoutTransform>

View File

@ -70,6 +70,22 @@ namespace CtrEditor.ObjetosSim
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)
{
if (_visualRepresentation != null)

View File

@ -202,18 +202,11 @@ namespace CtrEditor.ObjetosSim
{
if (_osMotor is osVMmotorSim motor)
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()
{
// El UserControl ya se ha cargado y podemos obtener las coordenadas para

View File

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

View File

@ -103,6 +103,8 @@ namespace CtrEditor.Siemens
}
public void Connect()
{
try
{
// Implementa la conexión utilizando PLCModel
PLCInterface.Instance = SimulationRuntimeManager.CreateInterface(Name);
@ -116,6 +118,11 @@ namespace CtrEditor.Siemens
ConnectionStatus = "connected";
IsConnected = true;
}
} catch (Exception ex)
{
LastError = ex.Message;
ConnectionStatus = "offline";
}
}
private void Instance_OnSoftwareConfigurationChanged(IInstance instance, SOnSoftwareConfigChangedParameter event_param)