using FeMM.Common.Models;
using FeMM.Grasshopper.Helpers;
using Grasshopper.Kernel;
using System;

namespace FeMM.Grasshopper.Components.Patterning
{
    public class DecompensationDescriptorComponent : GH_Component
    {
        /// <summary>
        /// Initializes a new instance of the CompensationComponent class.
        /// </summary>
        public DecompensationDescriptorComponent()
          : base("DecompensationDescriptor", "DD", "Create a descriptor for decompensation", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_PATTERNING)
        {
        }

        /// <summary>
        /// Registers all the input parameters for this component.
        /// </summary>
        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            pManager.AddNumberParameter("Length", "L", "The decompensation length", GH_ParamAccess.item, 0.1);
            pManager.AddBooleanParameter("Normalized", "N", "If true length is a fraction of the curve", GH_ParamAccess.item, true);
            pManager.AddNumberParameter("X Scale Ends", "XE", "Scale in X axis at curve ends", GH_ParamAccess.item);
            pManager.AddNumberParameter("X Scale Center", "XC", "Scale in X axis at curve center", GH_ParamAccess.item);
            pManager.AddNumberParameter("Y Scale Ends", "YE", "Scale in Y axis at curve ends", GH_ParamAccess.item);
            pManager.AddNumberParameter("Y Scale Center", "YC", "Scale in Y axis at curve center", GH_ParamAccess.item);

        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddGenericParameter("Descriptor", "D", "The decompensation descriptor for a curve", 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)
        {
            double length = 0.0;
            double warpmin = 0.0;
            double warpmax = 0.0;
            double weftmin = 0.0;
            double weftmax = 0.0;
            bool normalized = true;

            if (!DA.GetData(0, ref length))
                return;
            if (!DA.GetData(1, ref normalized))
                return;
            if (!DA.GetData(2, ref warpmin))
                return;
            if (!DA.GetData(3, ref warpmax))
                return;
            if (!DA.GetData(4, ref weftmin))
                return;
            if (!DA.GetData(5, ref weftmax))
                return;

            warpmin = 1.0 - warpmin;
            warpmax = 1.0 - warpmax;
            weftmin = 1.0 - weftmin;
            weftmax = 1.0 - weftmax;

            var descriptor = new DecompensationDescriptor();
            if (length == 0)
            {
                descriptor.Points.Add(new DecompensationDescriptor.DecompensationPoint(true, 0, warpmax, weftmax));
                descriptor.Points.Add(new DecompensationDescriptor.DecompensationPoint(true, 1, warpmax, weftmax));
            }
            else
            {
                descriptor.Points.Add(new DecompensationDescriptor.DecompensationPoint(true, 0, warpmin, weftmin));
                descriptor.Points.Add(new DecompensationDescriptor.DecompensationPoint(normalized, length, warpmax, weftmax));
                descriptor.Points.Add(new DecompensationDescriptor.DecompensationPoint(normalized, -length, warpmax, weftmax));
                descriptor.Points.Add(new DecompensationDescriptor.DecompensationPoint(true, 1, warpmin, weftmin));
            }

            DA.SetData(0, descriptor);
        }

        public override GH_Exposure Exposure => GH_Exposure.quarternary;

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

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("4e5480bb-ac1e-441b-b0d8-c28aa5d92e50");
    }
}
303 files24 directories