using FeMM.Common.Helpers;
using FeMM.Common.Models;
using FeMM.Grasshopper.DataTypes.FeMM;
using FeMM.Grasshopper.Helpers;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Parameters;
using Grasshopper.Kernel.Types;
using System;
using System.Runtime.Versioning;

namespace FeMM.Grasshopper.Components.ElementProperties
{
#if NETCOREAPP
    [SupportedOSPlatform("windows")]
#endif
    public class NomenclatureBeamPropertyComponent : GH_Component
    {
        /// <summary>
        /// Initializes a new instance of the NomenclatureBeamPropertyComponent class.
        /// </summary>
        public NomenclatureBeamPropertyComponent()
          : base("Nomenclature Beam Property", "NBP", "Nomenclature Beam properties", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_PROPERTIES)
        {
        }

        /// <summary>
        /// Registers all the input parameters for this component.
        /// </summary>
        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            pManager.AddTextParameter("Nomenclature", "N", "Full or partial Maffeis nomenclature", GH_ParamAccess.item);
            pManager.AddGenericParameter("Custom Config", "C", "Configrations for custom sections", GH_ParamAccess.item);
            pManager[1].Optional = true;
            pManager.AddIntegerParameter("Element Type", "E", "Type of FEM element", GH_ParamAccess.item, -1);
            var param = pManager[2] as Param_Integer;
            param.AddNamedValue("Cable [1]", (int)BeamPropertyType.Cable);
            param.AddNamedValue("Truss [2]", (int)BeamPropertyType.Truss);
            param.AddNamedValue("Cutoff Bar [3]", (int)BeamPropertyType.CutoffBar);
            param.AddNamedValue("Beam [5]", (int)BeamPropertyType.Beam);
            param.AddNamedValue("Connection [8]", (int)BeamPropertyType.Connection);
            pManager[2].Optional = true;
            pManager.AddGenericParameter("Material", "M", "Material", GH_ParamAccess.item);
            pManager[3].Optional = true;
            pManager.AddNumberParameter("Max Tension", "MT", "Max tension (Cutoff Bar)", GH_ParamAccess.item, 0);
            pManager.AddNumberParameter("Max Compression", "MC", "Max compression (Cutoff Bar)", GH_ParamAccess.item, 0);
        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddGenericParameter("Property", "BP", "Beam property", GH_ParamAccess.item);
            pManager.AddGenericParameter("ValidMask", "VM", "Is a valid name boolean mask", 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 nomenclature = "";
            NamingConventionHelper.CustomSectionsConfiguration? config = null;
            var wrapper = new GH_ObjectWrapper();
            var elementType = (int)BeamPropertyType.Undefined;
            var ghmaterial = new FeMM.Grasshopper.DataTypes.FeMM.GH_Material();
            var maxTension = 0.0;
            var maxCompression = 0.0;

            if (!DA.GetData(0, ref nomenclature))
                return;
            if (DA.GetData(1, ref wrapper))
            {
                if (wrapper.Value is NamingConventionHelper.CustomSectionsConfiguration customSectionConfiguration)
                    config = customSectionConfiguration;
                else
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid configuration");
            }
            DA.GetData(2, ref elementType);
            DA.GetData(3, ref ghmaterial);
            DA.GetData(4, ref maxTension);
            DA.GetData(5, ref maxCompression);

            try
            {
                var profile = AppsClient.Profiles.Profiles.GetProfileByMaffeisName(nomenclature);

                if (profile == null)
                {
                    DA.SetData(1, false);
                    return;
                }

                var bp = NamingConventionHelper.ConvertProfileToBeamProperty(profile, config, (BeamPropertyType)elementType, ghmaterial.Value);

                if (elementType == (int)BeamPropertyType.CutoffBar)
                {
                    bp.CutoffType = CutoffType.Brittle;
                }

                if (maxTension != 0 || maxCompression != 0 || elementType == (int)BeamPropertyType.CutoffBar)
                {
                    bp.MaxTension     = maxTension;
                    bp.MaxCompression = maxCompression;
                }

                GH_FunctionDefinition gh_bp = new(bp);
                DA.SetData(0, gh_bp);
                DA.SetData(1, bp != null && bp.Section.IsValid && bp.SectionArea > 0);
            }
            catch
            {
                DA.SetData(0, null);
                DA.SetData(1, false);
            }
        }

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

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("949ba094-c736-4f19-b967-07e9d668d406");

    }
}
303 files24 directories