IGH_InitCodeAware doesn't work as expected?

Hi,
I want to search in the rh o gh forum from the canvas search box by typing:

forum gh textToSearch

or

forum rh textToSearch

but my code doesn’t work and I can’t figure out why.

  public class SearchOnForumComponent : GH_Component , IGH_InitCodeAware{
    string initcode;
    public SearchOnForumComponent() : base("Forum Search", "Forum", "Search on gh or rh forum", ComponentsUtil.Category, ComponentsUtil.Subcategory) {  }
    public override Guid ComponentGuid => new Guid("faa8fe19-51d8-4617-8e4b-2cfabf84dbf4"); 
    public void SetInitCode(string code) {
        try
        {
            initcode = code;
            Rhino.RhinoApp.WriteLine("code is: " + code); //Not shown in rhino
            if (code.StartsWith("forum", StringComparison.OrdinalIgnoreCase))
            { 
                code = code.Remove(0, 5).Replace(" ", "");
                Rhino.RhinoApp.WriteLine(code);
                if (code.StartsWith("gh", StringComparison.OrdinalIgnoreCase))
                {
                    code = code.Remove(0, 2);
                    Rhino.RhinoApp.WriteLine(code);
                    System.Diagnostics.Process.Start(
                        string.Format("http://www.grasshopper3d.com/page/search-results?site=&q={0}+site%3Agrasshopper3d.com", code)
                        );
                    Rhino.RhinoApp.WriteLine("GH ");
                }
                else if (code.StartsWith("rh", StringComparison.OrdinalIgnoreCase))
                {
                    code = code.Remove(0, 2);
                    Rhino.RhinoApp.WriteLine(code);
                    System.Diagnostics.Process.Start(
                        string.Format("https://discourse.mcneel.com/search?q={0}", code)
                        );
                }
                else {
                    Rhino.RhinoApp.WriteLine("NO");
                }
            }
        }
        catch (Exception e)
        {
            Rhino.RhinoApp.WriteLine(e.ToString());
        }
    } 
    protected override void RegisterInputParams(GH_InputParamManager pManager)
    { 
    }

    protected override void RegisterOutputParams(GH_OutputParamManager pManager)
    { 
    }

    protected override void SolveInstance(IGH_DataAccess DA)
    {
        Rhino.RhinoApp.WriteLine("solveinstance code: " + initcode);
    }
}

The only thing the rhino history shows is:

solveinstance code:

But the component appears in the search as the first option. It’s also instantiated.

What am I doing wrong?

Another thing is that I want to remove the component when the search is performed. In what context should I do this? In SetInitCode()? in AddedToDocument()? in SolveInstance()?

Thank you.

1 Like

Copy pasting your class and running it here seems to work fine. If I type Forum = ...... into the search box then I get a string containing ...... in the SetInitCode method. It won’t work without the equals symbol.

I’d also recommend using the simplest object possible since you’re not doing any calculations. Just derive from GH_DocumentObject.

You can delete your object from the document inside the AddedToDocument method. Seems like the best way.

using System;
using Grasshopper.Kernel;

namespace Components
{
  internal class SearchObjectAttributes : GH_Attributes<SearchOnForumObject>
  {
    public SearchObjectAttributes(SearchOnForumObject owner)
      : base(owner)
    { }
  }
  public class SearchOnForumObject : GH_DocumentObject, IGH_InitCodeAware
  {
    public SearchOnForumObject()
      : base("Forum Search", "Forum", "Search on gh or rh forum", "Search", "Search") { }
    public override void CreateAttributes()
    {
      m_attributes = new SearchObjectAttributes(this);
    }
    public override Guid ComponentGuid => new Guid("faa8fe19-51d8-4617-8e4b-2cfabf84dbf4");

    private const string UrlRhino = "https://discourse.mcneel.com/search?q={0}";
    private const string UrlGrasshopper = "http://www.grasshopper3d.com/page/search-results?site=&q={0}+site%3Agrasshopper3d.com";

    public void SetInitCode(string code)
    {
      code = code.Trim();
      string url = UrlRhino;
      if (code.ToUpperInvariant().StartsWith("GH"))
      {
        url = UrlGrasshopper;
        code = code.Substring(2);
      }
      else if (code.ToUpperInvariant().StartsWith("RH"))
        code = code.Substring(2);

      // You might want to http-escape the code string here...
      url = string.Format(url, code.Trim());

      try
      {
        System.Diagnostics.Process.Start(url);
      }
      catch (Exception e)
      {
        Rhino.RhinoApp.WriteLine(e.ToString());
      }
    }

    public override void AddedToDocument(GH_Document document)
    {
      document.RemoveObject(this, true);
    }
  }
}

Thank you :heart: