//移动相应层级之前的元素
 private static void MoveElements(LadderLogicModule ladderLogicModule, int countlevel)
 {
     for (int i = ladderLogicModule.startY; i <= ladderLogicModule.endY; i++)
     {
         var VLines = ladderLogicModule.LadderVerticalLines.Where(x => { return(x.CountLevel == countlevel && x.Y == i - 1); }).OrderBy(x => { return(x.CountLevel); }).ToList();
         if (VLines.Count == 0)
         {
             VLines = ladderLogicModule.LadderVerticalLines.Where(x => { return(x.CountLevel == countlevel && x.Y == i); }).OrderBy(x => { return(x.CountLevel); }).ToList();
         }
         if (VLines.Count != 0)
         {
             var VLine      = VLines.First();
             var tempVLines = ladderLogicModule.LadderVerticalLines.Where(x => { return(x.CountLevel < countlevel && (x.Y == i || x.Y == i - 1)); }).OrderBy(x => { return(x.CountLevel); }).ToList();
             //若此层级VLine之前没有前一层级的VLine,则直接移动元素
             if (tempVLines.Count == 0)
             {
                 var tempList = ladderLogicModule.LadderElements.Where(x => { return(x.Type != ElementType.HLine && x.Type != ElementType.Output && x.Y == i && x.X <= VLine.X); }).OrderBy(x => { return(x.X); }).ToList();
                 for (int j = 0; j < tempList.Count; j++)
                 {
                     if (tempList[j].X != j)
                     {
                         HorizontalLineViewModel HLine = new HorizontalLineViewModel();
                         HLine.X = tempList[j].X;
                         HLine.Y = tempList[j].Y;
                         var oldele = ladderLogicModule.ReplaceElement(HLine);
                         oldele.X = j;
                         ladderLogicModule.ReplaceElement(oldele);
                     }
                 }
             }//否则,元素在两个层级VLine之间移动
             else
             {
                 var tempVLine = tempVLines.Last();
                 var tempList  = ladderLogicModule.LadderElements.Where(x => { return(x.Type != ElementType.HLine && x.Type != ElementType.Output && x.Y == i && x.X <= VLine.X && x.X > tempVLine.X); }).OrderBy(x => { return(x.X); }).ToList();
                 for (int j = 0; j < tempList.Count; j++)
                 {
                     if (tempList[j].X != j + tempVLine.X + 1)
                     {
                         HorizontalLineViewModel HLine = new HorizontalLineViewModel();
                         HLine.X = tempList[j].X;
                         HLine.Y = tempList[j].Y;
                         var oldele = ladderLogicModule.ReplaceElement(HLine);
                         oldele.X = j + tempVLine.X + 1;
                         ladderLogicModule.ReplaceElement(oldele);
                     }
                 }
             }
         }
     }
 }
Exemple #2
0
        private void MainCanvas_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            int x = _selectRect.X;
            int y = _selectRect.Y;

            switch (e.Key)
            {
            case Key.F1:
                var viewmodel1 = new LDViewModel()
                {
                    X = x, Y = y
                };
                AddElement(viewmodel1);
                break;

            case Key.F2:
                var viewmodel2 = new OUTViewModel()
                {
                    X = x, Y = y
                };
                AddElement(viewmodel2);
                break;

            case Key.F3:
                var vline = new VerticalLineViewModel()
                {
                    X = x, Y = y
                };
                AddVerticalLine(vline);
                break;

            case Key.F4:
                var hline = new HorizontalLineViewModel()
                {
                    X = x, Y = y
                };
                AddElement(hline);
                break;

            case Key.F5:
                Compile();
                break;

            default:
                break;
            }
        }
        private static void Movement(LadderLogicModule ladderLogicModule)
        {
            var tempList = ladderLogicModule.LadderElements.Where(x => { return(x.Type != ElementType.Output && x.Type != ElementType.HLine); }).OrderBy(x => { return(x.X); }).ToList();

            for (int i = 0; i < tempList.Count; i++)
            {
                if (tempList[i].X != i)
                {
                    HorizontalLineViewModel HLine = new HorizontalLineViewModel();
                    HLine.X = tempList[i].X;
                    HLine.Y = tempList[i].Y;
                    var oldele = ladderLogicModule.ReplaceElement(HLine);
                    oldele.X = i;
                    ladderLogicModule.ReplaceElement(oldele);
                }
            }
        }
 //移动每行最大层级VLine之后的元素
 private static void MoveResidueEle(LadderLogicModule ladderLogicModule)
 {
     for (int i = ladderLogicModule.startY; i <= ladderLogicModule.endY; i++)
     {
         var VLines = ladderLogicModule.LadderVerticalLines.Where(x => { return(x.Y == i - 1 || x.Y == i); }).OrderBy(x => { return(x.CountLevel); });
         if (VLines.Count() > 0)
         {
             var VLine    = VLines.Last();
             var tempList = ladderLogicModule.LadderElements.Where(x => { return(x.Type != ElementType.Output && x.Type != ElementType.HLine && x.Y == i && x.X > VLine.X); }).OrderBy(x => { return(x.X); }).ToList();
             for (int j = 0; j < tempList.Count; j++)
             {
                 if (tempList[j].X != j + VLine.X + 1)
                 {
                     HorizontalLineViewModel HLine = new HorizontalLineViewModel();
                     HLine.X = tempList[j].X;
                     HLine.Y = tempList[j].Y;
                     var oldele = ladderLogicModule.ReplaceElement(HLine);
                     oldele.X = j + VLine.X + 1;
                     ladderLogicModule.ReplaceElement(oldele);
                 }
             }
         }
     }
 }
Exemple #5
0
        static private void Read(LadderNetworkViewModel lnvmodel)
        {
            int sz = ReadE32();

            sz += eid;
            if ((option & OPTION_COMMENT) != 0)
            {
                lnvmodel.NetworkBrief = ReadTextE8();
            }
            lnvmodel.RowCount = edata[eid++];
            int le = (lnvmodel.RowCount * GlobalSetting.LadderXCapacity) >> 2;

            for (int y = 0; y < lnvmodel.RowCount; y++)
            {
                for (int x = 0; x < GlobalSetting.LadderXCapacity; x++)
                {
                    int p  = y * GlobalSetting.LadderXCapacity + x;
                    int p1 = p >> 2;
                    int p2 = (p & 3) * 2;
                    if ((edata[eid + p1] & (1 << p2)) != 0)
                    {
                        HorizontalLineViewModel hlvmodel = new HorizontalLineViewModel();
                        hlvmodel.X = x; hlvmodel.Y = y;
                        lnvmodel.ReplaceElement(hlvmodel);
                    }
                    if ((edata[eid + p1] & (1 << (p2 + 1))) != 0)
                    {
                        VerticalLineViewModel vlvmodel = new VerticalLineViewModel();
                        vlvmodel.X = x; vlvmodel.Y = y;
                        lnvmodel.ReplaceVerticalLine(vlvmodel);
                    }
                }
            }
            while (eid < sz)
            {
                BaseViewModel bvmodel = null;
                int           x       = edata[eid++];
                int           y       = edata[eid++];
                int           catalog = LadderInstViewModelPrototype.GetCatalogFromOrder(edata[eid++]);
                bvmodel   = LadderInstViewModelPrototype.Clone(catalog);
                bvmodel.X = x;
                bvmodel.Y = y;
                for (int i = 0; i < bvmodel.Model.ParaCount; i++)
                {
                    IValueModel ivmold = bvmodel.Model.GetPara(i);
                    IValueModel ivmnew = regs[ReadE16()];
                    if (ivmnew is DWordValue || ivmnew is DDoubleWordValue || ivmnew is DFloatValue)
                    {
                        if (ivmold is WordValue)
                        {
                            ivmnew = new DWordValue(ivmnew.Index, ivmnew.Offset);
                        }
                        if (ivmold is DoubleWordValue)
                        {
                            ivmnew = new DDoubleWordValue(ivmnew.Index, ivmnew.Offset);
                        }
                        if (ivmold is FloatValue)
                        {
                            ivmnew = new DFloatValue(ivmnew.Index, ivmnew.Offset);
                        }
                    }
                    bvmodel.Model.SetPara(i, ivmnew);
                }
                lnvmodel.ReplaceElement(bvmodel);
            }
            InstructionCommentManager.RaiseMappedMessageChangedEvent();
        }
 private static void MoveVerticalLines(LadderLogicModule ladderLogicModule, List <VerticalLineViewModel> VLines, int cnt)
 {
     for (int j = 0; j < VLines.Count(); j++)
     {
         var tempVLine = VLines.ElementAt(j);
         if (tempVLine.X != cnt - 1)
         {
             IntPoint point = new IntPoint();
             //大于cnt - 1则表示向前移,小于则向后移
             if (tempVLine.X > cnt - 1)
             {
                 //检查VLine周围元素的分布关系,判断是否在移动时需要添加或减少HLine
                 DirectionStatus status = CheckVLine(ladderLogicModule, tempVLine);
                 if (status == DirectionStatus.Down_Inc || status == DirectionStatus.Up_Inc)
                 {
                     if (status == DirectionStatus.Down_Inc)
                     {
                         point.Y = tempVLine.Y + 1;
                     }
                     else if (status == DirectionStatus.Up_Inc)
                     {
                         point.Y = tempVLine.Y;
                     }
                     for (int k = cnt; k <= tempVLine.X; k++)
                     {
                         point.X = k;
                         if (!ladderLogicModule.LadderElements.Exists(x => { return(x.X == point.X && x.Y == point.Y); }))
                         {
                             HorizontalLineViewModel HLine = new HorizontalLineViewModel();
                             HLine.X = point.X;
                             HLine.Y = point.Y;
                             ladderLogicModule.ReplaceElement(HLine);
                         }
                     }
                 }
                 if (status == DirectionStatus.Down_Dec || status == DirectionStatus.Up_Dec)
                 {
                     if (status == DirectionStatus.Down_Dec)
                     {
                         point.Y = tempVLine.Y + 1;
                     }
                     else
                     {
                         point.Y = tempVLine.Y;
                     }
                     for (int k = cnt; k <= tempVLine.X; k++)
                     {
                         point.X = k;
                         ladderLogicModule.RemoveElement(point.X, point.Y);
                     }
                 }
             }
             else
             {
                 for (int k = tempVLine.X + 1; k <= cnt - 1; k++)
                 {
                     point.X = k;
                     point.Y = tempVLine.Y + 1;
                     if (!ladderLogicModule.LadderElements.Exists(x => { return(x.X == point.X && x.Y == point.Y); }))
                     {
                         HorizontalLineViewModel HLine = new HorizontalLineViewModel();
                         HLine.X = point.X;
                         HLine.Y = point.Y;
                         ladderLogicModule.ReplaceElement(HLine);
                     }
                 }
             }
             ladderLogicModule.RemoveVerticalLine(tempVLine.X, tempVLine.Y);
             tempVLine.X = cnt - 1;
             ladderLogicModule.ReplaceVerticalLine(tempVLine);
         }
     }
 }