What's wrong with this code?

I’m trying to dynamically add components at run-time. It seems working, but the components generated are not clickable and the document becomes heavy. Is there any magic word I’m missing?

Thanks!

    var doc = Grasshopper.Instances.ActiveCanvas.Document;
    for(int i = 0;i < 20;i++)
    {
      var newcomponent = new Grasshopper.Kernel.Components.GH_ExplodeTreeComponent();
      newcomponent.CreateAttributes();
      newcomponent.Attributes.Pivot = new System.Drawing.PointF((float) (i * 10 * Math.Cos(i / 2d)), (float) (i * 10 * Math.Sin(i / 2d)));
      doc.Objects.Add(newcomponent);
      newcomponent.ExpireSolution(true);

    }
    this.Component.ExpireSolution(true);   

image

I can recommend using the Metahopper plugin for this. It already has components to instantiate components in the Grasshopper editor. Maybe it helps.

I’m doing this just for fun :smiley:

2 Likes

Now I just wanted to try it out using Metahopper and have some fun too :slight_smile:

fun_with_metahopper.gh (18.2 KB)

1 Like

:smiley: :smiley: :smiley:

ps: no slowdown with 500 instances. Maybe @andheum (author of Metahopper) can point at what the problem is in your code.

1 Like

Still not clickable…

dynamicallygenerate.gh (7.5 KB)

1 Like

Found an answer in the thread below.

Instead of doing

Objects.Add(newcomponent);

, do

AddObject(newcomponent,false);

That’s it.
dynamicallygenerate.gh (6.2 KB)

Thanks Mikity, I’ll give it a try.