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.Linq;
using System.Runtime.Versioning;

namespace FeMM.Grasshopper.Components.Elements
{
#if NETCOREAPP
    [SupportedOSPlatform("windows")]
#endif
    public class NodeOverrideComponent : GH_Component
    {
        /// <summary>
        /// Initializes a new instance of the BeamOverrideComponent class.
        /// </summary>
        public NodeOverrideComponent()
          : base("Node Override", "NO", "Override the creation parameters", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_ELEMENTS)
        {
        }

        /// <summary>
        /// Registers all the input parameters for this component.
        /// </summary>
        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            pManager.AddGenericParameter("Node", "N", "The node where override the assignments", GH_ParamAccess.item);
            pManager.AddGenericParameter("Groups", "G", "The groups to assign to the beam", GH_ParamAccess.list);
            pManager[1].Optional = true;
            pManager.AddNumberParameter("Coordinate System", "CS", "The new node coordinate system", GH_ParamAccess.item);
            pManager[2].Optional = true;
            pManager.AddVectorParameter("Offset", "O", "The translation offset along the global axis", GH_ParamAccess.item);
            pManager[3].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;
            var groups = new List<GH_Group>();
            GH_FunctionDefinition cs = null;
            Vector3d offset = Vector3d.Unset;
            if (!DA.GetData("Node", ref node))
                return;
            bool has_groups = DA.GetDataList("Groups", groups);
            bool has_CS = DA.GetData("Coordinate System", ref cs);
            bool has_offset = DA.GetData("Offset", ref offset);

            if (!has_groups && !has_CS && !has_offset)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Almost one of parameter must to collect data");
                DA.SetData(0, new GH_Node(node));
                return;
            }

            var newNode = new GH_Node(node);
            if (has_groups)
            {
                newNode.Value.Groups.Clear();
                foreach (GroupModel group in groups.Select(g => g.Value))
                    newNode.Value.Groups.Add(group);
            }
            if (has_CS)
            {
                if(cs != null && cs.IsValid && cs.Value is CoordinateSystemModel coordinateSystemModel)
                    newNode.Value.CoordinateSystem= coordinateSystemModel;
            }

            if (has_offset)
            {
                Point3d newPos = newNode.Value.Position;
                newPos += offset;
                newNode.Value.Position = newPos;
            }

            DA.SetData(0, newNode);
        }

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

        public override GH_Exposure Exposure => GH_Exposure.secondary;

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("8883f344-7a61-4e8f-ad21-fd8a00642f5a");
    }
}
303 files24 directories