using FeMM.Common.Models;
using FeMM.Grasshopper.Components.Parameters;
using FeMM.Grasshopper.DataTypes.FeMM;
using FeMM.Grasshopper.Helpers;
using GH_IO.Serialization;
using Grasshopper.Kernel;
using Rhino.Geometry;
using System;
using System.Runtime.Versioning;

namespace FeMM.Grasshopper.Components.Loads
{
#if NETCOREAPP
    [SupportedOSPlatform("windows")]
#endif
    public class BeamPointLoadComponent : GH_Component
    {
        /// <summary>
        /// Initializes a new instance of the BeamPointLoadComponent class.
        /// </summary>
        public BeamPointLoadComponent()
          : base("Beam Point Load", "BPL", "Defines the beam point load force", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_LOADS)
        {
        }

        public override void CreateAttributes()
        {
            var attr = new ComponentAttributes.ComponentWithImageAttributes(this, Properties.Resources.BeamPointLoadImage);
            attr.AddOverlappedBitmap(new System.Drawing.PointF(5, -65), Properties.Resources.LocalAxisImage);
            m_attributes = attr;
        }

        /// <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 point force load", GH_ParamAccess.item);
            pManager.AddGenericParameter("LoadCase", "LC", "The load case", GH_ParamAccess.item);
            var cs_param = new CoordinateSystemParam
            {
                Optional = true
            };
            cs_param.ObjectChanged += Param_CoordinateSystem_ObjectChanged;
            pManager.AddParameter(cs_param, "Coordinate System", "CS", "The coordinate system of the load. Default value: Global Coordinate System", GH_ParamAccess.item);
            pManager.AddNumberParameter("P1", "P1", "The load in the 1 axis", GH_ParamAccess.item, 0);
            pManager.AddNumberParameter("P2", "P2", "The load in the 2 axis", GH_ParamAccess.item, 0);
            pManager.AddNumberParameter("P3", "P3", "The load in the 3 axis", GH_ParamAccess.item, 0);
            pManager.AddNumberParameter("a", "a", "The distance ratio (0-1) from the starting node", GH_ParamAccess.item);
        }

        private void Param_CoordinateSystem_ObjectChanged(IGH_DocumentObject sender, GH_ObjectChangedEventArgs e)
        {
            SetInputsName();
        }

        private void SetInputsName()
        {
            CoordinateSystemParam cs_param = (CoordinateSystemParam)Params.Input[2];
            if (cs_param.PersistentData.Branches.Count == 0)
            {
                Params.Input[3].NickName = "P1";
                Params.Input[4].NickName = "P2";
                Params.Input[5].NickName = "P3";
            }
            else
            {
                Params.Input[3].NickName = "PX";
                Params.Input[4].NickName = "PY";
                Params.Input[5].NickName = "PZ";
            }
        }

        /// <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)
        {
            SetInputsName();

            GH_Beam beam = null;
            GH_Case lc = null;
            GH_FunctionDefinition cs = null;
            double p1 = 0, p2 = 0, p3 = 0;
            double a = 0;

            if (!DA.GetData("Beam", ref beam))
                return;
            if (!DA.GetData("LoadCase", ref lc))
                return;
            bool has_cs = DA.GetData("Coordinate System", ref cs);
            bool p1_has_value = DA.GetData("P1", ref p1);
            bool p2_has_value = DA.GetData("P2", ref p2);
            bool p3_has_value = DA.GetData("P3", ref p3);
            bool a_has_value = DA.GetData("a", ref a);

            LoadCaseModel loadCaseModel = null;
            if (lc.Value != null && lc.Value is LoadCaseModel lccc)
                loadCaseModel = new LoadCaseModel(lccc);
            else
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Input case must be a load case");
                return;
            }

            if (a >= 1)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "The 'a' value must be lower than 1");
                return;
            }
            if (loadCaseModel.NonStructuralMassAcceleration)
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "The connected LoadCase have the Non-structural mass flag set to on");

            var load_value = new BeamPointLoadModel.LoadData()
            {
                P = new Vector3d(p1, p2, p3),
                A = a
            };

            CoordinateSystemModel cccs = CoordinateSystemModel.Global;
            if (cs != null && cs.Value is CoordinateSystemModel css)
                cccs = css;
            load_value.CoordinateSystem = cccs;
            var load = new BeamPointLoadModel(loadCaseModel, load_value, BeamPointLoadModel.LoadType.Force)
            {
                CoordinateSystem = load_value.CoordinateSystem
            };
            var newBeam = new GH_Beam(beam);
            newBeam.Value.Loads.Add(load);

            Message = string.Format("Coordinate System: {0}", load.CoordinateSystem.Name);
            DA.SetData(0, newBeam);
        }

        public override bool Read(GH_IReader reader)
        {
            bool success = base.Read(reader);

            SetInputsName();

            return success;
        }

        public override GH_Exposure Exposure => GH_Exposure.secondary;

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

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("08795ed9-9536-4f38-bc7f-960a3bf30266");
    }
}
303 files24 directories