using FeMM.Grasshopper.DataTypes.F2R;
using FeMM.Grasshopper.Helpers;
using FemToRhino.Common;
using FemToRhino.CSI.ApiWrapper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Parameters;
using Rhino;
using System;
using System.Collections.Generic;
using System.Runtime.Versioning;

namespace FeMM.Grasshopper.Components.F2R
{
#if NETCOREAPP
    [SupportedOSPlatform("windows")]
#endif
    public class BakeEtabsModelComponent : GH_Component
    {
        protected Dictionary<int, string> _outputPresets = new()
        {
            { 0, "All" },
            { 1, "Geometry assignements" },
            { 2, "Section properties" },
        };

        /// <summary>
        /// Initializes a new instance of the MyComponent1 class.
        /// </summary>
        public BakeEtabsModelComponent()
            : base("Bake Etabs Model", "BEM", "Bake ETABS model", CategoryNameConstants.CATEGORY_F2R, CategoryNameConstants.SUBCATEGORY_F2R_BAKE)
        {
        }

        private bool _run = false;

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

            m_attributes = buttonAttributes;
        }

        protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
        {
            pManager.AddTextParameter("Model Path", "MP", "ETABS 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;
            pManager.AddTextParameter("EtabsPath", "EP", "Specify ETABS exe file to change version. If no path is given, latest version is used.", GH_ParamAccess.item, "");
            pManager[2].Optional = true;

            int i = pManager.AddIntegerParameter("Output preferences", "O", "", GH_ParamAccess.item, 1);
            Param_Integer mtParam = pManager[i] as Param_Integer;
            foreach (KeyValuePair<int, string> v in _outputPresets)
                mtParam.AddNamedValue(v.Value, v.Key);
        }

        protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
        {
        }

        protected override void SolveInstance(IGH_DataAccess DA)
        {
            string modelPath = "";
            string etabsExePath = "";
            bool attachToInstance = false;
            bool visible = false;
            int type = 0;

            //input
            DA.GetData(0, ref modelPath);
            DA.GetData(1, ref attachToInstance);
            DA.GetData(2, ref etabsExePath);
            DA.GetData(3, ref type);

            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;
                    visible = true;
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "ETABS model path not valid, opening an empty file");
                }

                int units;

                if (RhinoDoc.ActiveDoc != null)
                {
                    string rhinoUnits = RhinoDoc.ActiveDoc.GetUnitSystemName(true, true, true, true);
                    if (rhinoUnits.ToLower() == "millimeter" || rhinoUnits.ToLower() == "mm")
                    {
                        units = Units.N_mm_C;
                    }
                    else if (rhinoUnits.ToLower() == "meter" || rhinoUnits.ToLower() == "m")
                    {
                        units = Units.N_m_C;
                    }
                    else if (rhinoUnits.ToLower() == "centimeter" || rhinoUnits.ToLower() == "cm")
                    {
                        units = Units.N_cm_C;
                    }
                    else
                    {
                        AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Rhino lenght units not recognized. The model will be open using kN, mm, C");
                        units = Units.N_mm_C;
                    }
                }
                else
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Rhino active doc is null");
                    units = Units.N_mm_C;
                }

                var options = new Model.Options();
                if (type == 0)
                {
                    options.ExportGeometries = true;
                    options.ExportSectionProperties = true;
                    options.ExportGeometryDatas = true;
                    options.ExportLoads = true;
                }
                if (type == 1)
                {
                    options.ExportGeometries = true;
                    options.ExportSectionProperties = true;
                    options.ExportGeometryDatas = true;
                    options.ExportLoads = false;
                }
                if (type == 2)
                {
                    options.ExportGeometries = true;
                    options.ExportSectionProperties = true;
                    options.ExportGeometryDatas = false;
                    options.ExportLoads = false;
                }
                if (type == 3)
                {
                    options.ExportGeometries = true;
                    options.ExportSectionProperties = false;
                    options.ExportGeometryDatas = false;
                    options.ExportLoads = false;
                }

                var modelType = new GH_CSIModel(CSIApiWrapper.CsiSoftware.Etabs, attachToInstance, etabsExePath, units, modelPath, visible);
                modelType.Value.Option = options;

                try
                {
                    modelType.Value.Process();
                }
                catch (Exception)
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Fail to read geometries");
                }

                try
                {
                    modelType.BakeGeometryCustom(RhinoDoc.ActiveDoc);
                }
                catch (Exception)
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Fail to bake geometries");

                }

                _run = false;
            }
        }

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

        public override Guid ComponentGuid => new("7410646c-248f-4d70-ba20-a2510ef006a7");
    }
}
303 files24 directories