using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Threading; using System.Windows; using System.Windows.Forms; using System.Windows.Input; using GummingCommon; using GummingEntity; using GummingSupport; using GummingControl; using System.Text; using GummingBusiness; using Newtonsoft.Json; using GummingLine; namespace Gumming { public class FormulaCOTViewModule : ViewModelBase { #region base parameters private int rows = 20; private int pagenumber = 1; public FormulaCOTView View { get; set; } private bool IsConfirm; private bool isLoading; private List armPrs; private List armEbrs; #endregion #region constructor public FormulaCOTViewModule() { View = new FormulaCOTView(); View.DataContext = this; InitializeCommands(); InitializeParameters(); } private void InitializeCommands() { AddStepCommand = new DelegateCommand(OnAddStepCommand); InsertStepCommand = new DelegateCommand(OnInsertStepCommand); DeleteStepCommand = new DelegateCommand(OnDeleteStepCommand); SaveStepCommand = new DelegateCommand(OnSaveStepCommand); AllowCommand = new DelegateCommand(OnAllowCommand); SelectStepCommand = new DelegateCommand(OnSelectStepCommand); CopyStepCommand = new DelegateCommand(OnCopyStepCommand); DisabledCommand = new DelegateCommand(OnDisabledCommand); PRCommand = new DelegateCommand(OnPRCommand); } public override void InitializeParameters(object content = null) { IsConfirm = false; WindowTitle = ""; AllowEdit = false; CreateBy = Global.CurrentUserCode; HardFormulaCOTs = new ObservableCollection(); armPrs = new List(); armPrs.Clear(); armPrs.Add(new ArmPrsEntity() { ArmPr = "", Tag = 0 }); armPrs.Add(new ArmPrsEntity() { ArmPr = "PR1", Tag = 1 }); armPrs.Add(new ArmPrsEntity() { ArmPr = "PR2", Tag = 2 }); armEbrs = new List(); armEbrs.Clear(); armEbrs.Add(new ArmEbrEntity() { ArmEbr = "", Tag = 0 }); armEbrs.Add(new ArmEbrEntity() { ArmEbr = "EBR", Tag = 1 }); //armEbrs.Add(new ArmEbrEntity() { ArmEbr = "EBR2", Tag = 2 }); //默认追加六步空白步骤 InitializeEmptyFormula(false); } ~FormulaCOTViewModule() { } #endregion #region Binding Properties public string WindowTitle { get; private set; } /// /// InjectPosition /// private decimal _InjectPosition; public decimal InjectPosition { get { return _InjectPosition; } set { _InjectPosition = value; OnPropertyChanged("InjectPosition"); } } private ObservableCollection _HardFormulaCOTs; public ObservableCollection HardFormulaCOTs { get { return _HardFormulaCOTs; } set { _HardFormulaCOTs = value; OnPropertyChanged("HardFormulaCOTs"); } } private HardFormulaCOTEntity _SelectedStep; public HardFormulaCOTEntity SelectedStep { get { return _SelectedStep; } set { _SelectedStep = value; OnPropertyChanged("SelectedStep"); } } private string _FormulaName; public string FormulaName { get { return _FormulaName; } set { _FormulaName = value; OnPropertyChanged("FormulaName"); } } private string _CreateBy; public string CreateBy { get { return _CreateBy; } set { _CreateBy = value; OnPropertyChanged("CreateBy"); } } private HardStationType _SelectedFormulaType; public HardStationType SelectedFormulaType { get { return _SelectedFormulaType; } set { _SelectedFormulaType = value; OnPropertyChanged("SelectedFormulaType"); } } private HardFormulaEntity _SelectedFormula; public HardFormulaEntity SelectedFormula { get { return _SelectedFormula; } set { _SelectedFormula = value; OnPropertyChanged("SelectedFormula"); } } private bool _AllowEdit; public bool AllowEdit { get { return _AllowEdit; } set { _AllowEdit = value; OnPropertyChanged("AllowEdit"); } } #endregion #region Binding Commands private ICommand _AddStepCommand; public ICommand AddStepCommand { get { return _AddStepCommand; } set { _AddStepCommand = value; OnPropertyChanged("AddStepCommand"); } } private ICommand _DeleteStepCommand; public ICommand DeleteStepCommand { get { return _DeleteStepCommand; } set { _DeleteStepCommand = value; OnPropertyChanged("DeleteStepCommand"); } } private ICommand _SaveStepCommand; public ICommand SaveStepCommand { get { return _SaveStepCommand; } set { _SaveStepCommand = value; OnPropertyChanged("SaveStepCommand"); } } private ICommand _AllowCommand; public ICommand AllowCommand { get { return _AllowCommand; } set { _AllowCommand = value; OnPropertyChanged("AllowCommand"); } } private ICommand _InsertStepCommand; public ICommand InsertStepCommand { get { return _InsertStepCommand; } set { _InsertStepCommand = value; OnPropertyChanged("InsertStepCommand"); } } private ICommand _SelectStepCommand; public ICommand SelectStepCommand { get { return _SelectStepCommand; } set { _SelectStepCommand = value; OnPropertyChanged("SelectStepCommand"); } } private ICommand _CopyStepCommand; public ICommand CopyStepCommand { get { return _CopyStepCommand; } set { _CopyStepCommand = value; OnPropertyChanged("CopyStepCommand"); } } private ICommand _DisabledCommand; public ICommand DisabledCommand { get { return _DisabledCommand; } set { _DisabledCommand = value; OnPropertyChanged("DisabledCommand"); } } private ICommand _PRCommand; public ICommand PRCommand { get { return _PRCommand; } set { _PRCommand = value; OnPropertyChanged("PRCommand"); } } #endregion #region Private Methods public void InitializeEmptyFormula(bool canEdit) { FormulaName = string.Empty; AllowEdit = canEdit; HardFormulaCOTs.Clear(); //默认追加六步空白步骤 for (int i = 0; i < 6; i++) { HardFormulaCOTEntity cot = new HardFormulaCOTEntity(); cot.StepIndex = i + 1; cot.ArmPrs = armPrs; cot.ArmEbrs = armEbrs; cot.ArmScanTimer = 1; cot.MainAxisAcc = 100; cot.Stillicide = false; cot.Bsr = false; cot.ArmUD = false; cot.Copy = (HardFormulaCOTEntity)cot.Clone(); HardFormulaCOTs.Add(cot); } } public void StartLoadTestItem(string formulaId) { try { isLoading = true; HardFormulaCOTs = new ObservableCollection(); var record = HardFormulaCOTDA.Load(new PagerEntity() { PageIndex = pagenumber, Rows = rows, Conditions = " and FormulaId='"+ formulaId + "' " }); List logs = (List)record.rows; View.Dispatcher.Invoke((Action)(() => { foreach (var s in logs) { s.ArmPrs = armPrs; s.SelecedArmPr = s.ArmPrs.FirstOrDefault(q=>q.Tag == s.ArmPr); s.ArmEbrs = armEbrs; s.SelecedArmEbr = s.ArmEbrs.FirstOrDefault(q => q.Tag == s.ArmEbr); s.Copy = (HardFormulaCOTEntity)s.Clone(); HardFormulaCOTs.Add(s); } OnPropertyChanged("HartFormulaCOT"); isLoading = false; })); } catch (Exception ex) { LogService.WriteErrorLog(ex); ShowErrorBox("Error:" + ex.Message); return; } finally { CloseProgressView(); } } private void OnAddStepCommand(Object sender) { if (HardFormulaCOTs.Count == 20) { ShowMessageBox("步骤不能超过20条"); return; } var cot = new HardFormulaCOTEntity(); cot.StepIndex = HardFormulaCOTs.Count + 1; cot.ArmPrs = armPrs; cot.ArmEbrs = armEbrs; cot.Stillicide = false; cot.ArmScanTimer = 1; cot.MainAxisAcc = 100; cot.Bsr = false; cot.ArmUD = false; cot.Copy = (HardFormulaCOTEntity)cot.Clone(); HardFormulaCOTs.Add(cot); } private void OnInsertStepCommand(Object sender) { if (HardFormulaCOTs.Count == 20) { ShowMessageBox("步骤不能超过20条"); return; } if (SelectedStep == null) { ShowMessageBox("请选择一个步骤再进行插入!"); return; } int curIndex = HardFormulaCOTs.IndexOf(SelectedStep); var cot = new HardFormulaCOTEntity(); cot.ArmPrs = armPrs; cot.ArmEbrs = armEbrs; cot.ArmScanTimer = 1; cot.MainAxisAcc = 100; cot.Stillicide = false; cot.Bsr = false; cot.ArmUD = false; cot.Copy = (HardFormulaCOTEntity)cot.Clone(); HardFormulaCOTs.Insert(curIndex, cot); for (int i = 0; i < HardFormulaCOTs.Count; i++) { HardFormulaCOTs[i].StepIndex = i + 1; } } private void OnDeleteStepCommand(Object sender) { if (SelectedStep == null) { ShowMessageBox("请选择一个步骤再进行删除!"); return; } HardFormulaCOTs.Remove(SelectedStep); for (int i = 0; i < HardFormulaCOTs.Count; i++) { HardFormulaCOTs[i].StepIndex = i + 1; } } public void OnSaveStepCommand(Object sender) { if (string.IsNullOrWhiteSpace(FormulaName)) { ShowMessageBox("配方名称不能为空"); return; } if (HardFormulaCOTs.Any(q => q.SelecedArmEbr != null && q.SelecedArmPr != null && q.SelecedArmEbr.Tag != 0 && q.SelecedArmPr.Tag != 0)) { ShowMessageBox("不能同时选择PR和EBR臂"); return; } var SafeStartPr = Common.ToDecimal(HardParameters.Read(SelectedFormulaType.StationCode + Constant.PrSafeStart)); var SafeEndPr = Common.ToDecimal(HardParameters.Read(SelectedFormulaType.StationCode + Constant.PrSafeEnd)); var SafeStartEbr = Common.ToDecimal(HardParameters.Read(SelectedFormulaType.StationCode + Constant.EbrSafeStart)); var SafeEndEbr = Common.ToDecimal(HardParameters.Read(SelectedFormulaType.StationCode + Constant.EbrSafeEnd)); for (int i = 0; i < HardFormulaCOTs.Count; i++) { var cot = HardFormulaCOTs[i]; var SafeStart = (cot.SelecedArmPr != null && cot.SelecedArmPr.Tag > 0) ? SafeStartPr : SafeStartEbr; var SafeEnd = (cot.SelecedArmPr != null && cot.SelecedArmPr.Tag > 0) ? SafeEndPr : SafeEndEbr; if (cot.ArmPosition1 < SafeStart) { cot.ArmPosition1 = 0; ShowMessageBox("手臂位置不能小于安全始位"); return; } if (cot.ArmPosition2 < SafeStart) { cot.ArmPosition2 = 0; ShowMessageBox("手臂位置不能小于安全始位"); return; } if (cot.ArmPosition1 > SafeEnd) { cot.ArmPosition1 = SafeEnd; ShowMessageBox("手臂位置不能大于安全止位"); return; } if (cot.ArmPosition2 > SafeEnd) { cot.ArmPosition2 = SafeEnd; ShowMessageBox("手臂位置不能大于安全止位"); return; } } HardFormulaEntity hfe = new HardFormulaEntity(); hfe.FormulaCode = FormulaName; hfe.FormulaName = FormulaName; hfe.CreateBy = Global.CurrentUserCode; hfe.StationCode = SelectedFormulaType.StationCode; hfe.StationName = SelectedFormulaType.StationName; hfe.StationType = (int)SelectedFormulaType.StationType; hfe.StillicideAmount = InjectPosition; hfe.RecId = SelectedFormula?.RecId; hfe.RecId = HardFormulaDA.Save(hfe, HardFormulaCOTs.ToList()); hfe.CreateTime = DateTime.Now; SelectedFormula = hfe; AllowEdit = false; for (int i = 0; i < HardFormulaCOTs.Count; i++) { HardFormulaCOTs[i].Copy = (HardFormulaCOTEntity)HardFormulaCOTs[i].Clone(); } AppEventAggregator.GetEventAggregator().GetEvent().Publish(new NotifyEventParameter() { NotifyType = CallbackType.Formula, NotifyContent = SelectedFormula }); } private void OnAllowCommand(Object sender) { AllowEdit = true; } private void OnDisabledCommand(Object sender) { AllowEdit = false; StartLoadTestItem(SelectedFormula?.RecId); } private void OnPRCommand(Object sender) { if (StationLockControl.StationIsLock(SelectedFormulaType.StationCode)) { ShowMessageBox(HintMessage.StationLocked); return; } ControlCOTPrViewModule ccvm = new ControlCOTPrViewModule(); ccvm.FormulaId = SelectedFormula?.RecId; ccvm.StationCode = SelectedFormulaType.StationCode; ccvm.LoadDefaultValue(1); ccvm.ShowDialog(); } private void OnSelectStepCommand(Object sender) { } private void OnCopyStepCommand(Object sender) { if (HardFormulaCOTs.Count == 20) { ShowMessageBox("步骤不能超过20条"); return; } if (SelectedStep == null) { ShowMessageBox("请选择一个步骤再进行拷贝!"); return; } var cot = (HardFormulaCOTEntity)SelectedStep.Clone(); cot.ArmPrs = armPrs; cot.ArmEbrs = armEbrs; cot.Copy = (HardFormulaCOTEntity)cot.Clone(); HardFormulaCOTs.Add(cot); for (int i = 0; i < HardFormulaCOTs.Count; i++) { HardFormulaCOTs[i].StepIndex = i + 1; } } #endregion } }