Import.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System.IO;
  2. using System.Windows.Controls;
  3. using System.Windows.Shapes;
  4. using System.Xml.Serialization;
  5. namespace DrawGraph
  6. {
  7. public class Import
  8. {
  9. public static Node[] NodesFromGraphML(string path)
  10. {
  11. if (File.Exists(path))
  12. {
  13. XmlSerializer xml = new XmlSerializer(typeof(Node));
  14. string xmlString = File.ReadAllText(path);
  15. Node[] nodes;
  16. using(TextReader reader = new StringReader(xmlString))
  17. {
  18. nodes = (Node[])xml.Deserialize(reader);
  19. }
  20. return nodes;
  21. }
  22. else
  23. {
  24. throw new FileNotFoundException("File not found: " + path);
  25. }
  26. }
  27. public static Edge[] EdgesFromGraphML(string path)
  28. {
  29. if (File.Exists(path))
  30. {
  31. XmlSerializer xml = new XmlSerializer(typeof(Edge));
  32. string xmlString = File.ReadAllText(path);
  33. Edge[] edges;
  34. using (TextReader reader = new StringReader(xmlString))
  35. edges = (Edge[])xml.Deserialize(reader);
  36. return edges;
  37. }
  38. else
  39. {
  40. throw new FileNotFoundException("File not found: " + path);
  41. }
  42. }
  43. public static void GraphMLToCanvas(string path, Canvas canvas)
  44. {
  45. Node[] nodes = NodesFromGraphML(path);
  46. Edge[] edges = EdgesFromGraphML(path);
  47. Ellipse[] ellipses = new Ellipse[nodes.Length];
  48. Line[] lines = new Line[edges.Length];
  49. ArrowLine[] arrowLines = new ArrowLine[edges.Length];
  50. for(int i = 0; i < nodes.Length; i++)
  51. {
  52. ellipses[i] = new Ellipse()
  53. {
  54. Fill = Settings.FillColor,
  55. Width = Settings.NodeWidth,
  56. Height = Settings.NodeHeight
  57. };
  58. Canvas.SetLeft(ellipses[i], nodes[i].X);
  59. Canvas.SetTop(ellipses[i], nodes[i].Y);
  60. canvas.Children.Add(ellipses[i]);
  61. }
  62. for(int i = 0; i < edges.Length; i++)
  63. {
  64. if (edges[i].IsFocused)
  65. {
  66. arrowLines[i] = new ArrowLine()
  67. {
  68. Stroke = Settings.FillColor,
  69. StrokeThickness = Settings.StrokeThickness,
  70. X1 = edges[i].SourceNode.GetCenterByX(),
  71. X2 = edges[i].TargetNode.GetCenterByX(),
  72. Y1 = edges[i].SourceNode.GetCenterByY(),
  73. Y2 = edges[i].TargetNode.GetCenterByY()
  74. };
  75. canvas.Children.Add(arrowLines[i]);
  76. }
  77. else
  78. {
  79. lines[i] = new Line()
  80. {
  81. Stroke = Settings.FillColor,
  82. StrokeThickness = Settings.StrokeThickness,
  83. X1 = edges[i].SourceNode.GetCenterByX(),
  84. X2 = edges[i].TargetNode.GetCenterByX(),
  85. Y1 = edges[i].SourceNode.GetCenterByY(),
  86. Y2 = edges[i].TargetNode.GetCenterByY()
  87. };
  88. canvas.Children.Add(lines[i]);
  89. }
  90. }
  91. }
  92. }
  93. }