| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727 | using Aitex.Core.RT.Log;using Aitex.Core.UI.View.Common;using Caliburn.Micro.Core;using MECF.Framework.Common.DataCenter;using MECF.Framework.Common.Equipment;using Microsoft.VisualBasic;using Microsoft.Win32;using Prism.Commands;using Prism.Mvvm;using Prism.Regions;using Prism.Services.Dialogs;using RecipeEditorLib.DGExtension.CustomColumn;using RecipeEditorLib.RecipeModel.Params;using System;using System.Collections.Generic;using System.Collections.ObjectModel;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using System.Windows.Input;using System.Windows.Media;using System.Xml;using Venus_Core;using Venus_MainPages.PMs;using Venus_MainPages.Sequence;using Venus_MainPages.Views;using WPF.Themes.UserControls;namespace Venus_MainPages.ViewModels{    internal class SequenceViewModel:BindableBase    {        #region 私有字段        //private IUiSequenceManager m_uiSequenceManager = new UiSequenceManager();        private SequenceView m_SequenceView;        private bool m_IsPageLoad = true;        private SequenceData m_CurrentSequence;        private SequenceColumnBuilder columnBuilder = new SequenceColumnBuilder();        private SequenceDataProvider provider = new SequenceDataProvider(new UiSequenceManager());        #endregion        #region 命令        private DelegateCommand<Object> m_LoadedCommand;        public DelegateCommand<Object> LoadedCommand =>            m_LoadedCommand ?? (m_LoadedCommand = new DelegateCommand<Object>(OnLoaded));        private DelegateCommand m_AddStepCommand;        public DelegateCommand AddStepCommand =>            m_AddStepCommand ?? (m_AddStepCommand = new DelegateCommand(AddStep));        private DelegateCommand m_InsertCommand;        public DelegateCommand InsertCommand =>            m_InsertCommand ?? (m_InsertCommand = new DelegateCommand(InsertStep));        private DelegateCommand m_CopyCommand;        public DelegateCommand CopyCommand =>            m_CopyCommand ?? (m_CopyCommand = new DelegateCommand(CopyStep));        private DelegateCommand m_PasteCommand;        public DelegateCommand PasteCommand =>            m_PasteCommand ?? (m_PasteCommand = new DelegateCommand(PasteStep));        private DelegateCommand m_DeleteStepCommand;        public DelegateCommand DeleteStepCommand =>            m_DeleteStepCommand ?? (m_DeleteStepCommand = new DelegateCommand(DeleteStep));        private DelegateCommand m_ReloadSequenceCommand;        public DelegateCommand ReloadSequenceCommand =>            m_ReloadSequenceCommand ?? (m_ReloadSequenceCommand = new DelegateCommand(ReloadSequence));        private DelegateCommand m_SaveSequenceCommand;        public DelegateCommand SaveSequenceCommand =>            m_SaveSequenceCommand ?? (m_SaveSequenceCommand = new DelegateCommand(SaveSequence));        private DelegateCommand<object> m_SelectRecipeCommand;        public DelegateCommand<object> SelectRecipeCommand =>            m_SelectRecipeCommand ?? (m_SelectRecipeCommand = new DelegateCommand<object>(SelectRecipe));        private DelegateCommand<Object> m_TreeSelectChangedCommand;        public DelegateCommand<Object> TreeSelectChangedCommand =>            m_TreeSelectChangedCommand ?? (m_TreeSelectChangedCommand = new DelegateCommand<Object>(TreeSelectChanged));        private DelegateCommand<MouseButtonEventArgs> m_TreeRightMouseDownCommand;        public DelegateCommand<MouseButtonEventArgs> TreeRightMouseDownCommand =>            m_TreeRightMouseDownCommand ?? (m_TreeRightMouseDownCommand = new DelegateCommand<MouseButtonEventArgs>(TreeRightMouseDown));        private DelegateCommand m_NewSequenceCommand;        public DelegateCommand NewSequenceCommand =>            m_NewSequenceCommand ?? (m_NewSequenceCommand = new DelegateCommand(NewSequence));        private DelegateCommand m_RenameCommand;        public DelegateCommand RenameCommand =>            m_RenameCommand ?? (m_RenameCommand = new DelegateCommand(RenameSequence));        private DelegateCommand m_DeleteCommand;        public DelegateCommand DeleteSequenceCommand =>            m_DeleteCommand ?? (m_DeleteCommand = new DelegateCommand(DeleteSequence));        private DelegateCommand m_SaveAsCommand;        public DelegateCommand SaveAsCommand =>            m_SaveAsCommand ?? (m_SaveAsCommand = new DelegateCommand(SaveAsSequence));        #endregion        #region 属性        ObservableCollection<FileNode> m_Files = new ObservableCollection<FileNode>();        public ObservableCollection<FileNode> Files        {            get { return m_Files; }            set { SetProperty(ref m_Files, value); }        }        private string m_SequenceName = String.Empty;        public string SequenceName        {            get { return m_SequenceName; }            set            {                if (value == m_SequenceName)                    return;                SetProperty(ref m_SequenceName, value);                ApplyFilter();            }        }        public SequenceData CurrentSequence        {            get { return m_CurrentSequence; }            set { SetProperty(ref m_CurrentSequence, value); }        }        public FileNode CurrentFileNode { get; private set; }        public ObservableCollection<EditorDataGridTemplateColumnBase> Columns { get; private set; }        #endregion        #region page_load        private void OnLoaded(Object sequenceView)        {            if (m_IsPageLoad)            {                m_IsPageLoad = false;                GetFiles(provider.GetSequenceNameList());                this.CurrentSequence = new SequenceData();                this.Columns = this.columnBuilder.Build();                SequenceColumnBuilder.ApplyTemplate((UserControl)sequenceView, this.Columns);                m_SequenceView = sequenceView as SequenceView;                this.Columns.Apply((c) =>                {                    c.Header = c;                    m_SequenceView.dgCustom.Columns.Add(c);                });                m_SequenceView.dgCustom.ItemsSource = CurrentSequence.Steps;                if (Files.Count > 0)                {                    this.CurrentFileNode = this.Files[0];                    this.SelectDefault(this.CurrentFileNode);                }            }        }        private void GetFiles(List<string> names)        {            Files = new ObservableCollection<FileNode>(RecipeSequenceTreeBuilder.GetFiles("", names));        }        #endregion        #region tree action        public void NewSequence()        {            InputFileNameDialogView dialog = new InputFileNameDialogView("Input New Sequence Name")            {                Owner = Application.Current.MainWindow            };            if (dialog.ShowDialog() == true)            {                string fullpath = dialog.FileName;                if (this.CurrentFileNode.IsFile)                    fullpath = (string.IsNullOrEmpty(this.CurrentFileNode.Parent.FullPath) ? dialog.FileName : this.CurrentFileNode.Parent.FullPath + "\\" + dialog.FileName);                else                    fullpath = (string.IsNullOrEmpty(this.CurrentFileNode.FullPath) ? dialog.FileName : this.CurrentFileNode.FullPath + "\\" + dialog.FileName);                if (string.IsNullOrEmpty(fullpath))                {                    MessageBox.Show("SequenceName cannot be null or empty!");                    return;                }                if (this.IsExist(fullpath))                {                    MessageBox.Show("Sequence already existed!");                }                else                {                    SequenceData sequence = new SequenceData();                    sequence.Name = fullpath;                    sequence.Creator = "Admin";                    sequence.CreateTime = DateTime.Now;                    sequence.Revisor = sequence.Creator;                    sequence.ReviseTime = DateTime.Now;                    sequence.Description = string.Empty;                    if (this.Save(sequence))                    {                        this.CurrentSequence.Name = sequence.Name;                        this.CurrentSequence.Creator = sequence.Creator;                        this.CurrentSequence.CreateTime = sequence.CreateTime;                        this.CurrentSequence.Revisor = sequence.Revisor;                        this.CurrentSequence.ReviseTime = sequence.ReviseTime;                        this.CurrentSequence.Description = sequence.Description;                        this.CurrentSequence.Steps.Clear();                        FileNode file = new FileNode();                        file.Name = dialog.FileName;                        file.FullPath = this.CurrentSequence.Name;                        file.IsFile = true;                        file.Parent = this.CurrentFileNode.IsFile ? this.CurrentFileNode.Parent : this.CurrentFileNode;                        if (this.CurrentFileNode.IsFile)                            this.CurrentFileNode.Parent.Files.Insert(this.FindInsertPosition(this.CurrentFileNode.Parent.Files), file);                        else                            this.CurrentFileNode.Files.Insert(this.FindInsertPosition(this.CurrentFileNode.Files), file);                    }                }            }        }        public void RenameSequence()        {            if (this.CurrentFileNode.IsFile)            {                InputFileNameDialogView dialog = new InputFileNameDialogView("Rename Sequence")                {                    Owner = Application.Current.MainWindow                };                dialog.FileName = this.CurrentFileNode.Name;                if (dialog.ShowDialog() == true)                {                    string fullpath = this.CurrentFileNode.Parent.FullPath == "" ? dialog.FileName : this.CurrentFileNode.Parent.FullPath + "\\" + dialog.FileName;                    if (string.IsNullOrEmpty(fullpath))                    {                        MessageBox.Show("SequenceName cannot be null or empty!");                        return;                    }                    if (this.IsExist(fullpath))                    {                        MessageBox.Show("Sequence already existed!");                    }                    else                    {                        this.CurrentSequence.Revisor = "Admin";                        this.CurrentSequence.ReviseTime = DateTime.Now;                        if (this.provider.Rename(this.CurrentSequence.Name, fullpath))                        {                            this.CurrentFileNode.Name = dialog.FileName;                            this.CurrentFileNode.FullPath = fullpath;                            this.CurrentSequence.Name = fullpath;                        }                        else                            MessageBox.Show("Rename failed!");                    }                }            }        }        public void DeleteSequence()        {            if (this.CurrentFileNode.IsFile)            {                if (WPFMessageBox.ShowQuestion($"Do you want to delete this {this.CurrentSequence.Name}?", "删除后无法恢复!!!") == MessageBoxResult.Yes)                {                    if (this.provider.Delete(this.CurrentSequence.Name))                    {                        this.CurrentFileNode.Parent.Files.Remove(this.CurrentFileNode);                    }                }            }        }        public void SaveAsSequence()        {            if (this.CurrentFileNode.IsFile)            {                InputFileNameDialogView dialog = new InputFileNameDialogView("Input New Sequence Name")                {                    Owner = Application.Current.MainWindow                };                if (dialog.ShowDialog() == true)                {                    string fullpath = (this.CurrentFileNode.Parent.FullPath == "" ? dialog.FileName : this.CurrentFileNode.Parent.FullPath + "\\" + dialog.FileName);                    if (string.IsNullOrEmpty(fullpath))                    {                        MessageBox.Show("SequenceName cannot be null or empty!");                        return;                    }                    if (this.IsExist(fullpath))                    {                        MessageBox.Show("Sequence already existed!");                    }                    else                    {                        this.CurrentSequence.Revisor = "Admin";                        this.CurrentSequence.ReviseTime = DateTime.Now;                        string tempname = this.CurrentSequence.Name;                        this.CurrentSequence.Name = fullpath;                        if (this.provider.SaveAs(fullpath, this.CurrentSequence))                        {                            FileNode node = new FileNode();                            node.Parent = this.CurrentFileNode.Parent;                            node.Name = dialog.FileName;                            node.FullPath = fullpath;                            node.IsFile = true;                            this.CurrentFileNode.Parent.Files.Add(node);                        }                        else                        {                            this.CurrentSequence.Name = tempname;                            MessageBox.Show("SaveAs failed!");                        }                    }                }            }        }        public void TreeSelectChanged(object selectItem)        {            FileNode file = (FileNode)selectItem;            if (file != null && file.IsFile)            {                this.LoadData(file.FullPath);                this.CurrentFileNode = file;            }            else            {                this.ClearData();            }        }        private TreeViewItem GetParentObjectEx<TreeViewItem>(DependencyObject obj) where TreeViewItem : FrameworkElement        {            DependencyObject parent = VisualTreeHelper.GetParent(obj);            while (parent != null)            {                if (parent is TreeViewItem)                {                    return (TreeViewItem)parent;                }                parent = VisualTreeHelper.GetParent(parent);            }            return null;        }        public void TreeRightMouseDown(MouseButtonEventArgs e)        {            if (e != null)            {                var item = GetParentObjectEx<TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem;                if (item != null)                {                    item.Focus();                }            }        }        #endregion        #region sequence action        public void AddStep()        {            this.CurrentSequence.Steps.Add(CurrentSequence.CreateStep(this.Columns));            int index = 1;            foreach (ObservableCollection<Param> parameters in this.CurrentSequence.Steps)            {                (parameters[0] as StepParam).Value = index.ToString();                index++;            }        }        public void InsertStep()        {            int index = -1;            bool found = false;            for (var i = 0; i < this.CurrentSequence.Steps.Count; i++)            {                if (this.CurrentSequence.Steps[i][0] is StepParam && ((StepParam)this.CurrentSequence.Steps[i][0]).Checked)                {                    index = i;                    found = true;                    break;                }            }            if (found)            {                this.CurrentSequence.Steps.Insert(index, this.CurrentSequence.CreateStep(this.Columns));                index = 1;                foreach (ObservableCollection<Param> parameters in this.CurrentSequence.Steps)                {                    (parameters[0] as StepParam).Value = index.ToString();                    index++;                }            }        }        private ObservableCollection<ObservableCollection<Param>> copySteps = new ObservableCollection<ObservableCollection<Param>>();        public void CopyStep()        {            this.copySteps.Clear();            for (var i = 0; i < this.CurrentSequence.Steps.Count; i++)            {                if (this.CurrentSequence.Steps[i][0] is StepParam && ((StepParam)this.CurrentSequence.Steps[i][0]).Checked)                {                    this.copySteps.Add(this.CurrentSequence.CloneStep(this.Columns, this.CurrentSequence.Steps[i]));                }            }        }        public void PasteStep()        {            if (this.copySteps.Count > 0)            {                if (this.CurrentSequence.Steps.Count > 0)                {                    for (var i = 0; i < this.CurrentSequence.Steps.Count; i++)                    {                        if (this.CurrentSequence.Steps[i][0] is StepParam && ((StepParam)this.CurrentSequence.Steps[i][0]).Checked)                        {                            for (var copyindex = 0; copyindex < this.copySteps.Count; copyindex++)                            {                                this.CurrentSequence.Steps.Insert(i, this.CurrentSequence.CloneStep(this.Columns, this.copySteps[copyindex]));                                i++;                            }                            break;                        }                    }                }                else                {                    for (var copyindex = 0; copyindex < this.copySteps.Count; copyindex++)                    {                        this.CurrentSequence.Steps.Insert(copyindex, this.CurrentSequence.CloneStep(this.Columns, this.copySteps[copyindex]));                    }                }                SetStepIndex();            }        }        public void DeleteStep()        {            List<ObservableCollection<Param>> steps = this.CurrentSequence.Steps.ToList();            for (var i = 0; i < steps.Count; i++)            {                if (steps[i][0] is StepParam && ((StepParam)steps[i][0]).Checked)                {                    this.CurrentSequence.Steps.Remove(steps[i]);                }            }            SetStepIndex();        }        public void ReloadSequence()        {            this.LoadData(this.CurrentSequence.Name);        }        public void SelectRecipe(object select)        {            PathFileParam param = (PathFileParam)select;            RecipeSequenceSelectView dialog = new RecipeSequenceSelectView()            {                Owner = Application.Current.MainWindow            };            dialog.Title = "Select Recipe";            var dataContext = new RecipeSequenceSelectViewModel();            dialog.DataContext = dataContext;            ObservableCollection<Param> parameters = param.Parent;            PositionParam posParam = null;            for (var index = 0; index < parameters.Count; index++)            {                if (parameters[index] is PositionParam)                {                    posParam = parameters[index] as PositionParam;                    break;                }            }            List<string> lstFiles = provider.GetRecipesByPath($"{param.PrefixPath}");//m_uiSequenceManager.GetRecipesByPath($"{param.PrefixPath}");            dataContext.Files = new ObservableCollection<FileNode>(RecipeSequenceTreeBuilder.GetFiles("", lstFiles));            if (dialog.ShowDialog() == true)            {                //param.Value = dialog.FullPath;                //param.Value = $"{param.PrefixPath}\\" + dialog.FullPath;                //param.FileName = param.Value;                string path = dialog.FullPath;                int index = path.LastIndexOf("\\");                if (index > -1)                {                    param.FileName = path.Substring(index + 1);                    param.Value = path.Substring(index + 1);                }                else                {                    param.FileName = path;                    param.Value = path;                }                param.IsSaved = false;            }        }        public void SaveSequence()        {            this.Save(this.CurrentSequence);        }        private void SetStepIndex()        {            int index = 1;            foreach (ObservableCollection<Param> parameters in this.CurrentSequence.Steps)            {                (parameters[0] as StepParam).Value = index.ToString();                index++;            }        }        #endregion        #region private        private void ApplyFilter()        {            var sequenceList = provider.GetSequenceNameList() ?? new List<string>();            GetFiles(sequenceList.Where(p => {                return string.IsNullOrWhiteSpace(SequenceName) || p.IndexOf(SequenceName, StringComparison.OrdinalIgnoreCase) > -1;            }).ToList());        }        private void ClearData()        {            this.CurrentSequence.Steps.Clear();            this.CurrentSequence.Name = string.Empty;            this.CurrentSequence.Description = string.Empty;        }        private bool Save(SequenceData seq)        {            bool result = false;            if (string.IsNullOrEmpty(seq.Name))            {                MessageBox.Show("Sequence name can't be empty");                return false;            }            string ruleCheckMessage = null;            if (!ValidateSequenceRules(seq, ref ruleCheckMessage))            {                MessageBox.Show(string.Format($"{seq.Name} rules check failed, don't allow to save: {ruleCheckMessage}"));                return false;            }            seq.Revisor = "Admin";            seq.ReviseTime = DateTime.Now;            result = this.provider.Save(seq);            if (result)            {                foreach (ObservableCollection<Param> parameters in seq.Steps)                {                    parameters.Apply(param => param.IsSaved = true);                }            }            else                MessageBox.Show("Save failed!");            return result;        }        private bool ValidateSequenceRules(SequenceData currentSequence, ref string ruleCheckMessage)        {            int preIndex = -1;            for (var i = 0; i < currentSequence.Steps.Count; i++)            {                var stepInfo = currentSequence.Steps[i];                if (string.IsNullOrEmpty((stepInfo[1] as PositionParam).Value))                {                    ruleCheckMessage = $"Step{(stepInfo[0] as StepParam).Value}, Position Is Empty";                    return false;                }                if ((stepInfo[1] as PositionParam).Value == "LL")                {                    if (preIndex != -1 && (i - preIndex) == 1)                    {                        ruleCheckMessage = $"Step{(stepInfo[0] as StepParam).Value}, The LL is set around the pm ";                        return false;                    }                    preIndex = i;                }            }            return true;        }        private XmlAttribute CreateAttribute(XmlNode node, string attributeName, string value)        {            try            {                XmlDocument doc = node.OwnerDocument;                XmlAttribute attr = null;                attr = doc.CreateAttribute(attributeName);                attr.Value = value;                node.Attributes.SetNamedItem(attr);                return attr;            }            catch (Exception err)            {                string desc = err.Message;                return null;            }        }        private void SelectDefault(FileNode node)        {            if (!node.IsFile)            {                if (node.Files.Count > 0)                {                    foreach (FileNode file in node.Files)                    {                        if (file.IsFile)                        {                            this.TreeSelectChanged(file);                            //break;                        }                    }                }            }        }        public void TreeSelectChanged(FileNode file)        {            if (file != null && file.IsFile)            {                //if (this.IsChanged)                //    if (MessageBox.Show("This sequence is changed,do you want to save it?", "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)                //        this.Save(this.CurrentSequence);                this.LoadData(file.FullPath);                this.CurrentFileNode = file;            }            else            {                //this.ClearData();                //this.editMode = EditMode.None;                this.CurrentSequence.Steps.Clear();            }            //this.UpdateView();        }        private void LoadData(string newSeqName)        {            SequenceData Sequence = this.provider.GetSequenceByName(this.Columns, newSeqName);            this.CurrentSequence.Name = Sequence.Name;            this.CurrentSequence.Creator = Sequence.Creator;            this.CurrentSequence.CreateTime = Sequence.CreateTime;            this.CurrentSequence.Revisor = Sequence.Revisor;            this.CurrentSequence.ReviseTime = Sequence.ReviseTime;            this.CurrentSequence.Description = Sequence.Description;            this.CurrentSequence.Steps.Clear();            Sequence.Steps.ToList().ForEach(step =>                this.CurrentSequence.Steps.Add(step));            //int index = 1;            //foreach (ObservableCollection<Param> parameters in this.CurrentSequence.Steps)            //{            //    (parameters[0] as StepParam).Value = index.ToString();            //    index++;            //    foreach (var para in parameters)            //    {            //        var pathFile = para as PathFileParam;            //        if (pathFile != null)            //        {            //            pathFile.Value = pathFile.Value.Replace($"{pathFile.PrefixPath}\\", "");            //        }            //    }            //}            //this.IsSavedDesc = true;            //this.NotifyOfPropertyChange("IsSavedDesc");            //this.editMode = EditMode.Normal;        }        private bool IsExist(string sequencename)        {            bool existed = false;            FileNode filenode = this.CurrentFileNode.IsFile ? this.CurrentFileNode.Parent : this.CurrentFileNode;            for (var index = 0; index < filenode.Files.Count; index++)            {                if (filenode.Files[index].FullPath.ToLower() == sequencename.ToLower())                {                    existed = true;                    break;                }            }            return existed;        }        private int FindInsertPosition(ObservableCollection<FileNode> files)        {            int pos = -1;            if (files.Count == 0)                pos = 0;            else            {                bool foundfolder = false;                for (var index = 0; index < files.Count; index++)                {                    if (!files[index].IsFile)                    {                        foundfolder = true;                        pos = index;                        break;                    }                }                if (!foundfolder)                    pos = files.Count;            }            return pos;        }        #endregion    }}
 |