Skip to content

Instantly share code, notes, and snippets.

@saitohiroaki1122
Last active Mar 12, 2018
Embed
What would you like to do?
GH C# Component tips_データ管理にDictionaryを利用する
private void RunScript(object x, ref object A)
{
if(Component.Params.Input[0].SourceCount != 0)
{
Dictionary<string, object> dict = (Dictionary<string, object>) x;
int mode = (int) dict["Section Mode"];
int ceilingRule = (int) dict["Ceiling Rule"];
List<System.Object> list = new List<System.Object>();
list.Add(mode);
list.Add(ceilingRule);
A = list;
}
}
private void RunScript(System.Object dict, string key, ref object outValue, ref object outKey)
{
if(Component.Params.Input[0].SourceCount != 0)
{
Dictionary<string, object> _dict = (Dictionary<string, object>) dict;
//Dictionaryに入っているKeyのリストを取得する
keyList = new List<string>(_dict.Keys);
//ValueListで選択された項目だけ出力する
outValue = _dict[key];
outKey = key;
}
}
// <Custom additional code>
List<string> keyList = new List<string>();
public override void BeforeRunScript()
{
//keyList.Clear();
}
public override void AfterRunScript()
{
GH_ComponentParamServer ghCPS = this.Component.Params;
GetValueList(
ghCPS.Input[0].SourceCount != 0 &&
ghCPS.Input[1].SourceCount == 0, "Result", keyList);
}
public void GetValueList(bool checkInput, string name, List<string> keyList)
{
if(checkInput)
{
//ValueListを新規作成する
var vallist = new Grasshopper.Kernel.Special.GH_ValueList();
vallist.CreateAttributes();
//ValueListを配置する座標を設定する
float widthComponent = (float) this.Component.Attributes.Bounds.X;
float widthVallist = (float) vallist.Attributes.Bounds.Width;
float heightComponent = (float) this.Component.Attributes.Bounds.Bottom;
vallist.Attributes.Pivot =
new PointF(widthComponent - widthVallist - 120, heightComponent - 60);
//初期のパラメーターを消去する
vallist.ListItems.Clear();
//ValueListに項目を追加するためにListItemを用意する
var items = new List<Grasshopper.Kernel.Special.GH_ValueListItem>();
foreach(string item in keyList)
{
string tmpText = "\"" + item + "\"";
items.Add(
new Grasshopper.Kernel.Special.GH_ValueListItem(item, tmpText));
}
vallist.Name = name;
vallist.NickName = name;
vallist.ListMode = Grasshopper.Kernel.Special.GH_ValueListMode.CheckList;
vallist.ListItems.AddRange(items);
vallist.ListItems[0].Selected = true;
//用意したValueListをカンバス上に反映する
GrasshopperDocument.AddObject(vallist, false);
//コンポーネントと接続する
this.Component.Params.Input[1].AddSource(vallist);
vallist.ExpireSolution(true);
}
}
// </Custom additional code>
private void RunScript(int x, int y, ref object A)
{
Dictionary<string, object> dict = new Dictionary<String, object>();
dict.Add("Section Mode", x);
dict.Add("Ceiling Rule", y);
//List<Dictionary<string, object>> wrapDict = new List<Dictionary<string, object>>();
//wrapDict.Add(dict);
GH_ObjectWrapper wrapDict = new GH_ObjectWrapper(dict);
A = wrapDict;
}
@saitohiroaki1122
Copy link
Author

saitohiroaki1122 commented Jan 17, 2018

複数のデータを一つのDictionaryに閉じ込んで出力し、それを受け取ったコンポーネントでDictionaryの中のデータを取得する
Listで取得したいデータを指定できるコンポーネントはDictionary型のデータであれば何でも受け取ることができる
getdictionarydata

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment