/// <summary> /// 将选定的指令替换 /// </summary> /// <param name="showdialog">是否显示提示窗口</param> private void Replace(bool showdialog = true) { // 替换成功和失败的个数,以及详细的错误信息 int success = 0; int error = 0; string errormsg = String.Empty; // 建立替换的命令集 NetworkReplaceElementsCommandGroup commandall = new NetworkReplaceElementsCommandGroup( this, Items.ToArray()); // 处理所有在表中选择的元素 foreach (ReplaceElement rele in DG_List.SelectedItems) { // 获得相关信息 BaseViewModel bvmodel = rele.BVModel; LadderNetworkViewModel lnvmodel = rele.LNVModel; LadderDiagramViewModel ldvmodel = rele.LDVModel; ldvmodel.IsModify = true; int x = bvmodel.X; int y = bvmodel.Y; _cmdmanager.LDVModel = ldvmodel; // 建立对应的替换命令 NetworkReplaceElementsCommand command = null; NetworkReplaceElementsCommand_ForReplaceWindow commandrw = null; try { command = RF_Change.Replace( RF_Input, rele.Detail, x, y, ldvmodel, lnvmodel); commandrw = new NetworkReplaceElementsCommand_ForReplaceWindow( lnvmodel, rele, command); commandall.Add(commandrw); success++; } catch (Exception exce2) { error++; if (Thread.CurrentThread.CurrentUICulture.Name.Contains("zh-Hans")) { errormsg += String.Format("在{0:s}的网络{1:d}的坐标({2:d},{3:d})处发生错误:{4:s}\r\n", ldvmodel.ProgramName, lnvmodel.NetworkNumber, x, y, exce2.Message); } else { errormsg += String.Format("An error occurred at the coordinates({2:d},{3:d})of the network {1:d} of {0:s}: {4:s}\r\n", ldvmodel.ProgramName, lnvmodel.NetworkNumber, x, y, exce2.Message); } } } // 执行替换 _cmdmanager.Execute(commandall); // 当需要显示结果,或者出现错误替换时显示 if (showdialog || error > 0) { ReplaceReportWindow report = new ReplaceReportWindow(); if (Thread.CurrentThread.CurrentUICulture.Name.Contains("zh-Hans")) { report.TB_Subtitle.Text = String.Format("总共进行了{0:d}次替换,{1:d}次成功,{2:d}次错误。" , success + error, success, error); } else { report.TB_Subtitle.Text = string.Format("A total of {0:d} times, {1:d} success, {2:d} Fail." , success + error, success, error); } report.TB_Message.Text = errormsg; report.WindowStartupLocation = WindowStartupLocation.CenterScreen; report.ShowDialog(); } }
/// <summary> /// 添加新的替换命令 /// </summary> /// <param name="command">替换命令</param> public void Add(NetworkReplaceElementsCommand_ForReplaceWindow command) { items.Add(command); eles_replaced.Add(command.Element); }