-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphViz.cs
105 lines (84 loc) · 3.98 KB
/
GraphViz.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Runtime.InteropServices;
namespace GraphVizNet
{
public static class GraphViz
{
private const string LibGvc = "gvc";
private const string LibGraph = "cgraph";
private const int Success = 0;
/// <summary>
/// Layouts and renders a graph
/// </summary>
/// <param name="source">The textual description of the graph</param>
/// <param name="layout">Specifies the layout algorithm (e.g. "dot"), see https://www.graphviz.org/pdf/dot.1.pdf for more layout algorithms.</param>
/// <param name="format">Specifies the output format (e.g. "png"), see https://www.graphviz.org/doc/info/output.html for more output formats.</param>
/// <returns></returns>
public static byte[] LayoutAndRender(string source, string layout, string format)
{
var context = IntPtr.Zero;
var graph = IntPtr.Zero;
var renderedGraph = IntPtr.Zero;
byte[] renderedGraphBytes;
try
{
// Create a GraphViz context
context = gvContext();
if (context == IntPtr.Zero)
throw new Exception("Failed to create GraphViz context.");
// Load the data into a graph
graph = agmemread(source);
if (graph == IntPtr.Zero)
throw new Exception("Failed to create graph from source. Check for syntax errors.");
// Apply a layout
if (gvLayout(context, graph, layout) != Success)
throw new Exception("Layout failed.");
// Render the graph
if (gvRenderData(context, graph, format, out renderedGraph, out var length) != Success)
throw new Exception("Render failed.");
// Create an array to hold the rendered graph
renderedGraphBytes = new byte[length];
// Copy from the pointer
Marshal.Copy(renderedGraph, renderedGraphBytes, 0, length);
}
finally
{
// Free up the resources
if (context != IntPtr.Zero)
{
if (graph != IntPtr.Zero)
{
gvFreeLayout(context, graph);
agclose(graph);
}
gvFreeContext(context);
if (renderedGraph != IntPtr.Zero)
{
gvFreeRenderData(renderedGraph);
}
}
}
return renderedGraphBytes;
}
#pragma warning disable IDE1006 // Naming Styles
// ReSharper disable IdentifierTypo
[DllImport(LibGvc, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr gvContext();
[DllImport(LibGraph, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr agmemread(string data);
[DllImport(LibGvc, CallingConvention = CallingConvention.Cdecl)]
private static extern int gvRenderData(IntPtr gvc, IntPtr g, string format, out IntPtr result, out int length);
[DllImport(LibGvc, CallingConvention = CallingConvention.Cdecl)]
private static extern int gvLayout(IntPtr gvc, IntPtr g, string engine);
[DllImport(LibGvc, CallingConvention = CallingConvention.Cdecl)]
private static extern int gvFreeLayout(IntPtr gvc, IntPtr g);
[DllImport(LibGvc, CallingConvention = CallingConvention.Cdecl)]
private static extern int gvFreeContext(IntPtr gvc);
[DllImport(LibGraph, CallingConvention = CallingConvention.Cdecl)]
private static extern int agclose(IntPtr g);
[DllImport(LibGvc, CallingConvention = CallingConvention.Cdecl)]
private static extern int gvFreeRenderData(IntPtr result);
// ReSharper restore IdentifierTypo
#pragma warning restore IDE1006 // Naming Styles
}
}