diff --git a/CtrEditor.csproj b/CtrEditor.csproj index 36e5d46..eab6b69 100644 --- a/CtrEditor.csproj +++ b/CtrEditor.csproj @@ -15,6 +15,11 @@ + + + + + @@ -48,6 +53,11 @@ + + + + + diff --git a/Icons/connect.png b/Icons/connect.png new file mode 100644 index 0000000..4a0bf70 Binary files /dev/null and b/Icons/connect.png differ diff --git a/Icons/disconnect.png b/Icons/disconnect.png new file mode 100644 index 0000000..2b6be8a Binary files /dev/null and b/Icons/disconnect.png differ diff --git a/Icons/save.png b/Icons/save.png new file mode 100644 index 0000000..18320c7 Binary files /dev/null and b/Icons/save.png differ diff --git a/Icons/start.png b/Icons/start.png new file mode 100644 index 0000000..c43be3f Binary files /dev/null and b/Icons/start.png differ diff --git a/Icons/stop.png b/Icons/stop.png new file mode 100644 index 0000000..eb7d756 Binary files /dev/null and b/Icons/stop.png differ diff --git a/MainViewModel.cs b/MainViewModel.cs index ddaeae2..9be5ac6 100644 --- a/MainViewModel.cs +++ b/MainViewModel.cs @@ -38,8 +38,10 @@ namespace CtrEditor public ObservableCollection listaImagenes { get; private set; } // Publicación de las claves del diccionario public ObservableCollection ListaOsBase { get; } = new ObservableCollection(); private ObservableCollection _objetosSimulables = new ObservableCollection(); - 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 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) diff --git a/MainWindow.xaml b/MainWindow.xaml index 7cdae76..d7a6a26 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -9,15 +9,35 @@ Height="900" Width="1600" ResizeMode="CanResize" Title="{Binding directorioTrabajo}"> - - - - - + + + + + + + + + + + @@ -60,7 +80,47 @@ - + + + + + + + + + + + + + + + + diff --git a/ObjetosSim/osBase.cs b/ObjetosSim/osBase.cs index 30ecf0a..d89efec 100644 --- a/ObjetosSim/osBase.cs +++ b/ObjetosSim/osBase.cs @@ -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) diff --git a/ObjetosSim/ucTransporteGuias.xaml.cs b/ObjetosSim/ucTransporteGuias.xaml.cs index 95552cb..c2b2881 100644 --- a/ObjetosSim/ucTransporteGuias.xaml.cs +++ b/ObjetosSim/ucTransporteGuias.xaml.cs @@ -196,24 +196,17 @@ namespace CtrEditor.ObjetosSim 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 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 diff --git a/ObjetosSim/ucTransporteTTop.xaml.cs b/ObjetosSim/ucTransporteTTop.xaml.cs index c5c4fb6..219305b 100644 --- a/ObjetosSim/ucTransporteTTop.xaml.cs +++ b/ObjetosSim/ucTransporteTTop.xaml.cs @@ -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() diff --git a/Siemens/PLCControl.xaml.cs b/Siemens/PLCControl.xaml.cs index 19e338f..f083d72 100644 --- a/Siemens/PLCControl.xaml.cs +++ b/Siemens/PLCControl.xaml.cs @@ -104,17 +104,24 @@ namespace CtrEditor.Siemens public void Connect() { - // Implementa la conexión utilizando PLCModel - PLCInterface.Instance = SimulationRuntimeManager.CreateInterface(Name); - PLCInterface.Instance.OnSoftwareConfigurationChanged += Instance_OnSoftwareConfigurationChanged; - //_plcModel.Instance.CommunicationInterface = ECommunicationInterface.Softbus; - - if (PLCInterface.Instance != null) + try { - PLCInterface.UpdateTagList(); - _timer.Start(); - ConnectionStatus = "connected"; - IsConnected = true; + // Implementa la conexión utilizando PLCModel + PLCInterface.Instance = SimulationRuntimeManager.CreateInterface(Name); + PLCInterface.Instance.OnSoftwareConfigurationChanged += Instance_OnSoftwareConfigurationChanged; + //_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"; } }