private void AddElementMenuItem_OnClick(object sender, RoutedEventArgs e)
        {
            ElementClass element = new ElementClass {
                Location = _mousePoint
            };
            ElementControl el = GraphClass.CreateElementControl(element, ConntectionMenuItem_Click);

            el.PreviewMouseMove += El_PreviewMouseMove;
            SaverClass.Elements.Add(element);
            EditorCanvas.Children.Add(el);
            new ElementProperty(el).ShowDialog();
        }
        private void AddOutMenuItem_OnClick(object sender, RoutedEventArgs e)
        {
            ElementClass element = new ElementClass()
            {
                InCount = 1, Type = 11, Name = $"Y{SaverClass.Elements.Count(c => c.Type == 11)}"
            };

            SaverClass.Elements.Add(element);
            InOutControl outControl = GraphClass.CreateInControl(element);

            EditorCanvas.Children.Add(outControl);
            UpdateViewOut();
        }
        private void AddInMenuItem_OnClick(object sender, RoutedEventArgs e)
        {
            ElementClass element = new ElementClass()
            {
                InCount = 0, Type = 10, Name = $"X{SaverClass.Elements.Count(c => c.Type == 10)}"
            };

            SaverClass.Elements.Add(element);
            InOutControl inControl = GraphClass.CreateInControl(element);

            inControl.MainGrid.ContextMenu = new ContextMenu();
            inControl.MainGrid.ContextMenu.Items.Add(new MenuItem()
            {
                Header = "_Соединить вход"
            });
            (inControl.MainGrid.ContextMenu.Items[0] as MenuItem).Click += ConntectionMenuItem_Click;
            EditorCanvas.Children.Add(inControl);
            UpdateViewIn();
        }
        /// <summary>
        /// Удаление элемента
        /// </summary>
        /// <param name="el"></param>
        void RemoveElement(Guid idElement)
        {
            EditorCanvas.Children.Remove(GraphClass.Elements.First(c => c.Element.Id == idElement));
            foreach (Connection connection in GraphClass.Connections.Where(c => c.Finish.Id == idElement).ToList())
            {
                EditorCanvas.Children.Remove(connection.Line);
                GraphClass.Connections.Remove(connection);
            }

            foreach (Connection connection in GraphClass.Connections.Where(c => c.Start.Id == idElement).ToList())
            {
                foreach (ElementClass elementClass in SaverClass.Elements.Where(c => c.Id == connection.Finish.Id))
                {
                    elementClass.InElements.Remove(idElement);
                }

                EditorCanvas.Children.Remove(connection.Line);
                GraphClass.Connections.Remove(connection);
            }
            GraphClass.RemoveElement(idElement);
        }
        /// <summary>
        /// Связывание элементов
        /// </summary>
        void SetConnection()
        {
            string elID = GraphClass.GetIdElementSource();

            if (elID == null)
            {
                _connectionMode = false;
                return;
            }
            //var el = GraphClass.Elements.FirstOrDefault(c => c.Element.Id == Guid.Parse(elID));

            var el = SaverClass.Elements.First(c => c.Id == Guid.Parse(elID));

            if (el.InElements.Count >= el.InCount)
            {
                MessageBox.Show("У данного элемента задействованы все входы!");
                _connectionMode = false;
                return;
            }

            if (el.InElements.Contains(_idSource))
            {
                MessageBox.Show("Данные элементы уже связаны!");
                _connectionMode = false;
                return;
            }

            SaverClass.Elements.First(c => c.Id == el.Id).InElements.Add(_idSource);
            Connection connection = new Connection()
            {
                Start  = SaverClass.Elements.First(c => c.Id == _idSource),
                Finish = el
            };

            Panel.SetZIndex(connection.Line, GraphClass.ConnectionZIndex++);
            connection.CalculatePoints();
            EditorCanvas.Children.Add(connection.Line);
            GraphClass.Connections.Add(connection);
            _connectionMode = false;
        }
        /// <summary>
        /// Загрузка схемы
        /// </summary>
        void OpenSchema()
        {
            OpenFileDialog ofd = new OpenFileDialog()
            {
                Title = "Выберите файл", Filter = "json files (*.json)|*.json"
            };

            if (ofd.ShowDialog() == true)
            {
                EditorCanvas.Children.Clear();
                GraphClass.Connections.Clear();
                SaverClass.LoadData(ofd.FileName);
                GraphClass.ElementZIndex    = 10000;
                GraphClass.ConnectionZIndex = 0;
                foreach (ElementClass element in SaverClass.Elements)
                {
                    switch (element.Type)
                    {
                    case 10:
                    {
                        InOutControl inControl = GraphClass.CreateInControl(element);
                        inControl.MainGrid.ContextMenu = new ContextMenu();
                        inControl.MainGrid.ContextMenu.Items.Add(new MenuItem()
                            {
                                Header = "_Соединить вход"
                            });
                        (inControl.MainGrid.ContextMenu.Items[0] as MenuItem).Click += ConntectionMenuItem_Click;
                        EditorCanvas.Children.Add(inControl);
                        continue;
                    }

                    case 11:
                    {
                        EditorCanvas.Children.Add(GraphClass.CreateOutControl(element));
                        foreach (Guid id in element.InElements)
                        {
                            Connection connection = new Connection()
                            {
                                Start  = SaverClass.Elements.First(c => c.Id == id),
                                Finish = element
                            };
                            connection.CalculatePoints();
                            Panel.SetZIndex(connection.Line, GraphClass.ConnectionZIndex++);
                            GraphClass.Connections.Add(connection);
                            EditorCanvas.Children.Add(connection.Line);
                        }
                        continue;
                    }
                    }

                    ElementControl el = GraphClass.CreateElementControl(element, ConntectionMenuItem_Click);
                    el.PreviewMouseMove += El_PreviewMouseMove;
                    EditorCanvas.Children.Add(el);
                    foreach (Guid id in element.InElements)
                    {
                        Connection connection = new Connection()
                        {
                            Start  = SaverClass.Elements.First(c => c.Id == id),
                            Finish = element
                        };
                        connection.CalculatePoints();
                        Panel.SetZIndex(connection.Line, GraphClass.ConnectionZIndex++);
                        GraphClass.Connections.Add(connection);
                        EditorCanvas.Children.Add(connection.Line);
                    }
                }
            }
        }
 private void EditorCanvas_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
 {
     GraphClass.ClearAllSelection();
     _mousePoint = e.GetPosition(EditorCanvas);
 }
 private void ClearAllMenuItem_OnClick(object sender, RoutedEventArgs e)
 {
     EditorCanvas.Children.Clear();
     GraphClass.RemoveAll();
 }
 private void ConntectionMenuItem_Click(object sender, RoutedEventArgs e)
 {
     _connectionMode = true;
     _idSource       = Guid.Parse(GraphClass.GetIdElementSource());
 }