Wire event - Grasshopper2024-03-28T16:33:38Zhttps://www.grasshopper3d.com/forum/topics/wire-event?commentId=2985220%3AComment%3A1471811&feed=yes&xn_auth=noHi Long!Thank very much for t…tag:www.grasshopper3d.com,2018-08-16:2985220:Comment:19258762018-08-16T15:27:41.395ZPaul Poinethttps://www.grasshopper3d.com/profile/PaulPoinet
<p>Hi Long!<br/><br/>Thank very much for this, super helpful!<br/>I noticed though that once that c# is computed, there is no effect when editing the lines of code inside "OnObjectChangedHandler". I have to restart rh/gh and change the script to see the how it behaves with the new instructions. Any tips to make my life easier...?<br/><br/>Best<br/>Paul</p>
<p></p>
<p>Hi Long!<br/><br/>Thank very much for this, super helpful!<br/>I noticed though that once that c# is computed, there is no effect when editing the lines of code inside "OnObjectChangedHandler". I have to restart rh/gh and change the script to see the how it behaves with the new instructions. Any tips to make my life easier...?<br/><br/>Best<br/>Paul</p>
<p></p> This only works with objects…tag:www.grasshopper3d.com,2016-02-25:2985220:Comment:14718112016-02-25T22:59:31.466ZDaniel González Abaldehttps://www.grasshopper3d.com/profile/DaniAbalde
<p>This only works with objects of type igh_param. If someone uses the code, change the methods Create() and destroy() by:</p>
<p></p>
<p>Private Sub Create()<br></br> <br></br> AddHandler ghDoc.ObjectsAdded, AddressOf AddedObjects</p>
<p>For Each obj As igh_DocumentObject In ghDoc.Objects<br></br> If TypeOf obj Is igh_param Then<br></br> AddHandler obj.ObjectChanged, AddressOf WireEvent<br></br> ElseIf TypeOf obj Is igh_Component Then<br></br> For Each p As igh_param In DirectCast(obj, igh_component).Params …<br></br></p>
<p>This only works with objects of type igh_param. If someone uses the code, change the methods Create() and destroy() by:</p>
<p></p>
<p>Private Sub Create()<br/> <br/> AddHandler ghDoc.ObjectsAdded, AddressOf AddedObjects</p>
<p>For Each obj As igh_DocumentObject In ghDoc.Objects<br/> If TypeOf obj Is igh_param Then<br/> AddHandler obj.ObjectChanged, AddressOf WireEvent<br/> ElseIf TypeOf obj Is igh_Component Then<br/> For Each p As igh_param In DirectCast(obj, igh_component).Params <br/> AddHandler p.ObjectChanged, AddressOf WireEvent<br/> Next<br/> Else<br/>'rhinoapp.writeline(obj.GetType.tostring())<br/> End If<br/> Next<br/> End Sub</p>
<p></p>
<p>Public Sub Destroy()</p>
<p>RemoveHandler ghDoc.ObjectsAdded, AddressOf AddedObjects</p>
<p>For Each obj As igh_DocumentObject In ghDoc.Objects<br/> If TypeOf obj Is igh_param Then<br/> RemoveHandler obj.ObjectChanged, AddressOf WireEvent<br/> ElseIf TypeOf obj Is igh_Component Then<br/> For Each p As igh_param In DirectCast(obj, igh_component).Params <br/> RemoveHandler p.ObjectChanged, AddressOf WireEvent<br/> Next<br/> End If<br/> Next</p>
<p>End Sub</p> Thanks a million Long Nguyen,…tag:www.grasshopper3d.com,2016-02-25:2985220:Comment:14715612016-02-25T18:38:00.071ZDaniel González Abaldehttps://www.grasshopper3d.com/profile/DaniAbalde
<p>Thanks a million Long Nguyen, I was going down the wrong path!</p>
<p></p>
<p>This class works on all objects in the document, not just added. I access to the param source from the same method, casting the igh_documentObject to igh_param and getting the source.</p>
<p></p>
<p>Thank you very much again, have a nice day! :)</p>
<p></p>
<p></p>
<p>Private Sub RunScript(ByVal x As Boolean)</p>
<p>If x Then<br></br> If C Is Nothing Then C = New Conexiones(GrasshopperDocument)<br></br> Else<br></br> If C…</p>
<p>Thanks a million Long Nguyen, I was going down the wrong path!</p>
<p></p>
<p>This class works on all objects in the document, not just added. I access to the param source from the same method, casting the igh_documentObject to igh_param and getting the source.</p>
<p></p>
<p>Thank you very much again, have a nice day! :)</p>
<p></p>
<p></p>
<p>Private Sub RunScript(ByVal x As Boolean)</p>
<p>If x Then<br/> If C Is Nothing Then C = New Conexiones(GrasshopperDocument)<br/> Else<br/> If C IsNot Nothing Then C.Destroy()<br/> End If</p>
<p>End Sub</p>
<p></p>
<p>'<Custom additional code></p>
<p>Public C As Conexiones</p>
<p></p>
<p>Public Class Conexiones</p>
<p>Public WithEvents ghDoc As gh_Document</p>
<p>Sub New(Doc As gh_Document)<br/> If Doc Is Nothing Then Exit Sub<br/> ghDoc = Doc<br/> Create()<br/> End Sub</p>
<p>Private Sub Create()</p>
<p>AddHandler ghDoc.ObjectsAdded, AddressOf AddedObjects</p>
<p>For Each obj As igh_DocumentObject In ghDoc.Objects<br/> AddHandler obj.ObjectChanged, AddressOf WireEvent<br/> Next<br/> End Sub</p>
<p>Public Sub Destroy()</p>
<p>RemoveHandler ghDoc.ObjectsAdded, AddressOf AddedObjects</p>
<p>For Each obj As igh_DocumentObject In ghDoc.Objects<br/> RemoveHandler obj.ObjectChanged, AddressOf WireEvent<br/> Next<br/> End Sub</p>
<p>Private Sub AddedObjects(sender As Object, e As kernel.GH_DocObjectEventArgs)<br/> For Each obj As igh_documentObject In e.Objects<br/> AddHandler obj.ObjectChanged, AddressOf WireEvent<br/> Next<br/> End Sub</p>
<p>Private Sub WireEvent(sender As igh_documentObject, e As GH_ObjectChangedEventArgs)</p>
<p>If e.Type = GH_ObjectEventType.Sources Then<br/> If TypeOf sender Is igh_param Then<br/> Dim target As igh_param = DirectCast(sender, igh_param)<br/> If target.SourceCount < 1 Then Exit Sub<br/> Dim source As igh_param = target.Sources.Last()<br/> rhinoapp.WriteLine(String.Format("Source: {0}, target: {1} | {2}", Source.Name, target.Name, Date.Now()))<br/> End If</p>
<p>End If<br/> End Sub</p>
<p><br/> End Class</p> Here is the file with the C#…tag:www.grasshopper3d.com,2016-02-25:2985220:Comment:14714622016-02-25T16:01:42.511ZLong Nguyenhttps://www.grasshopper3d.com/profile/LongNguyen
<p>Here is the file with the C# code above.</p>
<p>Here is the file with the C# code above.</p> This is one way to do it: Whe…tag:www.grasshopper3d.com,2016-02-25:2985220:Comment:14713062016-02-25T15:58:57.411ZLong Nguyenhttps://www.grasshopper3d.com/profile/LongNguyen
<p>This is one way to do it: Whenever a new component is added to the document, you add a handler to the ObjectChanged event of that component. This handler will have the knowledge whether the source parameter for this component has been changed (i.e. when you connect new wires to it) and also has access to the component itself, so you can record the information accordingly :)<br></br><br></br>This is how I would do it in C#</p>
<p></p>
<p>private void RunScript()<br></br> {<br></br> …</p>
<p>This is one way to do it: Whenever a new component is added to the document, you add a handler to the ObjectChanged event of that component. This handler will have the knowledge whether the source parameter for this component has been changed (i.e. when you connect new wires to it) and also has access to the component itself, so you can record the information accordingly :)<br/><br/>This is how I would do it in C#</p>
<p></p>
<p>private void RunScript()<br/> {<br/> GrasshopperDocument.ObjectsAdded -= addSourceChangeHandlerToNewlyAddedObjects;<br/> GrasshopperDocument.ObjectsAdded += addSourceChangeHandlerToNewlyAddedObjects;<br/> }</p>
<p></p>
<p>// <Custom additional code> <br/> void addSourceChangeHandlerToNewlyAddedObjects(Object sender, GH_DocObjectEventArgs e)<br/> {<br/> foreach (IGH_DocumentObject ghDocumentObject in e.Objects)<br/> {<br/> ghDocumentObject.ObjectChanged -= OnObjectChangedHandler;<br/> ghDocumentObject.ObjectChanged += OnObjectChangedHandler;<br/> }<br/> }</p>
<p></p>
<p>void OnObjectChangedHandler(IGH_DocumentObject sender, GH_ObjectChangedEventArgs e)<br/> {</p>
<p> // Here we only care about changes that are caused by the change in the source data<br/> if (e.Type == GH_ObjectEventType.Sources)<br/> RhinoApp.WriteLine("Sources changed: " + sender.NickName);<br/> }</p> I managed to touch the wire e…tag:www.grasshopper3d.com,2016-02-25:2985220:Comment:14714432016-02-25T15:01:30.447ZDaniel González Abaldehttps://www.grasshopper3d.com/profile/DaniAbalde
<p>I managed to touch the wire event by undo operation, but can not access the data of the components involved :/</p>
<p></p>
<p>Private Sub RunScript(ByVal x As Boolean, ByRef A As Object) <br></br> <br></br> If x Then<br></br> <br></br> If ghDoc Is Nothing Then<br></br> ghDoc = Me.Component.OnPingDocument()<br></br> AddHandler ghDoc.UndoStateChanged , AddressOf WireEvent<br></br> End If<br></br> Else<br></br> <br></br> If ghDoc IsNot Nothing Then<br></br> RemoveHandler ghDoc.UndoStateChanged , AddressOf WireEvent<br></br> ghDoc =…</p>
<p>I managed to touch the wire event by undo operation, but can not access the data of the components involved :/</p>
<p></p>
<p>Private Sub RunScript(ByVal x As Boolean, ByRef A As Object) <br/> <br/> If x Then<br/> <br/> If ghDoc Is Nothing Then<br/> ghDoc = Me.Component.OnPingDocument()<br/> AddHandler ghDoc.UndoStateChanged , AddressOf WireEvent<br/> End If<br/> Else<br/> <br/> If ghDoc IsNot Nothing Then<br/> RemoveHandler ghDoc.UndoStateChanged , AddressOf WireEvent<br/> ghDoc = Nothing<br/> End If<br/> End If</p>
<p>End Sub</p>
<p>'<Custom additional code> <br/> <br/> Public WithEvents ghDoc As gh_document</p>
<p>Sub WireEvent(sender As Object, e As GH_DocUndoEventArgs) Handles ghDoc.UndoStateChanged <br/> If e.Operation = GH_UndoOperation.RecordAdded Then<br/> For Each ac As kernel.Undo.igh_UndoAction In e.Record.Actions<br/> If TypeOf ac Is kernel.Undo.Actions.GH_WireAction Then<br/>'Dim wa As undo.Actions.GH_WireAction = DirectCast(ac, undo.Actions.GH_WireAction)<br/> <br/> rhino.RhinoApp.writeline("Wire" & Date.now())<br/> Exit For<br/> End If<br/> Next<br/> End If<br/> <br/> End Sub<br/> '</Custom additional code> <br/>End Class</p>
<p></p>
<p>mmm I could deserialize this action and access the data? How I can know what to look for?</p>