using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; using System.Collections.ObjectModel; using System.Windows.Media; using System.Windows; using ISOCNC.Remoting; using Zebra.Sdk.Comm; using Zebra.Sdk.Device; using Zebra.Sdk.Printer; namespace EgtCNCPLCComm { class MainWindowVM : VMBase { #region FIELDS & PROPERTIES // connesso private bool _Connected = false; public bool Connected { get { return _Connected; } } public SolidColorBrush Connect_Background { get { return (_Connected ? Brushes.Green : Brushes.Red); } } // istanza della classe di comunicazione del controllo private TPAComm _Tpa = null; public TPAComm Tpa { get { return _Tpa; } } // path corrent private string _ProgramPath; public string ProgramPath { get { return _ProgramPath; } } // indice a cui inserire o togliere il programma CNC private int _ProgramIndex = 0; public int ProgramIndex { get { return _ProgramIndex; } set { _ProgramIndex = value; } } // nome della variabile private string _VarName; public string VarName { get { return _VarName; } set { _VarName = value; } } // valore della variabile private string _VarValue = ""; public string VarValue { get { return _VarValue; } set { _VarValue = value; } } public void SetVarValue( object value) { _VarValue = (string)value; NotifyPropertyChanged(nameof(VarValue)); } // lista assi private ObservableCollection _AxisList; public ObservableCollection AxisList { get { return _AxisList; } } private List _OPStateList; public List OPStateList { get { return _OPStateList; } set { _OPStateList = value; } } private ISOCNC.Remoting.MachineOperatingState _SelOPState; public ISOCNC.Remoting.MachineOperatingState SelOPState { get { return _SelOPState; } set { _SelOPState = value; } } private string _ErrCycle; public string ErrCycle { get { return _ErrCycle; } } public void UpdateErrCycle(string msg) { _ErrCycle = msg; NotifyPropertyChanged(nameof(ErrCycle)); } private string _Iso; public string Iso { get { return _Iso; } } public void UpdateIso(string msg) { _Iso = msg; NotifyPropertyChanged(nameof(Iso)); } private string _Message; public string Message { get { return _Message; } } public void UpdateMessage(string msg) { _Message = msg; NotifyPropertyChanged(nameof(Message)); } private string _ErrSystem; public string ErrSystem { get { return _ErrSystem; } } public void UpdateErrSystem(string msg) { _ErrSystem = msg; NotifyPropertyChanged(nameof(ErrSystem)); } // definizione comandi ICommand m_cmdConnect; ICommand m_cmdDisconnect; ICommand m_cmdStart; ICommand m_cmdStop; ICommand m_cmdStep; ICommand m_cmdSearchFilePath; ICommand m_cmdAddProgram; ICommand m_cmdRemoveProgram; ICommand m_cmdReadVar; ICommand m_cmdWriteVar; ICommand m_cmdDeleteAlarms; ICommand m_cmdSetPoint; ICommand m_cmdPrintLabel; #endregion FIELDS & PROPERTIES #region CONSTRUCTOR public MainWindowVM() { _AxisList = new ObservableCollection { new Axis("X"), new Axis("Y"), new Axis("Z"), new Axis("C"), new Axis("B") }; _OPStateList = new List { MachineOperatingState.Start, MachineOperatingState.Stop, MachineOperatingState.End, MachineOperatingState.SetPoint, MachineOperatingState.Pending, MachineOperatingState.Unspecified}; NotifyPropertyChanged(nameof(Connect_Background)); } #endregion CONSTRUCTOR #region COMMANDS #region Connect // Returns a command that manage the MainWindow_Unloaded command public ICommand Connect_Command { get { if (m_cmdConnect == null) m_cmdConnect = new Command(Connect); return m_cmdConnect; } } // Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded. public void Connect(Object param) { _Tpa = new TPAComm(this); _Connected = Tpa.remObject.OnConnect(); NotifyPropertyChanged(nameof(Connect_Background)); if (_Connected) Init(); } private void Init() { _SelOPState = (MachineOperatingState)Tpa.remObject.MachineOperativeStatus; NotifyPropertyChanged(nameof(SelOPState)); string sAxesValue = ""; double dAxesValue = 0; // Tpa.remObject.SetVariableCommand((int)VariableCommands.ReadAxis, null, sAxesVal); string sAxesName = "0.TESTA.AsseX.X"; Tpa.remObject.SetVariableCommand((int)ISOCNC.Remoting.VariableCommands.ReadVar, sAxesName, sAxesValue); double.TryParse(sAxesValue, out dAxesValue); _AxisList[0].Value = dAxesValue; sAxesName = "0.TESTA.AsseY.Y"; Tpa.remObject.SetVariableCommand((int)ISOCNC.Remoting.VariableCommands.ReadVar, sAxesName, sAxesValue); double.TryParse(sAxesValue, out dAxesValue); _AxisList[1].Value = dAxesValue; sAxesName = "0.TESTA.AsseZ.Z"; Tpa.remObject.SetVariableCommand((int)ISOCNC.Remoting.VariableCommands.ReadVar, sAxesName, sAxesValue); double.TryParse(sAxesValue, out dAxesValue); _AxisList[2].Value = dAxesValue; sAxesName = "0.TESTA.AsseC.C"; Tpa.remObject.SetVariableCommand((int)ISOCNC.Remoting.VariableCommands.ReadVar, sAxesName, sAxesValue); double.TryParse(sAxesValue, out dAxesValue); _AxisList[3].Value = dAxesValue; sAxesName = "0.TESTA.AsseB.B"; Tpa.remObject.SetVariableCommand((int)ISOCNC.Remoting.VariableCommands.ReadVar, sAxesName, sAxesValue); double.TryParse(sAxesValue, out dAxesValue); _AxisList[4].Value = dAxesValue; } #endregion Connect #region Disconnect // Returns a command that manage the MainWindow_Unloaded command public ICommand Disconnect_Command { get { if (m_cmdDisconnect == null) m_cmdDisconnect = new Command(Disconnect); return m_cmdDisconnect; } } // Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded. public void Disconnect(Object param) { _Connected = !Tpa.remObject.OnClose(); NotifyPropertyChanged(nameof(Connect_Background)); } #endregion Disconnect #region Start // Returns a command that manage the MainWindow_Unloaded command public ICommand Start_Command { get { if (m_cmdStart == null) m_cmdStart = new Command(Start); return m_cmdStart; } } // Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded. public void Start(Object param) { //int StartCommand; //if (SelOPState == MachineOperatingState.Pending) //{ // StartCommand = (int)ISOCNC.Remoting.Commands.Start_Program_Soft; //} //else //{ // StartCommand = (int)ISOCNC.Remoting.Commands.Start; //} //Tpa.remObject.SetCommand(StartCommand); Tpa.remObject.SetCommand((int)ISOCNC.Remoting.Commands.Start); Tpa.remObject.SetVariableCommand((int)ISOCNC.Remoting.VariableCommands.WriteVar, "0.FUNM.E80048", "0"); } #endregion Start #region Stop // Returns a command that manage the MainWindow_Unloaded command public ICommand Stop_Command { get { if (m_cmdStop == null) m_cmdStop = new Command(Stop); return m_cmdStop; } } // Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded. public void Stop(Object param) { Tpa.remObject.SetCommand((int)ISOCNC.Remoting.Commands.Stop); } #endregion Stop #region Step // Returns a command that manage the MainWindow_Unloaded command public ICommand Step_Command { get { if (m_cmdStep == null) m_cmdStep = new Command(Step); return m_cmdStep; } } // Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded. public void Step(Object param) { Tpa.remObject.SetCommand((int)ISOCNC.Remoting.Commands.End); Tpa.remObject.RemoveAllProgramsFromList(); } #endregion Step #region SetPoint // Returns a command that manage the MainWindow_Unloaded command public ICommand SetPoint_Command { get { if (m_cmdSetPoint == null) m_cmdSetPoint = new Command(SetPoint); return m_cmdSetPoint; } } // Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded. public void SetPoint(Object param) { Tpa.remObject.SetCommand((int)ISOCNC.Remoting.Commands.SetPoint); } #endregion SetPoint #region PrintLabel // Returns a command that manage the MainWindow_Unloaded command public ICommand PrintLabel_Command { get { if (m_cmdPrintLabel == null) m_cmdPrintLabel = new Command(PrintLabel); return m_cmdPrintLabel; } } // Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded. public void PrintLabel(Object param) { EgtCNCPLCComm.ZebraPrinter.ZebraPrinter Test = new EgtCNCPLCComm.ZebraPrinter.ZebraPrinter(); Test.Print(); } #endregion PrintLabel #region SearchFilePath // Returns a command that manage the MainWindow_Unloaded command public ICommand SearchFilePath_Command { get { if (m_cmdSearchFilePath == null) m_cmdSearchFilePath = new Command(SearchFilePath); return m_cmdSearchFilePath; } } // Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded. public void SearchFilePath(Object param) { Microsoft.Win32.OpenFileDialog OpenFileDialog = new Microsoft.Win32.OpenFileDialog(); if (OpenFileDialog.ShowDialog() == true) _ProgramPath = OpenFileDialog.FileName; NotifyPropertyChanged(nameof(ProgramPath)); } #endregion SearchFilePath #region AddProgram // Returns a command that manage the MainWindow_Unloaded command public ICommand AddProgram_Command { get { if (m_cmdAddProgram == null) m_cmdAddProgram = new Command(AddProgram); return m_cmdAddProgram; } } // Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded. public void AddProgram(Object param) { //if (_ProgramIndex < 0) //{ // string Path = _ProgramPath.Replace("\\\\", "\\"); // bool x = Tpa.remObject.AddProgramToList(Path); //} //else // Tpa.remObject.AddProgramToListAtIndex(_ProgramPath, _ProgramIndex); if (SelOPState == MachineOperatingState.Pending) { string Path = _ProgramPath.Replace("\\\\", "\\"); bool x = Tpa.remObject.AddProgramToList(Path); Tpa.remObject.SetCommand((int)ISOCNC.Remoting.Commands.Start_Program_Soft); } } #endregion AddProgram #region RemoveProgram // Returns a command that manage the MainWindow_Unloaded command public ICommand RemoveProgram_Command { get { if (m_cmdRemoveProgram == null) m_cmdRemoveProgram = new Command(RemoveProgram); return m_cmdRemoveProgram; } } // Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded. public void RemoveProgram(Object param) { if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift) Tpa.remObject.RemoveAllProgramsFromList(); else if (_ProgramIndex < 0) Tpa.remObject.RemoveProgramFromList(_ProgramPath); else Tpa.remObject.RemoveProgramFromListAtIndex(_ProgramIndex); } #endregion RemoveProgram #region ReadVar // Returns a command that manage the MainWindow_Unloaded command public ICommand ReadVar_Command { get { if (m_cmdReadVar == null) m_cmdReadVar = new Command(ReadVar); return m_cmdReadVar; } } // Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded. public void ReadVar(Object param) { Tpa.remObject.SetVariableCommand((int)ISOCNC.Remoting.VariableCommands.ReadVar, _VarName, _VarValue); } #endregion ReadVar #region WriteVar // Returns a command that manage the MainWindow_Unloaded command public ICommand WriteVar_Command { get { if (m_cmdWriteVar == null) m_cmdWriteVar = new Command(WriteVar); return m_cmdWriteVar; } } // Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded. public void WriteVar(Object param) { Tpa.remObject.SetVariableCommand((int)ISOCNC.Remoting.VariableCommands.WriteVar, _VarName, _VarValue); } #endregion WriteVar #region DeleteAlarms // Returns a command that manage the MainWindow_Unloaded command public ICommand DeleteAlarms_Command { get { if (m_cmdDeleteAlarms == null) m_cmdDeleteAlarms = new Command(DeleteAlarms); return m_cmdDeleteAlarms; } } // Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded. public void DeleteAlarms(Object param) { Tpa.remObject.DeleteAlarms((int)ISOCNC.Remoting.AlarmType.ISO); //Tpa.remObject.GetListInfo(ISOCNC.Remoting.ListInfoRequested.ProgramList, -1); } #endregion DeleteAlarms #endregion COMMANDS } class Axis : VMBase { private string _Name; public string Name { get { return _Name; } set { _Name = value; } } private double _Value; public double Value { get { return _Value; } set { _Value = value; } } public Axis(string Name) { _Name = Name; } } }