using FeMM.Common.Helpers;
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;

namespace FeMM.Grasshopper.Components.Definitions
{
    public class ConstraintDiaphragmComponent : GH_Component
    {
        /// <summary>
        /// Initializes a new instance of the GroupComponent class.
        /// </summary>
        public ConstraintDiaphragmComponent()
          : base("Diaphragm Constraint", "D", "Defines a diaphragm constraint", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_DEFINITIONS)
        {
        }

        /// <summary>
        /// Registers all the input parameters for this component.
        /// </summary>
        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            pManager.AddTextParameter("Name", "N", "The load case name", GH_ParamAccess.item);
            pManager.AddIntegerParameter("Axes", "A", "This is one of the following items from the eConstraintAxis enumeration.\n" +
                "It specifies the axis in the specified coordinate system that is perpendicular to the plane of the constraint.\n" +
                "If AutoAxis is specified, the axis of the constraint is automatically determined from the joints assigned to the constraint.", GH_ParamAccess.item, 3);
            Param_Integer type_param = (Param_Integer)pManager[1];
            Array values = Enum.GetValues(typeof(NodeDiaphragmConstraintModel.Axis));
            foreach (NodeDiaphragmConstraintModel.Axis val in values)
                type_param.AddNamedValue(val.GetDescription(), (int)val);
            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);
        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddGenericParameter("Diaphragm", "D", "The defined Diaphragm", 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 name = "";
            int axes = 0;
            GH_FunctionDefinition cs = null;
            if (!DA.GetData(0, ref name))
                return;
            if (!DA.GetData(1, ref axes))
                return;
            bool has_cs = DA.GetData("Coordinate System", ref cs);

            Message = name;

            var model = new NodeDiaphragmConstraintModel(name)
            {
                Axes = (NodeDiaphragmConstraintModel.Axis)axes,
            };
            if(has_cs)
            {
                if(cs.IsValid && cs.Value is CoordinateSystemModel css)
                    model.CoordinateSystem = css;
            }
            else
                model.CoordinateSystem = CoordinateSystemModel.Global;
            var fd = new GH_FunctionDefinition(model);
            DA.SetData(0, fd);
        }
        
        public override GH_Exposure Exposure => GH_Exposure.secondary;

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

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("ace10a38-2030-484a-a782-58200a36773e");
    }
}
303 files24 directories