How to handle different input types with one Grasshopper parameter in C#

This example shows how one input parameter of a Grasshopper component can handle different types of input. The goal of the example code is to retrieve a set of points, either directly from Point3d or from any kind of Curve objects. Multiple types are allowed to be present at the same time.

The code snippet originates from the CreateLinksComponent of Pamela.

int in_pts = -1;

protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
//in_pts = pManager.AddCurveParameter("Polyline", "Pts", "List of points (polyline) to use as fixed / supported endpoints.", GH_ParamAccess.list);
in_pts = pManager.AddGenericParameter("Polyline", "Pts", "List of points (polyline) to use as fixed / supported endpoints.", GH_ParamAccess.list);

}

protected override void SolveInstance(IGH_DataAccess DA)
{
List objs = new List();
List points = new List();
List crvlist = new List();

// First, we need to retrieve all data from the input parameters.
if (!DA.GetDataList(in_pts, objs) || objs == null || objs.Count == 0)
{
return;
}

Point3d pt = Point3d.Unset;
Curve crv = new LineCurve();
// try to convert input data
for (int j = 0; j < objs.Count; j++) { object obj = objs[j]; if (GH_Convert.ToPoint3d(obj, ref pt, GH_Conversion.Both)) { points.Add(pt); } else if (GH_Convert.ToCurve(obj, ref crv, GH_Conversion.Both)) { crvlist.Add(crv); } } if (crvlist.Count > 0)
{
// explode curves into segments
List crvs = new List();
for (int i = 0; i < crvlist.Count; i++) { GeometryUtil.CurveSegments(crvs, crvlist[i], true); } // extract points from LineCurves HashSet pts = new HashSet();

for (int i = 0; i < crvs.Count; i++) { LineCurve line = crvs[i] as LineCurve; if (line != null) { pts.Add(line.PointAtStart); pts.Add(line.PointAtEnd); } } // Convert HashSet to List, Merge with existing objects in points points.AddRange(new List(pts));

}

if (points.Count == 0)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Unable to convert input data into points");
return;
}