using FeMM.Common.Models;
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.Attributes
{
#if NETCOREAPP
    [SupportedOSPlatform("windows")]
#endif
    public class PlateSupportComponent : GH_Component
    {
        /// <summary>
        /// Initializes a new instance of the PlateSupportComponent class.
        /// </summary>
        public PlateSupportComponent()
          : base("Plate Support", "PS", "Define a plate support", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_ATTRIBUTES)
        {
        }

        /// <summary>
        /// Registers all the input parameters for this component.
        /// </summary>
        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            pManager.AddGenericParameter("Plate", "P", "The plate where define the support", GH_ParamAccess.item);
            pManager.AddNumberParameter("Stiffness", "S", "The stiffness of the support", GH_ParamAccess.item);
            pManager.AddBooleanParameter("Compression only", "CO", "Compression only support", GH_ParamAccess.item, false);
            pManager.AddIntegerParameter("Face", "F", "Face", GH_ParamAccess.item, 0);
            Param_Integer type_param = (Param_Integer)pManager[3];
            type_param.AddNamedValue("Top", 0);
            type_param.AddNamedValue("Bottom", 1);
            int i = pManager.AddGenericParameter("Freedom Cases", "FC", "The freedom cases", GH_ParamAccess.list);
            pManager[i].Optional = true;
        }

        /// <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;
            double stiffness = 0;
            bool compressionOnly = false;
            int face = 0;
            var freedomCases = new List<GH_FreedomCase>();

            if (!DA.GetData("Plate", ref plate))
                return;
            if (!DA.GetData(1, ref stiffness))
                return;
            if (!DA.GetData(2, ref compressionOnly))
                return;
            if (!DA.GetData(3, ref face))
                return;
            DA.GetDataList(4, freedomCases);

            var newPlate = new GH_Plate(plate);

            var attr = new PlateSupportAttributeModel.AttributeData
            {
                Stiffness = stiffness,
                CompressionOnly = compressionOnly
            };
            if (face == 0)
            {
                attr.Face = PlateSupportAttributeModel.SupportedFace.Top;
            }
            else if (face == 1)
            {
                attr.Face = PlateSupportAttributeModel.SupportedFace.Bottom;
            }
            else
            {
                throw new NotSupportedException($"Face value {face} is not valid");
            }

            var plateSupportAttributeModel = new PlateSupportAttributeModel(attr);
            if (freedomCases.Count > 0)
            {
                for (int i = 0; i < freedomCases.Count; i++)
                    plateSupportAttributeModel.Value.FreedomCases.Add(new FreedomCaseModel(freedomCases[i].Value));
            }
            else
                plateSupportAttributeModel.Value.FreedomCases.Add(FreedomCaseModel.UnsetFreedomCase);

            newPlate.Value.Attributes.Add(plateSupportAttributeModel);

            DA.SetData("Plate", 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.PlateSupportIcon;

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("AD0431E8-D68F-45E9-A5C8-AB0C084CFC5A");
    }
}
303 files24 directories