在Rhino.inside.Revit手册中,c#开发提供了两个案例,其中有个案例我没有成功。

这个案例提示了这个错误。


源码如下

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

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

    if(BREP != null) {
      // create a color object. modify the logic as you wish
      var color = System.Drawing.Color.FromName("DeepSkyBlue");

      // now create the material using the node-in-code
      // note that just like the Grasshopper component, the node-in-code also
      // takes 3 inputs in the exact same order (top to bottom)
      var newMaterials = addMaterial.Invoke("Sky Material", true, color);

      // and now use the AddGeometryDirectShape node-in-code to
      // create the DirectShape element in Revit
      // Notes:
      //    - BREP is our input Brep object
      //    - new_materials is a list of new materials so we are grabbing the first element
      //    - get_category is a function that finds a Revit category from its name
      var dsElements = addGeomDirectshape.Invoke
        (
        "Custom DS",
        GetCategory("Walls"),
        BREP,
        newMaterials[0]
        );

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


  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;
  }


请使用这个功能添加代码块,帖子帮你编辑好了

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

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

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

    if(BREP != null) {
      // create a color object. modify the logic as you wish
      var color = System.Drawing.Color.FromName("DeepSkyBlue");

      // now create the material using the node-in-code
      // note that just like the Grasshopper component, the node-in-code also
      // takes 3 inputs in the exact same order (top to bottom)
      var newMaterials = addMaterial.Invoke("Sky Material", true, color);

      // and now use the AddGeometryDirectShape node-in-code to
      // create the DirectShape element in Revit
      // Notes:
      //    - BREP is our input Brep object
      //    - new_materials is a list of new materials so we are grabbing the first element
      //    - get_category is a function that finds a Revit category from its name
      var dsElements = addGeomDirectshape.Invoke
        (
        "Custom DS",
        GetCategory("Walls"),
        BREP,
        newMaterials[0]
        );

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


//方法
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;
  }

案例的代码很完整
你没有贴完整
从第一行using开始截取完整

using System;
using System.Collections;
using System.Collections.Generic;

using Rhino;
using Rhino.Geometry;

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

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

/// <summary>
/// This class will be instantiated on demand by the Script component.
/// </summary>
public class Script_Instance : GH_ScriptInstance
{
#region Utility functions
  /// <summary>Print a String to the [Out] Parameter of the Script component.</summary>
  /// <param name="text">String to print.</param>
  private void Print(string text) { /* Implementation hidden. */ }
  /// <summary>Print a formatted String to the [Out] Parameter of the Script component.</summary>
  /// <param name="format">String format.</param>
  /// <param name="args">Formatting parameters.</param>
  private void Print(string format, params object[] args) { /* Implementation hidden. */ }
  /// <summary>Print useful information about an object instance to the [Out] Parameter of the Script component. </summary>
  /// <param name="obj">Object instance to parse.</param>
  private void Reflect(object obj) { /* Implementation hidden. */ }
  /// <summary>Print the signatures of all the overloads of a specific method to the [Out] Parameter of the Script component. </summary>
  /// <param name="obj">Object instance to parse.</param>
  private void Reflect(object obj, string method_name) { /* Implementation hidden. */ }
#endregion

#region Members
  /// <summary>Gets the current Rhino document.</summary>
  private readonly RhinoDoc RhinoDocument;
  /// <summary>Gets the Grasshopper document that owns this script.</summary>
  private readonly GH_Document GrasshopperDocument;
  /// <summary>Gets the Grasshopper script component that owns this script.</summary>
  private readonly IGH_Component Component;
  /// <summary>
  /// Gets the current iteration count. The first call to RunScript() is associated with Iteration==0.
  /// Any subsequent call within the same solution will increment the Iteration count.
  /// </summary>
  private readonly int Iteration;
#endregion

  /// <summary>
  /// This procedure contains the user code. Input parameters are provided as regular arguments,
  /// Output parameters as ref arguments. You don't have to assign output parameters,
  /// they will have a default value.
  /// </summary>
  private void RunScript(object BREP, ref object DS)
  {
    // AddMaterial component to create a material
    var addMaterial = Components.FindComponent("RhinoInside_AddMaterial");
    // and AddGeometryDirectShape to create a DirectShape element in Revit
    var addGeomDirectshape = Components.FindComponent("RhinoInside_AddGeometryDirectShape");

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

    if(BREP != null) {
      // create a color object. modify the logic as you wish
      var color = System.Drawing.Color.FromName("DeepSkyBlue");

      // now create the material using the node-in-code
      // note that just like the Grasshopper component, the node-in-code also
      // takes 3 inputs in the exact same order (top to bottom)
      var newMaterials = addMaterial.Invoke("Sky Material", true, color);

      // and now use the AddGeometryDirectShape node-in-code to
      // create the DirectShape element in Revit
      // Notes:
      //    - BREP is our input Brep object
      //    - new_materials is a list of new materials so we are grabbing the first element
      //    - get_category is a function that finds a Revit category from its name
      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;
  }
  // </Custom additional code> 
}

提示的意思是现在只需要2个输入端,你给了3个

如果案例是3个参数,但是实际上他只要2个,可能是新版的inside里面add material运算器做了修改
将参数从3个改成了2个
而案例还没改过来

你可以尝试GH内拉出inside的Add material运算器,看看是不是输入端成了2个

收到!感谢大神提示 :smiley: