Exemplo n.º 1
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="_lnvmodel">网络的显示模型</param>
 /// <param name="_element">要替换的列表元素</param>
 /// <param name="_command">替换命令</param>
 public NetworkReplaceElementsCommand_ForReplaceWindow
 (
     LadderNetworkViewModel _lnvmodel,
     ReplaceElement _element,
     NetworkReplaceElementsCommand _command
 )
 {
     lnvmodel = _lnvmodel;
     Element  = _element;
     BVM_old  = _command.OldElements.First();
     BVM_new  = _command.NewElements.First();
 }
Exemplo n.º 2
0
        /// <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();
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// 添加新的替换命令
 /// </summary>
 /// <param name="command">替换命令</param>
 public void Add(NetworkReplaceElementsCommand command)
 {
     items.Add(command);
 }
Exemplo n.º 4
0
        /// <summary>
        /// 根据该格式进行替换操作,生成替换命令
        /// </summary>
        /// <param name="prototype">查找格式</param>
        /// <param name="input">指令文本</param>
        /// <param name="x">元件X坐标</param>
        /// <param name="y">元件Y坐标</param>
        /// <param name="ldvmodel">所属网络</param>
        /// <param name="lnvmodel">所属程序</param>
        /// <returns>替换命令</returns>
        public NetworkReplaceElementsCommand Replace
        (
            ReplaceFormat prototype,
            string input, int x, int y,
            LadderDiagramViewModel ldvmodel,
            LadderNetworkViewModel lnvmodel
        )
        {
            // 将指令文本转为正则格式
            ReplaceFormat iformat = new ReplaceFormat(input);
            // 要匹配的参数项
            ReplaceFormatArg arg;

            arg.Type   = ARG_INVAILD;
            arg.Low    = arg.High = arg.OLow = arg.OHigh = 0;
            arg.Offset = arg.Base = arg.Text = String.Empty;
            // 替换后的指令文本
            string output = String.Empty;

            // 检查原指令的每个参数
            for (int i = 0; i < iformat.ArgsCount; i++)
            {
                // 替换格式为寄存器格式
                if (prototype.Type == TYPE_REGISTER)
                {
                    // 查找格式也为寄存器格式,并且不匹配
                    if (Type == TYPE_REGISTER &&
                        !Match(prototype.GetArgs(0), iformat.GetArgs(i)))
                    {
                        // 保留原样
                        output += iformat.GetArgs(i).Text + " ";
                    }
                    else
                    {
                        arg.Type = ARG_INVAILD;
                        // 寄存器格式只读第一个参数
                        if (Type == TYPE_REGISTER)
                        {
                            arg = GetArgs(0);
                        }
                        // 指令格式读相应位置的参数
                        else if (i <= ArgsCount)
                        {
                            arg = GetArgs(i);
                        }
                        // 当前的参数格式
                        switch (arg.Type)
                        {
                        // 指令名称
                        case ARG_INSTRUCTION:
                            // 对应位置不是指令时保留原样
                            if (iformat.GetArgs(i).Type != ARG_INSTRUCTION)
                            {
                                output += iformat.GetArgs(i).Text + " ";
                                break;
                            }
                            // 替换对应的指令名称
                            output += arg.Text + " ";
                            break;

                        // 寄存器
                        case ARG_REGISTER:
                            // 对应位置不是寄存器时保留原样
                            if (iformat.GetArgs(i).Type != ARG_REGISTER &&
                                iformat.GetArgs(i).Type != ARG_UNDEFINED)
                            {
                                output += iformat.GetArgs(i).Text + " ";
                                break;
                            }
                            // 判断查找模型的这个参数是否制定唯一的寄存器
                            bool isunique = false;
                            isunique  = (!prototype.GetArgs(0).Base.Equals(arg.Base));
                            isunique |= (prototype.GetArgs(0).Low == prototype.GetArgs(0).High);
                            isunique |= (arg.Low == arg.High);
                            int baseid = arg.Low;
                            // 不是唯一的情况下需要地址转移
                            if (!isunique)
                            {
                                baseid += iformat.GetArgs(i).Low - prototype.GetArgs(0).Low;
                            }
                            // 得到对应的转换后的寄存器的文本
                            output += String.Format("{0:s}{1:d}",
                                                    arg.Base, baseid);
                            if (!arg.Offset.Equals(String.Empty))
                            {
                                output += String.Format("{0:s}{1:d}",
                                                        arg.Offset, arg.OLow);
                            }
                            output += " ";
                            break;

                        // 其他格式
                        default:
                            output += iformat.GetArgs(i).Text + " ";
                            break;
                        }
                    }
                    continue;
                }
                // 超出的部分保留原样
                if (i >= ArgsCount)
                {
                    output += iformat.GetArgs(i).Text + " ";
                    continue;
                }
                // 根据当前参数的格式
                switch (GetArgs(i).Type)
                {
                // 指令名称
                case ARG_INSTRUCTION:
                    output += GetArgs(i).Text + " ";
                    break;

                // 寄存器
                case ARG_REGISTER:
                    // 策略和上面一致
                    bool isunique = false;
                    isunique |= (i >= prototype.ArgsCount);
                    if (!isunique)
                    {
                        isunique  = (prototype.GetArgs(i).Low == prototype.GetArgs(i).High);
                        isunique |= (GetArgs(i).Low == GetArgs(i).High);
                    }
                    int baseid = GetArgs(i).Low;
                    if (!isunique)
                    {
                        baseid += iformat.GetArgs(i).Low - prototype.GetArgs(i).Low;
                    }
                    output += String.Format("{0:s}{1:d}",
                                            GetArgs(i).Base, baseid);
                    if (!GetArgs(i).Offset.Equals(String.Empty))
                    {
                        output += String.Format("{0:s}{1:d}",
                                                GetArgs(i).Offset, GetArgs(i).OLow);
                    }
                    output += " ";
                    break;

                // 其他格式
                default:
                    output += iformat.GetArgs(i).Text + " ";
                    break;
                }
            }
            // 建立命令并返回
            NetworkReplaceElementsCommand command = null;
            int rectX = ldvmodel.SelectionRect.X;

            ldvmodel.RegisterInstructionInput(
                output, x, y, lnvmodel, ref command, ref rectX);
            return(command);
        }