NodeInCode Example in RIR

I am following the C# scripting instruction on the official webpage.
https://www.rhino3d.com/inside/revit/1.0/guides/rir-csharp

in the example section, there’s one example illustrates how we can take the advantage from NodeInCode and to rearrange RhinoInside Components in C# code.

The problem I am facing is that I don’t know how to get it work, if there’re some inputs missing.

For example,

I want to implement the same logic in C# scripting.


private void RunScript(Brep BREP, object y, ref object DS)
  {
    // AddMaterial component to create a material
    var queryMaterials = Components.FindComponent("RhinoInside_QueryMaterials");
    // and AddGeometryDirectShape to create a DirectShape element in Revit
    var addGeomDirectshape = Components.FindComponent("RhinoInside_AddGeometryDirectShape");

    if (queryMaterials == null || addGeomDirectshape == null) {
      throw new Exception("One or more of the necessary components are not available as node-in-code");
    }

    if(BREP != null) {
      var newMaterials = queryMaterials.Invoke("", "Glass", null);

      var dsElements = addGeomDirectshape.Invoke(
        "Custom DS",
        GetCategory("Walls"),
        BREP,
        newMaterials[0]
        );

      // assign the new DirectShape element to output
      DS = dsElements[0];
    }

  }

  // <Custom additional code> 
  public DB.Category GetCategory(string categoryName){
    var doc = RIR.Revit.ActiveDBDocument;
    foreach(DB.Category cat in doc.Settings.Categories) {
      if (cat.Name == categoryName)
        return cat;
    }
    return null;
  }

I think it’s because I missed the first and third input for queryMaterial node, and I currently don’t know how can I tackle it.

Any suggestion will be helpful. Thanks.
NodeInCodeQuestino.gh (9.8 KB)

I noticed that the example given in the official page using ComponentFunctionInfo.Invoke() but somehow I make it work with ComponentFunctionInfo.Evavluate(). And the following code is the result I had. Works in my local machine.

private void RunScript(Brep BREP, ref object DS)
  {


    // AddMaterial component to create a material
    var queryMaterials = Components.FindComponent("RhinoInside_QueryMaterials");
    // and AddGeometryDirectShape to create a DirectShape element in Revit
    var addGeomDirectshape = Components.FindComponent("RhinoInside_AddGeometryDirectShape");

    if (queryMaterials == null || addGeomDirectshape == null) {
      throw new Exception("One or more of the necessary components are not available as node-in-code");
    }


    if(BREP != null) {
      string[] warns;

      object[] newMaterials = queryMaterials.Evaluate(
        new object[]{
        "",
        "Glass",
        null},
        false,
        out warns);



      string[] warnings;

      object[] dsElements = addGeomDirectshape.Evaluate(
        new object[]{
        "Custom DS",
        GetCategory("Walls"),
        BREP,
        newMaterials[0]
        },
        false,
        out warnings
        );


      // assign the new DirectShape element to output
      DS = dsElements[0];
    }

  }

  // <Custom additional code> 
  public DB.Category GetCategory(string categoryName){
    var doc = RIR.Revit.ActiveDBDocument;
    foreach(DB.Category cat in doc.Settings.Categories) {
      if (cat.Name == categoryName)
        return cat;
    }
    return null;
  }

So I am using Rhino8 and Revit2021, I think the component name are different

// Grasshopper Script Instance

#r "RevitAPI.dll"
#r "RevitAPIUI.dll"
#r "RhinoInside.Revit.dll"
// r "RevitAPI.DB"
#region Usings
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;

using Rhino;
using Rhino.Geometry;

using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;

using ARDB = Autodesk.Revit.DB;
using UI = Autodesk.Revit.UI;
using RIR = RhinoInside.Revit;
using RhinoInside.Revit.Convert.Geometry;
using NIC = Rhino.NodeInCode;

#endregion

public class Script_Instance : GH_ScriptInstance
{
    #region Notes
    /* 
      Members:
        RhinoDoc RhinoDocument
        GH_Document GrasshopperDocument
        IGH_Component Component
        int Iteration

      Methods (Virtual & overridable):
        Print(string text)
        Print(string format, params object[] args)
        Reflect(object obj)
        Reflect(object obj, string method_name)
    */
    #endregion

    private void RunScript(Brep BREP, object y, ref object a, ref object DS)
    {
         // AddMaterial component to create a material
        var queryMaterials = NIC.Components.FindComponent("RhinoInside_QueryMaterials");
        // and AddGeometryDirectShape to create a DirectShape element in Revit
        var addGeomDirectshape = NIC.Components.FindComponent("RhinoInside_AddDirectShapeGeometry");

        if (queryMaterials == null || addGeomDirectshape == null) {
        throw new Exception("One or more of the necessary components are not available as node-in-code");
        }


        if(BREP != null) {
        string[] warns;

        object[] newMaterials = queryMaterials.Evaluate(
            new object[]{
            "",
            "Glass",
            null},
            false,
            out warns);



        string[] warnings;

        object[] dsElements = addGeomDirectshape.Evaluate(
            new object[]{
            "Custom DS",
            GetCategory("Walls"),
            BREP,
            newMaterials[0]
            },
            false,
            out warnings
            );


        // assign the new DirectShape element to output
        DS = dsElements[0];
    }

    
}

public ARDB.Category GetCategory(string categoryName){
        var doc = RIR.Revit.ActiveDBDocument;
        foreach(ARDB.Category cat in doc.Settings.Categories) {
        if (cat.Name == categoryName)
            return cat;
        }
        return null;
    }
}

works.

We can check the name of components by

 a = Rhino.NodeInCode.Components.NodeInCodeFunctions.GetDynamicMemberNames();
1 Like