using FeMM.Common.Models;
using FeMM.Grasshopper.DataTypes.FeMM;
using FeMM.Grasshopper.Helpers;
using Grasshopper.Kernel;
using System;
using System.Collections.Generic;
using System.Linq;

namespace FeMM.Grasshopper.Components.Functions
{
    public class ModalRitzComponent : GH_Component
    {
        /// <summary>
        /// Initializes a new instance of the SuperElementComponent class.
        /// </summary>
        public ModalRitzComponent()
          : base("Modal Ritz", "MR", "Defines modal ritz load case in SAP", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_DEFINITIONS)
        {
        }

        /// <summary>
        /// Registers all the input parameters for this component.
        /// </summary>
        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            pManager.AddTextParameter("Name", "N", "The name of the Modal Ritz", GH_ParamAccess.item);
            pManager.AddIntegerParameter("Max number of Modes", "Max", "The maximum number of modes requested", GH_ParamAccess.item, 12);
            pManager.AddIntegerParameter("Min number of Modes", "Min", "The minimum number of modes requested", GH_ParamAccess.item, 1);
            pManager.AddGenericParameter("Initial Case", "IC", "This is blank, None, or the name of an existing analysis case. This item specifies " +
                "if the load case starts from zero initial conditions, that is, an unstressed state, or if it starts using the stiffness that occurs" +
                " at the end of a nonlinear static or nonlinear direct integration time history load case", GH_ParamAccess.item);
            pManager[pManager.ParamCount - 1].Optional = true;
            pManager.AddGenericParameter("Modal Ritz Loads", "MRL", "Modal ritz acceleration loads", GH_ParamAccess.list);
        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddGenericParameter("Ritz Modal", "RM", "The Ritz Modal", 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)
        {
            string name = string.Empty;
            int maxModes = 0;
            int minModes = 0;
            GH_Case initialCase = null;
            var loadType = new List<GH_FunctionDefinition>();

            if (!DA.GetData(0, ref name))
                return;
            DA.GetData(1, ref maxModes);
            DA.GetData(2, ref minModes);
            DA.GetData(3, ref initialCase);
            DA.GetDataList(4, loadType);

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

            if (loadType.Any(i => i.Value.GetType() != typeof(ModalTargetLoadModel)))
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Input case must be a modale ritz acceleration load");
                return;
            }

            var data = new ModalRitzModel()
            {
                Name = name,
                MaxModes = maxModes,
                MinModes = minModes,
                LoadType = [.. loadType.Select(i => (ModalTargetLoadModel)i.Value)],
            };

            if (initialCase != null)
                data.InitialCase = loadCaseModel;
            Message = name;

            var gH_Function = new GH_Case(data);
            DA.SetData(0, gH_Function);
        }

        public override GH_Exposure Exposure => GH_Exposure.tertiary;

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

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("4765cad6-8b1c-4c24-b256-0411470d6cf9");
    }
}
303 files24 directories