using RoboforkApp.DataModel; //using RoboforkApp.Entities; //using RoboforkApp.Services; using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Markup; using System.Windows.Media; using System.Windows.Shapes; using System.Xml; using System.Data; using RoboforkApp.View; using RoboforkApp.Services; using RoboforkApp.Entities; namespace RoboforkApp { public class DesignerCanvas : Canvas { const double RADIUS_NODE = 25d; //8d; const double RADIUS_CURVER_LINE = 130d; const double DISTANCE_AUTO_NODES = 100d; const double DISTANCE_START_NODES = 30d; const double DISTANCE_END_NODES = 30d; const double DISTANCE_FREE_NODES = 40d; const double STROKE_ROOT_LINE = 6d; const double STROKE_LINE = 4d; const double STROKE_NODE = 1d; const double DISTANCE_RATIO = 100; const double UCNODE_SETLEFT = 17; const double UCNODE_SETTOP = 17; const double UCNODE_WIDTH = 34; const double UCNODE_HEIGHT = 34; const double UCSCHEDULENODE_SETLEFT = 12; const double UCSCHEDULENODE_SETTOP = 12; const double UCSCHEDULENODE_WIDTH = 24; const double UCSCHEDULENODE_HEIGHT = 24; private List shapeList = new List(); private TextBlock shapePosIndicator; private TextBlock shapeSizeIndicator; private int currentLine; private ucStartEndButton _startPoint; private ucNode _ucNodeFree; private ucStartEndButton _goalPoint; private ucDisplayCoordinate _displayAxiPosition; // Add variable for draw route public Path pLine = new Path(); public Path pRootLine = new Path(); public Path pCurverLine = new Path(); public Path pRedNode = new Path(); public Path pYellowNode = new Path(); public GeometryGroup gGrpLine = new GeometryGroup(); public GeometryGroup gGrpRootLine = new GeometryGroup(); public GeometryGroup gGrpCurverLine = new GeometryGroup(); public GeometryGroup gGrpRedNode = new GeometryGroup(); public GeometryGroup gGrpYellowNode = new GeometryGroup(); public int currentShape; public static bool isStartDrawRoute = false; public static bool isGoalDrawRoute = false; public static bool readMapFan = true; public static double CanvasScheduleWidth = 0; public static double CanvasScheduleHeight = 0; public const double NodeSchedule_X = 50; public double PointMapStart_X = 127.85783428698775; public double PointMapStart_Y = 26.336094486866543; public double PointMapEnd_X = 128.85783428698775; public double PointMapEnd_Y = 27.336094486866543; const double DISTANCEMAPDISPLAY = 1000; public double Scale_X = 0; public double Scale_Y = 0; public struct NodeInfo { public double X; public double Y; public String Mode1; public String Mode2; public String Mode3; } List NodeInfo_List = new List(); // Add variable for Set Schedule public Path pScheduleNode = new Path(); public Path pScheduleLine = new Path(); public GeometryGroup gGrpScheduleNode = new GeometryGroup(); public GeometryGroup gGrpScheduleLine = new GeometryGroup(); public static bool EditNodeFlag = false; public static bool DragDeltaFlag = false; public static bool LoadDBFlag = false; //Add variable for Vehicle Item click public string VehicleItem = "FK_15"; public int VehicleIndex = 0; public string ColorNode_Add = "Blue"; public string ColorNode_Insert = "Brown"; public int ProjectIndex = 0; public string ProjectItem = "ProjectAAA"; public ProjectModel ProjectModel = new ProjectModel(); //2017/03/04 NAM ADD START public static bool isDrawingNode = false; public static bool isDragNode = false; public Path pNewLine = new Path(); public GeometryGroup gGrpNewLine = new GeometryGroup(); public struct NewNodeInfo { public double X; public double Y; public String Mode; } List NewNodeInfo_List = new List(); //List Node's Info List Lst_Node_tmp = new List(); List NodeNo = new List(); List ucNode_Lst = new List(); public List ucScheduleNode_Lst = new List(); int stt = 1; //2017/03/04 NAM ADD END public struct ListIndexNodeInsert { public double X; public double Y; } List IndexNodeInsert_List = new List(); public struct ListNameNode { public double X; public double Y; public string nameNode; } List NameNode_List = new List(); public ScheduleCanvas scheduleCanvas = new ScheduleCanvas(); // Add variable for Set Auto Nodes public Path pBlueNode = new Path(); private GeometryGroup gGrpBlueNode = new GeometryGroup(); // Add variable for Actual Nodes public Path pActualNode = new Path(); private GeometryGroup gGrpActualNode = new GeometryGroup(); // Add variable for Set Free Nodes public Path pFreeNode = new Path(); //private GeometryGroup gGrpFreeNode = new GeometryGroup(); // The part of the rectangle the mouse is over. private enum HitType { None, Body, UL, UR, LR, LL, L, R, T, B }; public enum OperationState { None, DrawObstract, DrawRoute, DrawSetFreeNode, EditNode, NewDrawSetFreeNode }; public enum MouseState { None, Draw, Drag, } public OperationState Operation = OperationState.None; public MouseState mouseState = MouseState.None; // The draw start point. private Point StartDrawPoint; // The drag's last point. private Point LastPoint; // The part of the rectangle under the mouse. HitType MouseHitType = HitType.None; public void Init() { if (shapePosIndicator == null) { shapePosIndicator = new TextBlock() { Foreground = Brushes.Black, Background = Brushes.Transparent, FontSize = 20, }; } if (shapeSizeIndicator == null) { shapeSizeIndicator = new TextBlock() { Foreground = Brushes.Black, Background = Brushes.Transparent, FontSize = 20, }; } } // Return a HitType value to indicate what is at the point. private HitType SetHitType(Point point) { if (shapeList.Count == 0) { currentShape = 0; return HitType.None; } for (int i = 0; i < shapeList.Count; i++) { Rectangle rect = shapeList[i]; double left = Canvas.GetLeft(rect); double top = Canvas.GetTop(rect); double right = left + rect.Width; double bottom = top + rect.Height; if (point.X < left) continue; if (point.X > right) continue; if (point.Y < top) continue; if (point.Y > bottom) continue; currentShape = i; const double GAP = 10; if (point.X - left < GAP) { // Left edge. if (point.Y - top < GAP) return HitType.UL; if (bottom - point.Y < GAP) return HitType.LL; return HitType.L; } if (right - point.X < GAP) { // Right edge. if (point.Y - top < GAP) return HitType.UR; if (bottom - point.Y < GAP) return HitType.LR; return HitType.R; } if (point.Y - top < GAP) return HitType.T; if (bottom - point.Y < GAP) return HitType.B; return HitType.Body; } currentShape = 0; return HitType.None; } // Set a mouse cursor appropriate for the current hit type. private void SetMouseCursor() { // See what cursor we should display. Cursor desired_cursor = Cursors.Arrow; switch (MouseHitType) { case HitType.None: desired_cursor = Cursors.Arrow; break; case HitType.Body: desired_cursor = Cursors.ScrollAll; break; case HitType.UL: case HitType.LR: desired_cursor = Cursors.SizeNWSE; break; case HitType.LL: case HitType.UR: desired_cursor = Cursors.SizeNESW; break; case HitType.T: case HitType.B: desired_cursor = Cursors.SizeNS; break; case HitType.L: case HitType.R: desired_cursor = Cursors.SizeWE; break; } // Display the desired cursor. if (Cursor != desired_cursor) Cursor = desired_cursor; } // constance int indicatorAligment = 2; /* private Point? dragStartPoint = null; */ public IEnumerable SelectedItems { get { var selectedItems = from item in this.Children.OfType() where item.IsSelected == true select item; return selectedItems; } } public void DeselectAll() { /* foreach (DesignerItem item in this.SelectedItems) { item.IsSelected = false; }*/ } protected override void OnMouseDown(MouseButtonEventArgs e) { base.OnMouseDown(e); MouseHitType = SetHitType(Mouse.GetPosition(this)); SetMouseCursor(); if (Operation == OperationState.DrawRoute && isStartDrawRoute) { if (isGoalDrawRoute) { return; } // Check state draw if (MouseHitType == HitType.None) { if (gGrpLine.Children.Count == 1) { LineGeometry lineGeometry = (LineGeometry)gGrpLine.Children[0]; lineGeometry.EndPoint = LastPoint; // Check end route if (IsEndRoute(_goalPoint, lineGeometry)) { isGoalDrawRoute = true; ProcessEndRoute(); return; } } else if (IsCurverNode((LineGeometry)gGrpLine.Children[currentLine - 1] , (LineGeometry)gGrpLine.Children[currentLine])) { // Set end point to finish draw line LineGeometry lineGeometry = (LineGeometry)gGrpLine.Children[currentLine]; lineGeometry.EndPoint = LastPoint; // Add node to curver postion AddNode(lineGeometry.StartPoint, gGrpRedNode); // Check end route if (IsEndRoute(_goalPoint, lineGeometry)) { isGoalDrawRoute = true; ProcessEndRoute(); return; } } else { // Remove current line gGrpLine.Children.RemoveAt(currentLine); // Set end point to finish draw line LineGeometry lineGeometry = (LineGeometry)gGrpLine.Children[currentLine - 1]; lineGeometry.EndPoint = LastPoint; // Check end route if (IsEndRoute(_goalPoint, lineGeometry)) { isGoalDrawRoute = true; ProcessEndRoute(); return; } } // Draw new line DrawLine(LastPoint, LastPoint, gGrpLine); // Setting start point for new line StartDrawPoint = LastPoint; mouseState = MouseState.Draw; currentLine = gGrpLine.Children.Count - 1; return; } } else if (Operation == OperationState.DrawSetFreeNode) { bool RightClick = false; if (IsStopDrawRoute(e)) RightClick = true; StartDrawPoint = e.MouseDevice.GetPosition(this); SetFreeNodes(StartDrawPoint, RightClick); } else if (Operation == OperationState.EditNode) { Point node_edited = e.MouseDevice.GetPosition(this); // start Edit Node Infor //EditNode(node_edited); } else if (Operation == OperationState.DrawObstract) { if (MouseHitType == HitType.None) { Rectangle shape = new Rectangle(); shape.Width = 1; shape.Height = 1; // Create a SolidColorBrush and use it to // paint the rectangle. shape.Stroke = Brushes.Blue; shape.StrokeThickness = 1; shape.Fill = new SolidColorBrush(Colors.LightCyan); StartDrawPoint = e.MouseDevice.GetPosition(this); shape.SetValue(Canvas.LeftProperty, StartDrawPoint.X); shape.SetValue(Canvas.TopProperty, StartDrawPoint.Y); this.Children.Add(shape); shapeList.Add(shape); mouseState = MouseState.Draw; currentShape = shapeList.Count() - 1; double shapeX = Canvas.GetLeft(shapeList[currentShape]); double shapeY = Canvas.GetTop(shapeList[currentShape]); shapePosIndicator.Text = "(" + Math.Round(shapeX, 0) + "," + Math.Round(shapeY, 0) + ")"; shapePosIndicator.SetValue(Canvas.LeftProperty, shapeX + indicatorAligment); shapePosIndicator.SetValue(Canvas.TopProperty, shapeY + indicatorAligment); double width = (int)shapeList[currentShape].Width; double height = (int)shapeList[currentShape].Height; shapeSizeIndicator.Text = "(" + Math.Round(width, 0) + "," + Math.Round(height, 0) + ")"; shapeSizeIndicator.SetValue(Canvas.LeftProperty, shapeX + indicatorAligment + width); shapeSizeIndicator.SetValue(Canvas.TopProperty, shapeY + indicatorAligment + height); this.Children.Add(shapePosIndicator); this.Children.Add(shapeSizeIndicator); return; } else { if (shapeList.Count() != 0) shapeList[currentShape].Fill = new SolidColorBrush(Colors.LightCyan); double shapeX = Canvas.GetLeft(shapeList[currentShape]); double shapeY = Canvas.GetTop(shapeList[currentShape]); shapePosIndicator.Text = "(" + Math.Round(shapeX, 0) + "," + Math.Round(shapeY, 0) + ")"; shapePosIndicator.SetValue(Canvas.LeftProperty, shapeX + indicatorAligment); shapePosIndicator.SetValue(Canvas.TopProperty, shapeY + indicatorAligment); double width = (int)shapeList[currentShape].Width; double height = (int)shapeList[currentShape].Height; shapeSizeIndicator.Text = "(" + Math.Round(width, 0) + "," + Math.Round(height, 0) + ")"; shapeSizeIndicator.SetValue(Canvas.LeftProperty, shapeX + indicatorAligment + width); shapeSizeIndicator.SetValue(Canvas.TopProperty, shapeY + indicatorAligment + height); this.Children.Add(shapePosIndicator); this.Children.Add(shapeSizeIndicator); } LastPoint = Mouse.GetPosition(this); mouseState = MouseState.Drag; } e.Handled = true; } protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (mouseState == MouseState.None) { MouseHitType = SetHitType(Mouse.GetPosition(this)); SetMouseCursor(); } else if (Operation == OperationState.DrawRoute && isStartDrawRoute) { LineGeometry lineGeometry = (LineGeometry)gGrpLine.Children[currentLine]; // See how much the mouse has moved. Point point = Mouse.GetPosition(this); double offset_x = point.X - StartDrawPoint.X; double offset_y = point.Y - StartDrawPoint.Y; // Get the line's current position. double new_x = lineGeometry.StartPoint.X; double new_y = lineGeometry.StartPoint.Y; if (offset_x != 0 || offset_y != 0) { if (Math.Abs(offset_x) >= Math.Abs(offset_y)) { new_x = point.X; } else { new_y = point.Y; } } // Set end point of current line LastPoint = new Point(new_x, new_y); lineGeometry.EndPoint = LastPoint; DisplayCoordinate(LastPoint); } else if (Operation == OperationState.DrawObstract) { if (mouseState == MouseState.Drag) { // See how much the mouse has moved. Point point = Mouse.GetPosition(this); double offset_x = point.X - LastPoint.X; double offset_y = point.Y - LastPoint.Y; // Get the rectangle's current position. double new_x = Canvas.GetLeft(shapeList[currentShape]); double new_y = Canvas.GetTop(shapeList[currentShape]); double new_width = shapeList[currentShape].Width; double new_height = shapeList[currentShape].Height; // Update the rectangle. switch (MouseHitType) { case HitType.Body: new_x += offset_x; new_y += offset_y; break; case HitType.UL: new_x += offset_x; new_y += offset_y; new_width -= offset_x; new_height -= offset_y; break; case HitType.UR: new_y += offset_y; new_width += offset_x; new_height -= offset_y; break; case HitType.LR: new_width += offset_x; new_height += offset_y; break; case HitType.LL: new_x += offset_x; new_width -= offset_x; new_height += offset_y; break; case HitType.L: new_x += offset_x; new_width -= offset_x; break; case HitType.R: new_width += offset_x; break; case HitType.B: new_height += offset_y; break; case HitType.T: new_y += offset_y; new_height -= offset_y; break; } // Don't use negative width or height. if ((new_width > 0) && (new_height > 0)) { // Update the rectangle. Canvas.SetLeft(shapeList[currentShape], new_x); Canvas.SetTop(shapeList[currentShape], new_y); shapeList[currentShape].Width = new_width; shapeList[currentShape].Height = new_height; // Save the mouse's new location. LastPoint = point; } } else if (mouseState == MouseState.Draw) { // See how much the mouse has moved. Point point = Mouse.GetPosition(this); double offset_x = point.X - StartDrawPoint.X; double offset_y = point.Y - StartDrawPoint.Y; // Get the rectangle's current position. double start_x = Canvas.GetLeft(shapeList[currentShape]); double start_y = Canvas.GetTop(shapeList[currentShape]); double new_x = Canvas.GetLeft(shapeList[currentShape]); double new_y = Canvas.GetTop(shapeList[currentShape]); double new_width = offset_x; double new_height = offset_y; if (offset_x < 0) { new_x = point.X; new_width = -offset_x; } if (offset_y < 0) { new_y = point.Y; new_height = -offset_y; } Canvas.SetLeft(shapeList[currentShape], new_x); Canvas.SetTop(shapeList[currentShape], new_y); shapeList[currentShape].Width = new_width; shapeList[currentShape].Height = new_height; } double shapeX = Canvas.GetLeft(shapeList[currentShape]); double shapeY = Canvas.GetTop(shapeList[currentShape]); shapePosIndicator.Text = "(" + Math.Round(shapeX, 0) + "," + Math.Round(shapeY, 0) + ")"; shapePosIndicator.SetValue(Canvas.LeftProperty, shapeX + indicatorAligment); shapePosIndicator.SetValue(Canvas.TopProperty, shapeY + indicatorAligment); double width = (int)shapeList[currentShape].Width; double height = (int)shapeList[currentShape].Height; shapeSizeIndicator.Text = "(" + Math.Round(width, 0) + "," + Math.Round(height, 0) + ")"; shapeSizeIndicator.SetValue(Canvas.LeftProperty, shapeX + indicatorAligment + width); shapeSizeIndicator.SetValue(Canvas.TopProperty, shapeY + indicatorAligment + height); } e.Handled = true; } protected override void OnMouseUp(MouseButtonEventArgs e) { base.OnMouseUp(e); if (Operation == OperationState.DrawObstract) { if (shapeList.Count() != 0) shapeList[currentShape].Fill = new SolidColorBrush(Colors.Blue); shapePosIndicator.Text = ""; shapeSizeIndicator.Text = ""; this.Children.Remove(shapePosIndicator); this.Children.Remove(shapeSizeIndicator); mouseState = MouseState.None; currentShape = 0; } e.Handled = true; } protected override void OnPreviewMouseUp(MouseButtonEventArgs e) { base.OnPreviewMouseUp(e); EllipseGeometry ellipseGeometry; bool flag = false; StartDrawPoint = e.MouseDevice.GetPosition(this); if (EditNodeFlag == true) { execEditNode(StartDrawPoint); EditNodeFlag = false; return; } if (DragDeltaFlag == true) { for (int i = 0; i < gGrpBlueNode.Children.Count; i++) { for (int j = 0; j < ucNode_Lst.Count; j++) { if (j == i) { _ucNodeFree = ucNode_Lst[j]; flag = true; } } if (flag) { if (gGrpNewLine.Children.Count > 0) { for (int k = 0; k < gGrpNewLine.Children.Count; k++) { LineGeometry lineGeometry = (LineGeometry)gGrpNewLine.Children[k]; Point p1 = lineGeometry.StartPoint; Point p2 = lineGeometry.EndPoint; //bool pInL = PointInLine(StartDrawPoint, p1, p2, 25); //if (pInL) //{ // //this.Children.Remove(_ucNodeFree); // //this.Children.Add(_ucNodeFree); // return; //} } } ellipseGeometry = (EllipseGeometry)gGrpBlueNode.Children[i]; ellipseGeometry.Center = new Point(StartDrawPoint.X, StartDrawPoint.Y); double centerY = Canvas.GetTop(_ucNodeFree); double centerX = Canvas.GetLeft(_ucNodeFree); mouseState = MouseState.Draw; StartDrawPoint = new Point(centerX + UCNODE_SETLEFT, centerY + UCNODE_SETTOP); ellipseGeometry.Center = new Point(centerX + UCNODE_SETLEFT, centerY + UCNODE_SETTOP); this.Children.Remove(_ucNodeFree); this.Children.Add(_ucNodeFree); Lst_Node_tmp[i].pointMap_X = ConvertPointDisplayToActual(StartDrawPoint.X, Scale_X, PointMapStart_X); Lst_Node_tmp[i].pointMap_Y = ConvertPointDisplayToActual(StartDrawPoint.Y, Scale_X, PointMapStart_Y); //Update Location for List NameNode_List -> Compare Location to get Name ListNameNode nmNode = new ListNameNode(); ListNameNode nmNode_old = new ListNameNode(); nmNode_old = NameNode_List[i]; nmNode.X = StartDrawPoint.X; nmNode.Y = StartDrawPoint.Y; nmNode.nameNode = nmNode_old.nameNode; NameNode_List[i] = nmNode; } } ReDrawAllNode(); NewDspRouteInfo(); SetScheduleRoute(); DragDeltaFlag = false; } return; } /// /// On Preview Mouse Down /// /// protected override void OnPreviewMouseDown(MouseButtonEventArgs e) { base.OnPreviewMouseDown(e); if (DesignerCanvas.LoadDBFlag) return; if (Operation != OperationState.NewDrawSetFreeNode) { return; } Point currentPoint = e.MouseDevice.GetPosition(this); if (isDrawingNode == false) { isDrawingNode = true; //InitDrawRoute(); mouseState = MouseState.Draw; StartDrawPoint = e.MouseDevice.GetPosition(this); execCreateNode(StartDrawPoint); //backup DB CreateVehicleNode(); return; } else { bool RightClick = false; bool LeftClick = false; if (IsMouseLeftClick(e)) LeftClick = true; if (IsStopDrawRoute(e)) RightClick = true; StartDrawPoint = e.MouseDevice.GetPosition(this); if (RightClick) { execDeleteNode(StartDrawPoint); //backup DB CreateVehicleNode(); } else if (LeftClick) { //check click to exist node for (int i = 0; i < gGrpBlueNode.Children.Count; i++) { EllipseGeometry ellipseGeometry = (EllipseGeometry)gGrpBlueNode.Children[i]; Point node = ellipseGeometry.Center; bool isEditNode = CheckIsNode(StartDrawPoint, node, RADIUS_NODE); if (isEditNode) { return; } } execCreateNode(StartDrawPoint); //backup DB CreateVehicleNode(); } } } #region Functions for draw route /// /// Check start or end draw route /// /// Button start /// Position for check /// true: start, false: not start private bool IsStartEndRoute(ucStartEndButton _ucStartEndButton, Point currentPoint) { if (_ucStartEndButton == null) { return false; } double centerX = Canvas.GetLeft(_ucStartEndButton); double centerY = Canvas.GetTop(_ucStartEndButton); if (currentPoint.X < centerX + 50 && currentPoint.X > centerX && currentPoint.Y < centerY + 50 && currentPoint.Y > centerY) { return true; } return false; } /// /// Process when end draw route /// private void ProcessEndRoute() { Operation = OperationState.None; AutoEditLine(); this.Children.Remove(_displayAxiPosition); this.Children.Remove(_goalPoint); this.Children.Add(_goalPoint); } /// /// Check end draw route /// /// Button end /// Position for check /// true: end, false: not end private bool IsEndRoute(ucStartEndButton _ucStartEndButton, LineGeometry lineGeometry) { if (_ucStartEndButton == null) { return false; } double centerX = Canvas.GetLeft(_ucStartEndButton); double centerY = Canvas.GetTop(_ucStartEndButton); Point startPoint = lineGeometry.StartPoint; Point endPoint = lineGeometry.EndPoint; if (IsVerticalLine(lineGeometry)) { if (endPoint.X < centerX || endPoint.X > centerX + 50) return false; if (startPoint.Y > centerY + 50 && endPoint.Y > centerY + 50) return false; if (startPoint.Y < centerY && endPoint.Y < centerY) return false; } else { if (endPoint.Y < centerY || endPoint.Y > centerY + 50) return false; if (startPoint.X > centerX + 50 && endPoint.X > centerX + 50) return false; if (startPoint.X < centerX && endPoint.X < centerX) return false; } return true; } /// /// Make root /// public void MakeRoot() { // If still not route if (gGrpLine.Children.Count == 0) return; pLine.Stroke = new SolidColorBrush(Colors.Red); pLine.StrokeThickness = STROKE_ROOT_LINE; } /// /// Auto edit leght of line /// private void AutoEditLine() { double temp; int index = gGrpLine.Children.Count - 1; double centerY = Canvas.GetTop(_goalPoint) + 25; double centerX = Canvas.GetLeft(_goalPoint) + 25; LineGeometry lastLine = (LineGeometry)gGrpLine.Children[index]; LineGeometry beforeLastLine = (LineGeometry)gGrpLine.Children[index]; if (gGrpLine.Children.Count > 1) { beforeLastLine = (LineGeometry)gGrpLine.Children[index - 1]; if (!IsCurverNode(beforeLastLine, lastLine)) { beforeLastLine.EndPoint = lastLine.EndPoint; gGrpLine.Children.RemoveAt(index); index = index - 1; lastLine = (LineGeometry)gGrpLine.Children[index]; } } if (index == gGrpRedNode.Children.Count + 1) { AddNode(lastLine.StartPoint, gGrpRedNode); } if (lastLine.EndPoint.X == centerX && lastLine.EndPoint.Y == centerY) return; if (IsVerticalLine(lastLine)) { temp = lastLine.StartPoint.Y; lastLine.StartPoint = new Point(centerX, temp); lastLine.EndPoint = new Point(centerX, centerY); if (gGrpLine.Children.Count > 1) { beforeLastLine = (LineGeometry)gGrpLine.Children[index - 1]; temp = beforeLastLine.EndPoint.Y; beforeLastLine.EndPoint = new Point(centerX, temp); } } else { temp = lastLine.StartPoint.X; lastLine.StartPoint = new Point(temp, centerY); lastLine.EndPoint = new Point(centerX, centerY); if (gGrpLine.Children.Count > 1) { beforeLastLine = (LineGeometry)gGrpLine.Children[index - 1]; temp = beforeLastLine.EndPoint.X; beforeLastLine.EndPoint = new Point(temp, centerY); } } // Draw curver line if (IsCurverNode(beforeLastLine, lastLine)) { EllipseGeometry ellipseGeometry = (EllipseGeometry)gGrpRedNode.Children[gGrpRedNode.Children.Count - 1]; ellipseGeometry.Center = lastLine.StartPoint; } } /// /// Check draw curver node /// /// Old line /// New line /// true:is curver, fasle: not curver private bool IsCurverNode(LineGeometry oldLine, LineGeometry newLine) { if (oldLine.StartPoint.Y == oldLine.EndPoint.Y && oldLine.StartPoint.Y == newLine.EndPoint.Y) { return false; } if (oldLine.StartPoint.X == oldLine.EndPoint.X && oldLine.StartPoint.X == newLine.EndPoint.X) { return false; } return true; } /// /// Check timming to stop draw route /// /// /// true:stop; false:continue private bool IsStopDrawRoute(MouseEventArgs e) { if (e.RightButton == MouseButtonState.Pressed) { return true; } return false; } //2017/03/05 NAM ADD START private bool IsMouseLeftClick(MouseEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { return true; } return false; } //2017/03/05 NAM ADD END /// /// Draw curver line and yellow node /// /// Old line /// New line private void DrawCurver(LineGeometry oldLine, LineGeometry newLine) { double radius = RADIUS_CURVER_LINE; Point startPoint; Point endPoint; // Get postion of yellow node on old line if (IsVerticalLine(oldLine)) { if (oldLine.StartPoint.Y > oldLine.EndPoint.Y) startPoint = new Point(oldLine.EndPoint.X, oldLine.EndPoint.Y + radius); else startPoint = new Point(oldLine.EndPoint.X, oldLine.EndPoint.Y - radius); } else { if (oldLine.StartPoint.X > oldLine.EndPoint.X) startPoint = new Point(oldLine.EndPoint.X + radius, oldLine.EndPoint.Y); else startPoint = new Point(oldLine.EndPoint.X - radius, oldLine.EndPoint.Y); } // Get postion of yellow node on new line if (IsVerticalLine(newLine)) { if (newLine.StartPoint.Y > newLine.EndPoint.Y) endPoint = new Point(newLine.StartPoint.X, newLine.StartPoint.Y - radius); else endPoint = new Point(newLine.StartPoint.X, newLine.StartPoint.Y + radius); } else { if (newLine.StartPoint.X > newLine.EndPoint.X) endPoint = new Point(newLine.StartPoint.X - radius, newLine.StartPoint.Y); else endPoint = new Point(newLine.StartPoint.X + radius, newLine.StartPoint.Y); } // Add node to postion distance 1300mm if (GetDistance(oldLine.StartPoint, oldLine.EndPoint) > RADIUS_CURVER_LINE) { AddNode(startPoint, gGrpYellowNode); } if (GetDistance(newLine.StartPoint, newLine.EndPoint) > RADIUS_CURVER_LINE) { AddNode(endPoint, gGrpYellowNode); } } /// /// Init data for draw route /// public void InitDrawRoute() { //2017/03/04 NAM ADD START pNewLine.Stroke = new SolidColorBrush(Colors.Red); pNewLine.StrokeThickness = STROKE_LINE; pNewLine.Data = gGrpNewLine; //2017/03/04 NAM ADD END pScheduleLine.Stroke = new SolidColorBrush(Colors.Red); pScheduleLine.StrokeThickness = STROKE_LINE; pScheduleLine.Data = gGrpScheduleLine; // Setting for path line pLine.Stroke = new SolidColorBrush(Colors.Blue); pLine.StrokeThickness = STROKE_LINE; pLine.Data = gGrpLine; // Setting for path of curver line pCurverLine.Stroke = Brushes.Red; pCurverLine.StrokeThickness = STROKE_LINE; pCurverLine.Data = gGrpCurverLine; // Setting for path of red node pRedNode.Stroke = new SolidColorBrush(Colors.Blue); pRedNode.Fill = new SolidColorBrush(Colors.Red); pRedNode.StrokeThickness = STROKE_NODE; pRedNode.Data = gGrpRedNode; // Setting for path of yellow node pYellowNode.Stroke = new SolidColorBrush(Colors.Blue); pYellowNode.Fill = new SolidColorBrush(Colors.Yellow); pYellowNode.StrokeThickness = STROKE_NODE; pYellowNode.Data = gGrpYellowNode; // Setting for path of Blue node pBlueNode.Stroke = new SolidColorBrush(Colors.Blue); pBlueNode.Fill = new SolidColorBrush(Colors.LightBlue); pBlueNode.StrokeThickness = STROKE_NODE; pBlueNode.Data = gGrpBlueNode; // Add paths to canvas this.Children.Add(pLine); this.Children.Add(pCurverLine); this.Children.Add(pRedNode); this.Children.Add(pYellowNode); //this.Children.Add(pNewLine); //scheduleCanvas.Children.Add(pScheduleLine); } /// /// Clear all route /// public void ClearRoute() { isStartDrawRoute = false; isGoalDrawRoute = false; gGrpLine.Children.Clear(); gGrpRootLine.Children.Clear(); gGrpCurverLine.Children.Clear(); gGrpRedNode.Children.Clear(); gGrpYellowNode.Children.Clear(); gGrpBlueNode.Children.Clear(); this.Children.Remove(pLine); this.Children.Remove(pRootLine); this.Children.Remove(pCurverLine); this.Children.Remove(pRedNode); this.Children.Remove(pYellowNode); this.Children.Remove(pBlueNode); } /// /// Draw line for route /// /// Start point /// End point /// Geometry Group private void DrawLine(Point startPoint, Point endPoint, GeometryGroup geometryGroup) { LineGeometry lineGeometry = new LineGeometry(); lineGeometry.StartPoint = startPoint; lineGeometry.EndPoint = endPoint; geometryGroup.Children.Add(lineGeometry); } /// /// Draw curver line /// /// Point start curver line /// Point end curver line /// Radius /// Geometry Group private void DrawCurverLine(Point startPoint, Point endPoint, SweepDirection sweepDirection, GeometryGroup geometryGroup) { PathGeometry pathGeometry = new PathGeometry(); PathFigure figure = new PathFigure(); figure.StartPoint = startPoint; figure.Segments.Add(new ArcSegment(endPoint, new Size(RADIUS_CURVER_LINE, RADIUS_CURVER_LINE), 90, false, sweepDirection, true)); pathGeometry.Figures.Add(figure); geometryGroup.Children.Add(pathGeometry); } /// /// Setting node /// /// Position of center node /// Geometry Group private void AddNode(Point centerPoit, GeometryGroup geometryGroup) { double radius = RADIUS_NODE; geometryGroup.Children.Add(new EllipseGeometry(centerPoit, radius, radius)); } /// /// Check line is vertical or horizontal /// /// /// true:Vertical, false:Horizontal private bool IsVerticalLine(LineGeometry line) { if (line.StartPoint.X == line.EndPoint.X) { // Vertical line return true; } // Horizontal line return false; } /// /// Get distance between two point /// /// Point 1 /// Point 2 /// Distance between two point private double GetDistance(Point point1, Point point2) { //pythagorean theorem c^2 = a^2 + b^2 //thus c = square root(a^2 + b^2) double a = (double)(point2.X - point1.X); double b = (double)(point2.Y - point1.Y); return Math.Sqrt(a * a + b * b); } /// /// Check point is valid for draw /// /// Poit need check /// true:Valid, false:Invalid private bool IsValidPoint(Point point) { return true; } /// /// Display coordinate position /// /// Position to display private void DisplayCoordinate(Point point) { if (_displayAxiPosition == null) { _displayAxiPosition = new ucDisplayCoordinate(); this.Children.Add(_displayAxiPosition); } _displayAxiPosition.Display(point); } #endregion #region Functions for Set Auto Nodes /// /// SetAutoNodes /// public void SetAutoNodes() { double radiusStart = DISTANCE_START_NODES; double radiusEnd = DISTANCE_END_NODES; double radiusCurver = RADIUS_CURVER_LINE; Point startNode; Point endNode; gGrpBlueNode.Children.Clear(); if (gGrpLine.Children.Count == 1) radiusCurver = radiusEnd; if (gGrpLine.Children.Count > 0) { for (int i = 0; i < gGrpLine.Children.Count; i++) { LineGeometry lineGeometry = (LineGeometry)gGrpLine.Children[i]; if (i == 0) { startNode = lineGeometry.EndPoint; endNode = lineGeometry.StartPoint; DrawAutoNodes(startNode, endNode, radiusCurver, radiusStart); } else if (i == gGrpLine.Children.Count - 1 && i > 0) { startNode = lineGeometry.StartPoint; endNode = lineGeometry.EndPoint; DrawAutoNodes(startNode, endNode, radiusCurver, radiusEnd); } else { startNode = lineGeometry.StartPoint; endNode = lineGeometry.EndPoint; DrawAutoNodes(startNode, endNode, radiusCurver, radiusCurver); } } } } /// /// DrawAutoNodes /// /// /// /// /// private void DrawAutoNodes(Point startNode, Point endNode, double radiusStart, double radiusEnd) { double distance = DISTANCE_AUTO_NODES; double i; Point node; // Get postion of blue node on line if (startNode.X == endNode.X) { if (startNode.Y > endNode.Y) { i = startNode.Y - radiusStart; if (i - distance > endNode.Y + radiusEnd) { do { i = i - distance; node = new Point(endNode.X, i); // Add node to postion distance 1000mm AddNode(node, gGrpBlueNode); } while (i > endNode.Y + radiusEnd + distance); } } else { i = startNode.Y + radiusStart; if (i + distance < endNode.Y - radiusEnd) { do { i = i + distance; node = new Point(endNode.X, i); // Add node to postion distance 1000mm AddNode(node, gGrpBlueNode); } while (i < endNode.Y - radiusEnd - distance); } } } else { if (startNode.X > endNode.X) { i = startNode.X - radiusStart; if (i - distance > endNode.X + radiusEnd) { do { i = i - distance; node = new Point(i, endNode.Y); // Add node to postion distance 1000mm AddNode(node, gGrpBlueNode); } while (i > endNode.X + radiusEnd + distance); } } else { i = startNode.X + radiusStart; if (i + distance < endNode.X - radiusEnd) { do { i = i + distance; node = new Point(i, endNode.Y); // Add node to postion distance 1000mm AddNode(node, gGrpBlueNode); } while (i < endNode.X - radiusEnd - distance); } } } } #endregion #region Functions for Set Free Nodes /// /// Draw Auto Blue node /// public void SetFreeNodes(Point FreeNode, bool RightClick) { double radiusStart = DISTANCE_START_NODES; double radiusEnd = DISTANCE_END_NODES; double radiusNode = RADIUS_NODE; double radiusCurver = RADIUS_CURVER_LINE + RADIUS_NODE; EllipseGeometry ellipseGeometry; Point node; bool deleteFlag = false; if (RightClick) { if (gGrpBlueNode.Children.Count > 0) { for (int i = 0; i < gGrpBlueNode.Children.Count; i++) { ellipseGeometry = (EllipseGeometry)gGrpBlueNode.Children[i]; node = ellipseGeometry.Center; if (FreeNode.X == node.X) { if (CheckIsNode(FreeNode, node, radiusNode)) { deleteFlag = true; } } else { if (CheckIsNode(FreeNode, node, radiusNode)) { deleteFlag = true; } } if (deleteFlag) { MessageBoxResult result = MessageBox.Show("Do You Delete This Node?", "Delete Node", MessageBoxButton.OKCancel); if (result == MessageBoxResult.OK) { gGrpBlueNode.Children.RemoveAt(i); this.Children.Remove(NodeNo[i]); return; } else { return; } } } } } else { for (int i = 0; i < gGrpBlueNode.Children.Count; i++) { ellipseGeometry = (EllipseGeometry)gGrpBlueNode.Children[i]; node = ellipseGeometry.Center; bool isEditNode = CheckIsNode(FreeNode, node, RADIUS_NODE); if (isEditNode) { //EditNode(node); return; } } MessageBoxResult result = MessageBox.Show("Do You Want To Add This Node?", "Add Node", MessageBoxButton.OKCancel); if (result == MessageBoxResult.OK) { AddNode(FreeNode, gGrpBlueNode); TextBlock textBlock = new TextBlock(); textBlock.Text = stt.ToString(); textBlock.FontSize = 17; textBlock.Foreground = new SolidColorBrush(Colors.Red); Canvas.SetLeft(textBlock, FreeNode.X - RADIUS_NODE / 2); Canvas.SetTop(textBlock, FreeNode.Y - 1.5 * RADIUS_NODE); this.Children.Add(textBlock); NodeNo.Add(textBlock); if (stt > 1) { int tmp = gGrpBlueNode.Children.Count; EllipseGeometry elip = (EllipseGeometry)gGrpBlueNode.Children[tmp - 2]; Point node1 = elip.Center; elip = (EllipseGeometry)gGrpBlueNode.Children[tmp - 1]; Point node2 = elip.Center; DrawLine(node1, node2, gGrpCurverLine); } stt++; } } if (gGrpLine.Children.Count > 0) { for (int i = 0; i < gGrpLine.Children.Count; i++) { LineGeometry lineGeometry = (LineGeometry)gGrpLine.Children[i]; // Get postion of node on line if (IsVerticalLine(lineGeometry)) { if (FreeNode.X < lineGeometry.EndPoint.X + radiusNode && FreeNode.X > lineGeometry.EndPoint.X - radiusNode) { FreeNode.X = lineGeometry.EndPoint.X; if (i == 0) { if (CheckFreeNodes(lineGeometry, FreeNode, radiusStart, radiusCurver)) { AddNode(FreeNode, gGrpBlueNode); } } else if (i == gGrpLine.Children.Count - 1 && i > 0) { if (CheckFreeNodes(lineGeometry, FreeNode, radiusCurver, radiusEnd)) { AddNode(FreeNode, gGrpBlueNode); } } else { if (CheckFreeNodes(lineGeometry, FreeNode, radiusCurver, radiusCurver)) { AddNode(FreeNode, gGrpBlueNode); } } } } else { if (FreeNode.Y < lineGeometry.EndPoint.Y + radiusNode && FreeNode.Y > lineGeometry.EndPoint.Y - radiusNode) { FreeNode.Y = lineGeometry.EndPoint.Y; if (i == 0) { if (CheckFreeNodes(lineGeometry, FreeNode, radiusStart, radiusCurver)) { AddNode(FreeNode, gGrpBlueNode); } } else if (i == gGrpLine.Children.Count - 1 && i > 0) { if (CheckFreeNodes(lineGeometry, FreeNode, radiusCurver, radiusEnd)) { AddNode(FreeNode, gGrpBlueNode); } } else { if (CheckFreeNodes(lineGeometry, FreeNode, radiusCurver, radiusCurver)) { AddNode(FreeNode, gGrpBlueNode); } } } } } } } public bool CheckFreeNodes(LineGeometry lineNode, Point Freenode, double radiusStart, double radiusEnd) { double distanceFreeNode = DISTANCE_FREE_NODES; double radiusNode = RADIUS_NODE; EllipseGeometry ellipseGeometry; Point node; if (IsVerticalLine(lineNode)) { if (lineNode.StartPoint.Y < lineNode.EndPoint.Y) { if (Freenode.Y < lineNode.StartPoint.Y + radiusStart || Freenode.Y > lineNode.EndPoint.Y - radiusEnd) { return false; } } else { if (Freenode.Y > lineNode.StartPoint.Y - radiusStart || Freenode.Y < lineNode.EndPoint.Y + radiusEnd) { return false; } } } else { if (lineNode.StartPoint.X < lineNode.EndPoint.X) { if (Freenode.X < lineNode.StartPoint.X + radiusStart || Freenode.X > lineNode.EndPoint.X - radiusEnd) { return false; } } else { if (Freenode.X > lineNode.StartPoint.X - radiusStart || Freenode.X < lineNode.EndPoint.X + radiusEnd) { return false; } } } if (gGrpBlueNode.Children.Count > 0) { for (int i = 0; i < gGrpBlueNode.Children.Count; i++) { ellipseGeometry = (EllipseGeometry)gGrpBlueNode.Children[i]; node = ellipseGeometry.Center; if (Freenode.X == node.X) { if (CheckIsNode(Freenode, node, radiusNode)) { return false; } else if (Freenode.Y < node.Y + distanceFreeNode && Freenode.Y > node.Y - distanceFreeNode) { return false; } } else if (Freenode.Y == node.Y) { if (CheckIsNode(Freenode, node, radiusNode)) { return false; } else if (Freenode.X < node.X + distanceFreeNode && Freenode.X > node.X - distanceFreeNode) { return false; } } } } return true; } public bool CheckIsNode(Point FreeNode, Point Node, double radiusNode) { if (FreeNode.X < Node.X + radiusNode && FreeNode.X > Node.X - radiusNode && FreeNode.Y < Node.Y + radiusNode && FreeNode.Y > Node.Y - radiusNode) { return true; } return false; } #endregion #region Edit Node public void InitNodeInfo_List() { //Reset List NodeInfo_List = new List(); if (gGrpBlueNode.Children.Count > 0) { // Get start point LineGeometry lineGeometry = (LineGeometry)gGrpNewLine.Children[0]; Point startPoint = lineGeometry.StartPoint; NodeInfo n1 = new NodeInfo(); n1.X = startPoint.X; n1.Y = startPoint.Y; n1.Mode1 = ""; n1.Mode2 = ""; n1.Mode3 = ""; NodeInfo_List.Add(n1); for (int i = 0; i < gGrpBlueNode.Children.Count; i++) { Point point; EllipseGeometry eGeometry = (EllipseGeometry)gGrpBlueNode.Children[i]; point = eGeometry.Center; NodeInfo Ninfo = new NodeInfo(); Ninfo.X = point.X; Ninfo.Y = point.Y; Ninfo.Mode1 = ""; Ninfo.Mode2 = ""; Ninfo.Mode3 = ""; NodeInfo_List.Add(Ninfo); } // Get end point lineGeometry = (LineGeometry)gGrpLine.Children[gGrpLine.Children.Count - 1]; Point endPoint = lineGeometry.EndPoint; NodeInfo n2 = new NodeInfo(); n2.X = startPoint.X; n2.Y = startPoint.Y; n2.Mode1 = ""; n2.Mode2 = ""; n2.Mode3 = ""; NodeInfo_List.Add(n2); //Resort NodeInfo_List From Start to Goal LineGeometry firstLine = (LineGeometry)gGrpLine.Children[0]; Point startNode = firstLine.StartPoint; Point endNode = firstLine.EndPoint; List tmpLst = new List(); // Create temp List if (startNode.X == endNode.X) { for (int i = 1; i < NodeInfo_List.Count; i++) { if (NodeInfo_List[i].X != endNode.X) { break; } else { tmpLst.Add(NodeInfo_List[i]); } } } if (startNode.Y == endNode.Y) { for (int i = 1; i < NodeInfo_List.Count; i++) { if (NodeInfo_List[i].Y != endNode.Y) { break; } else { tmpLst.Add(NodeInfo_List[i]); } } } // Sort NodeInfo_List for (int i = 0; i < tmpLst.Count; i++) { NodeInfo_List[i + 1] = tmpLst[tmpLst.Count - 1 - i]; } } } //public void EditNode(Point node_edited) //{ // EllipseGeometry ellipseGeometry; // Point node; // double radiusNode = RADIUS_NODE; // bool flag = false; // if (gGrpBlueNode.Children.Count > 0) // { // for (int i = 0; i < gGrpBlueNode.Children.Count; i++) // { // ellipseGeometry = (EllipseGeometry)gGrpBlueNode.Children[i]; // node = ellipseGeometry.Center; // if (CheckIsNode(node_edited, node, radiusNode)) // { // flag = true; // } // if (flag) // { // node_edited.X = node.X; // node_edited.Y = node.Y; // // show form edit node // EditNodeWindow edtNodeWindow = new EditNodeWindow(); // edtNodeWindow.ShowDialog(); // string result1 = edtNodeWindow._txtMode1; // string result2 = edtNodeWindow._txtMode2; // string result3 = edtNodeWindow._txtMode3; // bool exit = edtNodeWindow._ExitFlg; // if (!exit) // { // SaveChanged(node_edited.X, node_edited.Y, result1, result2, result3); // } // return; // } // } // } //} public void SaveChanged(double x, double y, string st1, string st2, string st3) { for (int i = 0; i < NodeInfo_List.Count; i++) { NodeInfo ni = new NodeInfo(); ni = NodeInfo_List[i]; if (ni.X == x && ni.Y == y) { ni.Mode1 = st1; ni.Mode2 = st2; ni.Mode3 = st3; NodeInfo_List[i] = ni; return; } } } #endregion #region Display RouteInfo public void DspRouteInfo() { //Clear Route Info Table ((RoboforkMenu)System.Windows.Application.Current.MainWindow).grdRouteInfo.Children.Clear(); if (NodeInfo_List.Count != 0) { int _RowIdx = 0; string _Content = ""; for (int i = 0; i < NodeInfo_List.Count; i++) { NodeInfo ni = new NodeInfo(); ni = NodeInfo_List[i]; //column 1 if (i == 0) { _Content = "S"; } else if (i == NodeInfo_List.Count - 1) { _Content = "G"; } else { _Content = i.ToString(); } AddLabeltoGrid(_RowIdx, 0, _Content); //column 2 // Display Node's Position _Content = ni.X + ", " + ni.Y; AddLabeltoGrid(_RowIdx, 1, _Content); // Display Node's Field if (ni.Mode1 != "" && ni.Mode1 != null) { char delimiterChars = '_'; string[] tmp = ni.Mode1.Split(delimiterChars); foreach (string s in tmp) { _RowIdx++; delimiterChars = ':'; if (s.Split(delimiterChars)[0] == "Mode") { double distance = 0; if (i == NodeInfo_List.Count - 1) { distance = 0; } else { distance = DistanceCalculate(NodeInfo_List[i].X, NodeInfo_List[i].Y, NodeInfo_List[i + 1].X, NodeInfo_List[i + 1].Y); } _Content = "MOVE " + distance.ToString() + "mm"; } else { _Content = s.Split(delimiterChars)[0] + " " + s.Split(delimiterChars)[1]; } AddLabeltoGrid(_RowIdx, 1, _Content); } } if (ni.Mode2 != "" && ni.Mode2 != null) { char delimiterChars = '_'; string[] tmp = ni.Mode2.Split(delimiterChars); foreach (string s in tmp) { _RowIdx++; delimiterChars = ':'; if (s.Split(delimiterChars)[0] == "Mode") { double distance = 0; if (i == NodeInfo_List.Count - 1) { distance = 0; } else { distance = DistanceCalculate(NodeInfo_List[i].X, NodeInfo_List[i].Y, NodeInfo_List[i + 1].X, NodeInfo_List[i + 1].Y); } _Content = "MOVE " + distance.ToString() + "mm"; } else { _Content = s.Split(delimiterChars)[0] + " " + s.Split(delimiterChars)[1]; } AddLabeltoGrid(_RowIdx, 1, _Content); } } if (ni.Mode3 != "" && ni.Mode3 != null) { char delimiterChars = '_'; string[] tmp = ni.Mode3.Split(delimiterChars); foreach (string s in tmp) { _RowIdx++; delimiterChars = ':'; if (s.Split(delimiterChars)[0] == "Mode") { double distance = 0; if (i == NodeInfo_List.Count - 1) { distance = 0; } else { distance = DistanceCalculate(NodeInfo_List[i].X, NodeInfo_List[i].Y, NodeInfo_List[i + 1].X, NodeInfo_List[i + 1].Y); } _Content = "MOVE " + distance.ToString() + "mm"; } else { _Content = s.Split(delimiterChars)[0] + " " + s.Split(delimiterChars)[1]; } AddLabeltoGrid(_RowIdx, 1, _Content); } } _RowIdx++; } } } public double DistanceCalculate(double _X1, double _Y1, double _X2, double _Y2) { double dist = 0; if (_X1 == _X2) { dist = System.Math.Abs(_Y1 - _Y2) * DISTANCE_RATIO; } else if (_Y1 == _Y2) { dist = System.Math.Abs(_X1 - _X2) * DISTANCE_RATIO; } return dist; } public void AddLabeltoGrid(int RowIdx, int ColIdx, string Content) { //Add Row to Grid RowDefinition _rd = new RowDefinition(); ((RoboforkMenu)System.Windows.Application.Current.MainWindow).grdRouteInfo.RowDefinitions.Add(_rd); // Add data to Grid Label dynamicLabel = new Label(); dynamicLabel.Content = Content; dynamicLabel.Margin = new Thickness(0, 0, 0, 0); dynamicLabel.Foreground = new SolidColorBrush(Colors.Black); dynamicLabel.Background = new SolidColorBrush(Colors.White); dynamicLabel.BorderBrush = new SolidColorBrush(Colors.LightGray); dynamicLabel.BorderThickness = new Thickness(1); Grid.SetRow(dynamicLabel, RowIdx); Grid.SetColumn(dynamicLabel, ColIdx); ((RoboforkMenu)System.Windows.Application.Current.MainWindow).grdRouteInfo.Children.Add(dynamicLabel); } #endregion public void CreateGoalPoint() { if (isGoalDrawRoute) { return; } isStartDrawRoute = false; if (_goalPoint == null) { _goalPoint = new ucStartEndButton(); _goalPoint.btnWidth = 50.0; _goalPoint.btnHeight = 50.0; _goalPoint.buttText = "G"; Canvas.SetLeft(_goalPoint, 675); Canvas.SetTop(_goalPoint, 75); this.Children.Add(_goalPoint); } } public void CreateStartPoint() { if (isGoalDrawRoute) { return; } isStartDrawRoute = false; if (_startPoint == null) { _startPoint = new ucStartEndButton(); _startPoint.btnWidth = 50.0; _startPoint.btnHeight = 50.0; _startPoint.buttText = "S"; Canvas.SetLeft(_startPoint, 75); Canvas.SetTop(_startPoint, 675); this.Children.Add(_startPoint); } } #region Draw New FreeNode /// /// Draw Auto Blue node /// public void NewSetFreeNodes(Point FreeNode, bool RightClick) { double radiusNode = RADIUS_NODE; EllipseGeometry ellipseGeometry; Point node; bool deleteFlag = false; if (RightClick) { if (gGrpBlueNode.Children.Count > 0) { for (int i = 0; i < gGrpBlueNode.Children.Count; i++) { ellipseGeometry = (EllipseGeometry)gGrpBlueNode.Children[i]; node = ellipseGeometry.Center; if (FreeNode.X == node.X) { if (CheckIsNode(FreeNode, node, radiusNode)) { deleteFlag = true; } } else { if (CheckIsNode(FreeNode, node, radiusNode)) { deleteFlag = true; } } if (deleteFlag) { MessageBoxResult result = MessageBox.Show("Do You Delete This Node?", "Delete Node", MessageBoxButton.OKCancel); if (result == MessageBoxResult.OK) { //Call Function Delete Node DeleteNode(i); return; } else { return; } } } } } else { //Check EditNode State for (int i = 0; i < gGrpBlueNode.Children.Count; i++) { ellipseGeometry = (EllipseGeometry)gGrpBlueNode.Children[i]; node = ellipseGeometry.Center; bool isEditNode = CheckIsNode(FreeNode, node, RADIUS_NODE); if (isEditNode) { NewEditNode(node); NewDspRouteInfo(); return; } } MessageBoxResult result = MessageBox.Show("Do You Want To Add This Node?", "Add Node", MessageBoxButton.OKCancel); if (result == MessageBoxResult.OK) { AddNode(FreeNode, gGrpBlueNode); TextBlock textBlock = new TextBlock(); textBlock.Text = stt.ToString(); textBlock.FontSize = 35; textBlock.VerticalAlignment = VerticalAlignment.Center; textBlock.HorizontalAlignment = HorizontalAlignment.Center; textBlock.Foreground = new SolidColorBrush(Colors.Red); Canvas.SetLeft(textBlock, FreeNode.X - RADIUS_NODE / 2); Canvas.SetTop(textBlock, FreeNode.Y - 1.5 * RADIUS_NODE); this.Children.Add(textBlock); NodeNo.Add(textBlock); if (stt > 1) { int tmp = gGrpBlueNode.Children.Count; EllipseGeometry elip = (EllipseGeometry)gGrpBlueNode.Children[tmp - 2]; Point node1 = elip.Center; elip = (EllipseGeometry)gGrpBlueNode.Children[tmp - 1]; Point node2 = elip.Center; DrawLine(node1, node2, gGrpNewLine); this.Children.Remove(pBlueNode); for (int i = 0; i < tmp; i++) { this.Children.Remove(NodeNo[i]); } this.Children.Add(pBlueNode); for (int i = 0; i < tmp; i++) { this.Children.Add(NodeNo[i]); } } stt++; NewInitNodeInfo_List(); NewDspRouteInfo(); } } } public void DeleteNode(int i) { //DrawLine(Point startPoint, Point endPoint, GeometryGroup geometryGroup) LineGeometry lineGeometry = new LineGeometry(); EllipseGeometry ellip; //check delete last node if (i == gGrpBlueNode.Children.Count - 1) { // delete line if (gGrpNewLine.Children.Count > 0) { gGrpNewLine.Children.RemoveAt(i - 1); } // delete ucNode ucNode _ucNode = new ucNode(); _ucNode = ucNode_Lst[i]; this.Children.Remove(_ucNode); ucNode_Lst.RemoveAt(i); // delete node gGrpBlueNode.Children.RemoveAt(i); NewNodeInfo_List.RemoveAt(i); } else { //delete all line and remove Point at 'i' gGrpNewLine.Children.Clear(); this.Children.Remove(pNewLine); gGrpBlueNode.Children.RemoveAt(i); //redraw line for (int j = 0; j < gGrpBlueNode.Children.Count - 1; j++) { ellip = (EllipseGeometry)gGrpBlueNode.Children[j]; Point node1 = ellip.Center; ellip = (EllipseGeometry)gGrpBlueNode.Children[j + 1]; Point node2 = ellip.Center; DrawLine(node1, node2, gGrpNewLine); } this.Children.Add(pNewLine); //remove ucNode for (int j = ucNode_Lst.Count - 1; j > i; j--) { ucNode_Lst[j].txtNode = ucNode_Lst[j - 1].txtNode; } this.Children.Remove(ucNode_Lst[i]); ucNode_Lst.RemoveAt(i); //redraw ucNode for (int k = 0; k < ucNode_Lst.Count; k++) { this.Children.Remove(ucNode_Lst[k]); this.Children.Add(ucNode_Lst[k]); } } //NewInitNodeInfo_List(); Lst_Node_tmp.RemoveAt(i); NewDspRouteInfo(); stt--; } public void ReDrawAllNode() { LineGeometry lineGeometry = new LineGeometry(); EllipseGeometry ellip; //delete all line gGrpNewLine.Children.Clear(); this.Children.Remove(pNewLine); //redraw line for (int j = 0; j < gGrpBlueNode.Children.Count - 1; j++) { ellip = (EllipseGeometry)gGrpBlueNode.Children[j]; Point node1 = ellip.Center; ellip = (EllipseGeometry)gGrpBlueNode.Children[j + 1]; Point node2 = ellip.Center; DrawLine(node1, node2, gGrpNewLine); } this.Children.Add(pNewLine); //redraw ucNode for (int k = 0; k < ucNode_Lst.Count; k++) { this.Children.Remove(ucNode_Lst[k]); this.Children.Add(ucNode_Lst[k]); } //backup DB CreateVehicleNode(); } public void NewInitNodeInfo_List() { //Reset List //NewNodeInfo_List = new List(); if (gGrpBlueNode.Children.Count > 0) { //for (int i = 0; i < gGrpBlueNode.Children.Count; i++) //{ int i = gGrpBlueNode.Children.Count - 1; Point point; EllipseGeometry eGeometry = (EllipseGeometry)gGrpBlueNode.Children[i]; point = eGeometry.Center; NewNodeInfo Ninfo = new NewNodeInfo(); Ninfo.X = point.X; Ninfo.Y = point.Y; Ninfo.Mode = ""; NewNodeInfo_List.Add(Ninfo); //} } } public void NewEditNode(Point node_edited) { EllipseGeometry ellipseGeometry; Point node; double radiusNode = RADIUS_NODE; bool flag = false; if (gGrpBlueNode.Children.Count > 0) { for (int i = 0; i < gGrpBlueNode.Children.Count; i++) { ellipseGeometry = (EllipseGeometry)gGrpBlueNode.Children[i]; node = ellipseGeometry.Center; if (CheckIsNode(node_edited, node, radiusNode)) { flag = true; } if (flag) { node_edited.X = node.X; node_edited.Y = node.Y; // show form edit node EditNodeWindow edtNodeWindow = new EditNodeWindow(); List lst_tmpdfd = new List(); if (ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList.Count > 0) { for (int k = 0; k < ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList.Count; k++) { double X = ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList[k].pointMap_X; double Y = ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList[k].pointMap_Y; if (node_edited.X == X && node_edited.Y == Y) { if (ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList[k].ListNodeInfo.Count > 0) { for (int m = 0; m < ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList[k].ListNodeInfo.Count; m++) { ListNodeInfo InputNodeInfo = new ListNodeInfo(); InputNodeInfo.Mode = ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList[k].ListNodeInfo[m].Mode; InputNodeInfo.Speed = ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList[k].ListNodeInfo[m].Speed; InputNodeInfo.Angle = ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList[k].ListNodeInfo[m].Angle; InputNodeInfo.Height = ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList[k].ListNodeInfo[m].Height; InputNodeInfo.ModeSelected = ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList[k].ListNodeInfo[m].ModeSelected; lst_tmpdfd.Add(InputNodeInfo); } edtNodeWindow.NodeInf_List = lst_tmpdfd; } edtNodeWindow.NameNode = ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList[k].nameNode; edtNodeWindow.LAT = X.ToString(); ; edtNodeWindow.LOC = Y.ToString(); } } } edtNodeWindow.ShowDialog(); //Save NameNode string strNameNode = edtNodeWindow.NameNode; ListNameNode nmNode = new ListNameNode(); nmNode.X = node.X; nmNode.Y = node.Y; nmNode.nameNode = strNameNode; NameNode_List[i] = nmNode; ucNode _ucNode = new ucNode(); _ucNode = ucNode_Lst[i]; this.Children.Remove(_ucNode); ucNode_Lst.RemoveAt(i); string color = ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList[i].color; CreateMapNode(node, i, color, strNameNode); string result = edtNodeWindow._txtMode; List lst_tmp = edtNodeWindow.NodeInf_List; Lst_Node_tmp[i].NodeInfo_tmp.Clear(); for (int j = 0; j < lst_tmp.Count; j++) { ListNodeInfo ni = new ListNodeInfo(); ni = lst_tmp[j]; Lst_Node_tmp[i].NodeInfo_tmp.Add(ni); } //backup DB CreateVehicleNode(); bool exit = edtNodeWindow._ExitFlg; if (!exit) { NewSaveChanged(node_edited.X, node_edited.Y, result); } return; } } } } //Save Node's Data Edited public void NewSaveChanged(double x, double y, string st) { for (int i = 0; i < NewNodeInfo_List.Count; i++) { NewNodeInfo ni = new NewNodeInfo(); ni = NewNodeInfo_List[i]; if (ni.X == x && ni.Y == y) { ni.Mode = st; NewNodeInfo_List[i] = ni; return; } } } public void NewDspRouteInfo() { //Clear Route Info Table ((RoboforkMenu)System.Windows.Application.Current.MainWindow).grdRouteInfo.Children.Clear(); int _RowIdx = 0; string _Content = ""; if (Lst_Node_tmp.Count != 0) { for (int i = 0; i < Lst_Node_tmp.Count; i++) { //column 1 : node index _Content = (i + 1).ToString(); AddLabeltoGrid(_RowIdx, 0, _Content); //column 2 // Display Node's Position _Content = "LAT " + Lst_Node_tmp[i].pointMap_X; AddLabeltoGrid(_RowIdx, 1, _Content); _RowIdx++; _Content = "LOC " + Lst_Node_tmp[i].pointMap_Y; AddLabeltoGrid(_RowIdx, 1, _Content); // Display Node's Field if (Lst_Node_tmp[i].NodeInfo_tmp.Count != 0) { foreach (ListNodeInfo ni in Lst_Node_tmp[i].NodeInfo_tmp) { if (ni.Speed != 0 && ni.ModeSelected == 1) { _RowIdx++; _Content = "SPD " + ni.Speed.ToString() + " Km/h"; AddLabeltoGrid(_RowIdx, 1, _Content); } } } _RowIdx++; } } } //2017/03/07 tach rieng CreateNode start private void execDeleteNode(Point FreeNode) { double radiusNode = RADIUS_NODE; EllipseGeometry ellipseGeometry; Point node; bool deleteFlag = false; if (gGrpBlueNode.Children.Count > 0) { for (int i = 0; i < gGrpBlueNode.Children.Count; i++) { ellipseGeometry = (EllipseGeometry)gGrpBlueNode.Children[i]; node = ellipseGeometry.Center; if (FreeNode.X == node.X) { if (CheckIsNode(FreeNode, node, radiusNode)) { deleteFlag = true; } } else { if (CheckIsNode(FreeNode, node, radiusNode)) { deleteFlag = true; } } if (deleteFlag) { MessageBoxResult result = MessageBox.Show("Do You Delete This Node?", "Delete Node", MessageBoxButton.OKCancel); if (result == MessageBoxResult.OK) { DeleteNode(i); SetScheduleRoute(); return; } else { return; } } } } } private void execEditNode(Point FreeNode) { EllipseGeometry ellipseGeometry; Point node; for (int i = 0; i < gGrpBlueNode.Children.Count; i++) { ellipseGeometry = (EllipseGeometry)gGrpBlueNode.Children[i]; node = ellipseGeometry.Center; bool isEditNode = CheckIsNode(FreeNode, node, RADIUS_NODE); if (isEditNode) { NewEditNode(node); NewDspRouteInfo(); return; } } } private void execCreateNode(Point FreeNode) { bool createNodeFlag = false; string NameNode = ""; //check new node in exist line if (gGrpNewLine.Children.Count > 0) { for (int i = 0; i < gGrpNewLine.Children.Count; i++) { LineGeometry lineGeometry = (LineGeometry)gGrpNewLine.Children[i]; Point p1 = lineGeometry.StartPoint; Point p2 = lineGeometry.EndPoint; bool pInL = PointInLine(FreeNode, p1, p2, UCNODE_SETLEFT); if (pInL) { // show form create node AddNodeView edtNodeWindow = new AddNodeView(); edtNodeWindow.ShowDialog(); createNodeFlag = edtNodeWindow.CreateNodeFlag; NameNode = edtNodeWindow.NameNode; //MessageBoxResult result = MessageBox.Show("Do You Want To Add This Node?", "Add Node", MessageBoxButton.OKCancel); if (createNodeFlag) { Point tmpPoint = ConvertNodeinLine(FreeNode, p1, p2); FreeNode = tmpPoint; CreateMapNode(FreeNode, i + 1, ColorNode_Insert, NameNode); gGrpBlueNode.Children.Insert(i + 1, new EllipseGeometry(FreeNode, RADIUS_NODE, RADIUS_NODE)); //set location infor to table info SetLoc_NodeInfoList(FreeNode, i + 1); SetScheduleRoute(); ListIndexNodeInsert ListIndex = new ListIndexNodeInsert(); ListIndex.X = FreeNode.X; ListIndex.Y = FreeNode.Y; IndexNodeInsert_List.Add(ListIndex); ListNameNode ListNamenodde = new ListNameNode(); ListNamenodde.X = FreeNode.X; ListNamenodde.Y = FreeNode.Y; ListNamenodde.nameNode = NameNode; NameNode_List.Add(ListNamenodde); //rename Point textName for (int j = 0; j < ucNode_Lst.Count; j++) { ucNode_Lst[j].txtNode = (j + 1).ToString(); this.Children.Remove(ucNode_Lst[j]); this.Children.Add(ucNode_Lst[j]); } ReDrawAllNode(); stt++; NewInitNodeInfo_List(); NewDspRouteInfo(); return; } else { return; } } } } if(readMapFan == true) { CreateMapNode(FreeNode, gGrpBlueNode.Children.Count, ColorNode_Add, NameNode); ListNameNode ListNamenodde = new ListNameNode(); ListNamenodde.X = FreeNode.X; ListNamenodde.Y = FreeNode.Y; ListNamenodde.nameNode = NameNode; NameNode_List.Add(ListNamenodde); gGrpBlueNode.Children.Insert(gGrpBlueNode.Children.Count, new EllipseGeometry(FreeNode, RADIUS_NODE, RADIUS_NODE)); //set location infor to table info SetLoc_NodeInfoList(FreeNode, gGrpBlueNode.Children.Count - 1); SetScheduleRoute(); //draw line if (stt > 1) { //delete all line gGrpNewLine.Children.Clear(); this.Children.Remove(pNewLine); //redraw line for (int j = 0; j < gGrpBlueNode.Children.Count - 1; j++) { EllipseGeometry ellip = (EllipseGeometry)gGrpBlueNode.Children[j]; Point node1 = ellip.Center; ellip = (EllipseGeometry)gGrpBlueNode.Children[j + 1]; Point node2 = ellip.Center; DrawLine(node1, node2, gGrpNewLine); } this.Children.Add(pNewLine); //rename Point textName for (int j = 0; j < ucNode_Lst.Count; j++) { ucNode_Lst[j].txtNode = (j + 1).ToString(); this.Children.Remove(ucNode_Lst[j]); this.Children.Add(ucNode_Lst[j]); } } stt++; NewInitNodeInfo_List(); NewDspRouteInfo(); } else { AddNodeView createNodeWindow = new AddNodeView(); createNodeWindow.ShowDialog(); createNodeFlag = createNodeWindow.CreateNodeFlag; NameNode = createNodeWindow.NameNode; if (createNodeFlag) { CreateMapNode(FreeNode, gGrpBlueNode.Children.Count, ColorNode_Add, NameNode); ListNameNode ListNamenodde = new ListNameNode(); ListNamenodde.X = FreeNode.X; ListNamenodde.Y = FreeNode.Y; ListNamenodde.nameNode = NameNode; NameNode_List.Add(ListNamenodde); gGrpBlueNode.Children.Insert(gGrpBlueNode.Children.Count, new EllipseGeometry(FreeNode, RADIUS_NODE, RADIUS_NODE)); //set location infor to table info SetLoc_NodeInfoList(FreeNode, gGrpBlueNode.Children.Count - 1); SetScheduleRoute(); //draw line if (stt > 1) { //delete all line gGrpNewLine.Children.Clear(); this.Children.Remove(pNewLine); //redraw line for (int j = 0; j < gGrpBlueNode.Children.Count - 1; j++) { EllipseGeometry ellip = (EllipseGeometry)gGrpBlueNode.Children[j]; Point node1 = ellip.Center; ellip = (EllipseGeometry)gGrpBlueNode.Children[j + 1]; Point node2 = ellip.Center; DrawLine(node1, node2, gGrpNewLine); } this.Children.Add(pNewLine); //rename Point textName for (int j = 0; j < ucNode_Lst.Count; j++) { ucNode_Lst[j].txtNode = (j + 1).ToString(); this.Children.Remove(ucNode_Lst[j]); this.Children.Add(ucNode_Lst[j]); } } stt++; NewInitNodeInfo_List(); NewDspRouteInfo(); } } } public void CreateMapNode(Point FreeNode, int stt, String Color, string NameNode) { ucNode _ucNode = new ucNode(); _ucNode.btnWidth = UCNODE_WIDTH; _ucNode.btnHeight = UCNODE_HEIGHT; _ucNode.txtNode = (stt + 1).ToString(); _ucNode.coordString = NameNode; _ucNode.NameNode = NameNode; _ucNode.fillColor = Color; _ucNode.IsDragDelta = true; Canvas.SetLeft(_ucNode, FreeNode.X - UCNODE_SETLEFT); Canvas.SetTop(_ucNode, FreeNode.Y - UCNODE_SETTOP); this.Children.Add(_ucNode); //ucNode_Lst.Add(_ucNode); ucNode_Lst.Insert(stt, _ucNode); //AddNode(FreeNode, gGrpBlueNode); //gGrpBlueNode.Children.Insert(stt, new EllipseGeometry(FreeNode, RADIUS_NODE, RADIUS_NODE)); } public void SetLoc_NodeInfoList(Point FreeNode, int stt) { //add location for NodeInfoList Node_tmp nodetmp = new Node_tmp(); nodetmp.pointMap_X = ConvertPointDisplayToActual(FreeNode.X,Scale_X,PointMapStart_X); nodetmp.pointMap_Y = ConvertPointDisplayToActual(FreeNode.Y,Scale_Y,PointMapStart_Y); Lst_Node_tmp.Insert(stt, nodetmp); } public void BindBlueNode2ucNode() { string NameNode = ""; bool NodeIsInsert = false; if (gGrpBlueNode.Children.Count > 0) { for (int i = 0; i < gGrpBlueNode.Children.Count; i++) { NodeIsInsert = false; EllipseGeometry ellipseGeometry = (EllipseGeometry)gGrpBlueNode.Children[i]; Point node = ellipseGeometry.Center; //Get Name Node if (NameNode_List.Count > 0) { for (int k = 0; k < NameNode_List.Count; k++) { if (node.X == NameNode_List[k].X && node.Y == NameNode_List[k].Y) { NameNode = NameNode_List[k].nameNode; } } } //Draw Node Insert Color is Brown if (IndexNodeInsert_List.Count > 0) { for (int j = 0; j < IndexNodeInsert_List.Count; j++) { if (node.X == IndexNodeInsert_List[j].X && node.Y == IndexNodeInsert_List[j].Y) { NodeIsInsert = true; } } } if (NodeIsInsert) { CreateMapNode(node, i, ColorNode_Insert, NameNode); } else { CreateMapNode(node, i, ColorNode_Add, NameNode); } } } } //2017/03/07 tach CreateNode End public Point ConvertNodeinLine(Point point, Point l1, Point l2) { Point pointResult = new Point(); double X = 0; double Y = 0; double distance_Line = 0; double distance_l1ToPoint = 0; distance_Line = DistanceTo(l1, l2); distance_l1ToPoint = DistanceTo(l1, point); if (l1.X == l2.X) { X = l1.X; Y = point.Y; } else if (l1.Y == l2.Y) { X = point.X; Y = l1.Y; } else { if (l1.X < l2.X) { if (l1.Y < l2.Y) { Y = l1.Y + (distance_l1ToPoint * (l2.Y - l1.Y) / distance_Line); X = l1.X + (distance_l1ToPoint * (l2.X - l1.X) / distance_Line); } else { Y = l1.Y - (distance_l1ToPoint * (l1.Y - l2.Y) / distance_Line); X = l1.X + (distance_l1ToPoint * (l2.X - l1.X) / distance_Line); } } else { if (l1.Y < l2.Y) { Y = l1.Y + (distance_l1ToPoint * (l2.Y - l1.Y) / distance_Line); X = l1.X - (distance_l1ToPoint * (l1.X - l2.X) / distance_Line); } else { Y = l1.Y - (distance_l1ToPoint * (l1.Y - l2.Y) / distance_Line); X = l1.X - (distance_l1ToPoint * (l1.X - l2.X) / distance_Line); } } } pointResult.X = X; pointResult.Y = Y; return pointResult; } public static double DistanceTo(Point point1, Point point2) { var a = (double)(point2.X - point1.X); var b = (double)(point2.Y - point1.Y); return Math.Sqrt(a * a + b * b); } public bool PointInLine(Point point, Point l1, Point l2, double radius) { double distance = 0; bool falg = false; if (l1.X < l2.X) { if (l1.Y < l2.Y) { if (point.X > l1.X - radius && point.X < l2.X + radius && point.Y > l1.Y - radius && point.Y < l2.Y + radius) { falg = true; } } else { if (point.X > l1.X - radius && point.X < l2.X + radius && point.Y < l1.Y + radius && point.Y > l2.Y - radius) { falg = true; } } } else { if (l1.Y < l2.Y) { if (point.X < l1.X + radius && point.X > l2.X - radius && point.Y > l1.Y - radius && point.Y < l2.Y + radius) { falg = true; } } else { if (point.X < l1.X + radius && point.X > l2.X - radius && point.Y < l1.Y + radius && point.Y > l2.Y - radius) { falg = true; } } } if (falg == false) { return false; } distance = DistanceFromPointToLine(point, l1, l2); if (distance > radius) { return false; } return true; } public static double DistanceFromPointToLine(Point point, Point l1, Point l2) { return Math.Abs((l2.X - l1.X) * (l1.Y - point.Y) - (l1.X - point.X) * (l2.Y - l1.Y)) / Math.Sqrt(Math.Pow(l2.X - l1.X, 2) + Math.Pow(l2.Y - l1.Y, 2)); } #endregion #region Schedule public void SetScheduleRoute() { EllipseGeometry ellipseGeometry_1; EllipseGeometry ellipseGeometry_2; Point node_1; Point node_2; Point node_Schedule = new Point(); //double x_1 = 50; //double y_1 = 80; //double Totaldistance = 1220; double x_1 = NodeSchedule_X; double y_1 = CanvasScheduleHeight / 2; double Totaldistance = CanvasScheduleWidth - NodeSchedule_X - NodeSchedule_X; if (ucScheduleNode_Lst.Count > 0) { for (int i = 0; i < ucScheduleNode_Lst.Count; i++) { ucNode _ucScheduleNode = new ucNode(); _ucScheduleNode = ucScheduleNode_Lst[i]; scheduleCanvas.Children.Remove(_ucScheduleNode); } ucScheduleNode_Lst.Clear(); } gGrpScheduleNode.Children.Clear(); gGrpScheduleLine.Children.Clear(); //Remove existed simulation if(scheduleCanvas.simulation !=null) { scheduleCanvas.Children.Remove(scheduleCanvas.simulation); } List distance = new List(); double addDistance; if (gGrpBlueNode.Children.Count > 0) { for (int i = 0; i < gGrpBlueNode.Children.Count; i++) { ellipseGeometry_2 = (EllipseGeometry)gGrpBlueNode.Children[i]; node_2 = ellipseGeometry_2.Center; if (i >= 1) { ellipseGeometry_1 = (EllipseGeometry)gGrpBlueNode.Children[i - 1]; node_1 = ellipseGeometry_1.Center; if (node_1.X == node_2.X) { addDistance = Math.Abs(node_1.Y - node_2.Y); distance.Add(addDistance); } else if (node_1.Y == node_2.Y) { addDistance = Math.Abs(node_1.X - node_2.X); distance.Add(addDistance); } else { var a = (double)(node_2.X - node_1.X); var b = (double)(node_2.Y - node_1.Y); addDistance = Math.Sqrt(a * a + b * b); distance.Add(addDistance); } } } } if (distance.Count > 0) { double total = 0; double distance_i; for (int i = 0; i < distance.Count; i++) { total = total + distance[i]; } for (int i = 0; i < distance.Count; i++) { distance_i = distance[i] * (Totaldistance / total); distance[i] = distance_i; } } if (gGrpBlueNode.Children.Count > 0) { node_Schedule.X = x_1; node_Schedule.Y = y_1; AddNode(node_Schedule, gGrpScheduleNode); } addDistance = x_1; for (int i = 0; i < distance.Count; i++) { node_Schedule.Y = y_1; addDistance = addDistance + distance[i]; node_Schedule.X = addDistance; AddNode(node_Schedule, gGrpScheduleNode); } if (gGrpScheduleNode.Children.Count > 0) { for (int i = 0; i < gGrpScheduleNode.Children.Count; i++) { ellipseGeometry_1 = (EllipseGeometry)gGrpScheduleNode.Children[i]; node_1 = ellipseGeometry_1.Center; if (i > 0) { ellipseGeometry_2 = (EllipseGeometry)gGrpScheduleNode.Children[i - 1]; node_2 = ellipseGeometry_2.Center; DrawLine(node_1, node_2, gGrpScheduleLine); } CreateScheduleNode(node_1, i + 1); } } } private void CreateScheduleNode(Point point, int indexNode) { ucNode _ucNode = new ucNode(); _ucNode.btnWidth = UCSCHEDULENODE_WIDTH; _ucNode.btnHeight = UCSCHEDULENODE_HEIGHT; _ucNode.txtNode = indexNode.ToString(); _ucNode.IsDragDelta = false; Canvas.SetLeft(_ucNode, point.X - UCSCHEDULENODE_SETLEFT); Canvas.SetTop(_ucNode, point.Y - UCSCHEDULENODE_SETTOP); scheduleCanvas.Children.Add(_ucNode); ucScheduleNode_Lst.Add(_ucNode); } #endregion #region Add Vehicle public void GetIndexProject() { bool flag = false; if (ProjectModel.ProjectModelList.Count > 0) { for (int i = 0; i < ProjectModel.ProjectModelList.Count; i++) { if (ProjectItem == ProjectModel.ProjectModelList[i].ProjectName) { ProjectIndex = i; flag = true; } } if (!flag) { ProjectIndex = ProjectModel.ProjectModelList.Count; } } else { ProjectIndex = 0; } } public void GetIndexVehicle() { bool flag = false; if (ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList.Count > 0) { for (int i = 0; i < ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList.Count; i++) { if (VehicleItem == ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[i].VehicleName) { VehicleIndex = i; flag = true; } } if (!flag) { VehicleIndex = ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList.Count; } } else { VehicleIndex = 0; } } public void GetdataVehicle() { string NameNode = ""; GetIndexProject(); GetIndexVehicle(); if (ProjectModel.ProjectModelList.Count > 0) { for (int i = 0; i < ucNode_Lst.Count; i++) { this.Children.Remove(ucNode_Lst[i]); } gGrpNewLine.Children.Clear(); this.Children.Remove(pNewLine); ucNode_Lst.Clear(); Lst_Node_tmp.Clear(); gGrpBlueNode.Children.Clear(); gGrpScheduleNode.Children.Clear(); IndexNodeInsert_List.Clear(); NameNode_List.Clear(); if (VehicleIndex < ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList.Count) { PointMapStart_X = ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapStart_X; PointMapStart_Y = ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapStart_Y; PointMapEnd_X = ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapEnd_X; PointMapEnd_Y = ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapEnd_Y; if (ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList.Count > 0) { for (int k = 0; k < ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList.Count; k++) { double X = ConvertPointActualToDisplay(ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList[k].pointMap_X,Scale_X,PointMapStart_X); double Y = ConvertPointActualToDisplay(ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList[k].pointMap_Y,Scale_Y,PointMapStart_Y); NameNode = ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList[k].nameNode; Point node = new Point(X, Y); AddNode(node, gGrpBlueNode); //Get list Node Insert if (ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList[k].color == ColorNode_Insert) { ListIndexNodeInsert ListIndex = new ListIndexNodeInsert(); ListIndex.X = X; ListIndex.Y = Y; IndexNodeInsert_List.Add(ListIndex); } //Get List Name Node ListNameNode ListNamenode = new ListNameNode(); ListNamenode.X = X; ListNamenode.Y = Y; ListNamenode.nameNode = NameNode; NameNode_List.Add(ListNamenode); //Bind VehicleModel's NodeInfo to List Node Info temp Node_tmp node_tmp = new Node_tmp(); node_tmp.pointMap_X = ConvertPointDisplayToActual(X,Scale_X,PointMapStart_X); node_tmp.pointMap_Y = ConvertPointDisplayToActual(Y, Scale_Y, PointMapStart_Y); foreach (ListNodeInfo tmp in ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList[k].ListNodeInfo) { node_tmp.NodeInfo_tmp.Add(tmp); } Lst_Node_tmp.Add(node_tmp); X = ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList[k].pointSchedule_X; Y = ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList[k].pointSchedule_Y; node = new Point(X, Y); AddNode(node, gGrpScheduleNode); NameNode = ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList[k].nameNode; } } } } BindBlueNode2ucNode(); ReDrawAllNode(); SetScheduleRoute(); NewDspRouteInfo(); //Clear Route Info Table //((RoboforkMenu)System.Windows.Application.Current.MainWindow).grdRouteInfo.Children.Clear(); } public void CreateVehicleNode() { EllipseGeometry ellipseGeometry; Point node; ListNodeInfo ListNodeInfo = new ListNodeInfo(); VehicleModelList VehicleModelList = new VehicleModelList(); bool NodeISInsert = false; string NameNode = ""; if (ProjectModel.ProjectModelList.Count == 0) { ProjectModelList ProjectModelList = new ProjectModelList(); ProjectModelList.ProjectName = ProjectItem; ProjectModel.ProjectModelList.Add(ProjectModelList); } if (ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList.Count > 0 && VehicleIndex < ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList.Count) { ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapStart_X = PointMapStart_X; ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapStart_Y = PointMapStart_Y; ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapEnd_X = PointMapEnd_X; ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapEnd_Y = PointMapEnd_Y; if (ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList.Count > 0) { ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList.Clear(); } //back up if (gGrpBlueNode.Children.Count > 0) { for (int i = 0; i < gGrpBlueNode.Children.Count; i++) { NodeISInsert = false; ellipseGeometry = (EllipseGeometry)gGrpBlueNode.Children[i]; node = ellipseGeometry.Center; PointMap pMap = new PointMap(); pMap.pointMap_X = ConvertPointDisplayToActual(node.X,Scale_X,PointMapStart_X); pMap.pointMap_Y = ConvertPointDisplayToActual(node.Y,Scale_Y,PointMapStart_Y); pMap.speed_Map = 1; //Backup List Node Insert of fork if (IndexNodeInsert_List.Count > 0) { for (int j = 0; j < IndexNodeInsert_List.Count; j++) { if (node.X == IndexNodeInsert_List[j].X && node.Y == IndexNodeInsert_List[j].Y) NodeISInsert = true; } } if (NodeISInsert) { pMap.color = ColorNode_Insert; } else { pMap.color = ColorNode_Add; } //Backup Name Node for fork if (NameNode_List.Count > 0) { for (int j = 0; j < NameNode_List.Count; j++) { if (node.X == NameNode_List[j].X && node.Y == NameNode_List[j].Y) NameNode = NameNode_List[j].nameNode; } } pMap.nameNode = NameNode; ellipseGeometry = (EllipseGeometry)gGrpScheduleNode.Children[i]; node = ellipseGeometry.Center; pMap.pointSchedule_X = node.X; pMap.pointSchedule_Y = node.Y; pMap.speed_Schedule = 0.2; //Hard code //Node info foreach (ListNodeInfo temp in Lst_Node_tmp[i].NodeInfo_tmp) { pMap.ListNodeInfo.Add(temp); //set tam pMap.speed_Map = Lst_Node_tmp[i].NodeInfo_tmp[0].Speed; } ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList[VehicleIndex].pointMapList.Add(pMap); } } } // create new else { VehicleModelList.VehicleName = VehicleItem; VehicleModelList.pointMapStart_X = PointMapStart_X; VehicleModelList.pointMapStart_Y = PointMapStart_Y; VehicleModelList.pointMapEnd_X = PointMapEnd_X; VehicleModelList.pointMapEnd_Y = PointMapEnd_Y; if (gGrpBlueNode.Children.Count > 0) { for (int i = 0; i < gGrpBlueNode.Children.Count; i++) { NodeISInsert = false; ellipseGeometry = (EllipseGeometry)gGrpBlueNode.Children[i]; node = ellipseGeometry.Center; PointMap pMap = new PointMap(); pMap.pointMap_X = ConvertPointDisplayToActual(node.X, Scale_X, PointMapStart_X); pMap.pointMap_Y = ConvertPointDisplayToActual(node.Y, Scale_Y, PointMapStart_Y); pMap.speed_Map = 1; //Backup List Node Insert of fork if (IndexNodeInsert_List.Count > 0) { for (int j = 0; j < IndexNodeInsert_List.Count; j++) { if (node.X == IndexNodeInsert_List[j].X && node.Y == IndexNodeInsert_List[j].Y) NodeISInsert = true; } } if (NodeISInsert) { pMap.color = ColorNode_Insert; } else { pMap.color = ColorNode_Add; } //Backup Name Node for fork if (NameNode_List.Count > 0) { for (int j = 0; j < NameNode_List.Count; j++) { if (node.X == NameNode_List[j].X && node.Y == NameNode_List[j].Y) NameNode = NameNode_List[j].nameNode; } } pMap.nameNode = NameNode; //Node info foreach (ListNodeInfo temp in Lst_Node_tmp[i].NodeInfo_tmp) { pMap.ListNodeInfo.Add(temp); } ellipseGeometry = (EllipseGeometry)gGrpScheduleNode.Children[i]; node = ellipseGeometry.Center; pMap.pointSchedule_X = node.X; pMap.pointSchedule_Y = node.Y; pMap.speed_Schedule = 0.2; //Hard code VehicleModelList.pointMapList.Add(pMap); } } ProjectModel.ProjectModelList[ProjectIndex].VehicleModelList.Add(VehicleModelList); } } #endregion #region Read file Mapfan public void ReadFile() { //Point node; bool line1 = true; string[] lines = System.IO.File.ReadAllLines(@"Mapfan\MapFan.index"); foreach (string line in lines) { // Use a tab to indent each line of the file. string[] tmp = line.Split('\t'); if (line1) { PointMapStart_X = Double.Parse(tmp[1]); PointMapStart_Y = Double.Parse(tmp[3]); line1 = false; } else { PointMapEnd_X = Double.Parse(tmp[1]); PointMapEnd_Y = Double.Parse(tmp[3]); } //double lat = ConvertPointActualToDisplay(Double.Parse(tmp[1]),Scale_X,PointMapStart_X); //double log = ConvertPointActualToDisplay(Double.Parse(tmp[3]),Scale_Y,PointMapStart_Y); //node = new Point(lat, log); //execCreateNode(node); ////AddNode(node, gGrpBlueNode); } getScaleMap(); //SetScheduleRoute(); ReDrawAllNode2(); readMapFan = false; ////backup DB //CreateVehicleNode(); } public void ReDrawAllNode2() { LineGeometry lineGeometry = new LineGeometry(); EllipseGeometry ellip; //delete all line gGrpScheduleLine.Children.Clear(); scheduleCanvas.Children.Remove(pScheduleLine); //redraw line for (int j = 0; j < gGrpScheduleNode.Children.Count - 1; j++) { ellip = (EllipseGeometry)gGrpScheduleNode.Children[j]; Point node1 = ellip.Center; ellip = (EllipseGeometry)gGrpScheduleNode.Children[j + 1]; Point node2 = ellip.Center; DrawLine(node1, node2, gGrpScheduleLine); } scheduleCanvas.Children.Add(pScheduleLine); //redraw ucNode for (int k = 0; k < ucScheduleNode_Lst.Count; k++) { scheduleCanvas.Children.Remove(ucScheduleNode_Lst[k]); scheduleCanvas.Children.Add(ucScheduleNode_Lst[k]); } ////backup DB //CreateVehicleNode(); } #endregion #region Get data from AWS // Get info fork public void GetInfoFork() { var service = new Fork2PCTableService(); /*Read*/ IEnumerable fork2PCs = service.GetAllFork2PCs().Where(x => x.ForkID == 1); if (fork2PCs.Count() == 0) return; var fork2PC = fork2PCs.ElementAt(0); //Clear Route Info Table ((RoboforkMenu)System.Windows.Application.Current.MainWindow).grdRouteInfo.Children.Clear(); int _RowIdx = 0; AddLabeltoGrid(_RowIdx, 0, "ID"); AddLabeltoGrid(_RowIdx, 1, fork2PC.ForkID.ToString()); _RowIdx++; AddLabeltoGrid(_RowIdx, 0, "LAT"); AddLabeltoGrid(_RowIdx, 1, fork2PC.ForkPos_x.ToString()); _RowIdx++; AddLabeltoGrid(_RowIdx, 0, "LOC"); AddLabeltoGrid(_RowIdx, 1, fork2PC.ForkPos_y.ToString()); _RowIdx++; AddLabeltoGrid(_RowIdx, 0, "SPD"); AddLabeltoGrid(_RowIdx, 1, fork2PC.spd_veh.ToString() + "Km/h"); _RowIdx++; AddLabeltoGrid(_RowIdx, 0, "GPS"); AddLabeltoGrid(_RowIdx, 1, fork2PC.GPS_data); } /// /// Get infor node /// public void GetInfoNode() { var service = new Robofork15DemoService(); /*Read*/ IEnumerable fork2PCs = service.GetAllRobofork15Demos().Where(x => x.ForkNo == 1).OrderBy(x => x.NodeID); //Clear Route Info Table ((RoboforkMenu)System.Windows.Application.Current.MainWindow).grdRouteInfo.Children.Clear(); // Process clear canvas ClearCanvas(); int _RowIdx = 0; foreach (var fork2PC in fork2PCs) { ucNode _ucNode = new ucNode(); _ucNode.txtNode = fork2PC.NodeID.ToString(); _ucNode.IsDragDelta = true; _ucNode._dataNode = fork2PC; Canvas.SetLeft(_ucNode, fork2PC.NodePos_x - UCNODE_SETLEFT); Canvas.SetTop(_ucNode, fork2PC.NodePos_y - UCNODE_SETTOP); ucNode_Lst.Add(_ucNode); this.Children.Add(_ucNode); //Column 1 : node index AddLabeltoGrid(_RowIdx, 0, fork2PC.NodeID.ToString()); //Column 2 : Pos_X, Pos_Y, Speed AddLabeltoGrid(_RowIdx, 1, "LAT: " + fork2PC.NodePos_x.ToString()); _RowIdx++; AddLabeltoGrid(_RowIdx, 1, "LOC: " + fork2PC.NodePos_y.ToString()); _RowIdx++; AddLabeltoGrid(_RowIdx, 1, "SPD: " + fork2PC.NodeVehSpd.ToString() + "Km/h"); _RowIdx++; } } /// /// Clear all data on canvas /// public void ClearCanvas() { // Clear data on designer canvas for (int i = 0; i < ucNode_Lst.Count; i++) { this.Children.Remove(ucNode_Lst[i]); } gGrpNewLine.Children.Clear(); this.Children.Remove(pNewLine); ucNode_Lst.Clear(); Lst_Node_tmp.Clear(); gGrpBlueNode.Children.Clear(); gGrpScheduleNode.Children.Clear(); IndexNodeInsert_List.Clear(); // Clear data on schedule canvas if (ucScheduleNode_Lst.Count > 0) { for (int i = 0; i < ucScheduleNode_Lst.Count; i++) { ucNode _ucScheduleNode = new ucNode(); _ucScheduleNode = ucScheduleNode_Lst[i]; scheduleCanvas.Children.Remove(_ucScheduleNode); } ucScheduleNode_Lst.Clear(); } gGrpScheduleNode.Children.Clear(); gGrpScheduleLine.Children.Clear(); } #endregion #region Add new Project public void AddNewProjectItem() { ProjectModelList ProjectModelList = new ProjectModelList(); ProjectModelList.ProjectName = ProjectItem; ProjectModel.ProjectModelList.Add(ProjectModelList); } #endregion #region get scale map public void getScaleMap() { Scale_X = ConvertScaleMap(PointMapEnd_X - PointMapStart_X, DISTANCEMAPDISPLAY); Scale_Y = ConvertScaleMap(PointMapEnd_Y - PointMapStart_Y, DISTANCEMAPDISPLAY); } public double ConvertScaleMap(double distance, double const1000) { return distance / const1000; } public double ConvertPointDisplayToActual(double x, double scale, double pointStart) { return pointStart + x * scale; } public double ConvertPointActualToDisplay(double x, double scale, double pointStart) { return (x - pointStart)/ scale; } #endregion } }