How to turn on a Custom Visualization when Rhino Object is Selected

Hello fellow grasshopperninans,

So I’m currently exploring options for making a better user experience for a grasshopper definition I created. What I’m trying to do is a way to detect automatically (inside a c# component) if inside rhino there is a object selected, and return a boolean. This so I can activate a custom visualization for selected objects.

Any ideas or reading material you guys can recommend?

Hi @LaMarrash ,
You could do that in Rhinocommon using ‘OnSelectObjects;’

RhinoDoc.SelectObjects += OnSelectObjects
public static void OnSelectObjects(object sender, Rhino.DocObjects.RhinoObjectSelectionEventArgs e)
{
  //do fun stuff;
}

Hope this helps,
Farouk

2 Likes

Thanks @farouk.serragedine ! It works! Just one small question… I’ve seen on forums that one must unsubscribe to events before subscribing, should I be concerned about that? Or am I getting the idea wrong?

@LaMarrash It’d be ideal to unsubscribe using the RemovedFromDocument override in your Component.

public override void RemovedFromDocument(GH_Document document)
{
	RhinoDoc.SelectObjects -= OnSelectObjects
}

I also think doing the subscribing using the AddedToDocument override would be an ideal pairing.

public override void AddedToDocument(GH_Document document)
{
	RhinoDoc.SelectObjects += OnSelectObjects
}

– cs

2 Likes

@csykes I’m not so sure how this should be implemented, sorry but inside which scope should I declare these? Should I declare the functions inside the RunScript Scope? I will try to learn in my own, so don’t worry, but if you can provide a gh file with the c# component to understand the structure, it would help me alot! Anyways, thanks for the input, will update with solution when found!

If you want to do it from a C# Scripting component, start by creating a bool input to enable/disable the event subscription like this on RunScript:

if(enabled){
    RhinoDoc.SelectObjects -= OnSelectObjects;
    RhinoDoc.SelectObjects += OnSelectObjects;
}else{
    RhinoDoc.SelectObjects -= OnSelectObjects;
}

But make sure you unsubscribe before any code change because this change will create a different OnSelectObjects method so your previous subscripted method will be running forever. As shown above you should unsubscribe before subscribe bc your component may run more than once with enabled=true, so in this way you avoid multiple subscriptions.

You also could get rid of this input by replacing it by an internal variable that you set true once you subscribe, but this is not a good approach if you are developing the code.

RunScript(){
if(_init){
    _init = false;
    RhinoDoc.SelectObjects -= OnSelectObjects;
    RhinoDoc.SelectObjects += OnSelectObjects; 
}
}
/// Additional code blabla
private bool _init = true;
3 Likes

Sorry I just noticed it’s a C# script not a custom component. My answer is not correct. @Dani_Abalde has the best answer here imo.

1 Like

Thank you very much @Dani_Abalde, @farouk.serragedine and @csykes for your input! This helped me alot, I appreciate your wisdom and guidance :stuck_out_tongue: