using FeMM.Grasshopper.Helpers;
using Grasshopper.GUI.Canvas;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Attributes;
using Grasshopper.Kernel.Parameters;
using Grasshopper.Kernel.Types;
using System;
using static FeMM.Common.Helpers.NamingConventionHelper;
using System.Drawing;

namespace FeMM.Grasshopper.Components.Sections
{
    /// <summary>
    /// Creates a configuration of how to interpret custom sections
    /// </summary>
    public class CustomSectionConfigComponent : GH_Component
    {
        public class Label_Attributes : GH_ComponentAttributes
        {
            public Label_Attributes(IGH_Component component) : base(component)
            {
            }

            protected override void Render(GH_Canvas canvas, Graphics graphics, GH_CanvasChannel channel)
            {
                base.Render(canvas, graphics, channel);

                if (channel == GH_CanvasChannel.Objects)
                {
                    var steelsBounds = new RectangleF(
                            new PointF(Bounds.X - 6, Bounds.Y + 2),
                            new SizeF(Bounds.Width + 4, 20)
                        );

                    var concretesBounds = new RectangleF(
                        new PointF(Bounds.X - 6, Bounds.Y + 20 * 6 + 2),
                        new SizeF(Bounds.Width + 4, 20)
                    );

                    GH_Capsule label = GH_Capsule.CreateTextCapsule(
                        steelsBounds,
                        steelsBounds,
                        GH_Palette.Grey,
                        "Steel",
                        2,
                        0);
                    label.Render(graphics, Selected, Owner.Locked, false);
                    label.Dispose();

                    label = GH_Capsule.CreateTextCapsule(
                        concretesBounds,
                        concretesBounds,
                        GH_Palette.Grey,
                        "Concrete",
                        2,
                        0);
                    label.Render(graphics, Selected, Owner.Locked, false);
                    label.Dispose();
                }
            }
        }

        private string[] enumTexts =
        [
            "Generic Section",
            "Geometry",
            "RHS approximation",
            "Bt x H Rectangle",
            "B x Hsa Rectangle",
            "B x Hsb Rectangle",
            "Tw x H Rectangle",
            "B x H Without Radius"
        ];


        /// <summary>
        /// Initializes a new instance of the BeamPropertyComponent class.
        /// </summary>
        public CustomSectionConfigComponent()
          : base("Custom Section Configuration", "CSC", "Defines configuration on how to interpret custom sections", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_SECTIONS)
        {
            this.m_attributes = new Label_Attributes(this);
        }

        /// <summary>
        /// Registers all the input parameters for this component.
        /// </summary>
        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            var pIndex = 0;
            Param_Integer param;

            pIndex = pManager.AddBooleanParameter("__Steel__", "__Steel__", "__Steel__", GH_ParamAccess.item);
            pManager[pIndex].Optional = true;

