using FeMM.Common.Models;
using FeMM.Grasshopper.DataTypes.FeMM;
using FeMM.Grasshopper.Helpers;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Types;
using System;
using System.Linq;
namespace FeMM.Grasshopper.Components.Attributes
{
public class GetElementIdComponent : GH_Component
{
/// <summary>
/// Initializes a new instance of the SuperElementComponent class.
/// </summary>
public GetElementIdComponent()
: base("Get Element Id", "GEI", "Get element ID for node/beam/plate", 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("Element", "E", "The element where assign the element ID", GH_ParamAccess.item);
}
/// <summary>
/// Registers all the output parameters for this component.
/// </summary>
protected override void RegisterOutputParams(GH_OutputParamManager pManager)
{
pManager.AddTextParameter("ID", "ID", "The ID assigned by user", GH_ParamAccess.item);
pManager.AddNumberParameter("Number", "N", "The internal number assigned by FeMM and the position in the list" +
"after build model preprocessing", 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)
{
IGH_GeometricGoo element = null;
if (!DA.GetData(0, ref element))
return;
System.Collections.Generic.List<ElementIdAttributeModel> ids = null;
int number = -1;
if (element is GH_Beam beam)
{
ids = [.. beam.Value.Attributes.Where(i => i is ElementIdAttributeModel).Select(i => (ElementIdAttributeModel)i)];
if (beam.Value.BeamId != string.Empty)
{
try
{
number = int.Parse(beam.Value.BeamId);
}
catch
{
number = -1;
}
}
}
else if (element is GH_Plate plate)
{
ids = [.. plate.Value.Attributes.Where(i => i is ElementIdAttributeModel).Select(i => (ElementIdAttributeModel)i)];
if (plate.Value.PlateId != string.Empty)
{
try
{
number = int.Parse(plate.Value.PlateId);
}
catch
{
number = -1;
}
}
}
else if (element is GH_Node node)
{
ids = [.. node.Value.Attributes.Where(i => i is ElementIdAttributeModel).Select(i => (ElementIdAttributeModel)i)];
if (node.Value.NodeId != string.Empty)
{
try
{
number = int.Parse(node.Value.NodeId);
}
catch
{
number = -1;
}
}
}
else if (element is GH_Link link)
{
ids = [.. link.Value.Attributes.Where(i => i is ElementIdAttributeModel).Select(i => (ElementIdAttributeModel)i)];
if (link.Value.LinkId != string.Empty)
{
try
{
number = int.Parse(link.Value.LinkId);
}
catch
{
number = -1;
}
}
}
else
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Unsupported element type");
}
if (ids.Count == 0)
{
DA.SetData(0, "");
}
else if (ids.Count > 1)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Beam has more than one ID");
DA.SetData(0, ids[0].Value);
}
else
{
DA.SetData(0, ids[0].Value);
}
DA.SetData(1, number);
}
public override GH_Exposure Exposure => GH_Exposure.quarternary;
/// <summary>
/// Provides an Icon for the component.
/// </summary>
protected override System.Drawing.Bitmap Icon => Properties.Resources.ElementIdIcon;
/// <summary>
/// Gets the unique ID for this component. Do not change this ID after release.
/// </summary>
public override Guid ComponentGuid => new("41a60818-29e8-4053-846c-6d9d6e62888a");
}
}