using FeMM.Common.Helpers;
using FeMM.Common.Models;
using FeMM.Common.Models.Interface;
using FeMM.Grasshopper.DataTypes.FeMM;
using FeMM.Grasshopper.Helpers;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Parameters;
using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using static FeMM.Common.Models.LoadCaseResponseSpectrumModel;

namespace FeMM.Grasshopper.Components
{
    public class LoadCaseResponseSpectrumLoadComponent : GH_Component
    {
        public enum LoadName
        {
            U1 = 1,
            U2 = 2,
            U3 = 3,
            R1 = 4,
            R2 = 5,
            R3 = 6,
        }

        public LoadCaseResponseSpectrumLoadComponent()
            : base("Response Spectrum Load", "RSC", "A Load Case Response Spectrum definition", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_DEFINITIONS)
        {
        }

        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            int i = pManager.AddIntegerParameter("Load Name", "LT", "This is an array that includes U1, U2, U3, R1, R2 or R3, indicating the direction of the load", GH_ParamAccess.item);
            Param_Integer type_param = (Param_Integer)pManager[i];
            Array values = Enum.GetValues(typeof(LoadCaseResponseSpectrumLoadModel.LoadTypes));
            foreach (LoadCaseResponseSpectrumLoadModel.LoadTypes val in values)
                type_param.AddNamedValue(val.GetDescription(), (int)val);
            pManager.AddGenericParameter("Response spectrum functions", "RSF", "This is an array that includes the name of the response spectrum function associated with each load", GH_ParamAccess.item);
            pManager.AddNumberParameter("Scale Factor", "SF", "This is an array that includes the scale factor of each load assigned to the load case. " +
                "[L/s2] for U1 U2 and U3; otherwise unitless", GH_ParamAccess.item, 1);
            i = pManager.AddGenericParameter("Coordinate System", "CS", "This is an array that includes the name of the coordinate system associated with each load. " +
                "If this item is a blank string, the Global coordinate system is assumed", GH_ParamAccess.item);
            pManager[i].Optional = true;
            pManager.AddNumberParameter("Angle", "A", "This is an array that includes the angle between the acceleration local 1 axis and the +X-axis of the coordinate " +
                "system specified by the CSys item. The rotation is about the Z-axis of the specified coordinate system. [deg]", GH_ParamAccess.item, 0);
        }

        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddGenericParameter("Response Spectrum Load Case", "RSLC", "The defined Response Spectrum Load Case", GH_ParamAccess.item);
        }

        protected override void SolveInstance(IGH_DataAccess DA)
        {
            int loadType = 0;
            GH_FunctionDefinition responseSpectr = null;
            double scaleFactor = 0;
            GH_FunctionDefinition gH_CoordinateSystem = null;
            double angle = 0;

            if (!DA.GetData(0, ref loadType))
                return;
            if (!DA.GetData(1, ref responseSpectr))
                return;
            DA.GetData(2, ref scaleFactor);
            bool hasCS = DA.GetData(3, ref gH_CoordinateSystem);
            DA.GetData(4, ref angle);

            if (responseSpectr != null && responseSpectr.Value != null && responseSpectr.Value is ResponseSpectrumFunctionModel responseSpectrumFunctionModel)
            {
                var model = new LoadCaseResponseSpectrumLoadModel()
                {
                    LoadType = (LoadCaseResponseSpectrumLoadModel.LoadTypes)loadType,
                    LoadFunction = responseSpectrumFunctionModel,
                    LoadScaleFactor = scaleFactor,
                    Angle = angle,
                };
                if (hasCS) 
                {
                    if (gH_CoordinateSystem != null && gH_CoordinateSystem.Value is CoordinateSystemModel css)                     
                        model.CoordinateSystemModel = css;                    
                }

                var outCase = new GH_Case(model);
                DA.SetData(0, outCase);
            }
            else
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Error response spectrum function");
                DA.SetData(0, new GH_Case());
                return;
            }
        }

        public override GH_Exposure Exposure => GH_Exposure.tertiary;

        protected override System.Drawing.Bitmap Icon => Properties.Resources.LoadCaseIcon;

        public override Guid ComponentGuid => new("cc9c6ca3-145d-45fe-9d05-77ad8e7f1cbb");
    }
}
303 files24 directories