using FeMM.Common.Helpers;
using FeMM.Common.Models;
using FeMM.Grasshopper.DataTypes.FeMM;
using FeMM.Grasshopper.Helpers;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Parameters;
using Rhino.Geometry;
using System;
using System.Collections.Generic;
using System.Runtime.Versioning;
namespace FeMM.Grasshopper.Components.Tools
{
#if NETCOREAPP
[SupportedOSPlatform("windows")]
#endif
class LinkFilterComponent : GH_Component
{
/// <summary>
/// Initializes a new instance of the ElementFilterComponent class.
/// </summary>
public LinkFilterComponent()
: base("Link Filter", "LF", "Link filter", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_TOOLS)
{
}
/// <summary>
/// Registers all the input parameters for this component.
/// </summary>
protected override void RegisterInputParams(GH_InputParamManager pManager)
{
pManager.AddGenericParameter("Link", "Ls", "The links to filter", GH_ParamAccess.list);
pManager.AddTextParameter("IDs", "ID", "The element IDs to search", GH_ParamAccess.list);
pManager[pManager.ParamCount - 1].Optional = true;
pManager.AddPointParameter("Ini Pos", "IPs", "The initial positions to search", GH_ParamAccess.list);
pManager[pManager.ParamCount - 1].Optional = true;
pManager.AddPointParameter("End Pos", "EPs", "The end positions to search", GH_ParamAccess.list);
pManager[pManager.ParamCount - 1].Optional = true;
pManager.AddIntegerParameter("Units", "U", "The measure units", GH_ParamAccess.item, 2);
Param_Integer type_param = (Param_Integer)pManager[pManager.ParamCount - 1];
Array names = Enum.GetValues(typeof(ModelModel.ModelUnits));
foreach (ModelModel.ModelUnits name in names)
type_param.AddNamedValue(name.GetDescription(), (int)name);
pManager.AddTextParameter("Properties name", "PNs", "The link property names to search", GH_ParamAccess.list);
pManager[pManager.ParamCount - 1].Optional = true;
pManager.AddGenericParameter("Groups", "G", "The node groups to search", GH_ParamAccess.list);
pManager[pManager.ParamCount - 1].Optional = true;
}
/// <summary>
/// Registers all the output parameters for this component.
/// </summary>
protected override void RegisterOutputParams(GH_OutputParamManager pManager)
{
pManager.AddGenericParameter("Links", "Ls", "The filtered links", GH_ParamAccess.list);
pManager.AddGenericParameter("Others", "Os", "The other links", 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)
{
var links = new List<GH_Link>();
var ids = new List<string>();
var iniPositions = new List<Point3d>();
var endPositions = new List<Point3d>();
int units = -1;
var props = new List<string>();
var groups = new List<GH_Group>();
if (!DA.GetDataList(0, links))
return;
DA.GetDataList(1, ids);
DA.GetDataList(2, iniPositions);
DA.GetDataList(3, endPositions);
DA.GetData(4, ref units);
DA.GetDataList(5, props);
DA.GetDataList(6, groups);
if (ids.Count == 0 && iniPositions.Count == 0 && endPositions.Count == 0 && props.Count == 0 && groups.Count == 0)
{
DA.AbortComponentSolution();
return;
}
var filtered = new List<GH_Link>();
var others = new List<GH_Link>();
double tol = Helpers.ModelHelper.GetTolerance();
for (int i = 0; i < links.Count; i++)
{
var b = links[i];
bool add = false;
for (int j = 0; j < ids.Count; j++)
{
string id = ids[j];
if (b.Value.HasId(id))
{
add = true;
break;
}
}
if (!add)
{
for (int j = 0; j < groups.Count; j++)
{
GH_Group g = groups[j];
if (b.Value.HasGroup(g.Value))
{
add = true;
break;
}
}
if (!add)
{
for (int k = 0; k < props.Count; k++)
{
string bp = props[k];
if (bp == b.Value.LinkProperty.Name)
{
add = true;
break;
}
}
if (!add)
{
for (int k = 0; k < iniPositions.Count; k++)
{
Point3d p = iniPositions[k];
if (b.Value.PointFrom.DistanceTo(p) < tol)
{
add = true;
break;
}
}
if (!add)
{
for (int j = 0; j < endPositions.Count; j++)
{
Point3d p = endPositions[j];
if (b.Value.PointTo.DistanceTo(p) < tol)
{
add = true;
break;
}
}
}
}
}
}
if (add)
filtered.Add(b);
else
others.Add(b);
}
DA.SetDataList(0, filtered);
DA.SetDataList(1, others);
}
public override GH_Exposure Exposure => GH_Exposure.secondary;
/// <summary>
/// Provides an Icon for the component.
/// </summary>
protected override System.Drawing.Bitmap Icon => Properties.Resources.BeamFilterIcon;
/// <summary>
/// Gets the unique ID for this component. Do not change this ID after release.
/// </summary>
public override Guid ComponentGuid => new("74d7ee42-2afe-4a8e-8b4a-07e307c04552");
}
}