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.Linq;

namespace FeMM.Grasshopper.Components
{
    public class LoadCaseResponseSpectrumComponent : GH_Component
    {
        public LoadCaseResponseSpectrumComponent()
            : base("Response Spectrum Load Case", "RSC", "A Load Case Response Spectrum definition", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_DEFINITIONS)
        {
        }

        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            pManager.AddTextParameter("Name", "N", "The name of the Response Spectrum", GH_ParamAccess.item);

            int i = pManager.AddIntegerParameter("Modal combination", "MC", "indicating the modal combination option. 1 = CQC. 2 = SRSS. 3 = Absolute. 4 = GMC. " +
                "5 = NRC 10 percent. 6 = Double sum.", GH_ParamAccess.item, 1);
            Param_Integer type_param = (Param_Integer)pManager[i];
            Array values = Enum.GetValues(typeof(LoadCaseResponseSpectrumModel.ModalCombinationOptions));
            foreach (LoadCaseResponseSpectrumModel.ModalCombinationOptions val in values)
                type_param.AddNamedValue(val.GetDescription(), (int)val);

            i = pManager.AddNumberParameter("GMC F1", "F1", "The GMC f1 factor. This item does not apply when MyType = 3. [cyc/s]", GH_ParamAccess.item, 1);            
            pManager.AddNumberParameter("GMC F2", "F2", "The GMC f2 factor. This item does not apply when MyType = 3. [cyc/s]", GH_ParamAccess.item, 0);

            i = pManager.AddIntegerParameter("periodic + rigid modal type", "PRT", "The periodic plus rigid modal combination option. 1 = SRSS. 2 = Absolute.", GH_ParamAccess.item, 1);
            type_param = (Param_Integer)pManager[i];
            values = Enum.GetValues(typeof(LoadCaseResponseSpectrumModel.PeriodicRigidCombTypes));
            foreach (LoadCaseResponseSpectrumModel.PeriodicRigidCombTypes val in values)
                type_param.AddNamedValue(val.GetDescription(), (int)val);

            pManager.AddGenericParameter("Modal Case", "MC", "A modal load case. It specifies the modal load case" +
                " on which any mode-type load assignments to the specified load case are based", GH_ParamAccess.item);
            pManager.AddNumberParameter("Eccentricity Ratio ", "E", "The eccentricity ratio that applies to all diaphragms", GH_ParamAccess.item, 0);

            i = pManager.AddIntegerParameter("Directional combination ", "DC", "the directional combination option. 1 = SRSS. 2 = ABS. 3 = CQC3", GH_ParamAccess.item, 2);
            type_param = (Param_Integer)pManager[i];
            values = Enum.GetValues(typeof(LoadCaseResponseSpectrumModel.DirectionalCombinationTypes));
            foreach (LoadCaseResponseSpectrumModel.DirectionalCombinationTypes val in values)
                type_param.AddNamedValue(val.GetDescription(), (int)val);

            pManager.AddGenericParameter("Response spectrum load", "RSL", "This is an array that includes the response spectrum load", GH_ParamAccess.list);
            i = pManager.AddNumberParameter("Damping Constant", "DC", "The constant damping for all modes (0 <= Damp < 1)", GH_ParamAccess.item, 0.05);
            pManager[i].Optional = true;
        }

        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)
        {
            string name = "";
            int modalCombType = 0;
            double gmcf1 = 0;
            double gmcf2 = 0;
            int modalCombRigid = 0;
            GH_Case modalCase = null;
            double eccentricity = 0;
            int dirextionalComb = 0;
            var loadTypes = new List<GH_Case> ();
            double dampConst = 0;

            if (!DA.GetData(0, ref name))
                return;
            if (!DA.GetData(1, ref modalCombType))
                return;
            DA.GetData(2, ref gmcf1);
            DA.GetData(3, ref gmcf2);
            DA.GetData(4, ref modalCombRigid);
            if (!DA.GetData(5, ref modalCase))
                return;
            DA.GetData(6, ref eccentricity);
            DA.GetData(7, ref dirextionalComb);
            if (!DA.GetDataList(8, loadTypes))
                return;
            DA.GetData(9, ref dampConst);

            if(loadTypes.Any(i => i.Value.GetType() != typeof(LoadCaseResponseSpectrumLoadModel)))
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Error cases input");
                DA.SetData(0, new GH_Case());
                return;
            }

            var model = new LoadCaseResponseSpectrumModel(name)
            {
                ModalCombinationOption = (LoadCaseResponseSpectrumModel.ModalCombinationOptions)(modalCombType),
                Modal = (IModalModel)modalCase.Value,
                DampingConstant = dampConst,
                Eccentricity = eccentricity,
                GMCF1 = gmcf1,
                GMCF2 = gmcf2,
                PriodicRigidCombType = (LoadCaseResponseSpectrumModel.PeriodicRigidCombTypes)(modalCombRigid),
                DirectionalCombinationType = (LoadCaseResponseSpectrumModel.DirectionalCombinationTypes)(dirextionalComb),
                LoadCaseResponseSpectrumLoads = [.. loadTypes.Select(i => (LoadCaseResponseSpectrumLoadModel)i.Value)],
            };

            var outCase = new GH_Case(model);
            Message = name;
            DA.SetData(0, outCase);
        }

        public override GH_Exposure Exposure => GH_Exposure.senary;

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

        public override Guid ComponentGuid => new("3ec1ea65-bef3-49ca-84dc-23f2383b6f4c");
    }
}
303 files24 directories