1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using System.IO;
- using System.Windows.Controls;
- using System.Windows.Shapes;
- using System.Xml.Serialization;
- namespace DrawGraph
- {
- public class Import
- {
- public static Node[] NodesFromGraphML(string path)
- {
- if (File.Exists(path))
- {
- XmlSerializer xml = new XmlSerializer(typeof(Node));
- string xmlString = File.ReadAllText(path);
- Node[] nodes;
- using(TextReader reader = new StringReader(xmlString))
- {
- nodes = (Node[])xml.Deserialize(reader);
- }
- return nodes;
- }
- else
- {
- throw new FileNotFoundException("File not found: " + path);
- }
- }
- public static Edge[] EdgesFromGraphML(string path)
- {
- if (File.Exists(path))
- {
- XmlSerializer xml = new XmlSerializer(typeof(Edge));
- string xmlString = File.ReadAllText(path);
- Edge[] edges;
- using (TextReader reader = new StringReader(xmlString))
- edges = (Edge[])xml.Deserialize(reader);
- return edges;
- }
- else
- {
- throw new FileNotFoundException("File not found: " + path);
- }
- }
- public static void GraphMLToCanvas(string path, Canvas canvas)
- {
- Node[] nodes = NodesFromGraphML(path);
- Edge[] edges = EdgesFromGraphML(path);
- Ellipse[] ellipses = new Ellipse[nodes.Length];
- Line[] lines = new Line[edges.Length];
- ArrowLine[] arrowLines = new ArrowLine[edges.Length];
- for(int i = 0; i < nodes.Length; i++)
- {
- ellipses[i] = new Ellipse()
- {
- Fill = Settings.FillColor,
- Width = Settings.NodeWidth,
- Height = Settings.NodeHeight
- };
- Canvas.SetLeft(ellipses[i], nodes[i].X);
- Canvas.SetTop(ellipses[i], nodes[i].Y);
- canvas.Children.Add(ellipses[i]);
- }
- for(int i = 0; i < edges.Length; i++)
- {
- if (edges[i].IsFocused)
- {
- arrowLines[i] = new ArrowLine()
- {
- Stroke = Settings.FillColor,
- StrokeThickness = Settings.StrokeThickness,
- X1 = edges[i].SourceNode.GetCenterByX(),
- X2 = edges[i].TargetNode.GetCenterByX(),
- Y1 = edges[i].SourceNode.GetCenterByY(),
- Y2 = edges[i].TargetNode.GetCenterByY()
- };
- canvas.Children.Add(arrowLines[i]);
- }
- else
- {
- lines[i] = new Line()
- {
- Stroke = Settings.FillColor,
- StrokeThickness = Settings.StrokeThickness,
- X1 = edges[i].SourceNode.GetCenterByX(),
- X2 = edges[i].TargetNode.GetCenterByX(),
- Y1 = edges[i].SourceNode.GetCenterByY(),
- Y2 = edges[i].TargetNode.GetCenterByY()
- };
- canvas.Children.Add(lines[i]);
- }
- }
- }
- }
- }
|