-
Notifications
You must be signed in to change notification settings - Fork 2
/
FixParams.cs
203 lines (156 loc) · 5.88 KB
/
FixParams.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
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
using System;
using System.Collections;
using System.Collections.Generic;
using Rhino;
using Rhino.Geometry;
using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;
using System.Text;
using System.Linq;
using Grasshopper.Kernel.Special;
/// <summary>
/// This class will be instantiated on demand by the Script component.
/// </summary>
public class Script_Instance : GH_ScriptInstance
{
#region Utility functions
/// <summary>Print a String to the [Out] Parameter of the Script component.</summary>
/// <param name="text">String to print.</param>
private void Print(string text) { /* Implementation hidden. */ }
/// <summary>Print a formatted String to the [Out] Parameter of the Script component.</summary>
/// <param name="format">String format.</param>
/// <param name="args">Formatting parameters.</param>
private void Print(string format, params object[] args) { /* Implementation hidden. */ }
/// <summary>Print useful information about an object instance to the [Out] Parameter of the Script component. </summary>
/// <param name="obj">Object instance to parse.</param>
private void Reflect(object obj) { /* Implementation hidden. */ }
/// <summary>Print the signatures of all the overloads of a specific method to the [Out] Parameter of the Script component. </summary>
/// <param name="obj">Object instance to parse.</param>
private void Reflect(object obj, string method_name) { /* Implementation hidden. */ }
#endregion
#region Members
/// <summary>Gets the current Rhino document.</summary>
private readonly RhinoDoc RhinoDocument;
/// <summary>Gets the Grasshopper document that owns this script.</summary>
private readonly GH_Document GrasshopperDocument;
/// <summary>Gets the Grasshopper script component that owns this script.</summary>
private readonly IGH_Component Component;
/// <summary>
/// Gets the current iteration count. The first call to RunScript() is associated with Iteration==0.
/// Any subsequent call within the same solution will increment the Iteration count.
/// </summary>
private readonly int Iteration;
#endregion
/// <summary>
/// This procedure contains the user code. Input parameters are provided as regular arguments,
/// Output parameters as ref arguments. You don't have to assign output parameters,
/// they will have a default value.
/// </summary>
private void RunScript(bool FixOnce, bool Enable, bool Debug)
{
_debug = Debug;
_enable = Enable;
msg.Clear();
this.Component.Message = "id: " + id.ToString();
if (Enable)
{
SetEventHandlers();
}
else
{
RemoveEventHandlers();
}
if (FixOnce)
{
FixAll();
}
Print(msg.ToString());
// Component setup
Component.Description = "Set up event handlers to change params to show nickname and not icon"
+ "\n\n"
+ "MIT License. Copyright Mathias Sønderskov Schaltz 2022";
Component.Params.Input[0].Description = "Run me once on the document";
Component.Params.Input[1].Description = "Toggle to enable event listener";
Component.Params.Input[Component.Params.Input.Count - 1].Description = "Set debug to true to get all the events printed in the rhino log";
}
// <Custom additional code>
public static Random rnd = new Random();
public int id = rnd.Next(0, 1000);
StringBuilder msg = new StringBuilder();
bool _debug = false;
bool _enable = false;
public Dictionary<IGH_ActiveObject, HashSet<GH_Group>> groupsPerObject = new Dictionary<IGH_ActiveObject, HashSet<GH_Group>>();
public void OnObjectsAdded(object sender, GH_DocObjectEventArgs e)
{
if (!IdExists(id))
{
DebugWrite("[FixParam] Component not relevant. Disabling old id " + id.ToString());
RemoveEventHandlers();
return;
}
if(_enable)
{
foreach(IGH_Param par in e.Objects.OfType<IGH_Param>())
{
FixInputs(par);
}
}
}
public void FixAll()
{
//DebugWrite("Fixing all?");
foreach(IGH_Param par in GrasshopperDocument.Objects.OfType<IGH_Param>())
{
FixInputs(par);
}
}
public void FixInputs(IGH_ActiveObject obj)
{
IGH_Param par = obj as IGH_Param;
if (par != null)
{
par.IconDisplayMode = GH_IconDisplayMode.name;
}
}
public void SetEventHandlers()
{
GrasshopperDocument.ObjectsAdded -= OnObjectsAdded;
GrasshopperDocument.ObjectsAdded += OnObjectsAdded;
GrasshopperDocument.ObjectsDeleted -= OnDeleteThisComponent;
GrasshopperDocument.ObjectsDeleted += OnDeleteThisComponent;
msg.AppendFormat("Added the eventhandlers to OnObjectsAdded\n", GrasshopperDocument.Objects.OfType<Grasshopper.Kernel.Special.GH_Group>().Count());
}
public void RemoveEventHandlers()
{
GrasshopperDocument.ObjectsAdded -= OnObjectsAdded;
GrasshopperDocument.ObjectsDeleted -= OnDeleteThisComponent;
msg.AppendFormat("Removed wire event handlers on document\n", GrasshopperDocument.Objects.OfType<Grasshopper.Kernel.Special.GH_Group>().Count());
}
public void OnDeleteThisComponent(object sender, GH_DocObjectEventArgs e)
{
if (e.Objects.OfType<Grasshopper.Kernel.GH_Component>().Where(o => o.NickName == this.Component.NickName).Any())
{
DebugWrite("Removed template component. Removing all the eventhandlers");
RemoveEventHandlers();
}
}
public bool IdExists(int id)
{
IList<IGH_DocumentObject> objs = Grasshopper.Instances.ActiveCanvas.Document.Objects;
return objs
.OfType<IGH_Component>()
.Where(ob => ob.NickName == this.Component.NickName)
.Where(ob => ob.GetType().ToString() == "ScriptComponents.Component_CSNET_Script")
.Where(ob => ob.Message == "id: " + id.ToString()).Any();
}
public void DebugWrite(string msg)
{
if (_debug)
{
Rhino.RhinoApp.WriteLine(String.Format("[FixParams {0}]: {1}", id, msg));
}
}
// </Custom additional code>
}