Insert output parameter automatically

Hello,
There’s a way to add “output parameters” to list item component, in automatically way (for e.g. via slider component or something like that)?

Thank you

Yep

private void RunScript(object listItem, int n)
  {
    Grasshopper.Kernel.IGH_Component lItem = listItem as Grasshopper.Kernel.IGH_Component;

    int count = lItem.Params.Output.Count;

    if (n <= 0)
    {
      n = 1;
    }

    int paramdiff = count - n;

    for (int i = 0; i < paramdiff; i++)
    {
      lItem.Params.UnregisterOutputParameter(lItem.Params.Output[lItem.Params.Output.Count - 1]);

    }

    for (int i = 0; i < -paramdiff; i++)
    {
      Grasshopper.Kernel.Parameters.Param_GenericObject ighp = new Grasshopper.Kernel.Parameters.Param_GenericObject();
      ighp.Name = "Item +" + (count + i);
      ighp.NickName = "+" + (count + i);
      lItem.Params.RegisterOutputParam(ighp);
    }

    lItem.ExpireSolution(true);

  }

AutoListItem.gh (6.1 KB)

2 Likes

Thank you @magicteddy,
It works perfect!.
I would like to know if it is possible to generate an automatic panel component as well as the example below


Thank you again

Hi again,

Yes, it’s possible. Problem is you can’t really control the position if there are already components in the way, and there is also a small bug when outputs are deleted - that may be solvable by deleting the wires… - Edit, ah it’s not. Scheduling solution could a way…

I think a better approach would be to build a custom component that you could call from the canvas using LI=X and that would instantiate a List Item with X outputs and X panels.

Like these funny things I made because I was tired of inserting the same groups of components :

private void RunScript(object listItem, int n)
  {
    Grasshopper.Kernel.IGH_Component lItem = listItem as Grasshopper.Kernel.IGH_Component;

    int count = lItem.Params.Output.Count;

    if (n <= 0)
    {
      n = 1;
    }

    int paramdiff = count - n;

    for (int i = 0; i < paramdiff; i++)
    {
      lItem.Params.UnregisterOutputParameter(lItem.Params.Output[lItem.Params.Output.Count - 1]);

    }

    for (int i = 0; i < -paramdiff; i++)
    {
      Grasshopper.Kernel.Parameters.Param_GenericObject ighp = new Grasshopper.Kernel.Parameters.Param_GenericObject();
      ighp.Name = "Item +" + (count + i);
      ighp.NickName = "+" + (count + i);
      lItem.Params.RegisterOutputParam(ighp);
    }

    lItem.ExpireSolution(true);
    lItem.Attributes.ExpireLayout();


    Grasshopper.Instances.ActiveCanvas.Refresh();

    for (int i = 0; i < lItem.Params.Output.Count; i++)
    {
      bool has_panel = false;
      IGH_Param ighp = lItem.Params.Output[i];
      foreach (IGH_DocumentObject ighdoc in ighp.Recipients)
      {
        if (ighdoc is Grasshopper.Kernel.Special.GH_Panel)
        {
          has_panel = true;
          break;
        }
      }

      //insert Panel
      if (!has_panel)
      {


        IGH_DocumentObject panel = Grasshopper.Instances.ComponentServer.EmitObject(Grasshopper.Kernel.Special.GH_Panel.PanelID);
        Grasshopper.Kernel.Special.GH_Panel Panel = panel as Grasshopper.Kernel.Special.GH_Panel;

        GrasshopperDocument.AddObject(Panel, false, GrasshopperDocument.ObjectCount);

        Panel.Attributes.Pivot = new System.Drawing.PointF(ighp.Attributes.Pivot.X + 100, ighp.Attributes.Pivot.Y - 25);
        Panel.Attributes.Bounds = new System.Drawing.RectangleF(ighp.Attributes.Pivot.X + 100, ighp.Attributes.Pivot.Y - 25, 100, 50);
        Panel.AddSource(ighp);
        //Panel.ExpireSolution(true);
        Panel.Attributes.ExpireLayout();
      }

    }

    Grasshopper.Instances.ActiveCanvas.Refresh();

  }

AutoListItem.gh (12.9 KB)

3 Likes

Hi @magicteddy,
thank you for your component! Do you think you can fix the bug when deleting items?
Thank you again