-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathGHCustomComponent.cs
More file actions
337 lines (282 loc) · 13.4 KB
/
GHCustomComponent.cs
File metadata and controls
337 lines (282 loc) · 13.4 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
using GH_IO.Serialization;
using GH_IO.Types;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Rhino;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
/*
Original work Copyright (c) 2021 Ali Torabi (ali@parametriczoo.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
*/
namespace GHCustomControls
{
/// <summary>
/// Inheriate from this calss to use custom controls in GH component
/// </summary>
public abstract class GHCustomComponent : GH_Component
{
protected GHCustomComponent(string name,string nickname,string description,string category,string subCategory) : base(name,nickname,description,category,subCategory) { }
public Dictionary<string, GHControl> CustomControls = new Dictionary<string, GHControl>();
public void AddCustomControl(GHControl customControl)
{
if (customControl != null)
{
// check for prexisting data ;
CustomControls.Add(customControl.Name, customControl);
customControl.SetAttribute((GHCustomAttributes)m_attributes);
}
}
public override void CreateAttributes()
{
m_attributes = new GHCustomAttributes(this);
}
public GHControl GetControl(params string[] path)
{
GHControl ct = CustomControls[path[0]];
if (path.Length == 1)
return ct;
if (ct is IGHPanel)
{
return ((IGHPanel)ct).GetControl(path.Skip(1));
}
throw new Exception("Invalid path.");
}
/// <summary>
/// retrive the value of the custom control by name.
/// if the path is invalid or the control is not a parameter then thorws an exception.
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value">the current value of the control</param>
/// <param name="path">path to the control</param>
/// <returns></returns>
public void GetControlData<T>(ref T value,params string[] path)
{
GHControl control = GetControl(path);
if (control is GHParameter)
{
GHParameter param = (GHParameter)control;
value = (T)param.CurrentValue;
return ;
}
throw new Exception("Invalid path to control");
}
#region Serialization and Deserialization
private void writeCustomControls(GH_IWriter writer, IEnumerable<GHControl> customControls, string path)
{
foreach (GHControl control in customControls)
{
string name = (path.Length > 0) ? (path + "." + control.Name): control.Name;
if (control is GHParameter)
{
GHParameter param = (GHParameter)control;
switch (param.Access)
{
case GH_ParamAccess.item:
switch (param.DataType)
{
case GH_Types.gh_bool:
writer.SetBoolean(name, (bool)param.CurrentValue);
break;
case GH_Types.gh_double:
writer.SetDouble(name, (double)param.CurrentValue);
break;
case GH_Types.gh_int32:
writer.SetInt32(name, (int)param.CurrentValue);
break;
case GH_Types.gh_decimal:
writer.SetDecimal(name, Convert.ToDecimal(param.CurrentValue));
break;
case GH_Types.gh_point2d:
writer.SetPoint2D(name, (GH_Point2D)param.CurrentValue);
break;
}
break;
case GH_ParamAccess.list:
switch (param.DataType)
{
case GH_Types.gh_bool:
writer.SetByteArray(name, ((bool[])param.CurrentValue).Select(i=> (byte)(i? 1:0)).ToArray());
break;
case GH_Types.gh_double:
writer.SetDoubleArray(name, (double[])param.CurrentValue);
break;
case GH_Types.gh_int32:
writer.SetDoubleArray(name, ((int[])param.CurrentValue).Cast<double>().ToArray());
break;
case GH_Types.gh_decimal:
writer.SetDoubleArray(name, ((float[])param.CurrentValue).Cast<double>().ToArray());
break;
case GH_Types.gh_point2d:
writer.SetDoubleArray(name, ((GH_Point2D[])param.CurrentValue).SelectMany(p => new double[] { p.x, p.y }).ToArray());
//writer.SetDoubleArray(name + ".y", ((GH_Point2D[])param.CurrentValue).Select(p => p.y).ToArray());
break ;
}
break;
}
}
if (control is IGHPanel)
{
if (control is TabPanel tabPanel)
{
foreach(var kpv in tabPanel._tabs)
{
writeCustomControls(writer, kpv.Value, name+"."+kpv.Key);
}
}
else
{
writeCustomControls(writer, ((IGHPanel)control).Items,name);
}
}
}
}
public override bool Write(GH_IWriter writer)
{
writeCustomControls(writer, CustomControls.Values,"");
return base.Write(writer);
}
private void readCustomControls(GH_IReader reader, IEnumerable<GHControl> customControls, string path)
{
foreach (GHControl control in customControls)
{
string name = (path.Length > 0) ? (path + "." + control.Name) : control.Name;
if (control is GHParameter)
{
if (reader.FindItem(name) == null)
continue;
GHParameter param = (GHParameter)control;
switch(param.Access)
{
case GH_ParamAccess.item:
switch (param.DataType)
{
case GH_Types.gh_bool:
param.CurrentValue = reader.GetBoolean(name);
break;
case GH_Types.gh_double:
param.CurrentValue = reader.GetDouble(name);
break;
case GH_Types.gh_int32:
param.CurrentValue = reader.GetInt32(name);
break;
case GH_Types.gh_decimal:
param.CurrentValue = (float)reader.GetDecimal(name);
break;
case GH_Types.gh_point2d:
param.CurrentValue = reader.GetPoint2D(name);
break;
}
break;
case GH_ParamAccess.list:
switch (param.DataType)
{
case GH_Types.gh_bool:
param.CurrentValue = reader.GetByteArray(name).Select(b=>b==1).ToArray();
break;
case GH_Types.gh_double:
param.CurrentValue = reader.GetDoubleArray(name);
break;
case GH_Types.gh_int32:
param.CurrentValue = reader.GetDoubleArray(name).Select(d=>(int)d).ToArray();
break;
case GH_Types.gh_decimal:
param.CurrentValue = reader.GetDoubleArray(name).Select(d => (float)d).ToArray();
break;
case GH_Types.gh_point2d:
var values = reader.GetDoubleArray(name);
var points = new GH_Point2D[values.Length / 2];
for (int i = 0; i < values.Length; i += 2)
points[i/2] = new GH_Point2D(values[i], values[i + 1]);
param.CurrentValue = points;
break;
}
break;
}
}
if (control is IGHPanel)
{
if (control is TabPanel tabPanel)
{
foreach (var kpv in tabPanel._tabs)
{
readCustomControls(reader, kpv.Value, name + "." + kpv.Key);
}
}
else
{
readCustomControls(reader, ((IGHPanel)control).Items, name);
}
//readCustomControls(reader, ((IGHPanel)control).Items, name);
}
}
}
public override bool Read(GH_IReader reader)
{
readCustomControls(reader , CustomControls.Values,"");
return base.Read(reader);
}
#endregion
#region Helper methods
/// <summary>
/// Rerun an image by its path.
/// </summary>
/// <param name="path">Path to your image : FLODER.IMAGENAME</param>
/// <returns></returns>
public Image GetImage(string path)
{
Assembly myAssembly = this.GetType().Assembly;
string title = myAssembly.GetCustomAttribute<AssemblyTitleAttribute>().Title;
Stream myStream = myAssembly.GetManifestResourceStream($"{title}.{path}");
if (myStream == null)
return null;
return Image.FromStream(myStream);
}
/// <summary>
/// Return an icon by its path.
/// </summary>
/// <param name="path">Path to your icon : FLODER.ICONNAME</param>
/// <returns></returns>
public Bitmap GetBitmap(string path)
{
Assembly myAssembly = this.GetType().Assembly;
string title = myAssembly.GetCustomAttribute<AssemblyTitleAttribute>().Title;
Stream myStream = myAssembly.GetManifestResourceStream($"{title}.{path}" );
if (myStream == null)
return null;
return new Bitmap(myStream);
}
/// <summary>
/// converts the given value from millimeter to internal units
/// </summary>
/// <param name="mm"></param>
/// <returns></returns>
public static double ToInternalUnits(double mm)
{
return RhinoMath.UnitScale(UnitSystem.Millimeters, Rhino.RhinoDoc.ActiveDoc.ModelUnitSystem) * mm;
}
/// <summary>
/// return the number of decimals which at least can represent millimiter in any unit system
/// </summary>
/// <returns></returns>
public static int NumDecimals()
{
return Math.Max(Convert.ToInt32(-Math.Log10(ToInternalUnits(1))), 0);
}
#endregion
}
}