Grasshopper

algorithmic modeling for Rhino

Hi everybody,

I created a new component in Visual Studio for Grasshopper.

The component gets two inputs, the first one could be a list of NumberSliders and the second input accepts a BooleanTugle.

I wanted to make it in a way when the BooleanTugle is 'true' the values of the sliders start to be changed for 10 times with some random numbers between the lower and upper bounds of the NumberSliders.

Problem: When I run the component in Grasshopper, it does not stop after 10 iterations and continue changing the NumberSliders values till I switch the BooleanTugle on 'false' again.

The code is below.

I would appreciate it if someone could tell me where I am doing wrong.

Thank you very much in advance.

code:

protected override void SolveInstance(IGH_DataAccess DA)
{
bool Run = false;
if (!DA.GetData(1, ref Run)) return;

if (Run == true)
{
ReadSetSliders();
}
}

public void ReadSetSliders()
{
List<GH_NumberSlider> sliders = new List<GH_NumberSlider>();
foreach (IGH_Param src in Params.Input[0].Sources)
{
if (src.GetType() == typeof(GH_NumberSlider))
{
GH_NumberSlider nslider = (GH_NumberSlider)src;
sliders.Add(nslider);
}
}

double[] LowerBand = new double[sliders.Count];
double[] UpperBand = new double[sliders.Count];

for (int i = 0; i < sliders.Count; i++)
{
LowerBand[i] = (double)sliders[i].Slider.Minimum;
UpperBand[i] = (double)sliders[i].Slider.Maximum;
}

Random rnd = new Random();

for (int i = 0; i < 10; i++)
{
for (int j = 0; j < sliders.Count; j++)
{
decimal newVal = (decimal)(LowerBand[j] + ((UpperBand[j] - LowerBand[j]) * rnd.NextDouble()));
GH_NumberSlider bslider = (GH_NumberSlider)Params.Input[0].Sources[j];
bslider.Slider.Value = newVal;
}
}
}

Views: 640

Replies to This Discussion

You are not allowed to modify sliders during a solution. An added problem here is that these new solutions you trigger will also trigger your component to run again, thus getting stuck in a recursive spiral.

Updates to the state of the document have to happen 'from the outside'. This is usually accomplished in one of two ways:

  1. A UI event handler, for example a button or menu click. This involves creating custom UI on your part. Not necessarily a lot of work, but more work than not.
  2. A solution schedule with registered callbacks. You can schedule new solutions on the GH_Document instance to happen N milliseconds after the current solution completes. Then, before this scheduled solution starts your callback method is invoked and this is where you modify the sliders.

Thank you very much David.

since I need to use the new component in a bigger script, it has to be in a way that could be run without any extra menu or user clicks. Then, I think the second solution would be a better option for me.

But I did not understand how should I do that; would you please show me in a simple example how I can do that.

Thank you very much in advance.

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service