private ConnectionDiagramBase GetDiagramByUUID(string uuid)
        {
            ConnectionDiagramBase result = null;

            foreach (var child in Children)
            {
                if (!(child is ConnectionDiagramBase))
                {
                    continue;
                }

                if ((child as ConnectionDiagramBase).DiagramUUID == uuid)
                {
                    result = (child as ConnectionDiagramBase);
                    break;
                }
            }

            return(result);
        }
        private void ReloadLayoutInCanvas(IMapLayout map)
        {
            Children.Clear();

            UndoStack.Clear();
            RedoStack.Clear();


            var diagrams = map.Diagrams;

            if (diagrams == null || !diagrams.Any())
            {
                return;
            }

            Dictionary <string, ILineLayout[]> uuidLines = new Dictionary <string, ILineLayout[]>();

            foreach (var diagram in diagrams)
            {
                var clnele = Activator.CreateInstance(diagram.DiagramUIType) as UIElement;

                if (clnele is ContentControl)
                {
                    (clnele as ContentControl).DataContext = diagram.DataContext;
                }

                //add the line by Diagram ActionComment after finish all diagrams
                uuidLines.Add(diagram.DiagramUUID, diagram.DepartureLines);

                Application.Current.Dispatcher.Invoke(() =>
                {
                    Canvas.SetRight(clnele, diagram.X);
                    Canvas.SetLeft(clnele, diagram.X);
                    Canvas.SetBottom(clnele, diagram.Y);
                    Canvas.SetTop(clnele, diagram.Y);


                    Children.Add(clnele);
                });
            }

            //update the canvas.
            UpdateLayout();


            foreach (var dialines in uuidLines)
            {
                ConnectionDiagramBase origindiagram = GetDiagramByUUID(dialines.Key);
                if (origindiagram != null && dialines.Value != null)
                {
                    foreach (var defline in dialines.Value)
                    {
                        var terminaldiagram = GetDiagramByUUID(defline.TerminalDiagramUUID);

                        dynamic conline;

                        if (terminaldiagram != null)
                        {
                            conline = Activator.CreateInstance(defline.LineUIType);

                            Assist.FrameworkElementAssist.SetOriginDiagram(conline, origindiagram);
                            Assist.FrameworkElementAssist.SetTerminalDiagram(conline, terminaldiagram);

                            if (conline is ConnectionLineBase)
                            {
                                //(conline as ConnectionLineBase).OriginDiagram = origindiagram;
                                //(conline as ConnectionLineBase).TerminalDiagram = terminaldiagram;
                                (conline as ConnectionLineBase).DataContext = defline.DataContext;
                                (conline as ConnectionLineBase).LineUUID    = string.IsNullOrWhiteSpace(defline.LineUUID) ? $"{conline.GetType().Name}_{Guid.NewGuid().ToString()}" : defline.LineUUID;


                                //TODOTODOTODO!!!!
                                //TODO: Posistion Recaluter
                                if (conline is ILinePosition)
                                {
                                    (conline as ILinePosition).X1 = (double)origindiagram.GetValue(Canvas.LeftProperty) - (double)terminaldiagram.GetValue(Canvas.LeftProperty) + origindiagram.CenterPosition.X;
                                    (conline as ILinePosition).Y1 = (double)origindiagram.GetValue(Canvas.TopProperty) - (double)terminaldiagram.GetValue(Canvas.TopProperty) + origindiagram.CenterPosition.Y;;
                                    (conline as ILinePosition).X2 = terminaldiagram.CenterPosition.X;
                                    (conline as ILinePosition).Y2 = terminaldiagram.CenterPosition.Y;
                                }

                                origindiagram.DepartureLines.Add(conline);
                                terminaldiagram.ArrivalLines.Add(conline);
                            }
                            Application.Current.Dispatcher.Invoke(() =>
                            {
                                Canvas.SetTop(conline, (double)terminaldiagram.GetValue(Canvas.TopProperty));
                                Canvas.SetLeft(conline, (double)terminaldiagram.GetValue(Canvas.LeftProperty));

                                Children.Add(conline);
                            });
                        }
                    }
                }
            }
        }