Hello, I was wondering if there is a way to programatically (C#) collect the numeric data from a given input? For example, you can obtain an input source’s nickname using the following expression:
this.Component.Params.Input[0].Sources[0].NickName
Is there a way to do something like this?
this.Component.Params.Input[0].Sources[0].Value?
I realize that it’s probably not that simple since the input components could be of different types (see image below) but I was wondering if there was a general way to (1) determine if the input value is a number, and (2) if so, what’s its value? Can someone point me in the right direction?
Thank you for your answer! It does seem to work but is there a way to convert the resulting IGH_StructureEnumerator data type to a regular numeric data type (double, float, decimal,…) so that I can use the resulting number in other calculations?
private void RunScript(DataTree<int> x, ref object A)
{
var ghNumbers = Component.Params.Input[0].Sources[1].VolatileData.AllData(false);
var numbers = new List<int>();
foreach(var ghNumber in ghNumbers)
{
int number;
if (GH_Convert.ToInt32(ghNumber, out number, GH_Conversion.Both))
numbers.Add(number);
}
A = numbers;
}
Hi @sofia.feist in that case you’ll need to iterate the IGH_StructureEnumerator and cast the IGH_Goo to the input type. Here’s an example VolatileData.gh (5.1 KB)
private void RunScript(List<int> x, ref object A)
{
int integer = new int();
foreach(var item in Component.Params.Input[0].Sources[0].VolatileData.AllData(false)){
item.CastTo(out integer);
}
Print(integer.GetType().ToString()); // returns System.Int32
A = integer;
}
If you want to collect just all the data in input, it’s Component.Params.Input[0].VolatileData.
If you cast with “as”, test for nullity. Casting can fail, and will if you have variable data types, you would want to handle those cases somehow (Ignore bad data types? Throw an exception?)
You might want to test what happens if you internalise data in the parameter