using FeMM.Grasshopper.Helpers;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;
using St7API;
using System;
using System.Collections.Generic;
namespace FeMM.Grasshopper.Components.ST2R
{
public class St7GetAllPlateConnectivityComponent : GH_Component
{
private bool _run = false;
public St7GetAllPlateConnectivityComponent()
: base("Get Plate Connectivity", "GetPlateConnectivity",
"Returns plate connectivity",
CategoryNameConstants.CATEGORY_F2R, CategoryNameConstants.SUBCATEGORY_ST2R_BAKE)
{ }
public override void CreateAttributes()
{
var buttonAttributes = new ComponentAttributes.ComponentOneButtonAttributes(this, "Get Plate Connectivity");
buttonAttributes.ButtonPressed += () =>
{
_run = true;
ExpireSolution(true); // triggers SolveInstance
};
m_attributes = buttonAttributes;
}
protected override void RegisterInputParams(GH_InputParamManager p)
{
p.AddIntegerParameter("uID", "ID", "Straus7 model file ID", GH_ParamAccess.item);
}
protected override void RegisterOutputParams(GH_OutputParamManager p)
{
p.AddIntegerParameter("PlateNumbers", "Plates", "Plate numbers", GH_ParamAccess.tree);
p.AddIntegerParameter("NumNodes", "NumNodes", "Number of nodes in each plate", GH_ParamAccess.tree);
p.AddIntegerParameter("NodeNumbers", "Nodes", "Node numbers for each plate", GH_ParamAccess.tree);
p.AddTextParameter("Log", "Log", "Debug messages", GH_ParamAccess.list);
}
protected override void SolveInstance(IGH_DataAccess DA)
{
int uid = 0;
if (!DA.GetData(0, ref uid)) return;
List<string> log = [];
GH_Structure<GH_Integer> plateNumbersTree = new();
GH_Structure<GH_Integer> numNodesTree = new();
GH_Structure<GH_Integer> nodeNumbersTree = new();
if (!_run)
{
DA.SetDataTree(0, plateNumbersTree);
DA.SetDataTree(1, numNodesTree);
DA.SetDataTree(2, nodeNumbersTree);
DA.SetDataList(3, log);
return;
}
int plateNum = 1;
while (true)
{
int[] connection = new int[20]; // max nodes per plate
long err = St7.St7GetElementConnection(uid, St7.tyPLATE, plateNum, connection);
if (err != 0 || connection[0] == 0)
{
log.Add($"Stopped at Plate {plateNum}, error code {err}");
break;
}
int numNodes = (int)connection[0]; // first number = number of nodes
GH_Path platePath = new(plateNum - 1);
// Output the plate number
plateNumbersTree.Append(new GH_Integer(plateNum), platePath);
// Output the number of nodes
numNodesTree.Append(new GH_Integer(numNodes), platePath);
// Output the node numbers (excluding the first number)
for (int i = 1; i <= numNodes; i++)
{
nodeNumbersTree.Append(new GH_Integer((int)connection[i]), platePath);
}
log.Add($"Plate {plateNum}: NumNodes={numNodes}, Nodes={string.Join(",", connection, 1, numNodes)}");
plateNum++;
}
DA.SetDataTree(0, plateNumbersTree);
DA.SetDataTree(1, numNodesTree);
DA.SetDataTree(2, nodeNumbersTree);
DA.SetDataList(3, log);
_run = false;
}
protected override System.Drawing.Bitmap Icon => Properties.Resources.St7GetAllPlateConnectivityIcon;
public override Guid ComponentGuid => new("b5c6d7e8-9012-4abc-9d12-abcdef123469");
}
}