using FeMM.Grasshopper.Helpers;
using Grasshopper.Kernel;
using St7API;
using System;
using System.Collections.Generic;
using System.Text;

namespace FeMM.Grasshopper.Components.ST2R
{
    public class St7ReadLoadCombinationNamesComponent : GH_Component
    {
        private bool _run = false;

        public St7ReadLoadCombinationNamesComponent()
            : base("Read Load Combinations Names", "ReadLoadCombinationsNames",
                  "Reads the names of load combinations from the result file",
                  CategoryNameConstants.CATEGORY_F2R, CategoryNameConstants.SUBCATEGORY_ST2R_BAKE)
        {
        }

        public override void CreateAttributes()
        {
            var buttonAttributes = new ComponentAttributes.ComponentOneButtonAttributes(this, "Read");
            buttonAttributes.ButtonPressed += () =>
            {
                _run = true;
                ExpireSolution(true);
            };
            m_attributes = buttonAttributes;
        }

        protected override void RegisterInputParams(GH_InputParamManager p)
        {
            p.AddIntegerParameter("uID", "ID", "uID of the already opened Straus7 model", GH_ParamAccess.item);
            p.AddTextParameter("Result File", "Res", "Path to the .RST / .NLA / .NLF result file", GH_ParamAccess.item);
        }

        protected override void RegisterOutputParams(GH_OutputParamManager p)
        {
            p.AddTextParameter("Load Combination Names", "Names", "Names of linear load case combinations", GH_ParamAccess.list);
            p.AddTextParameter("Log", "Log", "Debug log messages", GH_ParamAccess.list);
        }

        protected override void SolveInstance(IGH_DataAccess DA)
        {
            int uID = 0;
            string resultFile = "";
            if (!DA.GetData(0, ref uID)) return;
            if (!DA.GetData(1, ref resultFile)) return;

            List<string> combinationNames = [];
            List<string> log = [];

            if (!_run)
            {
                DA.SetDataList(0, combinationNames);
                DA.SetDataList(1, log);
                return;
            }

            log.Add($"Opening result file: {resultFile}");
            int numPrimary = 0, numSecondary = 0;
            int err = St7.St7OpenResultFile(uID, resultFile, null, St7.kNoCombinations, ref numPrimary, ref numSecondary);

            if (err != St7.ERR7_NoError)
            {
                log.Add($"Error opening result file (code {err})");
                DA.SetDataList(0, combinationNames);
                DA.SetDataList(1, log);
                _run = false;
                return;
            }

            log.Add($"Result file opened. Primary={numPrimary}, Secondary={numSecondary}");

            // Iterate over load combinations (1..numPrimary)
            for (int i = 1; i <= numPrimary; i++)
            {
                StringBuilder name = new(256);
                err = St7.St7GetLSACombinationName(uID, i, name, name.Capacity);
                if (err != St7.ERR7_NoError)
                {
                    log.Add($"Error reading load combination {i} (code {err})");
                    continue;
                }

                combinationNames.Add(name.ToString().TrimEnd('\0'));
                log.Add($"Found load combination {i}: {name}");
            }

            DA.SetDataList(0, combinationNames);
            DA.SetDataList(1, log);

            _run = false;
        }

        protected override System.Drawing.Bitmap Icon => Properties.Resources.St7ReadLoadCombinationNamesIcon;

        public override Guid ComponentGuid => new("fa7d1b52-3e4f-4f7e-9b12-123456789abc");
    }
}
303 files24 directories