using Rhino;
using Rhino.DocObjects;
using System;
using System.Collections.Generic;
using System.Runtime.Versioning;

namespace FeMM.Grasshopper.Components.F2R
{
#if NETCOREAPP
    [SupportedOSPlatform("windows")]
#endif
    public static class F2RModelHelper
    {
        private static void AddObjToHashSet(Layer layer, HashSet<RhinoObject> hashset)
        {
            RhinoObject[] childRhobjs = RhinoDoc.ActiveDoc.Objects.FindByLayer(layer);
            if (childRhobjs != null)
            {
                for (int k = 0; k < childRhobjs.Length; k++)
                    hashset.Add(childRhobjs[k]);
            }
        }

        public static void AddLayerToHashSet(Layer layer, string layerToMatch, HashSet<RhinoObject> hashset)
        {
            if (layer.FullPath == layerToMatch)
            {
                RecursiveAdd(layer, hashset);
            }
        }

        public static void RecursiveAdd(Layer layer, HashSet<RhinoObject> hashset)
        {
            AddObjToHashSet(layer, hashset);

            Layer[] children = layer.GetChildren();
            if (children != null && children.Length > 0)
            {
                for (int j = 0; j < children.Length; j++)
                {
                    RecursiveAdd(children[j], hashset);
                }
            }
        }

        public static void GetAllLayerChildren(Layer layer, ref List<Layer> layers)
        {
            layers.Add(layer);

            Layer[] children = layer.GetChildren();
            if (children != null && children.Length > 0)
            {
                for (int j = 0; j < children.Length; j++)
                {
                    GetAllLayerChildren(children[j], ref layers);
                }
            }
        }

        public static Layer[] GetAllLayerChildren(Layer layer)
        {
            var layers = new List<Layer>();

            GetAllLayerChildren(layer, ref layers);

            return [.. layers];
        }

        public static CSIEditingTable GetAllFields(SAP2000v1.cSapModel model, string tableName)
        {
            int NumberFields = 0;
            int TableVersion = 0;
            string[] FieldKey = [];
            string[] FieldName = [];
            string[] Description = [];
            string[] UnitsString = [];
            bool[] IsImportable = [];

            if (model.DatabaseTables.GetAllFieldsInTable(tableName, ref TableVersion, ref NumberFields, ref FieldKey, ref FieldName, ref Description, ref UnitsString, ref IsImportable) == 0)
            {
                var table = new CSIEditingTable(tableName)
                {
                    NumberFields = NumberFields,
                    TableVersion = TableVersion,
                    FieldKey = FieldKey,
                    Description = Description,
                    FieldName = FieldName,
                    UnitsString = UnitsString,
                    IsImportable = IsImportable,
                };

                return table;
            }
            else
                return null;
        }

        public static CSIEditingTable GetAllFields(ETABSv1.cSapModel model, string tableName)
        {
            int NumberFields = 0;
            int TableVersion = 0;
            string[] FieldKey = [];
            string[] FieldName = [];
            string[] Description = [];
            string[] UnitsString = [];
            bool[] IsImportable = [];

            if (model.DatabaseTables.GetAllFieldsInTable(tableName, ref TableVersion, ref NumberFields, ref FieldKey, ref FieldName, ref Description, ref UnitsString, ref IsImportable) == 0)
            {
                var table = new CSIEditingTable(tableName)
                {
                    NumberFields = NumberFields,
                    TableVersion = TableVersion,
                    FieldKey = FieldKey,
                    Description = Description,
                    FieldName = FieldName,
                    UnitsString = UnitsString,
                    IsImportable = IsImportable,
                };

                return table;
            }
            else
                return null;
        }

        public static SAP2000v1.cSapModel GetSapModel(out SAP2000v1.cOAPI sapObject, string modelPath = "", bool attachToInstance = false, SAP2000v1.eUnits eUnits = SAP2000v1.eUnits.N_mm_C)
        {
            try
            {
                ExternalProgram.CSIComObjects.InizializeSAPModel(out SAP2000v1.cSapModel mySapModel, out sapObject, out SAP2000v1.cHelper myHelper, attachToInstance, modelPath, SAP2000v1.eUnits.N_mm_C, true);
                return mySapModel;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static ETABSv1.cSapModel GetEtabsModel(out ETABSv1.cOAPI sapObject, string modelPath = "", bool attachToInstance = false, ETABSv1.eUnits eUnits = ETABSv1.eUnits.N_mm_C, string exePath = "")
        {
            try
            {
                ExternalProgram.CSIComObjects.InizializeETABSModel(out ETABSv1.cSapModel mySapModel, out sapObject, out ETABSv1.cHelper myHelper, attachToInstance, modelPath, ETABSv1.eUnits.N_mm_C, true, exePath);
                return mySapModel;
            }
            catch (Exception)
            {
                throw;
            }
        }        
    }

    public class CSIEditingTable
    {
        public string Name { get; set; }
        public int NumberFields { get; set; }
        public int TableVersion { get; set; }
        public string[] FieldKey { get; set; }
        public string[] FieldName { get; set; }
        public string[] Description { get; set; }
        public string[] UnitsString { get; set; }
        public bool[] IsImportable { get; set; }

        public CSIEditingTable(string name)
        {
            Name = name;
            NumberFields = 0;
            TableVersion = 0;
            FieldKey = [];
            FieldName = [];
            Description = [];
            UnitsString = [];
            IsImportable = [];
        }
    }
}
303 files24 directories