/// <summary> /// Sets the nodes position from custom graph. /// </summary> /// <param name="graph">The graph.</param> private void SetNodesPositionFromCustomGraph(CustomGraph graph) { StringBuilder s = new StringBuilder(); for (int i = 0; i < graph.Vertices.Count(); i++) { GraphLayout.SetX(graphLayout.GetVertexControl(graphLayout.Graph.Vertices.ElementAt(i)), graph.Vertices.ElementAt(i).X); GraphLayout.SetY(graphLayout.GetVertexControl(graphLayout.Graph.Vertices.ElementAt(i)), graph.Vertices.ElementAt(i).Y); } }
/// <summary> /// Sets the nodes position to save. /// </summary> private void SetNodesPositionToSave(CustomGraph graphToSave) { foreach (CustomVertex vertex in graphLayout.Graph.Vertices) { double vertexX = GraphLayout.GetX(graphLayout.GetVertexControl(vertex)); double vertexY = GraphLayout.GetY(graphLayout.GetVertexControl(vertex)); graphToSave.Vertices.Where(var => var.CompareTo(vertex)).FirstOrDefault().X = vertexX; graphToSave.Vertices.Where(var => var.CompareTo(vertex)).FirstOrDefault().Y = vertexY; } }
/// <summary> /// Saves the graph. /// </summary> /// <param name="graph">The graph.</param> /// <param name="filename">The filename.</param> public static void SaveGraph(CustomGraph graph, string filename) { ////create the xml writer if (!string.IsNullOrEmpty(filename)) { using (var writer = XmlWriter.Create(filename)) { var serializer = new GraphMLSerializer <CustomVertex, CustomEdge, CustomGraph>(); ////serialize the graph serializer.Serialize(writer, graph, v => v.Text + ";" + v.BackgroundColor.ToString() + ";" + v.X.ToString() + ";" + v.Y.ToString() + ";" + v.Highlight.ToString() + ";", e => e.EdgeColor.ToString() + ";" + e.Trigger); } } }
/// <summary> /// Loads the graph. /// </summary> /// <param name="filename">The filename.</param> /// <returns></returns> public static CustomGraph LoadGraph(string filename) { ////open the file of the graph var reader = XmlReader.Create(filename); ////create the serializer var serializer = new GraphMLDeserializer <CustomVertex, CustomEdge, CustomGraph>(); ////graph where the vertices and edges should be put in var customGraph = new CustomGraph(); ////deserialize the graph serializer.Deserialize(reader, customGraph, id => new CustomVertex(id.Split(';').First(), (Color)ColorConverter.ConvertFromString(id.Split(';').ElementAt(1)), id.Split(';').ElementAt(4), double.Parse(id.Split(';').ElementAt(2)), double.Parse(id.Split(';').ElementAt(3))), (source, target, name) => new CustomEdge(name.Split(';').ElementAt(1), source, target, (Color)ColorConverter.ConvertFromString(name.Split(';').FirstOrDefault()))); return(customGraph); }
/// <summary> /// Opens the specified custom graph. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param> private void Open(object sender, RoutedEventArgs e) { string path = Utilities.GetPath("Select the custom graph to open"); if (path != null) { try { CustomGraph graph = new CustomGraph(); graph = SerializeHelper.LoadGraph(path); this.DataContext = graph; MessageBox.Show("Graph opened succesfully!"); this.SetNodesPositionFromCustomGraph(SerializeHelper.LoadGraph(path)); } catch (Exception ex) { console.Text += ex.Message + "\r\nIncorrect file type!\r\n"; scrConsole.ScrollToEnd(); } } }