private void RunScript(bool reset, string compoentnName, int n)
{
if(reset)
{
_n = n;
_compoentnName = compoentnName;
GrasshopperDocument.ScheduleSolution(5, SolutionCallback);
}
}
// <Custom additional code>
private int _n;
private string _compoentnName;
private void SolutionCallback(GH_Document doc)
{
foreach(IGH_DocumentObject obj in GrasshopperDocument.Objects)
{
if (obj.NickName == _compoentnName)
{
IGH_Component targetComponent = obj as IGH_Component;
Random rnd = new Random();
if(targetComponent.Params.Input[1].SourceCount > 0)
{
List<IGH_Param> sources = new List<IGH_Param>(targetComponent.Params.Input[1].Sources);
GrasshopperDocument.RemoveObjects(sources, false);
}
for(int i = 0;i < _n;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 = targetComponent.Params.Input[1].SourceCount;
slid.Attributes.Pivot = new System.Drawing.PointF((float) targetComponent.Attributes.DocObject.Attributes.Bounds.Left - slid.Attributes.Bounds.Width - 30, (float) targetComponent.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
targetComponent.Params.Input[1].AddSource(slid);
}
break;
}
}
}
3 Likes