Hello,
I have made the following console application to try and work through the logic here. This is working:
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace numberPacker
{
class Program
{
static void Main(string[] args)
{
var remainders = new List<int> { 52 };
var host = remainders[0];
var children = new List<int>() { 22, 6, 3 };
var packCount = new List<int>();
for (var i = 0; i < children.Count; i++)
{
var modulo = remainders.LastOrDefault() % children[i];
remainders.Add(modulo);
}
for (var i = 0; i < children.Count; i++)
{
var pack = (remainders[i] - remainders[i + 1]) / children[i];
packCount.Add(pack);
}
remainders.RemoveAt(0);
Console.WriteLine(@"Host: {0}", host);
Console.WriteLine(@"Children: " + string.Join(",", children));
Console.WriteLine(@"Pack counts: " + string.Join(",", packCount));
Console.WriteLine(@"Remainders: " + string.Join(",", remainders));
}
}
}
The result is:
Host: 52
Children: 22,6,3
Pack counts: 2,1,0
Remainders: 8,2,2
Now I’m trying to implement this into a GH plugin with this code:
namespace packingPlugin
{
public class packingPluginComponent : GH_Component
{
/// <summary>
/// Each implementation of GH_Component must provide a public
/// constructor without any arguments.
/// Category represents the Tab in which the component will appear,
/// Subcategory the panel. If you use non-existing tab or panel names,
/// new tabs/panels will automatically be created.
/// </summary>
public packingPluginComponent()
: base("packingPlugin", "packNumbers",
"",
"Math", "Util")
{
}
/// <summary>
/// Registers all the input parameters for this component.
/// </summary>
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
// Use the pManager object to register your input parameters.
// You can often supply default values when creating parameters.
// All parameters must have the correct access type. If you want
// to import lists or trees of values, modify the ParamAccess flag.
pManager.AddIntegerParameter("Host", "H", "Host number", GH_ParamAccess.item, 45);
pManager.AddIntegerParameter("Children", "C", "Child numbers", GH_ParamAccess.list);
// If you want to change properties of certain parameters,
// you can use the pManager instance to access them by index:
//pManager[0].Optional = true;
}
/// <summary>
/// Registers all the output parameters for this component.
/// </summary>
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
// Use the pManager object to register your output parameters.
// Output parameters do not have default values, but they too must have the correct access type.
pManager.AddIntegerParameter("Remainders", "Rem", "what remains", GH_ParamAccess.list);
pManager.AddIntegerParameter("Pack counts", "Packs", "what is packed", GH_ParamAccess.list);
// Sometimes you want to hide a specific parameter from the Rhino preview.
// You can use the HideParameter() method as a quick way:
//pManager.HideParameter(0);
}
/// <summary>
/// This is the method that actually does the work.
/// </summary>
/// <param name="DA">The DA object can be used to retrieve data from input parameters and
/// to store data in output parameters.</param>
protected override void SolveInstance(IGH_DataAccess DA)
{
// First, we need to retrieve all data from the input parameters.
// We'll start by declaring variables and assigning them starting values.
List<int> remainders = new List<int>();
var host = 0;
List<int> children = new List<int>();
List<int> packCount = new List<int>();
// Then we need to access the input parameters individually.
// When data cannot be extracted from a parameter, we should abort this method.
if (!DA.GetData(0, ref host)) return;
if (!DA.GetDataList(1, children)) return;
// We should now validate the data and warn the user if invalid data is supplied.
if (host <= 0)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Host must be bigger than or equal to one");
return;
}
//do the work of finding the modulos
var modulo = 0;
for (var i = 0; i < children.Count; i++)
{
modulo = remainders.LastOrDefault() % children[i];
}
remainders.Add(modulo);
for (var i = 0; i < children.Count; i++)
{
var pack = (remainders[i] - remainders[i + 1]) / children[i];
packCount.Add(pack);
}
remainders.RemoveAt(0);
DA.SetDataList(0, remainders);
DA.SetDataList(1, packCount);
}
/// <summary>
/// The Exposure property controls where in the panel a component icon
/// will appear. There are seven possible locations (primary to septenary),
/// each of which can be combined with the GH_Exposure.obscure flag, which
/// ensures the component will only be visible on panel dropdowns.
/// </summary>
public override GH_Exposure Exposure
{
get { return GH_Exposure.primary; }
}
/// <summary>
/// Provides an Icon for every component that will be visible in the User Interface.
/// Icons need to be 24x24 pixels.
/// </summary>
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;
}
}
/// <summary>
/// Each component must have a unique Guid to identify it.
/// It is vital this Guid doesn't change otherwise old ghx files
/// that use the old ID will partially fail during loading.
/// </summary>
public override Guid ComponentGuid
{
get { return new Guid("98c532fe-f3ff-4560-af44-40b5d87c9bd0"); }
}
}
}
But I’m getting this error:
Is there something wrong with how I have registered inputs/outputs?
Help appreciated!
Dan