Returning a list in a custom GH component

Hello there, I’m new to Grasshopper and I’m trying to create a custom component using visual studio that calculates the intersection between two objects, and returns the points where said objects intersect. This works fine when I return a single point of intersection.

protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
        {  
            pManager.AddMeshParameter("Mesh", "M", "Mesh to use", GH_ParamAccess.item);
            pManager.AddLineParameter("Line", "L", "Line to intersect", GH_ParamAccess.item);
        }

     
        protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
        {         
            pManager.AddPointParameter("Intersection", "I", "Point of Intersection", GH_ParamAccess.item);
        }

        protected override void SolveInstance(IGH_DataAccess DA)
        {
           
            Mesh mesh1 = new Mesh();
            Line line1 = Line.Unset;
            if (!DA.GetData(0, ref mesh1)) return;
            if (!DA.GetData(1, ref line1)) return;
          
            Point3d[] intersections = Rhino.Geometry.Intersect.Intersection.MeshLine(mesh1, line1);
           
            Point3d inter = intersections[0];
          
            DA.SetData(0, result);

        }

This way it works, however, if I wnat to return instead of one point, a list of points (because there is more than 1 intersection) it wont work. This is the code

 protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
        {
            pManager.AddMeshParameter("Mesh", "M", "Mesh to use", GH_ParamAccess.item);
            pManager.AddLineParameter("Line", "L", "Line to intersect", GH_ParamAccess.item);
        }

        protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
        {

            pManager.AddPointParameter("Intersection", "I", "Point of Intersection", GH_ParamAccess.list);

        }

        protected override void SolveInstance(IGH_DataAccess DA)
        {

            Mesh mesh1 = new Mesh();
            Line line1 = Line.Unset;

            if (!DA.GetData(0, ref mesh1)) return;
            if (!DA.GetData(1, ref line1)) return;

            Point3d[] intersections = Rhino.Geometry.Intersect.Intersection.MeshLine(mesh1, line1);
            List<Point3d> result = new List<Point3d>();
            foreach (Point3d p in intersections)
            {

                result.Add(p);
            }
            DA.SetData(0, result);     
        }

When I run this I get this error

What am I doing wrong? I know it can be done with list of numbers but i cant make it work with objects

Hi
Use DA.SetDataList(intersections).

protected override void SolveInstance(IGH_DataAccess DA)
        {

            Mesh mesh1 = new Mesh();
            Line line1 = Line.Unset;

            if (!DA.GetData(0, ref mesh1)) return;
            if (!DA.GetData(1, ref line1)) return;

            Point3d[] intersections = Rhino.Geometry.Intersect.Intersection.MeshLine(mesh1, line1);
            DA.SetDataList(0, intersections);     
        }
1 Like

Hi, it works, thank you very much!