using FeMM.Common.Models;
using FeMM.Grasshopper.DataTypes.FeMM;
using FeMM.Grasshopper.Helpers;
using Grasshopper.Kernel;
using System;
using System.Runtime.Versioning;

namespace FeMM.Grasshopper.Components.Attributes
{
#if NETCOREAPP
    [SupportedOSPlatform("windows")]
#endif
    public class FixedCompensationComponent : GH_Component
    {
        /// <summary>
        /// Initializes a new instance of the FixedLengthComponent class.
        /// </summary>
        public FixedCompensationComponent()
          : base("Target compensation", "FC", "The target compensation attribute", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_ATTRIBUTES)
        {
        }

        /// <summary>
        /// Registers all the input parameters for this component.
        /// </summary>
        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            pManager.AddGenericParameter("Beam", "B", "The beam where add the target Length attribute", GH_ParamAccess.item);
            pManager.AddGenericParameter("LoadCase", "LC", "The load case", GH_ParamAccess.item);
            pManager.AddNumberParameter("Compensation(%)", "C%", "The fixed deformation value in % (greater than 0 = reduction)", GH_ParamAccess.item);
            pManager.AddNumberParameter("Weight", "W", "The weight of the fixed length value in the non-linear solver", GH_ParamAccess.item, 1);
        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddGenericParameter("Beam", "B", "The modified beam", 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)
        {
            GH_Beam beam = null;
            GH_Case lc = null;
            double value = 0;
            double weight = 0;
            double newLength;

            if (!DA.GetData(0, ref beam))
                return;
            if (!DA.GetData(1, ref lc))
                return;
            if (!DA.GetData(2, ref value))
                return;
            DA.GetData(3, ref weight);

            if (weight <= 0)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Weight parameter must be > 0");
                return;
            }

            //>0=reduction
            newLength = beam.Value.PointFrom.DistanceTo(beam.Value.PointTo) * (1 - value / 100);

            var newBeam = new BeamModel(beam.Value);
            newBeam.Loads.Add(new FixedLengthModel((LoadCaseModel)lc.Value, newLength, weight));
            DA.SetData(0, new GH_Beam(newBeam));
        }

        public override GH_Exposure Exposure => GH_Exposure.secondary;

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

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("7608AC1C-9E5A-4DBB-8806-DD6791A6AA35");
    }
}
303 files24 directories