Created
August 15, 2022 14:26
-
-
Save lin-ycv/f61a7e2fe8a2edd7f555f98a8ea84f80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Drawing; | |
using System.Windows.Forms; | |
using System.Windows.Input; | |
this.Component.Description = "Copy all wires connected to a node to another node."; | |
this.Component.Name = "Copy Wires"; | |
this.Component.NickName = "Copy"; | |
Instances.ActiveCanvas.KeyUp -= keyHandle; | |
Instances.ActiveCanvas.KeyUp += keyHandle; | |
void keyHandle(object s, KeyEventArgs e) | |
{ | |
if((e.KeyCode == Keys.Insert) && !recorded) | |
{ | |
Find(); | |
if(recorded) | |
{ | |
var toolbar = (System.Windows.Forms.ToolStrip) (Grasshopper.Instances.DocumentEditor.Controls[0].Controls[1]); | |
var bitmap = new Bitmap(1, 1); | |
bitmap.SetPixel(0, 0, Color.Red); | |
var button = new ToolStripButton("Clear", bitmap, (sender,ev) => Clear()); | |
button.Name = "NAME"; | |
button.DisplayStyle = ToolStripItemDisplayStyle.Image; | |
toolbar.Items.Add(button); | |
} | |
} | |
else if(recorded && (e.KeyCode == Keys.Insert)) | |
{ | |
GrasshopperDocument.ScheduleSolution(5, Callback); | |
} | |
else if(recorded && (e.KeyCode == Keys.Escape)) | |
{ | |
Clear(); | |
} | |
} | |
bool recorded = false; | |
bool inputGrip = true; | |
int node = -1; | |
dynamic obj; | |
double disTo(PointF p1, PointF p2) | |
{ | |
var x = Math.Abs(p1.X - p2.X); | |
var y = Math.Abs(p1.Y - p2.Y); | |
return x * x + y * y; | |
} | |
void Find() | |
{ | |
var coor = Instances.ActiveCanvas.CursorCanvasPosition; | |
obj = (GH_Component) Instances.ActiveCanvas.Document.FindObject(coor, 10); | |
var dist = double.MaxValue; | |
for(int i = 0;i < obj.Params.Input.Count; i++) | |
{ | |
var pos = obj.Params.Input[i].Attributes.Pivot; | |
var d = disTo(pos, coor); | |
if(d < dist) | |
{ | |
dist = d; | |
inputGrip = true; | |
node = i; | |
recorded = true; | |
} | |
} | |
for(int i = 0;i < obj.Params.Output.Count;i++) | |
{ | |
var pos = obj.Params.Output[i].Attributes.Pivot; | |
var d = disTo(pos, coor); | |
if(d < dist) | |
{ | |
dist = d; | |
inputGrip = false; | |
node = i; | |
recorded = true; | |
} | |
} | |
} | |
void Callback(GH_Document doc) | |
{ | |
var connected = inputGrip ? obj.Params.Input[node].Sources : obj.Params.Output[node].Recipients; | |
Find(); | |
if(inputGrip) | |
{ | |
foreach(var c in connected) | |
{ | |
obj.Params.Input[node].AddSource(c); | |
} | |
} | |
else | |
{ | |
for(int i = 0;i < connected.Count;i++) | |
{ | |
connected[i].AddSource(obj.Params.Output[node]); | |
} | |
} | |
} | |
void Clear() | |
{ | |
recorded = false; | |
node = -1; | |
var toolbar = (System.Windows.Forms.ToolStrip) (Grasshopper.Instances.DocumentEditor.Controls[0].Controls[1]); | |
var buttons = new List<ToolStripItem>(); | |
foreach (ToolStripItem item in toolbar.Items) | |
if (item.Name == "NAME") | |
buttons.Add(item); | |
foreach (var btn in buttons) | |
toolbar.Items.Remove(btn); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment