using FeMM.Common.Models;
using FeMM.Grasshopper.DataTypes.FeMM;
using FeMM.Grasshopper.Helpers;
using Grasshopper.Kernel;
using Rhino.Geometry;
using System;
using System.Runtime.Versioning;

namespace FeMM.Grasshopper.Components.Loads
{
#if NETCOREAPP
    [SupportedOSPlatform("windows")]
#endif
    public class NodeLoadComponent : GH_Component
    {
        /// <summary>
        /// Initializes a new instance of the NodeLoadComponent class.
        /// </summary>
        public NodeLoadComponent()
          : base("Node Load", "NL", "Defines a node load", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_LOADS)
        {
        }

        /// <summary>
        /// Registers all the input parameters for this component.
        /// </summary>
        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            pManager.AddGenericParameter("Node", "N", "The node where define the restraits", GH_ParamAccess.item);
            pManager.AddGenericParameter("LoadCase", "LC", "The load case", GH_ParamAccess.item);
            pManager.AddVectorParameter("Force", "F", "The vector force load in the global XYZ direction", GH_ParamAccess.item);
            int i = pManager.AddGenericParameter("Coordinate System", "CS", "The restraint coordiante system", GH_ParamAccess.item);
            pManager[i].Optional = true;
        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddGenericParameter("Node", "N", "The modified node", 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_Node node = null;
            GH_Case lc = null;
            Vector3d force = Vector3d.Unset;
            GH_FunctionDefinition coordinateSystemModel = null;

            if (!DA.GetData("Node", ref node))
                return;
            if (!DA.GetData("LoadCase", ref lc))
                return;
            if (!DA.GetData("Force", ref force))
                return;
            bool hasCS = DA.GetData("Coordinate System", ref coordinateSystemModel);

            var load = new NodeForceLoadModel((LoadCaseModel)lc.Value, new Vector3d(force.X, force.Y, force.Z));
            if (hasCS)
            {
                if(coordinateSystemModel != null && coordinateSystemModel.Value is CoordinateSystemModel css) 
                    load.CoordinateSystem = css;
            }
            else
                load.CoordinateSystem = CoordinateSystemModel.Global;

            var newNode = new GH_Node(node);
            newNode.Value.Loads.Add(load);
            Message = string.Format("Coordinate System: {0}", load.CoordinateSystem.Name);

            DA.SetData("Node", newNode);
        }

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

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("554499f4-e448-46a5-a457-40e308be5c87");
    }
}
303 files24 directories