using FeMM.Grasshopper.Helpers;
using Grasshopper.Kernel;
using St7API;
using System;
using System.Collections.Generic;
namespace FeMM.Grasshopper.Components.ST2R
{
public class St7SelectBeamsByGroupComponent : GH_Component
{
private bool _run = false;
public St7SelectBeamsByGroupComponent()
: base(
"Select Beams by Group",
"SelectBeams",
"Selects beams in specified groups",
CategoryNameConstants.CATEGORY_F2R, CategoryNameConstants.SUBCATEGORY_ST2R_BAKE)
{ }
public override void CreateAttributes()
{
var buttonAttributes = new ComponentAttributes.ComponentOneButtonAttributes(this, "Apply");
buttonAttributes.ButtonPressed += () =>
{
_run = true;
ExpireSolution(true);
};
m_attributes = buttonAttributes;
}
protected override void RegisterInputParams(GH_InputParamManager p)
{
p.AddIntegerParameter("uID", "ID", "Straus7 model file ID", GH_ParamAccess.item);
p.AddIntegerParameter("Groups", "G", "Group IDs to select beams from", GH_ParamAccess.list);
}
protected override void RegisterOutputParams(GH_OutputParamManager p)
{
p.AddIntegerParameter("Total Beams", "Tot", "Total number of beam elements in the model", GH_ParamAccess.item);
p.AddIntegerParameter("Selected Beams", "SelTot", "Number of beams currently selected in the model", GH_ParamAccess.item);
p.AddTextParameter("Log", "Log", "Debug messages", GH_ParamAccess.list);
}
protected override void SolveInstance(IGH_DataAccess DA)
{
int uID = 0;
List<int> groups = [];
if (!DA.GetData(0, ref uID)) return;
if (!DA.GetDataList(1, groups)) return;
List<string> log = [];
if (!_run)
{
DA.SetData(0, 0);
DA.SetData(1, 0);
DA.SetDataList(2, log);
return;
}
// 1. Get total beams in the model
int totalBeams = 0;
int err = St7.St7GetTotal(uID, St7.tyBEAM, ref totalBeams);
if (err != 0)
{
log.Add($"Error reading total beams: {err}");
DA.SetData(0, 0);
DA.SetData(1, 0);
DA.SetDataList(2, log);
_run = false;
return;
}
log.Add($"Total beams in model: {totalBeams}");
// 2. Select beams for each group (+1 to user input)
foreach (int gid in groups)
{
int adjustedGid = gid + 1; // add 1 to the group ID
int errSel = St7.St7SetEntitySelectStateByGroup(uID, St7.tyBEAM, adjustedGid, 1); // always select
if (errSel != 0)
log.Add($"Error selecting beams in Group {adjustedGid}: {errSel}");
else
log.Add($"Selected all beams in Group {adjustedGid}");
}
// 3. Get number of selected beams
int selectedBeams = 0;
int errCount = St7.St7GetEntitySelectCount(uID, St7.tyBEAM, ref selectedBeams);
if (errCount != 0)
{
log.Add($"Error reading selected beam count: {errCount}");
selectedBeams = 0;
}
else
{
log.Add($"Total beams currently selected: {selectedBeams}");
}
// 4. Output
DA.SetData(0, totalBeams);
DA.SetData(1, selectedBeams);
DA.SetDataList(2, log);
_run = false;
}
protected override System.Drawing.Bitmap Icon => Properties.Resources.St7SelectBeamsByGroupIcon;
public override Guid ComponentGuid =>
new("b55a5c60-a334-4e16-aa19-12bca4fe1d65");
}
}