-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetFile.cs
More file actions
117 lines (98 loc) · 4.1 KB
/
GetFile.cs
File metadata and controls
117 lines (98 loc) · 4.1 KB
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
using System;
using System.Collections.Generic;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Special;
using Grasshopper.Kernel.Types;
using Rhino.Geometry;
using System.IO;
using System.Linq;
using static FileHopper.Util;
using FileHopper.Properties;
using Grasshopper.Kernel.Parameters;
namespace FileHopper
{
public class GetFile : GH_Component
{
public GetFile()
: base("GetFile", "File",
"Append a file to a directory path through utilising wildcard matching or auto-filled value lists ",
"Params", "FileHopper")
{
this.Params.ParameterSourcesChanged += UpdateValueList;
}
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
pManager.AddParameter(new Param_FilePath(),"Path", "P", "Windows directory to search within", GH_ParamAccess.item);
pManager.AddTextParameter("File[]", "F[]", "Pattern to match files against", GH_ParamAccess.item);
}
private readonly int pathParamIndex = 0;
private readonly int fileParamIndex = 1;
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
pManager.AddTextParameter("Path","P", "Path with appended file(s)", GH_ParamAccess.list);
}
protected override void SolveInstance(IGH_DataAccess DA)
{
string path = "";
DA.GetData(pathParamIndex, ref path);
string file = "";
DA.GetData(fileParamIndex, ref file);
var pathsOut = new string[0];
var dir = new DirectoryInfo(path);
if (dir.Exists)
{
var files = dir.GetFiles();
var goodFiles = files.Where(f => f.Name.WildCardMatch(file));
pathsOut = goodFiles.Select(goodFile => goodFile.FullName).ToArray();
}
DA.SetDataList(0, pathsOut);
}
void UpdateValueList(object sender, GH_ParamServerEventArgs e)
{
if (e.ParameterIndex == fileParamIndex)
{
var valueLists = GetConnectedValueLists();
if (valueLists.Count > 1)
{
this.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Multiple value lists connected, only the first value list that was connected will be populated with files");
}
if (valueLists.Count >= 1)
{
var valueList = valueLists[0];
var paths = GetPaths();
HashSet<string> newFileNames = GetFiles(paths);
HashSet<string> oldFileNames = GetValueListNames(valueList);
if (!oldFileNames.SetEquals(newFileNames))
{
var newNameValuePairs = newFileNames.Select(name => (name, name.ToValueListString()));
RebuildValueList(valueList, newNameValuePairs);
}
}
}
}
private List<GH_ValueList> GetConnectedValueLists()
{
return this.Params.Input[fileParamIndex].Sources.Where(source => source is GH_ValueList)
.Select(source => (GH_ValueList)source)
.ToList();
}
private List<DirectoryInfo> GetPaths()
{
return this.Params.Input[pathParamIndex].VolatileData.AllData(true)
.Select(data => ((GH_String)data).Value)
.Select(path => new DirectoryInfo(path))
.ToList();
}
protected override System.Drawing.Bitmap Icon
{
get
{
return Resources.File;
}
}
public override Guid ComponentGuid
{
get { return new Guid("b2879ae1-e745-4c61-908b-7bf914e76aeb"); }
}
}
}