using ETABSv1;
using FeMM.Grasshopper.Helpers;
using Grasshopper.Kernel;
using System;
using System.Runtime.Versioning;

namespace FeMM.Grasshopper.Components.F2R
{
#if NETCOREAPP
    [SupportedOSPlatform("windows")]
#endif
    public class ETABSGetEditingTableNameComponent : GH_Component
    {
        /// <summary>
        /// Initializes a new instance of the MyComponent1 class.
        /// </summary>
        public ETABSGetEditingTableNameComponent()
            : base("Get Editing Table Name", "GETN", "Get ETABS Editing Table Name", CategoryNameConstants.CATEGORY_F2R, CategoryNameConstants.SUBCATEGORY_F2R_INTERACTIVEETABS)
        {
        }

        private bool _run = false;

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

            m_attributes = buttonAttributes;
        }

        /// <summary>
        /// Registers all the input parameters for this component.
        /// </summary>
        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            pManager.AddTextParameter("Model Path", "MP", "SAP2000 model path to open. If empty it will open an empty file", GH_ParamAccess.item, "");
            pManager[0].Optional = true;
            pManager.AddBooleanParameter("Attach to Instance", "AI", "Attach to instance. If true, 'model path' will be ignored", GH_ParamAccess.item, false);
            pManager[1].Optional = true;
        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddTextParameter("Table Keys", "TK", "The key for the available tables", GH_ParamAccess.list);
            pManager.AddTextParameter("Table Names", "TN", "The table names for the available tables", GH_ParamAccess.list);
            pManager.AddIntegerParameter("Import Type", "IT", "This is either 0, 1, 2 or 3 indicating the import type for the table. " +
                "\n0= not importable. " +
                "\n1= importable, but not interactively importable. " +
                "\n2= importable and interactive importable when he model is unlocked. " +
                "\n3= importable and interactive importable when he model is unlocked and locked", 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)
        {
            string modelPath = "";
            bool attachToInstance = false;

            //input
            DA.GetData(0, ref modelPath);
            DA.GetData(1, ref attachToInstance);

            if (_run)
            {
                if (!System.IO.File.Exists(modelPath) && !attachToInstance) // model path non valido, Se path modello non valido fa partire con stringa vuota (file vuoto)
                {
                    modelPath = string.Empty;
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Sap model path not valid, opening an empty file");
                }

                cSapModel sapModel = F2RModelHelper.GetEtabsModel(out _, modelPath, attachToInstance);

                // Gets table of available tables
                int numberTables = 0;
                string[] tableKeys = null;
                string[] tableNames = null;
                int[] importTypes = null;
                sapModel.DatabaseTables.GetAvailableTables(ref numberTables, ref tableKeys, ref tableNames, ref importTypes);

                DA.SetDataList(0, tableKeys);
                DA.SetDataList(1, tableNames);
                DA.SetDataList(2, importTypes);

                _run = false;
            }
        }

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

        public override Guid ComponentGuid => new("7eae0a61-98be-4a21-8b1c-543eed2f4972");
    }
}
303 files24 directories