Adding component to canvas by name?

Hello
How to add a component by name to the canvas?

This way didn’t work

tofind = Rhino.NodeInCode.Components.FindComponent("MassAddition")
comp = Grasshopper.Instances.ActiveCanvas.Document.FindComponent(tofind.ComponentGuid)

How to get list from NodeInCode.NodeInCodeTable()

table = Rhino.NodeInCode.NodeInCodeTable()
table.GetDynamicMemberNames()

<System.Linq.OrderedEnumerable`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] object at 0x00000000000000C6 [System.Linq.OrderedEnumerable`2[System.String,System.String]]>

I tried with c# but no result

    Rhino.NodeInCode.NodeInCodeTable table = new Rhino.NodeInCode.NodeInCodeTable();
    var enumerator = table.GetDynamicMemberNames().GetEnumerator();
    List<string> names = new List<string>();
    while (enumerator.MoveNext()) 
    {
      names.Add(enumerator.Current);
    }
    A = names;

The method you want to use is Grasshopper.Instances.ActiveCanvas.InstantiateNewObject

2022-12-26 03_00_52-Window
create GH component from name.gh (3.6 KB)

code:

  private void RunScript(string name, bool go)
  {
    if(wasgo & !go){
      id = Rhino.NodeInCode.Components.FindComponent(name).ComponentGuid;
      wasgo = false;
      GrasshopperDocument.ScheduleSolution(5, CreateComponent);
    }else{
      if(go)wasgo = true;
    }
  }
  // <Custom additional code> 
  bool wasgo;
  System.Guid id;
  private void CreateComponent(GH_Document doc){
    Grasshopper.Instances.ActiveCanvas.InstantiateNewObject(id, new System.Drawing.PointF(0, 0), true);
  }

Also:


create GH component from guid.gh (7.1 KB)

1 Like

Thanks @maje90

Yes i found it in another thread and i used it

And this works faster and don’t need SheduleSolution

import Grasshopper as gh
import System
import clr

clr.AddReferenceToFileAndPath(r"C:\Program Files\Rhino 7\Plug-ins\Grasshopper\Components\CurveComponents.gha")
clr.AddReferenceToFileAndPath(r"C:\Program Files\Rhino 7\Plug-ins\Grasshopper\Components\MathComponents.gha")
clr.AddReferenceToFileAndPath(r"C:\Program Files\Rhino 7\Plug-ins\Grasshopper\Components\SurfaceComponents.gha")
clr.AddReferenceToFileAndPath(r"C:\Program Files\Rhino 7\Plug-ins\Grasshopper\Components\TriangulationComponents.gha")
clr.AddReferenceToFileAndPath(r"C:\Program Files\Rhino 7\Plug-ins\Grasshopper\Components\VectorComponents.gha")
clr.AddReferenceToFileAndPath(r"C:\Program Files\Rhino 7\Plug-ins\Grasshopper\Components\XformComponents.gha")
import CurveComponents,MathComponents,SurfaceComponents,TriangulationComponents,VectorComponents,XformComponents

doc = gh.Instances.ActiveCanvas.Document

comp = CurveComponents.Component_Arc()

comp.CreateAttributes()
comp.Attributes.Pivot = System.Drawing.PointF(pt.X*10,-pt.Y*10)
if x:
    doc.AddObject(comp, False)
1 Like