Hey !
I am trying to create multiple sliders based based upon an integer input. I have modified James -ramden code:.
The problem is it works fine on the first attempt, but the number of sliders increases exponentially , I want them to erase the old sliders and generate new ones according to the integer input.
Here is my modified code.
private void RunScript(bool x, object y, int z, ref object A)
{
Random rnd = new Random();
int i = 0;
if(x)
{
for(i = 0;i < z;i++)
{
//instantiate new slider
Grasshopper.Kernel.Special.GH_NumberSlider slid = new Grasshopper.Kernel.Special.GH_NumberSlider();
slid.CreateAttributes(); //sets up default values, and makes sure your slider doesn't crash rhino
//customise slider (position, ranges etc)
int inputcount = this.Component.Params.Input[1].SourceCount;
slid.Attributes.Pivot = new System.Drawing.PointF((float) this.Component.Attributes.DocObject.Attributes.Bounds.Left - slid.Attributes.Bounds.Width - 30, (float) this.Component.Params.Input[1].Attributes.Bounds.Y + inputcount * 30);
slid.Slider.Maximum = 10;
slid.Slider.Minimum = 0;
slid.Slider.DecimalPlaces = 2;
slid.SetSliderValue((decimal) (rnd.Next(1000) * 0.01));
//Until now, the slider is a hypothetical object.
// This command makes it 'real' and adds it to the canvas.
GrasshopperDocument.AddObject(slid, false);
//Connect the new slider to this component
this.Component.Params.Input[1].AddSource(slid);
}
}
In addition to this, lets say I have a component that has 3 inputs and input 2 has two sliders connected to it. I want to delete these sliders when solution is generated. I want to do this because so that I can add new sliders to input 2 for another solution- i will plugin the code for generating new sliders which would get connected to input 2 automatically.
private void RunScript(bool x, List<object> y, int z)
{
Random rnd = new Random();
if(x)
{
if(this.Component.Params.Input[1].SourceCount > 0)
{
List<IGH_Param> sources = new List<IGH_Param>(this.Component.Params.Input[1].Sources);
GrasshopperDocument.RemoveObjects(sources, false);
}
for(int i = 0;i < z;i++)
{
//instantiate new slider
Grasshopper.Kernel.Special.GH_NumberSlider slid = new Grasshopper.Kernel.Special.GH_NumberSlider();
slid.CreateAttributes(); //sets up default values, and makes sure your slider doesn't crash rhino
//customise slider (position, ranges etc)
int inputcount = this.Component.Params.Input[1].SourceCount;
slid.Attributes.Pivot = new System.Drawing.PointF((float) this.Component.Attributes.DocObject.Attributes.Bounds.Left - slid.Attributes.Bounds.Width - 30, (float) this.Component.Params.Input[1].Attributes.Bounds.Y + inputcount * 30);
slid.Slider.Maximum = 10;
slid.Slider.Minimum = 0;
slid.Slider.DecimalPlaces = 2;
slid.SetSliderValue((decimal) (rnd.Next(1000) * 0.01));
//Until now, the slider is a hypothetical object.
// This command makes it 'real' and adds it to the canvas.
GrasshopperDocument.AddObject(slid, false);
//Connect the new slider to this component
this.Component.Params.Input[1].AddSource(slid);
}
}
}
Furthermore, If I want to connect this to another component Input(x), How do I go about this.
Rationally, I should try and find the component first and use <input .AddSource(myslid); >on that component. But how do I find that component through component name or ID? I could use FInd component from NodeinCode but the I’am afraid it will find similar components on the canvas.
Any suggestions.
You can get rid of this error by scheduling a new solution and registering a callback:
private void RunScript(bool x, List<object> y, int z)
{
if(x && z != _z)
GrasshopperDocument.ScheduleSolution(5, SolutionCallback);
_z = z;
}
// <Custom additional code>
private int _z;
private void SolutionCallback(GH_Document doc)
{
Random rnd = new Random();
if(this.Component.Params.Input[1].SourceCount > 0)
{
List<IGH_Param> sources = new List<IGH_Param>(this.Component.Params.Input[1].Sources);
GrasshopperDocument.RemoveObjects(sources, false);
}
for(int i = 0;i < _z;i++)
{
//instantiate new slider
Grasshopper.Kernel.Special.GH_NumberSlider slid = new Grasshopper.Kernel.Special.GH_NumberSlider();
slid.CreateAttributes(); //sets up default values, and makes sure your slider doesn't crash rhino
//customise slider (position, ranges etc)
int inputcount = this.Component.Params.Input[1].SourceCount;
slid.Attributes.Pivot = new System.Drawing.PointF((float) this.Component.Attributes.DocObject.Attributes.Bounds.Left - slid.Attributes.Bounds.Width - 30, (float) this.Component.Params.Input[1].Attributes.Bounds.Y + inputcount * 30);
slid.Slider.Maximum = 10;
slid.Slider.Minimum = 0;
slid.Slider.DecimalPlaces = 2;
slid.SetSliderValue((decimal) (rnd.Next(1000) * 0.01));
//Until now, the slider is a hypothetical object.
// This command makes it 'real' and adds it to the canvas.
GrasshopperDocument.AddObject(slid, false);
//Connect the new slider to this component
this.Component.Params.Input[1].AddSource(slid);
}
}
tognatta.aditya (new solution).gh (13.8 KB)
P.S. Obviously you can also get rid of this error by activating Do not show this message again check box.
If you want to know why you get this message, you need to read @DavidRutten explanation here.
Hi @Mahdiyar If we want to determine which slides are added to which component input code? For example, in the example below, I want to add to the second input, the Radius(Gh_input.(index))
my understanding is that the ToolStripMenuItems are only instantiated at right-clicks. don’t think you can find it outside the event.
you may be able to access the values or properties those ToolStripMenuItems modify. i suspect those are what you’re really after, although they may be private