Grasshopper

algorithmic modeling for Rhino

Basically what I am trying to accomplish is to create a component that reads an external settings text file, creates a dictionary from it and makes that Settings dictionary with its values accessible to other classes in the add-on.  I'm getting stuck because everything seems to be enclosed in the SolveInstance function and I'm not entirely sure what can be changed without creating a problem for the input/ output functions of the component.

Currently I have:

protected override void SolveInstance(IGH_DataAccess DA)
{


List<string> inputList = new List<string>();


if (!DA.GetDataList(0, inputList)) return;

inputList.ToArray();
char splitChar = '=';
Dictionary<string, string> Settings = new Dictionary<string, string>();

for (int i = 0; i<inputList.Length; i++)
{
string[] parts = inputList[i].Split(splitChar);
string dkey = parts[0];
string dvalue = parts[1];
Settings.Add(dkey,dvalue);

}

DA.SetData(0, Settings);

}

What is the best way to make the data collected in the SolveInstance function in the template GH Addon Class accessible another class? Can you change it from being protected override to a public something without causing problems?

Views: 2333

Replies to This Discussion

And by other end I mean, the input of the Setting User Component.

Ok, so at least you're storing the dictionary. The problem must be on the other end. I just tested this and there is a casting omission in GH_ObjectWrapper. It fails to cast if the data it wraps around is not an IGH_Goo type. 

You can fix this by getting the GH_ObjectWrapper and then looking at the Value field:

GH_ObjectWrapper wrapper = null;

if (!DA.GetData(0, ref wrapper)) { return; }

Dictionary<string,string> dic = wrapper.Value as Dictionary<string,string>;

if (dic == null) { return; }

--

David Rutten

david@mcneel.com

Poprad, Slovakia

It works but throws the error for an incorrect dictionary format "The Settings Dictionary must be a valid dictionary" (which is what I wrote in mine). 

Not sure if I understood the question correctly, but doesn't it work if you declare the Settings variable outside the Solve Instance method? I mean, something like:

public MyClass: GH_Component

{
    Dictionary<string, string> Settings; //make this public or use get() set() or your own method so that it is accessible to other classes

 

    //MyClass constructor
    //GH_Component methods, i.e., RegisterInputParam(), SolveInstance(), etc

}

 

Cheers,

Sridevi

Sridevi,

I thought the fact that SolveInstance was a protected override meant that it couldn't be accessed from outside the method?  Not sure I understand entirely what protected means though.

private = only accessible from within a class/structure

protected = only accessible from within a class/structure or any class which derives from it

friend/internal = only accessible from within the same assembly

public = accessible to everyone, everywhere

--

David Rutten

david@mcneel.com

Poprad, Slovakia

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