Getting and setting values to GH Player’s input components with C#

I’m trying to achieve something similar to the Hops component in Grasshopper within my own C# code. I would like to detect input components, change their values and display the geometry without opening the Grasshopper or running the GH Player.
I found out how to get access to the instance of the GH_Document from the file and I figured out how to detect the input components. I started to play around with the “Get Number” and I have some difficulties reading and setting the values. For example, I can detect how many instances of the component there are in the GH Canvas and I can read properties such as Prompt, AtLeast and AtMost. Unfortunately, I’m not sure how to get other values such as Minimum, Maximum, Presets and the currently stored values. In the picture below, number of values is 0, even with the Sliders.


image

So, I’m wondering what is the best approach here? I’m missing something when I try to read the values and I’m not sure at the moment how to set them. Also, what is the best practice for retrieving the geometry – can I simply use the CollectData method and access it using the VolatileData property?

This is the code that I have at the moment:

public static GH_Document GHDocumentFromPath(string filePath)
{
if (string.IsNullOrEmpty(filePath) || string.IsNullOrWhiteSpace(filePath))
return null;

GH_DocumentIO io = new GH_DocumentIO();
io.Open(filePath);
GH_Document ghDoc = io.Document;

return ghDoc;
}

public static void FindContextualComponents_GetNumber(GH_Document ghDoc, out List<ContextualComponents.GetNumberParameter> cxContainers)
{
List<IGH_ActiveObject> allActiveObjects = ghDoc.ActiveObjects();

cxContainers = new List<ContextualComponents.GetNumberParameter>();

foreach (IGH_ActiveObject ao in allActiveObjects)
{
if (ao is ContextualComponents.GetNumberParameter)
{
ContextualComponents.GetNumberParameter cx = ao as ContextualComponents.GetNumberParameter;

if (cx != null)
cxContainers.Add(cx);
}
}
}

public static void GetValues_GetNumber(ContextualComponents.GetNumberParameter gn,
            out string prompt, out int countMin, out int countMax, out List<double> numbers)
        {
            prompt = gn.Prompt;
            countMin = gn.AtLeast;
            countMax = gn.AtMost;
            //double min = gn. //TODO: get min value
            //double max = gn. //TODO: get max value
            //List<double> presets = gn. //TODO: get presets

            numbers = new List<double>();

            DataTree<GH_Number> ghNumbers = gn.ContextualDataTree;

            if (ghNumbers != null && ghNumbers.BranchCount > 0)
                foreach (List<GH_Number> list in ghNumbers.Branches)
                    foreach (GH_Number item in list)
                        numbers.Add(item.Value);
        }

Thank you in advance!
Ognjen

1 Like

Does something like this work for you? I’ve modified your GetValues_GetNumbers method.

public static void GetValues_GetNumber(IGH_ContextualParameter gn,
    out string prompt, out int atLeast, out int atMost, out List<object> numbers)
  {
    prompt = gn.Prompt;
    atLeast = gn.AtLeast;
    atMost = gn.AtMost;
    var data = new List<GH_Number>();
    data.Add(new GH_Number(3.0));
    //setter
    gn.AssignContextualData(data);
    //getter
    numbers = gn.ContextualData.ToList();
  }
2 Likes

Hello Andy,
Yes, this worked! Thank you very much!

However, I had difficulties simply reading the values before using the AssignContextualData method. So, If understand this correctly, I need to assign some values to the GetNumber in the first place and then read them. It is not possible to simply get the values before assigning anything (when there are sliders), or is it?

Also, it would be really helpful if you could tell me how to get other properties of this component such as Minimum and Maximum. I tried to find the documentation for this, but at the moment I’m simply exploring everything within Visual Studio with the autocomplete.

Thank you again!
Ognjen

1 Like

Hello, I am also very interested in getting AND setting the values of Contextual Parameters. I noticed your reply didn’t address Ognjen’s desire to get the min/max values or any other value besides the prompt/atleast/atmost, but your code worked for him as an answer.

I want to get and set other values inside of Contextual Parameters. I have tried with iterating through the toolstripdropdown on the parameter, I am currently trying with Reflection (to no avail). I would appreciate really any help on this matter. I have made a thread here: perhaps you can take a look