GH_Panel.UserText

Hi,
I’m trying to modify GH_Panel UserText in C#. I’m able to achieve that by Python but not in C#, probably because of the wrong data type (Error CS0266).

foreach( var obj in GrasshopperDocument.Objects)
{
  if (obj.GetType().ToString() == "Grasshopper.Kernel.Special.GH_Panel")
  {
    if (obj.NickName == "Cs")
    {
      Print("Found " + obj.ToString() + " panel");
      Grasshopper.Kernel.Special.GH_Panel panel = obj;
      panel.UserText = "Text inserted by C#";
      panel.ExpireSolution(true);
    }
  }
}

Thanks

GH_panel_UserText.gh (9.6 KB)

1 Like
Grasshopper.Kernel.Special.GH_Panel panel = (Grasshopper.Kernel.Special.GH_Panel)obj

Remove the Print statement

1 Like

You can also pack things in to methods an do many more things


{

    ModifyPanels(nickName, text, type, run, reset);

  }

  // <Custom additional code> 

  Random ran = new Random();
  const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  public void ModifyPanels(List<string> nickNames, string text, int type, bool run, bool reset)
  {

    string idle = "";
    foreach( var obj in GrasshopperDocument.Objects)
    {
      if(obj is Grasshopper.Kernel.Special.GH_Panel)
      {
        Grasshopper.Kernel.Special.GH_Panel panel = obj as Grasshopper.Kernel.Special.GH_Panel;

        for (int i = 0; i < nickNames.Count; i++)
        {
          if(panel.NickName == nickNames[i])
          {
            if(type == 0 && reset)
            {
              panel.UserText = "";
              panel.ExpireSolution(true);
            }

            else if(type == 0 && run)
            {
              panel.UserText = RandomString(ran.Next(5, 20));
              panel.ExpireSolution(true);
            }

            else if(type == 1 && reset)
            {
              panel.UserText = "";
              panel.ExpireSolution(true);
            }

            else if(type == 1 && run)
            {
              panel.UserText = text;
              panel.ExpireSolution(true);
            }

            else if(type == 1 && !run && !reset)
            {
              idle = "I need something to do!";
              break;
            }

            else if(type == 0 && !run && !reset)
            {
              idle = "I need something to do!";
              break;
            }

            else
              throw new ArgumentException("Parameter not defined!");
          }
        }

      }

    }
    
    Print(idle);

  }


  public  string RandomString(int length)
  {
    return new string(Enumerable.Repeat(chars, length)
      .Select(s => s[ran.Next(s.Length)]).ToArray());
  }

C#WriteToGHPanels.gh (10.1 KB)

3 Likes

Hi All, what if i wish to read panel data?

private void RunScript(string PanelNickName, ref object UserTexts)
  {
    var texts = new List<string>();

    foreach (var obj in GrasshopperDocument.Objects)
    {
      if (obj.GetType() != typeof(Grasshopper.Kernel.Special.GH_Panel)
        || obj.NickName != PanelNickName)
        continue;
      texts.Add(((Grasshopper.Kernel.Special.GH_Panel) obj).UserText);
    }

    UserTexts = texts;
  }

Fun exercise!

1 Like