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

namespace FeMM.Grasshopper.Components.Attributes
{
#if NETCOREAPP
    [SupportedOSPlatform("windows")]
#endif
    public class NodeStiffnessComponent : GH_Component
    {
        /// <summary>
        /// Initializes a new instance of the NodeRestraintsComponent class.
        /// </summary>
        public NodeStiffnessComponent()
          : base("Stiffness", "NS", "Define a node stiffness", 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("Node", "N", "The node where define the restraits", GH_ParamAccess.item);
            int i = 0;
            i = pManager.AddVectorParameter("Transational", "T", "The transational stiffness", GH_ParamAccess.item);
            pManager[i].Optional = true;
            i = pManager.AddVectorParameter("Rotational", "R", "The rotational stiffness", GH_ParamAccess.item);
            pManager[i].Optional = true;
            i = pManager.AddGenericParameter("Freedom Cases", "FC", "The freedom cases", GH_ParamAccess.list);
            pManager[i].Optional = true;
            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;
            Vector3d trans = Vector3d.Unset;
            Vector3d rot = Vector3d.Unset;
            var freedomCases = new List<GH_FreedomCase>();
            GH_FunctionDefinition coordinateSystemModel = null;

            if (!DA.GetData("Node", ref node))
                return;
            DA.GetData("Transational", ref trans);
            DA.GetData("Rotational", ref rot);
            DA.GetDataList(3, freedomCases);
            DA.GetData(4, ref coordinateSystemModel);

            if (trans == Vector3d.Unset && rot == Vector3d.Unset)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Insert a value of stiffness");
                DA.AbortComponentSolution();
                return;
            }

            var newNode = new GH_Node(node);

            var attr = new NodeStiffnessAttributeModel.AttributeData();
            if (trans != Vector3d.Unset)
            {
                attr.Tx = trans.X;
                attr.Ty = trans.Y;
                attr.Tz = trans.Z;
            }
            if (rot != Vector3d.Unset)
            {
                attr.Rx = rot.X;
                attr.Ry = rot.Y;
                attr.Rz = rot.Z;
            }

            var nodeAttr = new NodeStiffnessAttributeModel(attr);
            if (freedomCases.Count > 0)
            {
                for (int i = 0; i < freedomCases.Count; i++)
                    nodeAttr.Value.FreedomCases.Add(new FreedomCaseModel(freedomCases[i].Value));
            }
            else
                nodeAttr.Value.FreedomCases.Add(FreedomCaseModel.UnsetFreedomCase);

            if (coordinateSystemModel != null && coordinateSystemModel.Value is CoordinateSystemModel css)
                nodeAttr.Value.CoordinateSystem = css;
            newNode.Value.Attributes.Add(nodeAttr);

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

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

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("CA28E9B9-5A50-4F45-904E-3B6D1CA6CAA1");
    }
}
303 files24 directories