using System;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Parameters;
namespace GH_Integration
{
public class MyComponent1 : GH_Component, IGH_VariableParameterComponent
{
///
/// Initializes a new instance of the MyComponent1 class.
///
public MyComponent1()
: base("MyComponent1",
"Nickname",
"Description",
"Category",
"Subcategory")
{
this.Params.ParameterSourcesChanged += new GH_ComponentParamServer.ParameterSourcesChangedEventHandler(this.ParamSourcesChanged);
}
private void ParamSourcesChanged(object sender, GH_ParamServerEventArgs e)
{
if (e.ParameterSide == GH_ParameterSide.Input && e.ParameterIndex == this.Params.Input.Count - 1)
{
// add another input
IGH_Param iGH_Param = CreateParameter(GH_ParameterSide.Input, this.Params.Input.Count);
this.Params.RegisterInputParam(iGH_Param);
this.VariableParameterMaintenance();
this.Params.OnParametersChanged();
// }
}
}
///
/// Registers all the input parameters for this component.
///
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
pManager.AddNumberParameter("Number 1", "no 1", "The value of the first number", GH_ParamAccess.item);
pManager.AddNumberParameter("Number 2", "no 2", "The value of the second number", GH_ParamAccess.item);
}
///
/// Registers all the output parameters for this component.
///
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
pManager.AddNumberParameter("Summation", "Sum", "The total value after addition", GH_ParamAccess.item);
}
///
/// This is the method that actually does the work.
///
/// The DA object is used to retrieve from inputs and store in outputs.
protected override void SolveInstance(IGH_DataAccess DA)
{
double valueOutput = 0;
double v = 0;
for (var i = 0; i < Params.Input.Count; i++)
{
if (DA.GetData(i, ref v)) {
valueOutput += v;
}
}
DA.SetData(0, valueOutput);
}
///
/// Provides an Icon for the component.
///
protected override System.Drawing.Bitmap Icon
{
get
{
//You can add image files to your project resources and access them like this:
// return Resources.IconForThisComponent;
return null;
}
}
///
/// Gets the unique ID for this component. Do not change this ID after release.
///
public override Guid ComponentGuid
{
get { return new Guid("F3005B64-3672-48A2-B20E-4FED67F546EE"); }
}
public bool CanInsertParameter(GH_ParameterSide side, int index) {
if (side == GH_ParameterSide.Input)
{
return true;
}
else {
return false;
}
}
public bool CanRemoveParameter(GH_ParameterSide side, int index) {
if (side == GH_ParameterSide.Input && Params.Input.Count > 2)
{
return true;
}
else {
return false;
}
}
public IGH_Param CreateParameter(GH_ParameterSide side, int index) {
Param_GenericObject param = new Param_GenericObject();
param.Name = "no " + (index).ToString();
param.NickName = param.Name;
param.Description = "A new numerical value to add";
param.Access = GH_ParamAccess.item;
param.Optional = true;
return param;
}
public bool DestroyParameter(GH_ParameterSide side, int index) {
return true;
}
public void VariableParameterMaintenance()
{
for (int i = 0; i < this.Params.Input.Count; i++)
{
IGH_Param param = Params.Input[i];
param.Name = "no " + (i).ToString();
param.NickName = param.Name;
param.Description = "A new numerical value to add";
param.Access = GH_ParamAccess.item;
param.Optional = true;
}
}
}
}