Grasshopper

algorithmic modeling for Rhino

I'm worried because I have developed a widget that needs to read when a wire is plugged in, knowing the source and target, I thought this would not be a problem but I can not find the way to do it. I hope it can be posible.

Is there any way, even very indirectly, to achieve this? Specifically, I want to launch a process after an object has been plugged into another, and know the two components.

Events of Gh_canvas and gh_document seem not to have a direct event of this, but perhaps could be done indirectly from any event available?

The gh_wireinteraction class does not give information of componentes...

This need comes from wanting to create a connectivity database which is updated every time there is a new plug. This could be solved by making it when close a document for example, but it was not what I had in mind...

Thanks in advance.

Views: 594

Replies to This Discussion

I managed to touch the wire event by undo operation, but can not access the data of the components involved :/

Private Sub RunScript(ByVal x As Boolean, ByRef A As Object)

If x Then

If ghDoc Is Nothing Then
ghDoc = Me.Component.OnPingDocument()
AddHandler ghDoc.UndoStateChanged , AddressOf WireEvent
End If
Else

If ghDoc IsNot Nothing Then
RemoveHandler ghDoc.UndoStateChanged , AddressOf WireEvent
ghDoc = Nothing
End If
End If

End Sub

'<Custom additional code>

Public WithEvents ghDoc As gh_document

Sub WireEvent(sender As Object, e As GH_DocUndoEventArgs) Handles ghDoc.UndoStateChanged
If e.Operation = GH_UndoOperation.RecordAdded Then
For Each ac As kernel.Undo.igh_UndoAction In e.Record.Actions
If TypeOf ac Is kernel.Undo.Actions.GH_WireAction Then
'Dim wa As undo.Actions.GH_WireAction = DirectCast(ac, undo.Actions.GH_WireAction)

rhino.RhinoApp.writeline("Wire" & Date.now())
Exit For
End If
Next
End If

End Sub
'</Custom additional code>
End Class

mmm I could deserialize this action and access the data? How I can know what to look for?

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 :)

This is how I would do it in C#

private void RunScript()
{
    GrasshopperDocument.ObjectsAdded -= addSourceChangeHandlerToNewlyAddedObjects;
    GrasshopperDocument.ObjectsAdded += addSourceChangeHandlerToNewlyAddedObjects;
}

// <Custom additional code>
void addSourceChangeHandlerToNewlyAddedObjects(Object sender, GH_DocObjectEventArgs e)
{
    foreach (IGH_DocumentObject ghDocumentObject in e.Objects)
    {
        ghDocumentObject.ObjectChanged -= OnObjectChangedHandler;
        ghDocumentObject.ObjectChanged += OnObjectChangedHandler;
    }
}

void OnObjectChangedHandler(IGH_DocumentObject sender, GH_ObjectChangedEventArgs e)
{

    // Here we only care about changes that are caused by the change in the source data
    if (e.Type == GH_ObjectEventType.Sources)
        RhinoApp.WriteLine("Sources changed: " + sender.NickName);
}

Here is the file with the C# code above.

Attachments:

Thanks a million Long Nguyen, I was going down the wrong path!

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.

Thank you very much again, have a nice day! :)

Private Sub RunScript(ByVal x As Boolean)

If x Then
If C Is Nothing Then C = New Conexiones(GrasshopperDocument)
Else
If C IsNot Nothing Then C.Destroy()
End If

End Sub

'<Custom additional code>

Public C As Conexiones

Public Class Conexiones

Public WithEvents ghDoc As gh_Document

Sub New(Doc As gh_Document)
If Doc Is Nothing Then Exit Sub
ghDoc = Doc
Create()
End Sub

Private Sub Create()

AddHandler ghDoc.ObjectsAdded, AddressOf AddedObjects

For Each obj As igh_DocumentObject In ghDoc.Objects
AddHandler obj.ObjectChanged, AddressOf WireEvent
Next
End Sub

Public Sub Destroy()

RemoveHandler ghDoc.ObjectsAdded, AddressOf AddedObjects

For Each obj As igh_DocumentObject In ghDoc.Objects
RemoveHandler obj.ObjectChanged, AddressOf WireEvent
Next
End Sub

Private Sub AddedObjects(sender As Object, e As kernel.GH_DocObjectEventArgs)
For Each obj As igh_documentObject In e.Objects
AddHandler obj.ObjectChanged, AddressOf WireEvent
Next
End Sub

Private Sub WireEvent(sender As igh_documentObject, e As GH_ObjectChangedEventArgs)

If e.Type = GH_ObjectEventType.Sources Then
If TypeOf sender Is igh_param Then
Dim target As igh_param = DirectCast(sender, igh_param)
If target.SourceCount < 1 Then Exit Sub
Dim source As igh_param = target.Sources.Last()
rhinoapp.WriteLine(String.Format("Source: {0}, target: {1} | {2}", Source.Name, target.Name, Date.Now()))
End If

End If
End Sub


End Class

This only works with objects of type igh_param. If someone uses the code, change the methods Create() and destroy() by:

Private Sub Create()

AddHandler ghDoc.ObjectsAdded, AddressOf AddedObjects

For Each obj As igh_DocumentObject In ghDoc.Objects
If TypeOf obj Is igh_param Then
AddHandler obj.ObjectChanged, AddressOf WireEvent
ElseIf TypeOf obj Is igh_Component Then
For Each p As igh_param In DirectCast(obj, igh_component).Params
AddHandler p.ObjectChanged, AddressOf WireEvent
Next
Else
'rhinoapp.writeline(obj.GetType.tostring())
End If
Next
End Sub

Public Sub Destroy()

RemoveHandler ghDoc.ObjectsAdded, AddressOf AddedObjects

For Each obj As igh_DocumentObject In ghDoc.Objects
If TypeOf obj Is igh_param Then
RemoveHandler obj.ObjectChanged, AddressOf WireEvent
ElseIf TypeOf obj Is igh_Component Then
For Each p As igh_param In DirectCast(obj, igh_component).Params
RemoveHandler p.ObjectChanged, AddressOf WireEvent
Next
End If
Next

End Sub

Hi Long!

Thank very much for this, super helpful!
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...?

Best
Paul

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2021   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service