-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetFolder.cs
More file actions
119 lines (98 loc) · 4.18 KB
/
GetFolder.cs
File metadata and controls
119 lines (98 loc) · 4.18 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
118
119
using System;
using System.Collections.Generic;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Special;
using Grasshopper.Kernel.Types;
using Grasshopper.Kernel.Parameters;
using Rhino.Geometry;
using System.IO;
using System.Linq;
using static FileHopper.Util;
using FileHopper.Properties;
namespace FileHopper
{
public class GetFolder : GH_Component
{
public GetFolder()
: base("GetFolder", "Fldr",
"Extends a path with all folders that match wildcard pattern",
"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("Folder[]", "F[]", "Pattern to match folders against", GH_ParamAccess.item);
}
private readonly int pathParamIndex = 0;
private readonly int folderParamIndex = 1;
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
pManager.AddTextParameter("Path", "P", "Path with appended folder(s)", GH_ParamAccess.list);
}
protected override void SolveInstance(IGH_DataAccess DA)
{
string path = "";
DA.GetData(pathParamIndex, ref path);
string folder = "";
DA.GetData(folderParamIndex, ref folder);
var pathsOut = new string[0];
var dir = new DirectoryInfo(path);
if(dir.Exists)
{
var subDirs = dir.GetDirectories();
var goodDirs = subDirs.Where(subDir => subDir.Name.WildCardMatch(folder));
pathsOut = goodDirs.Select(goodDir => goodDir.FullName).ToArray();
}
DA.SetDataList(0, pathsOut);
}
void UpdateValueList(object sender, GH_ParamServerEventArgs e)
{
if (e.ParameterIndex == folderParamIndex)
{
var valueLists = GetConnectedValueLists();
if (valueLists.Count > 1)
{
this.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Multiple value lists connected, only first value list will be populated with files");
}
if (valueLists.Count >= 1)
{
var valueList = valueLists[0];
var paths = GetPaths();
HashSet<string> newFolderNames = GetFolders(paths);
HashSet<string> oldFolderNames = GetValueListNames(valueList);
if (!oldFolderNames.SetEquals(newFolderNames))
{
var newNameValuePairs = newFolderNames.Select(name => (name, name.ToValueListString()));
RebuildValueList(valueList, newNameValuePairs);
}
}
}
}
private List<GH_ValueList> GetConnectedValueLists()
{
return this.Params.Input[folderParamIndex].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.Folder;
}
}
public override Guid ComponentGuid
{
get { return new Guid("590a587b-a8ed-486b-ac5a-0b4630075978"); }
}
}
}