Grasshopper

algorithmic modeling for Rhino

Is there sample code for recomputing a solution based on a layerTableEvent?

For example, code that could be used to trigger a recompute for the canvas environment if any of the following take place:

1) existing rhino object changes layer.

2) a new object is built on a specific layer.

3) a new layer is created?

Views: 1134

Replies to This Discussion

The first two aren't layer table events, they are object events. But yes, it should be possible to listen to events and trigger a new solution in Grasshopper. What exactly is the context of this question?

--

David Rutten

david@mcneel.com

I'm trying to have a c# component perform like the pipeline component and harvest material from a layer but I'm having trouble producing code that will do this. A piece of example code that I could modify (if required) for any event (object / layer) would be useful.

Thank you.

Step 1, define handlers for all the events you want to be watching out for. These are probably:

  • RhinoDoc.EndOpenDocument
  • RhinoDoc.AddRhinoObject
  • RhinoDoc.DeleteRhinoObject
  • RhinoDoc.UndeleteRhinoObject
  • RhinoDoc.ModifyObjectAttributes
  • RhinoDoc.LayerTableEvent
  • Command.UndoRedo

most of these events should set a 'dirty' flag on your component. Only EndOpenDocument and UndoRedo should actually start a new GH solution. Basically UndoRedo allows you to postpone recalculating in GH until all changes in Rhino have been made. You don't want to compute a new solution for every one of the 2500 objects the user decides to delete in one go.

Write a method which adds all of these handlers to the appropriate events, and another which removes them all. 

Override AddedToDocument and RemovedFromDocument on your parameter/component and add/remove the handlers accordingly. Basically you only want to listen to these events if you're part of a document.

Override DocumentContextChanged and remove the handlers if the document is cosed, unloaded or locked. If the document is loaded, opened or unlocked, add the handlers again.

Unfortunately there's a lot of bookkeeping involved in doing this right. Boring, difficult code to write.

--

David Rutten

david@mcneel.com

David,

I really appreciate your assistance and you are right that it is confusing. As a newbie to this would it be possible for you to post just a little bit of code so that I can grasp the structure that I need to follow in implementing these types of items?

From other posts (and your reply above) I believe that I need an "unregister" and a "register" function that can be called through the RunScript as per the below. What I am having difficulty with is understanding the way to write an event handler for something like "RhinoDoc.LayerTableEvent".  I believe that this event would call a third function "ChangeEvent" but I could be mistaken.

I have also been looking at the references to locked but am unsure of that too. I believe it's for a locked canvas solver, is that right? Example code (if it's short) for that would also be welcome.

/////////////////////////////

// in RunScript

if(!eventRegister)

RegisterEvents();

...

// In Custom Additional Code

private bool eventRegister = false;

private void RegisterEvents()

{

// a Rhino event handler that refers to the "ChangeEvent" function goes here (an example of this code would help).

}

private void UnregisterEvents()

{

// I can get this code after understanding the basic structure of above as it seems much the same.

}

private void ChangeEvent()

{

// Code that receives an event, checks for null, !null, and I am guessing would trigger a new solution (not sure how to do this and an example would be welcome if possible)

}

You can't do this in a C# script component, you just don't get to override some of the methods you want to override. You'll need to write this in Visual Studio if you want something this complex.

--

David Rutten

david@mcneel.com

David,

Thanks for the clarification, it is useful because I often test smaller bits of code from Visual Studio in the c# component, so I will avoid this.

Being new to the use of event handlers some assistance with the required syntax on the other side of the Rhino Event Handler would be useful. An example of the layer table would be great. Hopefully I am able to work the rest out from there.

RhinoDoc.LayerTableEvent += ... ?

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service