How to subscribe to an event that fires when new component is added to canvas?

Hey guys,

I’m in middle of grasshopper hackathon for 30 more hours :slight_smile:

I’m trying to create a grasshopper component using C# that when dragged to canvas subscribes to some event that from that moment on everytime user drags new component to canvas I can get the id/name and other stuff from that component that was just added.

Also if you know how to get a list of all the components in the canvas I would like to know that.

All the help is appreciated.

I’m using rhino5 and grasshopper 0.9.0.0.7.6

Thanks

GrasshopperDocument.ObjectsAdded and GrasshopperDocument.Objects.

Don’t forget to download the Grasshopper SDK doc file from the Grasshopper help menu.

sounds like a nice competition. :smirk: can you win something?

Just in addition: In case you „hack“ within a scriptcomponent instead of writing an own .gha. Be very careful with using events. On scripting level eventhandling can lead to very weird behaviour if you don‘t know what you are doing, because you cannot properly subscribe and unsubscribe.

Hey,

No winning prizes here, just experience :slight_smile:

I got this to work, thanks for the help

Hi everyone,
I am trying to do something similiar in VS.
I try to open a WindowDialog after a certain amount of components is on the canvas.
The code compiles and the count is correct, the problem is, that the window appears several times after breaking the second time the treshhold.
Maybe someone could help me with this?

 protected override void SolveInstance(IGH_DataAccess DA)
        {

            //get count of objects already on the canvas
            count =  Grasshopper.Instances.ActiveCanvas.Document.ObjectCount;
            //event when component gets dropped on canvas
            Grasshopper.Instances.ActiveCanvas.Document.ObjectsAdded += Document_ObjectsAdded;


            if (count > 4)
            {
                System.Windows.Forms.MessageBox.Show("hello", "wuwu", System.Windows.Forms.MessageBoxButtons.OKCancel);
               
            }

            Message = count.ToString();
        }


        public int count = 0;

        private void Document_ObjectsAdded(object sender, GH_DocObjectEventArgs e)
        {
                               
            count += 1;    
            ExpireSolution(true);
        }

Any help would be really apreciated.
Many thanks!
File:20190328_ComponentCount.zip (6.9 MB)