//完全特殊处理的 void CalLen(线路绘图工具.GraphicElement Element) { if (Element is Section) { Section sc = Element as Section; double fk = 0, nk = 0;//之前的斜率和当前斜率 foreach (Graphic g in sc.Graphics) { Line l = g as Line; nk = (l.Pt1.Y - l.Pt0.Y) / (l.Pt1.X - l.Pt0.X); if (fk == nk) { sc.Lens[0] += CalDisOf2Point(l.Pt1, l.Pt0); } else { sc.Lens[1] += CalDisOf2Point(l.Pt1, l.Pt0); } fk = nk; } } else if (Element is RailSwitch) { // 直、定、反 RailSwitch rs = Element as RailSwitch; foreach (int i in rs.SectionIndexList[0]) { Line l = rs.Graphics[i] as Line; rs.Lens[0] += CalDisOf2Point(l.Pt0, l.Pt1); } foreach (int i in rs.SectionIndexList[1]) { Line l = rs.Graphics[i] as Line; rs.Lens[1] += CalDisOf2Point(l.Pt0, l.Pt1); } foreach (int i in rs.SectionIndexList[2]) { Line l = rs.Graphics[i] as Line; rs.Lens[2] += CalDisOf2Point(l.Pt0, l.Pt1); } Line l1 = rs.Graphics[rs.SectionIndexList[0].Last()] as Line; Line l2 = rs.Graphics[rs.SectionIndexList[1].First()] as Line; Line l3 = rs.Graphics[rs.SectionIndexList[2].First()] as Line; rs.Lens[1] += CalDisOf2Point(l1.Pt1, l2.Pt0); rs.Lens[2] += CalDisOf2Point(l1.Pt1, l3.Pt0); } }
/// <summary> /// 更新方向向量,当前的的Element /// </summary> /// <param name="item"></param> /// <param name="dir"></param> /// <returns></returns> void UpdatePos(线路绘图工具.GraphicElement item, byte dir, double offset) { Dir = dir; if (item != null) { Point origin = new Point(Canvas.GetLeft(item), Canvas.GetTop(item)); Log4ATS.Info(origin.X + " " + origin.Y + " " + offset); Vector LineDirVector = new Vector(); Canvas.SetZIndex(this, -1); #region 直道 if (item is Section) { Section s = item as Section; if (s.DirVectorList.Count == 0) { s.CreateDirVectors(); } //左走 if (dir == (byte)(TrainDir.左行)) { if (true) //if (preid!=nowid||pretype!=nowtype) { //D 0斜 1直 线路绘图工具.Line l = s.Graphics.Last() as 线路绘图工具.Line; StartPoint = l.Pt1; DirVector[0] = CalDirUnitVector(l.Pt1, l.Pt0); l = s.Graphics.First() as 线路绘图工具.Line; DirVector[1] = CalDirUnitVector(l.Pt1, l.Pt0); k = (s.Lens[0] + s.Lens[1]) / s.Distance; preid = nowid; pretype = nowtype; } offset *= k; //l 0直道长度 1斜线长度 if (offset > s.Lens[1]) { LineDirVector = s.DirVectorList.First(); double len = offset - s.Lens[1]; double x = len * DirVector[1].X + s.Lens[1] * DirVector[0].X + StartPoint.X + origin.X; double y = len * DirVector[1].Y + s.Lens[1] * DirVector[0].Y + StartPoint.Y + origin.Y; UpdateTrainPosByXY(new Point(x, y)); } else { LineDirVector = s.DirVectorList.Last(); double x = offset * DirVector[0].X + StartPoint.X + origin.X; double y = offset * DirVector[0].Y + StartPoint.Y + origin.Y; UpdateTrainPosByXY(new Point(x, y)); } } else//右行 { if (true) //if (preid != nowid || pretype != nowtype) { //D 0斜 1直 线路绘图工具.Line l = s.Graphics.First() as 线路绘图工具.Line; StartPoint = l.Pt0; DirVector[0] = CalDirUnitVector(l.Pt0, l.Pt1); l = s.Graphics.Last() as 线路绘图工具.Line; DirVector[1] = CalDirUnitVector(l.Pt0, l.Pt1); k = (s.Lens[0] + s.Lens[1]) / s.Distance; preid = nowid; pretype = nowtype; } offset *= k; //l 0直道长度 1斜线长度 if (offset > s.Lens[0]) { LineDirVector = s.DirVectorList.Last(); double len = offset - s.Lens[0]; double x = len * DirVector[1].X + s.Lens[0] * DirVector[0].X + StartPoint.X + origin.X; double y = len * DirVector[1].Y + s.Lens[0] * DirVector[0].Y + StartPoint.Y + origin.Y; UpdateTrainPosByXY(new Point(x, y)); } else { LineDirVector = s.DirVectorList.First(); double x = offset * DirVector[0].X + StartPoint.X + origin.X; double y = offset * DirVector[0].Y + StartPoint.Y + origin.Y; UpdateTrainPosByXY(new Point(x, y)); } } } #endregion #region 弯道 else if (item is RailSwitch) { RailSwitch rs = item as RailSwitch; if (rs.DirVectorList.Count == 0) { rs.CreateDirVectors(); } if (rs.Position == RailSwitch.SwitchPosition.PosNormal) { LineDirVector = rs.DirVectorList.First(); if (dir == (byte)(TrainDir.左行)) { //左行驶 //if (preid != nowid || pretype != nowtype) if (true) { //定 左 k = (rs.Lens[0] + rs.Lens[1]) / rs.NormalDistance; if (!rs.IsLeft) { 线路绘图工具.Line l = rs.Graphics.First() as 线路绘图工具.Line; DirVector[0] = CalDirUnitVector(l.Pt1, l.Pt0); StartPoint = (rs.Graphics[rs.SectionIndexList[1].Last()] as 线路绘图工具.Line).Pt1; DirVector[1] = DirVector[0];//因为直线所以简化处理 } else { 线路绘图工具.Line l = rs.Graphics.First() as 线路绘图工具.Line; DirVector[0] = CalDirUnitVector(l.Pt0, l.Pt1); StartPoint = (rs.Graphics[rs.SectionIndexList[0].First()] as 线路绘图工具.Line).Pt0; DirVector[1] = DirVector[0]; } preid = nowid; pretype = nowtype; } offset *= k; double x = DirVector[0].X * offset + StartPoint.X + origin.X; double y = DirVector[0].Y * offset + StartPoint.Y + origin.Y; UpdateTrainPosByXY(new Point(x, y)); } else { //右行驶 if (true) //if (preid != nowid || pretype != nowtype) { //定右 k = (rs.Lens[0] + rs.Lens[1]) / rs.NormalDistance; if (!rs.IsLeft) { StartPoint = (rs.Graphics[rs.SectionIndexList[0].First()] as 线路绘图工具.Line).Pt0; 线路绘图工具.Line l = rs.Graphics.First() as 线路绘图工具.Line; DirVector[0] = CalDirUnitVector(l.Pt0, l.Pt1); DirVector[1] = DirVector[0];//因为直线所以简化处理 } else { StartPoint = (rs.Graphics[rs.SectionIndexList[1].Last()] as 线路绘图工具.Line).Pt1; 线路绘图工具.Line l = rs.Graphics.First() as 线路绘图工具.Line; DirVector[0] = CalDirUnitVector(l.Pt1, l.Pt0); DirVector[1] = DirVector[0];//因为直线所以简化处理 } preid = nowid; pretype = nowtype; } offset *= k; double x = DirVector[0].X * offset + StartPoint.X + origin.X; double y = DirVector[0].Y * offset + StartPoint.Y + origin.Y; UpdateTrainPosByXY(new Point(x, y)); } } else { //反位左行 if (dir == (byte)(TrainDir.左行)) { //if (preid != nowid || pretype != nowtype) if (true) { if (!rs.IsLeft) { 线路绘图工具.Line l = rs.Graphics[rs.SectionIndexList[2].Last()] as 线路绘图工具.Line; DirVector[0] = CalDirUnitVector(l.Pt1, l.Pt0); StartPoint = l.Pt1; l = rs.Graphics[rs.SectionIndexList[0].First()] as 线路绘图工具.Line; DirVector[1] = CalDirUnitVector(l.Pt1, l.Pt0); } else { 线路绘图工具.Line l = rs.Graphics[rs.SectionIndexList[0].First()] as 线路绘图工具.Line; DirVector[0] = CalDirUnitVector(l.Pt0, l.Pt1); StartPoint = l.Pt0; l = rs.Graphics[rs.SectionIndexList[2].Last()] as 线路绘图工具.Line; DirVector[1] = CalDirUnitVector(l.Pt0, l.Pt1); } //直 定 反 k = (rs.Lens[0] + rs.Lens[2]) / rs.ReverseDistance; preid = nowid; pretype = nowtype; } offset *= k; if (!rs.IsLeft) { if (offset > rs.Lens[2]) { LineDirVector = rs.DirVectorList[0]; double len = offset - rs.Lens[2]; double x = DirVector[0].X * rs.Lens[2] + DirVector[1].X * len + StartPoint.X + origin.X; double y = DirVector[0].Y * rs.Lens[2] + DirVector[1].Y * len + StartPoint.Y + origin.Y; UpdateTrainPosByXY(new Point(x, y)); } else { LineDirVector = rs.DirVectorList[2]; double x = DirVector[0].X * offset + StartPoint.X + origin.X; double y = DirVector[0].Y * offset + StartPoint.Y + origin.Y; UpdateTrainPosByXY(new Point(x, y)); } } else { if (offset > rs.Lens[0]) { LineDirVector = rs.DirVectorList[2]; double len = offset - rs.Lens[0]; double x = DirVector[0].X * rs.Lens[0] + DirVector[1].X * len + StartPoint.X + origin.X; double y = DirVector[0].Y * rs.Lens[0] + DirVector[1].Y * len + StartPoint.Y + origin.Y; UpdateTrainPosByXY(new Point(x, y)); } else { LineDirVector = rs.DirVectorList[0]; double x = DirVector[0].X * offset + StartPoint.X + origin.X; double y = DirVector[0].Y * offset + StartPoint.Y + origin.Y; UpdateTrainPosByXY(new Point(x, y)); } } } else { //反位右行 if (true) //if (preid != nowid || pretype != nowtype) { if (!rs.IsLeft) { 线路绘图工具.Line l = rs.Graphics[rs.SectionIndexList[0].First()] as 线路绘图工具.Line; DirVector[0] = CalDirUnitVector(l.Pt0, l.Pt1); StartPoint = l.Pt0; l = rs.Graphics[rs.SectionIndexList[2].Last()] as 线路绘图工具.Line; DirVector[1] = CalDirUnitVector(l.Pt0, l.Pt1); } else { 线路绘图工具.Line l = rs.Graphics[rs.SectionIndexList[2].Last()] as 线路绘图工具.Line; DirVector[0] = CalDirUnitVector(l.Pt1, l.Pt0); StartPoint = l.Pt1; l = rs.Graphics[rs.SectionIndexList[0].Last()] as 线路绘图工具.Line; DirVector[1] = CalDirUnitVector(l.Pt1, l.Pt0); } //直 定 反 k = (rs.Lens[0] + rs.Lens[2]) / rs.ReverseDistance; preid = nowid; pretype = nowtype; } offset *= k; if (!rs.IsLeft) { if (offset > rs.Lens[0]) { LineDirVector = rs.DirVectorList[2]; double len = offset - rs.Lens[0]; double x = DirVector[0].X * rs.Lens[0] + len * DirVector[1].X + StartPoint.X + origin.X; double y = DirVector[0].Y * rs.Lens[0] + len * DirVector[1].Y + StartPoint.Y + origin.Y; UpdateTrainPosByXY(new Point(x, y)); } else { LineDirVector = rs.DirVectorList[0]; double x = DirVector[0].X * offset + StartPoint.X + origin.X; double y = DirVector[0].Y * offset + StartPoint.Y + origin.Y; UpdateTrainPosByXY(new Point(x, y)); } } else { if (offset > rs.Lens[2]) { LineDirVector = rs.DirVectorList[0]; double len = offset - rs.Lens[2]; double x = DirVector[0].X * rs.Lens[2] + len * DirVector[1].X + StartPoint.X + origin.X; double y = DirVector[0].Y * rs.Lens[2] + len * DirVector[1].Y + StartPoint.Y + origin.Y; UpdateTrainPosByXY(new Point(x, y)); } else { LineDirVector = rs.DirVectorList[2]; double x = DirVector[0].X * offset + StartPoint.X + origin.X; double y = DirVector[0].Y * offset + StartPoint.Y + origin.Y; UpdateTrainPosByXY(new Point(x, y)); } } } } } #endregion if (item is RailSwitch) { RailSwitch rs = item as RailSwitch; if (rs.DirVectorList.Count == 0) { rs.CreateDefaultGraphic(); } if (!rs.IsLeft) { } else { LineDirVector.Negate(); } } this.TrainRotate(LineDirVector, dir); } }