/
GHSupport.cs
167 lines (166 loc) · 6.91 KB
/
GHSupport.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
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
using System.Collections.Generic;
using Rhino.Geometry;
using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;
namespace CulebraData.Utilities
{
/// <summary>
/// GHSupport class is meant to ease the pain of dealing with DataTrees(for scripting components) and GH_Structure(for GHA Dev),
/// it allows you to create nested lists that cannot be used in grasshopper, for example getAllFaceVertices returns a (List List Point3d ),
/// this data cannot be returned in grasshopper as points in space, so using GHSupport.convertToGHDataTree() we can convert that nested list into a
/// data tree structure and we can now use nested lists of whatever
/// </summary>
public static class GHSupport
{
/// <summary>
/// Converts nested list of points into GH_Structure (for gha dev)
/// </summary>
/// <param name="conversionData">the nested list to convert</param>
/// <returns>The data structure</returns>
public static GH_Structure<IGH_Goo> ConvertToGHDataStructure(List<List<Point3d>> conversionData)
{
GH_Structure<IGH_Goo> ghStructure = new GH_Structure<IGH_Goo>();
for (int i = 0; i < conversionData.Count; i++)
{
List<GH_Point> ghPtList = ConvertTOGHPTList(conversionData[i]);
ghStructure.AppendRange(ghPtList, new GH_Path(i));
}
return ghStructure;
}
/// <summary>
/// Converts nested list of lines into GH_Structure (for gha dev)
/// </summary>
/// <param name="conversionData">the nested list to convert</param>
/// <returns>The data structure</returns>
public static GH_Structure<IGH_Goo> ConvertToGHDataStructure(List<List<Line>> conversionData)
{
GH_Structure<IGH_Goo> ghStructure = new GH_Structure<IGH_Goo>();
for (int i = 0; i < conversionData.Count; i++)
{
List<GH_Line> ghLineList = ConvertTOGHLineList(conversionData[i]);
ghStructure.AppendRange(ghLineList, new GH_Path(i));
}
return ghStructure;
}
/// <summary>
/// Converts nested list of polylines into GH_Structure (for gha dev)
/// </summary>
/// <param name="conversionData">the nested list to convert</param>
/// <returns>The data structure</returns>
public static GH_Structure<IGH_Goo> ConvertToGHDataStructure(List<List<Polyline>> conversionData)
{
GH_Structure<IGH_Goo> ghStructure = new GH_Structure<IGH_Goo>();
for (int i = 0; i < conversionData.Count; i++)
{
List<GH_Curve> ghCurveList = ConvertTOGHCurveList(conversionData[i]);
ghStructure.AppendRange(ghCurveList, new GH_Path(i));
}
return ghStructure;
}
/// <summary>
/// Converts nested list of points into data tree (for scripting components inside gh)
/// </summary>
/// <param name="conversionData">the nested list to convert</param>
/// <returns>The data structure</returns>
public static DataTree<Point3d> ConvertToGHDataTree(List<List<Point3d>> conversionData)
{
DataTree<Point3d> ghTree = new DataTree<Point3d>();
for (int i = 0; i < conversionData.Count; i++)
{
ghTree.AddRange(conversionData[i], new GH_Path(i));
}
return ghTree;
}
/// <summary>
/// Converts nested list of lines into data tree (for scripting components inside gh)
/// </summary>
/// <param name="conversionData">the nested list to convert</param>
/// <returns>The data structure</returns>
public static DataTree<Line> ConvertToGHDataTree(List<List<Line>> conversionData)
{
DataTree<Line> ghTree = new DataTree<Line>();
for (int i = 0; i < conversionData.Count; i++)
{
ghTree.AddRange(conversionData[i], new GH_Path(i));
}
return ghTree;
}
/// <summary>
/// Converts a point into a GH_Point
/// </summary>
/// <param name="pt">The point to convert</param>
/// <returns>The GH_Point</returns>
public static GH_Point ConvertToGHPoint(Point3d pt)
{
GH_Point ghp = new GH_Point();
bool c = GH_Convert.ToGHPoint(pt, GH_Conversion.Both, ref ghp);
return ghp;
}
/// <summary>
/// Converts a line into a GH_Line
/// </summary>
/// <param name="line">The line to convert</param>
/// <returns>The GH_Line</returns>
public static GH_Line ConvertToGHLine(Line line)
{
GH_Line ghl = new GH_Line();
bool c = GH_Convert.ToGHLine(line, GH_Conversion.Both, ref ghl);
return ghl;
}
/// <summary>
/// Converts a polyline into a GH_Curve
/// </summary>
/// <param name="pline">The polyline to convert</param>
/// <returns>The GH_Curve</returns>
public static GH_Curve ConvertToGHCurve(Polyline pline)
{
GH_Curve ghl = new GH_Curve();
bool c = GH_Convert.ToGHCurve(pline, GH_Conversion.Both, ref ghl);
return ghl;
}
/// <summary>
/// Converts a point list into a list of GH_Points
/// </summary>
/// <param name="ptList">The list to convert</param>
/// <returns>The list of GH_Points</returns>
public static List<GH_Point> ConvertTOGHPTList(List<Point3d> ptList)
{
List<GH_Point> ghPtList = new List<GH_Point>();
foreach (Point3d p in ptList)
{
ghPtList.Add(ConvertToGHPoint(p));
}
return ghPtList;
}
/// <summary>
/// Converts a list of lines into a list of GH_Lines
/// </summary>
/// <param name="edgeList">The list to convert</param>
/// <returns>The list of GH_Lines</returns>
public static List<GH_Line> ConvertTOGHLineList(List<Line> edgeList)
{
List<GH_Line> ghLineList = new List<GH_Line>();
foreach (Line p in edgeList)
{
ghLineList.Add(ConvertToGHLine(p));
}
return ghLineList;
}
/// <summary>
/// Converts a list of polylines into a list of GH_Curves
/// </summary>
/// <param name="edgeList">The list to convert</param>
/// <returns>The list of GH_Curves</returns>
public static List<GH_Curve> ConvertTOGHCurveList(List<Polyline> edgeList)
{
List<GH_Curve> ghCurveList = new List<GH_Curve>();
foreach (Polyline p in edgeList)
{
ghCurveList.Add(ConvertToGHCurve(p));
}
return ghCurveList;
}
}
}