using FeMM.Common.Models;
using FeMM.Grasshopper.Components.Parameters;
using FeMM.Grasshopper.DataTypes.FeMM;
using FeMM.Grasshopper.Helpers;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Parameters;
using System;
using System.Collections.Generic;
using System.Runtime.Versioning;

namespace FeMM.Grasshopper.Components.Loads
{
#if NETCOREAPP
    [SupportedOSPlatform("windows")]
#endif
    public class PlateLoadUniformToFrameComponent : GH_Component
    {
        protected Dictionary<int, string> _directionTypeOptions = new()
        {
            { 0, "Local1Axis" },
            { 1, "Local2Axis" },
            { 2, "Local3Axis" },
            { 3, "XDirection" },
            { 4, "YDirection" },
            { 5, "ZDirection" },
            { 6, "ProjectedXDirection" },
            { 7, "ProjectedYDirection" },
            { 8, "ProjectedZDirection" },
            { 9, "GravityDirection" },
            { 10, "ProjectedGravityDirection" },
        };

        /// <summary>
        /// Initializes a new instance of the PlatePressureComponent class.
        /// </summary>
        public PlateLoadUniformToFrameComponent()
          : base("Plate Load Uniform to Frame", "PLUTF", "Assign a plate load uniform to frame (only for SAP)", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_LOADS)
        {
        }

        /// <summary>
        /// Registers all the input parameters for this component.
        /// </summary>
        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            pManager.AddGenericParameter("Plate", "P", "The plate where add the pressure", GH_ParamAccess.item);
            pManager.AddGenericParameter("LoadCase", "LC", "The load case", GH_ParamAccess.item);
            pManager.AddNumberParameter("Value", "V", "Uniform load value", GH_ParamAccess.item);
            int d = pManager.AddIntegerParameter("Direction", "D", "Direction of the load", GH_ParamAccess.item, 9);
            Param_Integer mtParam = pManager[d] as Param_Integer;
            foreach (KeyValuePair<int, string> v in _directionTypeOptions)
                mtParam.AddNamedValue(v.Value, v.Key);
            var cs_param = new CoordinateSystemParam { Optional = true };
            pManager.AddParameter(cs_param, "Coordinate System", "CS", "The coordinate system of the load. Default value: Global Coordinate System", GH_ParamAccess.item);
            int dt = pManager.AddIntegerParameter("Distribution type", "DT", "indicating the load distribution type", GH_ParamAccess.item, 0);
            Param_Integer typeParamDT = (Param_Integer)pManager[dt];
            typeParamDT.AddNamedValue("One-way ", 0);
            typeParamDT.AddNamedValue("Two-way ", 1);
        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddGenericParameter("Plate", "P", "The modified plate", 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_Plate plate = null;
            GH_Case lc = null;
            double loadValue = 0;
            GH_FunctionDefinition cs = null;
            int direction = 0;
            int distributionType = 0;

            if (!DA.GetData("Plate", ref plate))
                return;
            if (!DA.GetData("LoadCase", ref lc))
                return;
            DA.GetData("Coordinate System", ref cs);

            if (!DA.GetData("Value", ref loadValue))
                return;
            if (!DA.GetData("Direction", ref direction))
                return;
            if (!DA.GetData("Distribution type", ref distributionType))
                return;

            if (cs == null && distributionType == 9)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Cannot set Gravity distribution type with Local coordinate system");
            }

            var newPlate = new GH_Plate(plate);

            if (loadValue != 0)
            {
                var load_value = new PlateLoadUniformToFrameModel.LoadData()
                {
                    Direction = (PlateLoadUniformToFrameModel.LoadData.Directions)(direction + 1),
                    Value = loadValue,
                    DistributionType = (PlateLoadUniformToFrameModel.LoadData.DistributionTypes)(distributionType + 1),
                };
                if (cs != null && cs.Value is CoordinateSystemModel css)
                    load_value.CoordinateSystem = css;

                var load = new PlateLoadUniformToFrameModel((LoadCaseModel)lc.Value, load_value);
                Message = string.Format("Coordinate System: {0}", load.CoordinateSystem.Name);

                newPlate.Value.Loads.Add(load);
            }

            DA.SetData(0, newPlate);
        }

        public override GH_Exposure Exposure => GH_Exposure.tertiary;

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

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("5be19b7b-d92f-41f2-a189-1ab4fdaa21e4");
    }
}
303 files24 directories