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

(based on Rhino 7 )

在Grasshopper中ShiftList的功能有三個inputs : List , Shift , Wrap 。

三個input給入的資訊(information),經過ShiftList的功能(function)轉換成一組經過位移的List,讓我們處理一組資訊陣列中的資料位置。List中的資訊可以是整數int、浮點數double、或甚至是其他在Rhino環境中常用的幾何資訊。

這篇寫的是如何做出一顆可以處理整數(int) 與 浮點數(double) 功能的C# component。

先看成果:


自製的C# component同樣有三個inputs : List , Shift , Wrap,並且有兩個output : out(功能狀態) , output(輸出shifted list)

除了ShiftList component原來具有的功能之外,我額外新增了一個NOTICE輸出目前功能的設定狀態顯示。原生的ShiftList component在沒有Wrap時如果Shift的位移量超過input List的資料長度就不顯示任何資訊,這個在當scripts接得很複雜時很難一眼看到發生甚麼事情,所以自製的C# component就可以改善狀態不透明這點。


規劃

component 設定

List : List Access > double
Shift : Item Access > int
Wrap : Item Access > boolean




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, int Shift, bool Wrap, ref object output) { List insertList = new List(); //create an empty list for insert list List shiftList = new List(); //create an empty list for result "shift" if(Wrap){ //Wrap = True,我們在此處先進行Wrap的布林值條件判斷,與第103列的else相呼應 //由於Wrap本身就是布林值,True或False,因此就不用再替if condition指定條件。if(Wrap)即可 if(Shift > input.Count){ //判斷Shift的位移量是否超出input list的長度,與90列的else相呼應 //(R75~R84)當shift量>input長度時,用除法計算位移量等於幾倍的input長度,然後送進一個insertList以迴圈先儲存起來 //(R75~R84)此時insertList可以當成是位移的總過程紀錄 //(R85~R87)之後在用迴圈將insertList中的項目從shift的位置開始ADD進用於輸出的shiftList //(R88)然後我再給予一個NOTICE顯示"Wrap = True, Shift > input list length" double Q = Shift / input.Count; double R = Shift - Q * input.Count; for(int round = 0; round < Q * input.Count; round++){ for(int i = 0;i < input.Count;i++){ insertList.Add(input[i]); } } for(int lastround = 0;lastround < R ; lastround++){ insertList.Add(input[lastround]); } for(int i = Shift; i < Shift + input.Count;i++){ shiftList.Add(insertList[i]); } Print("Wrap = True, Shift > input list length"); } else{ for(int i = 0 ; i < input.Count; i++){ insertList.Add(input[i]); } for(int i = 0 ; i < Shift; i++){ insertList.Add(input[i]); } for(int i = Shift; i < insertList.Count; i++){ shiftList.Add(insertList[i]); } Print("Wrap = True, Shift < input list length"); } } else{ //Wrap = False if (Shift > input.Count){ //判斷Shift的位移量是否超出input list的長度,與100列的else相呼應 Print("Wrap = False, You can't shift over list length !!!"); }else{ for(int i = Shift ; i < input.Count; i++){ shiftList.Add(input[i]); } Print("Wrap = False, Shift < input list length"); } } output = shiftList; } // //here to insert some custome functions // }



留言