forked from Torabi/GHCustomControls
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGHCustomComponent.cs
More file actions
241 lines (200 loc) · 8.53 KB
/
Copy pathGHCustomComponent.cs
File metadata and controls
241 lines (200 loc) · 8.53 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
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.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;
}
}
if (control is IGHPanel)
{
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.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;
}
}
if (control is IGHPanel)
{
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
}
}