            pIndex = pManager.AddIntegerParameter("BUX", "BUX", "Built-Up X section", GH_ParamAccess.item, 0);
            param = pManager[pIndex] as Param_Integer;
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.GENERIC_SECTION], (int)CustomSectionConfigOption.GENERIC_SECTION);
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.GEOMETRY], (int)CustomSectionConfigOption.GEOMETRY);

            pIndex = pManager.AddIntegerParameter("BUR", "BUR", "Built-Up Rectangle / Box", GH_ParamAccess.item, 0);
            param = pManager[pIndex] as Param_Integer;
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.GENERIC_SECTION], (int)CustomSectionConfigOption.GENERIC_SECTION);
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.GEOMETRY], (int)CustomSectionConfigOption.GEOMETRY);
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.RHS_APPROX], (int)CustomSectionConfigOption.RHS_APPROX);

            pIndex = pManager.AddIntegerParameter("BURI", "BURI", "Built-Up RHS with added middle Web", GH_ParamAccess.item, 0);
            param = pManager[pIndex] as Param_Integer;
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.GENERIC_SECTION], (int)CustomSectionConfigOption.GENERIC_SECTION);
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.GEOMETRY], (int)CustomSectionConfigOption.GEOMETRY);
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.RHS_APPROX], (int)CustomSectionConfigOption.RHS_APPROX);

            pIndex = pManager.AddIntegerParameter("BURX", "BURX", "Built-Up RHS with added X shape inside", GH_ParamAccess.item, 0);
            param = pManager[pIndex] as Param_Integer;
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.GENERIC_SECTION], (int)CustomSectionConfigOption.GENERIC_SECTION);
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.GEOMETRY], (int)CustomSectionConfigOption.GEOMETRY);
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.RHS_APPROX], (int)CustomSectionConfigOption.RHS_APPROX);

            pIndex = pManager.AddIntegerParameter("BUO", "BUO", "Built-Up Omega section", GH_ParamAccess.item, 0);
            param = pManager[pIndex] as Param_Integer;
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.GENERIC_SECTION], (int)CustomSectionConfigOption.GENERIC_SECTION);
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.GEOMETRY], (int)CustomSectionConfigOption.GEOMETRY);
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.RHS_APPROX], (int)CustomSectionConfigOption.RHS_APPROX);

            pIndex = pManager.AddBooleanParameter("__Concrete__", "__Concrete__", "__Concrete__", GH_ParamAccess.item);
            pManager[pIndex].Optional = true;

            pIndex = pManager.AddIntegerParameter("T", "T", "T Section", GH_ParamAccess.item, 0);
            param = pManager[pIndex] as Param_Integer;
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.GENERIC_SECTION], (int)CustomSectionConfigOption.GENERIC_SECTION);
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.GEOMETRY], (int)CustomSectionConfigOption.GEOMETRY);
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.RECT_BtxH], (int)CustomSectionConfigOption.RECT_BtxH);
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.RECT_BxHsa], (int)CustomSectionConfigOption.RECT_BxHsa);
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.RECT_BxHsb], (int)CustomSectionConfigOption.RECT_BxHsb);

            pIndex = pManager.AddIntegerParameter("L", "L", "L Section", GH_ParamAccess.item, 0);
            param = pManager[pIndex] as Param_Integer;
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.GENERIC_SECTION], (int)CustomSectionConfigOption.GENERIC_SECTION);
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.GEOMETRY], (int)CustomSectionConfigOption.GEOMETRY);
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.RECT_BtxH], (int)CustomSectionConfigOption.RECT_BtxH);
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.RECT_BxHsa], (int)CustomSectionConfigOption.RECT_BxHsa);

            pIndex = pManager.AddIntegerParameter("I", "I", "I Section", GH_ParamAccess.item, 0);
            param = pManager[pIndex] as Param_Integer;
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.GENERIC_SECTION], (int)CustomSectionConfigOption.GENERIC_SECTION);
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.GEOMETRY], (int)CustomSectionConfigOption.GEOMETRY);
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.RECT_TwxH], (int)CustomSectionConfigOption.RECT_TwxH);

            pIndex = pManager.AddIntegerParameter("CRH", "CRH", "Half Circular Section", GH_ParamAccess.item, 0);
            param = pManager[pIndex] as Param_Integer;
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.GENERIC_SECTION], (int)CustomSectionConfigOption.GENERIC_SECTION);
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.GEOMETRY], (int)CustomSectionConfigOption.GEOMETRY);

            pIndex = pManager.AddIntegerParameter("CRHB", "CRHB", "Rectangle plus a Half Circular Section", GH_ParamAccess.item, 0);
            param = pManager[pIndex] as Param_Integer;
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.GENERIC_SECTION], (int)CustomSectionConfigOption.GENERIC_SECTION);
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.GEOMETRY], (int)CustomSectionConfigOption.GEOMETRY);
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.RECT_BxHminusR], (int)CustomSectionConfigOption.RECT_BxHminusR);

            pIndex = pManager.AddIntegerParameter("E", "E", "Rectangle plus two Half Circular Sections", GH_ParamAccess.item, 0);
            param = pManager[pIndex] as Param_Integer;
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.GENERIC_SECTION], (int)CustomSectionConfigOption.GENERIC_SECTION);
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.GEOMETRY], (int)CustomSectionConfigOption.GEOMETRY);
            param.AddNamedValue(enumTexts[(int)CustomSectionConfigOption.RECT_BxHminusR], (int)CustomSectionConfigOption.RECT_BxHminusR);
        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddGenericParameter("Configuration", "C", "Configuration for generic sections inputs", 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)
        {
            int bux = -1, bur = -1, buri = -1, burx = -1, buo = -1, t = -1, l = -1, i = -1, crh = -1, crhb = -1, e = -1;

            int inputCount = 0;

            inputCount++; // skip steel label
            if (!DA.GetData(inputCount, ref bux)) { return; }
            inputCount++;
            if (!DA.GetData(inputCount, ref bur)) { return; }
            inputCount++;
            if (!DA.GetData(inputCount, ref buri)) { return; }
            inputCount++;
            if (!DA.GetData(inputCount, ref burx)) { return; }
            inputCount++;
            if (!DA.GetData(inputCount, ref buo)) { return; }
            inputCount++;
            inputCount++; // skip concrete label
            if (!DA.GetData(inputCount, ref t)) { return; }
            inputCount++;
            if (!DA.GetData(inputCount, ref l)) { return; }
            inputCount++;
            if (!DA.GetData(inputCount, ref i)) { return; }
            inputCount++;
            if (!DA.GetData(inputCount, ref crh)) { return; }
            inputCount++;
            if (!DA.GetData(inputCount, ref crhb)) { return; }
            inputCount++;
            if (!DA.GetData(inputCount, ref e)) { return; }
            inputCount++;

            var config = new CustomSectionsConfiguration
            {
                BUX = (CustomSectionConfigOption)bux,
                BUR = (CustomSectionConfigOption)bur,
                BURI = (CustomSectionConfigOption)buri,
                BURX = (CustomSectionConfigOption)burx,
                BUO = (CustomSectionConfigOption)buo,
                T = (CustomSectionConfigOption)t,
                L = (CustomSectionConfigOption)l,
                I = (CustomSectionConfigOption)i,
                CRH = (CustomSectionConfigOption)crh,
                CRHB = (CustomSectionConfigOption)crhb,
                E = (CustomSectionConfigOption)e
            };

            var wrappedConfig = new GH_ObjectWrapper(config);

            DA.SetData(0, wrappedConfig);
        }

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

        public override GH_Exposure Exposure => GH_Exposure.tertiary;

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("b07d34ec-5382-4fac-994c-ae48a5e2d194");
    }
}
303 files24 directories