// Use this for initialization void Start() { // Start the child process. Process p = new Process(); // Redirect the output stream of the child process. p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.FileName = @"C:\Program Files (x86)\Graphviz2.38\bin\dot.exe"; //p.StartInfo.FileName = @"C:\Program Files (x86)\Graphviz2.38\bin\neato.exe"; //p.StartInfo.FileName = @"C:\Program Files (x86)\Graphviz2.38\bin\fdp.exe"; p.StartInfo.Arguments = @"-Tplain H:\unity_projects\graphvizinunity\Assets\graph.gv"; p.Start(); // Do not wait for the child process to exit before // reading to the end of its redirected stream. //p.WaitForExit(); // Read the output stream first and then wait. string output = p.StandardOutput.ReadToEnd(); p.WaitForExit(); //UnityEngine.Debug.Log("output: " + output); Parser parser = new Parser(new UnityLogger()); GVGraph graph = parser.ParseGraphSource(output); DrawGraph(graph); }
private void DrawGraph(GVGraph graph) { // Graph outline { GameObject graphCubeGO = Instantiate(Resources.Load <GameObject>("graphCube")); graphCubeGO.transform.localScale = new Vector3(graph.width * 2, graph.height * 2, 4f); graphCubeGO.transform.position = new Vector3(0, 0, 0f); } // Nodes for (int i = 0; i < graph.nodes.Length; i++) { GameObject graphNodeGO = Instantiate(Resources.Load <GameObject>("graphNode")); graphNodeGO.transform.localScale = new Vector3(graph.nodes[i].xsize, graph.nodes[i].ysize, 0.5f); graphNodeGO.transform.position = new Vector3(graph.nodes[i].x, graph.nodes[i].y, 0f); graphNodeGO.GetComponentInChildren <Text>().text = graph.nodes[i].name + "|" + graph.nodes[i].label; } // Edges for (int i = 0; i < graph.edges.Length; i++) { GameObject lineGO = Instantiate(Resources.Load <GameObject>("line")); LineRenderer lineRenderer = lineGO.GetComponent <LineRenderer>(); Vector3[] positions = new Vector3[graph.edges[i].pointCount]; for (int j = 0; j < positions.Length; j++) { positions[j].x = graph.edges[i].points[j].x; positions[j].y = graph.edges[i].points[j].y; } Color lineColor; switch (graph.edges[i].color) { case "red": lineColor = Color.red; break; case "green": lineColor = Color.green; break; case "blue": lineColor = Color.blue; break; default: lineColor = Color.black; break; } lineRenderer.SetWidth(0.1f, 0.1f); lineRenderer.SetColors(lineColor, lineColor); lineRenderer.SetVertexCount(positions.Length); lineRenderer.SetPositions(positions); } }
public void ParseGraphSourceTest() { var fileStream = new FileStream("graph_plain_01", FileMode.Open); StreamReader strmRdr = new StreamReader(fileStream, Encoding.UTF8); string source = strmRdr.ReadToEnd(); Parser parser = new Parser(new TestLogger()); GVGraph graph = parser.ParseGraphSource(source); Assert.IsNotNull(graph); Assert.AreEqual(1f, graph.scalefactor); Assert.AreEqual(0.75f, graph.width); Assert.AreEqual(1.5f, graph.height); Assert.AreEqual(2, graph.nodes.Length); Assert.AreEqual("a", graph.nodes[0].name); Assert.AreEqual(0.375f, graph.nodes[0].x); Assert.AreEqual(1.25f, graph.nodes[0].y); Assert.AreEqual(0.75f, graph.nodes[0].xsize); Assert.AreEqual(0.5f, graph.nodes[0].ysize); Assert.AreEqual("a", graph.nodes[0].label); Assert.AreEqual("solid", graph.nodes[0].style); Assert.AreEqual("ellipse", graph.nodes[0].shape); Assert.AreEqual("black", graph.nodes[0].color); Assert.AreEqual("lightgrey", graph.nodes[0].fillcolor); Assert.AreEqual("b", graph.nodes[1].name); Assert.AreEqual(0.375f, graph.nodes[1].x); Assert.AreEqual(0.25f, graph.nodes[1].y); Assert.AreEqual(0.75f, graph.nodes[1].xsize); Assert.AreEqual(0.5f, graph.nodes[1].ysize); Assert.AreEqual("b", graph.nodes[1].label); Assert.AreEqual("solid", graph.nodes[1].style); Assert.AreEqual("ellipse", graph.nodes[1].shape); Assert.AreEqual("black", graph.nodes[1].color); Assert.AreEqual("lightgrey", graph.nodes[1].fillcolor); Assert.AreEqual(1, graph.edges.Length); Assert.AreEqual("a", graph.edges[0].tailNodeName); Assert.AreEqual("b", graph.edges[0].headNodeName); Assert.AreEqual(4, graph.edges[0].pointCount); Assert.AreEqual(4, graph.edges[0].points.Length); Assert.AreEqual(new Vector2(0.375f, 0.99579f), graph.edges[0].points[0]); Assert.AreEqual(new Vector2(0.375f, 0.84509f), graph.edges[0].points[1]); Assert.AreEqual(new Vector2(0.375f, 0.65162f), graph.edges[0].points[2]); Assert.AreEqual(new Vector2(0.375f, 0.50145f), graph.edges[0].points[3]); Assert.AreEqual(null, graph.edges[0].label); Assert.AreEqual(Vector2.zero, graph.edges[0].labelPosition); Assert.AreEqual("solid", graph.edges[0].style); Assert.AreEqual("black", graph.edges[0].color); }
GVGraph ParseGraphLine(string graphString) { GVGraph graph = new GVGraph(); string[] strings = graphString.Split(' '); Debug.Assert(strings.Length == 4); Debug.Assert(strings[0] == "graph"); graph.scalefactor = float.Parse(strings[1]); graph.width = float.Parse(strings[2]); graph.height = float.Parse(strings[3]); return(graph); }