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;

namespace FeMM.Grasshopper.Components.Results
{
    public class PlateResultsComponent : GH_Component
    {
        /// <summary>
        /// Initializes a new instance of the PlateResultsComponent class.
        /// </summary>
        public PlateResultsComponent()
          : base("Plate Results", "PR", "The Plate analysis results", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_RESULTS)
        {
        }

        /// <summary>
        /// Registers all the input parameters for this component.
        /// </summary>
        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            pManager.AddGenericParameter("Plate", "P", "The plate with results", GH_ParamAccess.item);
        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddTextParameter("Load cases", "LC", "The load case name", GH_ParamAccess.list);
            pManager.AddTextParameter("Nodes", "N", "Node id of the results", GH_ParamAccess.list);
            pManager.AddNumberParameter("Steps", "S", "The steps", GH_ParamAccess.list);
            pManager.AddVectorParameter("Force", "F", "Internal direct force per length (X = F11 direct force, Y = F22 direct force, Z = F12 shear force) (>0 st7)", GH_ParamAccess.list);
            pManager.AddNumberParameter("Force Max", "FMAX", "The maximum principal force per length (>0 st7)", GH_ParamAccess.list);
            pManager.AddNumberParameter("Force Min", "FMIN", "The minimum principal force per length (>0 st7)", GH_ParamAccess.list);
            pManager.AddNumberParameter("Force Angle", "FANG", "The angle from the area local 1 axis to the direction of the maximum principal force (>0 st7)", GH_ParamAccess.list);
            pManager.AddNumberParameter("Force Von Mises", "FVM", "The internal Von Mises force per length (>0 st7)", GH_ParamAccess.list);
            pManager.AddVectorParameter("Moment", "M", "The internal moment per length (X = M11 bending moment, Y = M22 bending moment, Z = M12 twisting moment) (>0 st7)", GH_ParamAccess.list);
            pManager.AddNumberParameter("Moment Max", "MMAX", "The maximum principal moment per length (>0 st7)", GH_ParamAccess.list);
            pManager.AddNumberParameter("Moment Min", "MMIN", "The minimum principal moment per length (>0 st7)", GH_ParamAccess.list);
            pManager.AddNumberParameter("Moment Angle", "MANG", "The angle from the area local 1 axis to the direction of the maximum principal moment (>0 st7)", GH_ParamAccess.list);
            pManager.AddVectorParameter("Shear Force", "SF", "The transverse shear force per length (X = V13, Y = V23) (>0 st7)", GH_ParamAccess.list);
            pManager.AddNumberParameter("Shear Force Max", "VMAX", "The maximum transverse shear force (only sap2000)", GH_ParamAccess.list);
            pManager.AddNumberParameter("Shear Force Angle", "VANG", "The angle from the area local 1 axis to the direction of Vmax (only sap2000)", GH_ParamAccess.list);
        }

        /// <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_PlateResults plate = null;
            if (!DA.GetData(0, ref plate))
                return;

            var lc = new List<string>();
            var nodes = new List<string>();
            var steps = new List<double>();
            var forces = new List<Vector3d>();
            var fmax = new List<double>();
            var fmin = new List<double>();
            var fang = new List<double>();
            var fvm = new List<double>();
            var moments = new List<Vector3d>();
            var mmax = new List<double>();
            var mmin = new List<double>();
            var mang = new List<double>();
            var shearForces = new List<Vector2d>();
            var sf_max = new List<double>();
            var sf_ang = new List<double>();
            foreach (ResultModel result in plate.Value)
            {
                lc.Add(result.LoadCase.Name);
                steps.Add(result.StepNum);
                if (result is PlateForceResultModel force)
                {
                    nodes.Add(force.NodeId);
                    forces.Add(force.Force);
                    fmax.Add(force.ForceMax);
                    fmin.Add(force.ForceMin);
                    fang.Add(force.ForceAngle);
                    fvm.Add(force.ForceVonMises);
                    moments.Add(force.Moment);
                    mmax.Add(force.MomentMax);
                    mmin.Add(force.MomentMin);
                    mang.Add(force.MomentAngle);
                    shearForces.Add(force.TransverseShearForce);
                    sf_max.Add(force.TransverseShearForceMax);
                    sf_ang.Add(force.TransverseShearForceAng);
                }
            }

            DA.SetDataList(0, lc);
            DA.SetDataList(1, nodes);
            DA.SetDataList(2, steps);
            DA.SetDataList(3, forces);
            DA.SetDataList(4, fmax);
            DA.SetDataList(5, fmin);
            DA.SetDataList(6, fang);
            DA.SetDataList(7, fvm);
            DA.SetDataList(8, moments);
            DA.SetDataList(9, mmax);
            DA.SetDataList(10, mmin);
            DA.SetDataList(11, mang);
            DA.SetDataList(12, shearForces);
            DA.SetDataList(13, sf_max);
            DA.SetDataList(14, sf_ang);
        }

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

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("30f8eb49-b4d2-4bd1-b667-7d26d45d2daf");
    }
}
303 files24 directories