GH C# - 利用C# component做出reverseList function

 (based on Rhino 7 , 2022.9)

在Grasshopper中ReverseList的功能有一個inputs : List。

這篇寫的是如何做出一顆可以處理整數(int) 與 浮點數(double) 功能的C# component,並且給予component正在運作的狀態題示,讓我們在scripting時方便閱讀與除錯。

這一顆寫起來蠻簡單的,是for迴圈的應用。

先看成果:



規劃

component 設定

input : List Access > double
reverse : Item Access > bool



C# script




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;



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

#region Members
  /// Gets the current Rhino document.
  private readonly RhinoDoc RhinoDocument;
  /// Gets the Grasshopper document that owns this script.
  private readonly GH_Document GrasshopperDocument;
  /// Gets the Grasshopper script component that owns this script.
  private readonly IGH_Component Component;
  /// 
  /// 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.
  /// 
  private readonly int Iteration;
#endregion

  /// 
  /// 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.
  /// 
  
  //--------------------------------------(主要內容於此處開始)-----------------------------------------------

  private void RunScript(List input, bool reverse, ref object output)
  {
  
    List reverseList = new List();
    switch(reverse)
    {

      case true:
        //input code here;
        for(int i = 0 ;i < input.Count ;i++){
          reverseList.Add(input[input.Count - i - 1]);
          //Print("running");
        }
        output = reverseList;
        Print("List is reversed.");
        break;

      case false:
        //input code here;
        output = input;
        Print("List remains origin sequence.");
        break;

    }



  }

  //  

  //  
}

留言