-
Notifications
You must be signed in to change notification settings - Fork 2
/
FixWires.cs
261 lines (199 loc) · 7.74 KB
/
FixWires.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
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
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();
SetDictionary();
if (Enable)
{
SetEventHandlers();
}
else
{
RemoveEventHandlers();
}
if (FixOnce)
{
FixAll();
}
Print(msg.ToString());
// Component setup
Component.Description = "Set up event handlers to change all wire types across groups to faint"
+ "\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 OnSolutionExpired(object sender, GH_DocUndoEventArgs e)
{
if(_enable)
FixAll();
}
public void FixAll()
{
//DebugWrite("Fixing all?");
foreach(IGH_ActiveObject key in groupsPerObject.Keys.Where(k => groupsPerObject[k].Count() > 0))
{
FixInputs(key);
}
}
public bool HasGroup(IGH_ActiveObject obj)
{
return groupsPerObject[obj].Count > 0;
}
public void FixInputs(IGH_ActiveObject obj)
{
if (!IdExists(id))
{
DebugWrite("Component not relevant. Disabling old id " + id.ToString());
RemoveEventHandlers();
return;
}
//List<List<IGH_ActiveObject>> parents = new List<List<IGH_ActiveObject>>();
IGH_Param par = obj as IGH_Param;
if (par != null && par.Sources != null)
{
IEnumerable<IGH_ActiveObject> p = par.Sources.Select(s => s.Attributes.GetTopLevel.DocObject as IGH_ActiveObject);
//parents.Add(p);
if(HasGroup(obj) && p.All(o => !HasGroupsInCommon(o, obj)) && par.WireDisplay != GH_ParamWireDisplay.faint)
{
DebugWrite("Fixing " + par.Attributes.GetTopLevel.DocObject.NickName);
par.WireDisplay = GH_ParamWireDisplay.faint;
}
}
IGH_Component comp = obj as IGH_Component;
if (comp != null)
{
foreach(IGH_Param inp in comp.Params.Input)
{
IEnumerable<IGH_ActiveObject> p = inp.Sources.Select(i => i.Attributes.GetTopLevel.DocObject as IGH_ActiveObject);
if(HasGroup(obj) && p.All(o => !HasGroupsInCommon(o, obj)) && inp.WireDisplay != GH_ParamWireDisplay.faint)
{
DebugWrite("Fixing " + inp.Attributes.GetTopLevel.DocObject.NickName);
inp.WireDisplay = GH_ParamWireDisplay.faint;
}
}
}
}
public void SetDictionary()
{
IList<IGH_DocumentObject> objects = Grasshopper.Instances.ActiveCanvas.Document.Objects.ToList();
//Print(String.Join(", ", objects.Select(o => o.NickName)));
groupsPerObject = objects
.Where(o => o.GetType() != typeof(GH_Group))
.OfType<IGH_ActiveObject>()
.ToDictionary(o => o, o => new HashSet<GH_Group>());
foreach(GH_Group grp in objects.OfType<GH_Group>())
{
foreach(IGH_ActiveObject obj in grp.ObjectsRecursive().OfType<IGH_ActiveObject>())
{
groupsPerObject[obj].Add(grp);
}
}
}
public bool HasGroupsInCommon(IGH_ActiveObject obj1, IGH_ActiveObject obj2)
{
HashSet<GH_Group> set1 = groupsPerObject[obj1];
HashSet<GH_Group> set2 = groupsPerObject[obj2];
set1.IntersectWith(set2);
return set1.Any();
}
public void SetEventHandlers()
{
GrasshopperDocument.UndoStateChanged -= OnSolutionExpired;
GrasshopperDocument.UndoStateChanged += OnSolutionExpired;
GrasshopperDocument.ObjectsDeleted -= OnDeleteThisComponent;
GrasshopperDocument.ObjectsDeleted += OnDeleteThisComponent;
msg.AppendFormat("Added the eventhandlers to UndoStateChanged\n", GrasshopperDocument.Objects.OfType<Grasshopper.Kernel.Special.GH_Group>().Count());
}
public void RemoveEventHandlers()
{
GrasshopperDocument.UndoStateChanged -= OnSolutionExpired;
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("[FixWire {0}]: {1}", id, msg));
}
}
// </Custom additional code>
}