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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Kitware.VTK;
using System.Text.RegularExpressions;
namespace FileTree
{
/// <summary>
/// This is an example that displays the
/// file layout in a particular directory.
/// The size of the file is relative size
/// of the node. The color of the node is
/// the level of depth in the file system,
/// blue being the lowest depth and red
/// being the greatest.
/// </summary>
public partial class Form1 : Form
{
// The view for rendering the graph.
public vtkTreeMapView view = null;
public vtkMutableDirectedGraph g;
public bool initialized;
string SelectedPath;
/// <summary>
/// Constructor
/// </summary>
public Form1()
{
InitializeComponent();
}
/// <summary>
/// Set up the view in the render window
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void renWinCTRL_Load(object sender, EventArgs e)
{
view = vtkTreeMapView.New();
}
/// <summary>
/// Open the explorer window of the selected file
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void view_SelectionChangedEvt(vtkObject sender, vtkObjectEventArgs e)
{
// Get the selection
//
vtkDataRepresentation dr = view.GetRepresentation(0);
vtkAnnotationLink al = dr.GetAnnotationLink();
vtkSelection sel = al.GetCurrentSelection();
vtkStringArray arr1 = null;
vtkSelectionNode node = sel.GetNode(0);
if (null != node)
{
arr1 = (vtkStringArray)node.GetSelectionList();
}
string path = "";
if (null != arr1)
{
// If it is a directory open it, if not cut off the end
// and open the directory
//
if (Directory.Exists(arr1.GetValue(0)))
{
path = arr1.GetValue(0);
}
else
{
path = Regex.Replace(arr1.GetValue(0),
"\\\\[^\\\\]+\\.[^\\\\]*$", "");
}
}
if (path != SelectedPath)
{
SelectedPath = path;
System.Diagnostics.Debug.WriteLine(SelectedPath);
if ("" != SelectedPath)
{
//Create the process
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo("cmd.exe",
"/C explorer " + SelectedPath);
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
System.Diagnostics.Process p =
System.Diagnostics.Process.Start(psi);
}
}
}
/// <summary>
/// Clean up
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
System.GC.Collect();
}
/// <summary>
/// Recursive function that creates a level of the file tree
/// </summary>
/// <param name="g"></param>
/// <param name="parent"></param>
/// <param name="path"></param>
private void buildTree(vtkMutableDirectedGraph g, long parent, string path)
{
vtkStringArray name = (vtkStringArray)g.GetVertexData().GetAbstractArray("name");
vtkLongLongArray size = (vtkLongLongArray)g.GetVertexData().GetAbstractArray("size");
vtkStringArray fullpath = (vtkStringArray)g.GetVertexData().GetAbstractArray("path");
if (Directory.Exists(path))
{
long v = 0;
if (parent == -1)
{
v = g.AddVertex();
}
else
{
v = g.AddChild((int)parent);
}
string[] pathparts = path.Split('\\');
int ipaths = pathparts.GetUpperBound(0);
fullpath.InsertNextValue(path);
name.InsertNextValue(pathparts[ipaths]);
size.InsertNextValue(1024);
this.label1.Text = "Processing " + path;
this.Update();
try
{
foreach (string f in Directory.GetFiles(path))
{
Console.Out.WriteLine(f);
buildTree(g, v, f);
}
foreach (string d in Directory.GetDirectories(path))
{
Console.Out.WriteLine(d);
buildTree(g, v, d);
}
}
catch (System.Exception excpt)
{
Console.Error.WriteLine(excpt.Message);
}
}
else if (File.Exists(path))
{
FileInfo info = new FileInfo(path);
//Do not graph files smaller than 1K
if (info.Length > 1024)
{
g.AddChild((int)parent);
fullpath.InsertNextValue(path);
name.InsertNextValue(Path.GetFileName(path));
size.InsertNextValue(info.Length);
}
}
}
/// <summary>
/// Open a folder browser and graph the
/// selected folder
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void setupView_Click(object sender, EventArgs e)
{
if (this.folderBrowserDialog1.ShowDialog().Equals(DialogResult.OK))
{
//Add the view to the render window
if (!initialized)
{
initialized = true;
this.view.GetRenderWindow().SetParentId(
this.renWinCTRL.RenderWindow.GetGenericWindowId());
this.ResizeTreeMapView();
//Add a handler to the view
view.SelectionChangedEvt += new vtkObject.vtkObjectEventHandler(this.view_SelectionChangedEvt);
}
g = vtkMutableDirectedGraph.New();
vtkStringArray name = vtkStringArray.New();
name.SetName("name");
g.GetVertexData().AddArray(name);
vtkStringArray path = vtkStringArray.New();
path.SetName("path");
g.GetVertexData().SetPedigreeIds(path);
vtkLongLongArray size = vtkLongLongArray.New();
size.SetName("size");
g.GetVertexData().AddArray(size);
string cur = Directory.GetCurrentDirectory();
buildTree(g, -1, this.folderBrowserDialog1.SelectedPath);
this.label1.Text = "Viewing "+this.folderBrowserDialog1.SelectedPath;
vtkTree t = vtkTree.New();
t.ShallowCopy(g);
view.SetLayoutStrategyToSquarify();
view.SetAreaLabelArrayName("name");
view.SetAreaHoverArrayName("path");
view.SetAreaColorArrayName("level");
view.SetAreaSizeArrayName("size");
view.AddRepresentationFromInput(t);
view.GetRenderer().ResetCamera();
view.Update();
view.Render();
}
}
private void ResizeTreeMapView()
{
int[] size = this.renWinCTRL.RenderWindow.GetSize();
this.view.GetRenderWindow().SetSize(size[0], size[1]);
}
private void renWinCTRL_Resize(object sender, EventArgs e)
{
this.ResizeTreeMapView();
}
}
}
|