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>
<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

@ -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

@ -196,24 +196,17 @@ namespace CtrEditor.ObjetosSim
public override void UpdateControl() public override void UpdateControl()
{ {
} }
public override void UpdatePLC(PLCModel plc, int elapsedMilliseconds) public override void UpdatePLC(PLCModel plc, int elapsedMilliseconds)
{ {
if (_osMotor != null) if (_osMotor != null)
{ {
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";
} }
} }