Grasshopper

algorithmic modeling for Rhino

Hello,
I have a question, is it possible to catch document events grom C# script, such as event OnDocumentLoad, OnObjectsAdded etc.?

Views: 1364

Replies to This Discussion

Yes, it's regular C# code so you can register all the event handlers you want.

Could you help me by showing a simple example of such. ObjectAdded event in GH_Document class. I am a beginner in C # also I would be very grateful.


Thanks

Here is a simple example where we create a simple method (called Foo) that is executed whenever a new Rhino object is added to the scene. The method "saves" the object in the C# script component so that the main part of the script can extract the object's id and output it.

RhinoObject rhinoObject = null; // the reference to the object that has just been added

// This method will be subscribed to the AddRhinoObject event.
void Foo(object sender, RhinoObjectEventArgs args)
{
    // Save the Rhino object that has just been added in the rhinoObject variable

    // so we can access it in the main script

    rhinoObject = args.TheObject;


    // Expire this C# component and schedule a recompute
    Component.ExpireSolution(true);
}

private void RunScript(ref object Guid)

{

   // Remove (unsubcribe) the Foo method from the AddRhinoObject event
   // in case it has already been there, to avoid multiple invokes 
   RhinoDoc.AddRhinoObject -= Foo;

   // Add (subscribe) the Foo method to the AddRhinoObject event
   RhinoDoc.AddRhinoObject += Foo;

   // Output the guid of the rhino object that has just been added to the scene
   if (rhinoObject != null)
   Guid = rhinoObject.Id;

 }

Attachments:

Very thanks Long, you helped me a lot.

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service