Grasshopper

algorithmic modeling for Rhino

Passing dictionaries or sorted lists between components, c#

Hello,

What should the DA.GetDataList call be when passing a dictionary or sorted list into a component? I haven't had much luck.

Standard lists work like this:

if (!DA.GetDataList<RecognizedClassTypeHere>(0, variablename)) { return; }

Dictionaries (as below) don't seem to work...

if (!DA.GetDataList<RecognizedClassTypeHere, RecognizedClassTypeHere>(0, variablename)) { return; }

What should it be?

Views: 1462

Replies to This Discussion

Parameters only support items, lists or trees, you cannot add dictionaries (or other collections such as hashsets, stacks, queues, hashtables) to this.

You can pass dictionaries between components, but you must put those dictionaries inside some type of Goo, and then treat them as items.

When outputting dictionaries:

Dictionary dictionary = MakeADictionary();

IGH_Goo dictionaryGoo = new GH_ObjectWrapper(dictionary);

DA.SetData(0, dictionaryGoo);

When inputting dictionaries:

GH_ObjectWrapper dictionaryGoo = null;

DA.GetData(0, ref dictionaryGoo);

Dictionary dictionary = dictionaryGoo.Value as Dictionary;

if (dictionary == null)

  throw new InvalidCastException("Data wasn't a Dictionary");

(I typed the above without testing any of it, there may be mistakes, I just wanted to show the basic concept of sharing custom data types).

David,

This makes a lot of sense - I will try it out now - a sincere thanks for the speedy reply!

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service