private static void MoveHorizontalLineEle(LadderLogicModule ladderLogicModule, int index, List <BaseViewModel> models) { BaseViewModel model; VerticalLineViewModel VLine; int line = models[0].Y; if (index != 0) { for (int i = 0; i < models.Count; i++) { model = models[i]; ladderLogicModule.RemoveElement(model.X, model.Y); model.Y -= index; ladderLogicModule.ReplaceElement(model); } //对每一行,只保留其上一行的VLine(保持图的基本连通性),并一起移动 var VLines = ladderLogicModule.LadderVerticalLines.Where(x => { return(x.Y == line - 1); }).ToList(); for (int i = 0; i < VLines.Count(); i++) { VLine = VLines[i]; ladderLogicModule.RemoveVerticalLine(VLine.X, VLine.Y); VLine.Y -= index; ladderLogicModule.ReplaceVerticalLine(VLine); } } }
private static void MoveAllElements(LadderLogicModule ladderLogicModule, int index) { var allElements = new List <BaseViewModel>(ladderLogicModule.LadderElements); allElements.AddRange(ladderLogicModule.LadderVerticalLines); foreach (var ele in allElements) { if (ele.Type == ElementType.VLine) { ladderLogicModule.RemoveVerticalLine(ele.X, ele.Y); ele.Y -= index; ladderLogicModule.ReplaceVerticalLine(ele as VerticalLineViewModel); } else { ladderLogicModule.RemoveElement(ele.X, ele.Y); ele.Y -= index; ladderLogicModule.ReplaceElement(ele); } } }
private static void RemoveVLines(LadderLogicModule ladderLogicModule, int desY, int x, int y) { IntPoint p = new IntPoint(); VerticalLineViewModel vline; p.X = x; p.Y = y; while (ladderLogicModule.LadderVerticalLines.Exists(a => { return(a.X == p.X && a.Y == p.Y); })) { vline = ladderLogicModule.LadderVerticalLines.Where(a => { return(a.X == p.X && a.Y == p.Y); }).First(); if (!ladderLogicModule.Parent.CheckVerticalLine(vline)) { ladderLogicModule.RemoveVerticalLine(vline.X, vline.Y); } p.Y--; if (p.Y < desY) { break; } } }
/// <summary> /// 网络纵向扫描 /// </summary> /// <param name="ladderLogicModule">代表扫描的最小单元</param> private static void VerticalScan(LadderLogicModule ladderLogicModule) { PreScan(ladderLogicModule); int emptyLineCnt = 0; int line = ladderLogicModule.endY + 1; for (int i = ladderLogicModule.startY; i < line; i++) { var tempList = ladderLogicModule.LadderElements.Where(x => { return(x.Y == i); }).ToList(); if (tempList.Count() == 0 || tempList.All(x => { return(x.Type == ElementType.HLine); })) { if (tempList.Count() != 0) { for (int j = 0; j < tempList.Count(); j++) { ladderLogicModule.RemoveElement(tempList[j].X, tempList[j].Y); } } var VLines = ladderLogicModule.LadderVerticalLines.Where(x => { return(x.Y <= i - 1 && x.Y >= i - 1 - emptyLineCnt); }).ToList(); for (int j = 0; j < VLines.Count(); j++) { ladderLogicModule.RemoveVerticalLine(VLines[j].X, VLines[j].Y); } emptyLineCnt++; } else { MoveHorizontalLineEle(ladderLogicModule, emptyLineCnt, tempList); } } line = ladderLogicModule.endY + 1; for (int i = ladderLogicModule.startY; i < line; i++) { var VLines = ladderLogicModule.LadderVerticalLines.Where(x => { return(x.Y == i); }).OrderBy(x => { return(x.X); }).ToList(); MoveLadderBlocks(ladderLogicModule, VLines); } }
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); } } }