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;
using static FeMM.Common.Models.InsertionPointModel.AttributeData;

namespace FeMM.Grasshopper.Components.Attributes
{
#if NETCOREAPP
    [SupportedOSPlatform("windows")]
#endif
    public class BeamInsertionPointComponent : GH_Component
    {
        protected Dictionary<int, string> _insertionPointTypeOptions = new()
        {
            { 0, "BottomLeft" },
            { 1, "BottomCenter" },
            { 2, "BottomRight" },
            { 3, "MiddleLeft" },
            { 4, "MiddleCenter" },
            { 5, "MiddleRight" },
            { 6, "TopLeft" },
            { 7, "TopCenter" },
            { 8, "TopRight" },
            { 9, "Centroid" },
            { 10, "ShearCenter" },
        };

        /// <summary>
        /// Initializes a new instance of the BeamReleasesComponent class.
        /// </summary>
        public BeamInsertionPointComponent()
          : base("Beam Insertion Point", "IP", "Beam ends insertion Point (only in SAP2000)", 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("Beam", "B", "The beam where to define the insertion point", GH_ParamAccess.item);
            int i = pManager.AddIntegerParameter("Cardinal Point", "CP", "The relative position of the frame section on the line representing the frame object", GH_ParamAccess.item, 10);
            Param_Integer mtParam = pManager[i] as Param_Integer;
            foreach (KeyValuePair<int, string> v in _insertionPointTypeOptions)
                mtParam.AddNamedValue(v.Value, v.Key);
            pManager.AddNumberParameter("Offset1", "O1", "Three joint offset distances for I-End", GH_ParamAccess.list);
            pManager.AddNumberParameter("Offset2", "O2", "Three joint offset distances for J-End", GH_ParamAccess.list);
            pManager.AddTextParameter("CoordinateSystem", "CS", "It is the coordinate system in which the Offset1 and Offset2 items are specified", GH_ParamAccess.item, "Local");
            pManager.AddBooleanParameter("Stiffness Transform", "ST", "If this item is True, the frame object stiffness is transformed" +
                " for cardinal point and joint offsets from the frame section centroid", GH_ParamAccess.item, false);
            pManager.AddBooleanParameter("Mirror2", "M2", "If this item is True, the frame object section is assumed to be mirrored (flipped)" +
                " about its local 2-axis", GH_ParamAccess.item, false);
            pManager.AddBooleanParameter("Mirror3", "M3", "If this item is True, the frame object section is assumed to be mirrored (flipped) " +
                "about its local 3-axis", GH_ParamAccess.item, false);
        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddGenericParameter("Beam", "B", "The modified beam", GH_ParamAccess.item);
        }

        protected override void SolveInstance(IGH_DataAccess DA)
        {
            GH_Beam beam = null;
            int type = 0;
            var offset1 = new List<double>();
            var offset2 = new List<double>();
            bool stiffness = false;
            bool mirror2 = false;
            bool mirror3 = false;
            string coordinateSystem = string.Empty;

            if (!DA.GetData(0, ref beam))
                return;
            if (!DA.GetData(1, ref type))
                return;
            if (!DA.GetDataList(2, offset1))
                return;
            if (!DA.GetDataList(3, offset2))
                return;

            DA.GetData(4, ref coordinateSystem);
            DA.GetData(5, ref stiffness);
            DA.GetData(6, ref mirror2);
            DA.GetData(7, ref mirror3);

            if (offset1 != null && offset1.Count != 3)
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Offset1 must be a 3 item list");
            if (offset2 != null && offset2.Count != 3)
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Offset2 must be a 3 item list");

            var newBeam = new GH_Beam(beam);

            var data = new InsertionPointModel.AttributeData()
            {
                CardinalPoint = (CardinalPoints)(type + 1),
                Offset1 = [.. offset1],
                Offset2 = [.. offset2],
                CoordinateSystem = coordinateSystem,
                StiffnessTransform = stiffness,
                Mirror2 = mirror2,
                Mirror3 = mirror3,
            };

            var attr = new InsertionPointModel(data);
            newBeam.Value.Attributes.Add(attr);

            DA.SetData("Beam", newBeam);
        }

        public override GH_Exposure Exposure => GH_Exposure.secondary;

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

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("d4da4c2d-0962-42dd-9812-6081e6f8de5c");
    }
}
303 files24 directories