In the CSharp component in Grasshopper for Rhino how to use Rhino's or Grasshopper's methods from my own class?

492 views Asked by At

I've got a problem when working with C# Component in Grasshopper. I'm not familiar with it, so I cannot understand the logic. For example, I have introduced a class:

  public class MyClass
  {
    public void TestFoo()
    {
      Print("it works!");
    }
  }

It contains the only method, that has to use the "Print" function and show the string message. But I get an error: Error (CS0038): Cannot access a non-static member of outer type 'Script_Instance' via nested type 'Script_Instance.MyClass' (line 762). As I understand I can't use Rhino's function within my own class. But how can I fix it? Thank you! I ask this question because I need to write the class that contains method of NurbsSurface creation, but I also cannot use Rhino's method doc.Objects.AddSurface...

I tried to implement C# inheritance from Rhino's methods, but it doesn't work...

1

There are 1 answers

0
urbancomputing207 On

The issue with accessing the Script_Instance from your custom class is independent of accessing the Rhino libraries from a custom class.

To access the Script_Instance methods, you can pass the object into your method and add the static tag.

For accessing rhino libraries there is no special inheritance.

The example blow demonstrates both implementations:

C# script component example


  private void RunScript(List<Point3d> PointsList, ref object NurbSrf, ref object SrfArea)
  {
    Print("Example:");
    
    //Access script object from custom class.
    MyClass.TestFoo(this);

    //Access Rhino doc in custom class.
    string docName = NurbsHelpers.ActiveDocName();
    string objectCount = NurbsHelpers.ActiveDocObjectCount();
    Print("doc '" + docName + "' has [" + objectCount + "] objects in it");

    //Use Nurbs Methods in custom class.
    NurbsSurface surface = NurbsHelpers.MakeNurbs(PointsList);
    double area = NurbsHelpers.NurbsArea(surface);

    //Assign script component outputs.
    NurbSrf = surface;
    SrfArea = area;

    //Bake surface into active doc
    NurbsHelpers.bakeNurbSrf(surface);
  }

  // <Custom additional code> 
  public class MyClass
  {
    public static void TestFoo(Script_Instance script)
    {
      script.Print("it works!");
    }
  }
  
  public class NurbsHelpers
  {

    public static NurbsSurface MakeNurbs(List<Point3d> Points){
      NurbsSurface surface = NurbsSurface.CreateFromCorners(
        Points[0],
        Points[1],
        Points[2],
        Points[3]
        );
      return (surface);
    }

    public static double NurbsArea(NurbsSurface surface){
      Brep surfaceBrep = surface.ToBrep();
      return surfaceBrep.GetArea();
    }

    public static string ActiveDocName(){
      return Rhino.RhinoDoc.ActiveDoc.Name;
    }

    public static string ActiveDocObjectCount(){
      return Rhino.RhinoDoc.ActiveDoc.Objects.Count.ToString();
    }

    public static void bakeNurbSrf(NurbsSurface nurbSrf){
      Rhino.RhinoDoc doc = Rhino.RhinoDoc.ActiveDoc;
      doc.Objects.AddSurface(nurbSrf);
    }

  }