using FeMM.Common.Helpers;
using FeMM.Grasshopper.Helpers;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Types;
using Rhino.Geometry;
using System;
using System.Collections.Generic;

namespace FeMM.Grasshopper.Components.Tools
{
    public class CurveToMeshComponent : GH_Component
    {
        /// <summary>
        /// Initializes a new instance of the FormFindComponent class.
        /// </summary>
        public CurveToMeshComponent()
          : base("Curve2Mesh", "C2M", "Create a mesh from a list of curve", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_TOOLS)
        {
        }

        /// <summary>
        /// Registers all the input parameters for this component.
        /// </summary>
        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            pManager.AddGenericParameter("Curves", "C", "The list of curves", GH_ParamAccess.list);
            pManager.AddIntegerParameter("Max Face Edges", "E", "The maximum number of edges for each faces", GH_ParamAccess.item, 4);
            pManager.AddNumberParameter("Tolerance", "T", "The matching tolerance", GH_ParamAccess.item, 0.01);
        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddMeshParameter("Mesh", "M", "The resulting mesh usefull to geometric operations", GH_ParamAccess.item);
        }

        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            Message = "";
            var curves = new List<Curve>();
            int totalEdges = 0;
            double tol = 0;

            if (!DA.GetDataList(0, curves))
                return;
            DA.GetData(1, ref totalEdges);
            DA.GetData(2, ref tol);

            var _mesh = new GH_Mesh();
            try
            {
                var nodes = new List<Point3d>();
                var mergedCurve = new List<Curve>();
                var plates = new List<Mesh>();

                for (int i = 0; i < curves.Count; i++)
                {
                    if (curves[i] != null)
                    {
                        mergedCurve.Add(curves[i].DuplicateCurve());
                    }
                }

                Common.Helpers.CommonModelHelper.MergeGeometry(ref nodes, ref mergedCurve, ref plates, tol);
                var mesh = FormFindingHelper.GenerateMeshFromCurves(mergedCurve, totalEdges - 1);
                _mesh = new GH_Mesh(mesh);
                Message = "Done";
            }
            catch (Exception e)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, e.Message);
                Message = "Error";
                return;
            }

            DA.SetData(0, _mesh);
        }

        /// <summary>
        /// Provides an Icon for the component.
        /// </summary>
        protected override System.Drawing.Bitmap Icon => Properties.Resources.Curve2MeshIcon;

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("2bfbb9cf-09b0-4ce6-be33-31bdaafffe92");

        public override GH_Exposure Exposure => GH_Exposure.primary;
    }
}
303 files24 directories