GH C# Component tips_入力したValueListの初期値を設定する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void RunScript(int x, int y, ref object A) | |
{ | |
} | |
// <Custom additional code> | |
GH_ComponentParamServer ghCPS; | |
public override void AfterRunScript() | |
{ | |
ghCPS = this.Component.Params; | |
for(int i = 0; i < ghCPS.Input.Count; i++) | |
{ | |
if(ghCPS.Input[i].SourceCount != 0) | |
{ | |
List<string> keyList = new List<string>(); | |
List<string> valueList = new List<string>(); | |
switch(i) | |
{ | |
case 0: | |
keyList.Add("Constant"); | |
keyList.Add("C-Value"); | |
valueList.Add("0"); | |
valueList.Add("1"); | |
SetValueList( | |
i, "Section Mode", keyList, valueList, 1); | |
break; | |
case 1: | |
keyList.Add("そのまま"); | |
keyList.Add("1mm"); | |
keyList.Add("5mm"); | |
valueList.Add("0"); | |
valueList.Add("1"); | |
valueList.Add("5"); | |
SetValueList( | |
i, "Ceiling Rule", keyList, valueList, 2); | |
break; | |
} | |
} | |
} | |
} | |
//AfterRunScript内でValueListを操作するためのメソッド | |
public void SetValueList(int row, string name, | |
List<string> keyList, List<string> valueList, int defaultItem) | |
{ | |
//コンポーネントのパラメーター情報を取得する | |
ghCPS = this.Component.Params; | |
//サンプルデータとして入力が想定されるコンポーネントを | |
//ValueListと決めつけた書き方(他のタイプだとエラーになる) | |
var vallist = (Grasshopper.Kernel.Special.GH_ValueList) | |
ghCPS.Input[row].Sources[0]; | |
//ValueListがデフォルトの名前を持っている場合に書き換えを実行する | |
//ユーザーが変更できるNickNameを判定には使わない | |
if(vallist.Name != name) | |
{ | |
//初期のパラメーターを消去する | |
vallist.ListItems.Clear(); | |
//ValueListに項目を追加するためにListItemを用意する | |
var items = | |
new List<Grasshopper.Kernel.Special.GH_ValueListItem>(); | |
//KeyList、ValueListに入っているデータを全部入れる | |
for(int i = 0; i < keyList.Count; i++) | |
{ | |
items.Add(new Grasshopper.Kernel.Special.GH_ValueListItem( | |
keyList[i], valueList[i])); | |
} | |
vallist.Name = name; | |
vallist.NickName = name; | |
vallist.ListItems.AddRange(items); | |
vallist.ListItems[defaultItem].Selected = true; | |
//以下の処理をしておかないと、挙動がちょっとおかしくなるのでやっておく | |
vallist.ExpireSolution(true); | |
} | |
} | |
// </Custom additional code> |
Author
saitohiroaki1122
commented
Jan 17, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment