using ETABSv1;
using FeMM.Grasshopper.DataTypes.FeMM;
using FeMM.Grasshopper.Helpers;
using Grasshopper.Kernel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Versioning;

namespace FeMM.Grasshopper.Components.Export
{
#if NETCOREAPP
    [SupportedOSPlatform("windows")]
#endif
    public class EtabsExportComponent : GH_Component
    {
        protected string _status;
        protected bool _run;

        /// <summary>
        /// Initializes a new instance of the SapExportComponent class.
        /// </summary>
        public EtabsExportComponent()
          : base("ETABS Export", "EDB", "Export to ETABS", CategoryNameConstants.CATEGORY_FEMM, CategoryNameConstants.SUBCATEGORY_EXPORT)
        {
            _status = "";
            _run = false;
        }

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

        /// <summary>
        /// Registers all the input parameters for this component.
        /// </summary>
        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            pManager.AddGenericParameter("Model", "M", "The FEM model", GH_ParamAccess.item);
            pManager.AddTextParameter("Output Path", "O", "The full path of the file model to create", GH_ParamAccess.item);
            pManager[pManager.ParamCount - 1].Optional = true;
            pManager.AddBooleanParameter("Attach", "A", "Attach to the active instance (if exists)", GH_ParamAccess.item, false);
            pManager[pManager.ParamCount - 1].Optional = true;
            pManager.AddTextParameter("ETABS Path", "P", "The path of the main ETABS executable", GH_ParamAccess.item);
            pManager[pManager.ParamCount - 1].Optional = true;
            pManager.AddBooleanParameter("Keep Model Open", "K", "Keep Model Open", GH_ParamAccess.item, true);
        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddGenericParameter("Status", "S", "The status of the export", GH_ParamAccess.item);
        }

        /// <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)
        {
            Message = "";
            GH_Model model = null;
            string outputPath = "";
            bool attachToInstance = false;
            string programPath = "";
            bool keepSAPOpen = true;

            if (!DA.GetData("Model", ref model))
                return;
            DA.GetData("Output Path", ref outputPath);
            DA.GetData("Attach", ref attachToInstance);
            DA.GetData("ETABS Path", ref programPath);
            DA.GetData("Keep Model Open", ref keepSAPOpen);

            if (!attachToInstance && string.IsNullOrWhiteSpace(outputPath))
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "No file path or attach to istance option");
                return;
            }
            if (!attachToInstance)
            {
                if (Path.GetExtension(outputPath).ToLower() != ".edb")
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid extension in file name");
                    return;
                }
            }

            if (_run)
            {
                try
                {
                    cOAPI etabsObject = null;
                    try
                    {
                        etabsObject = Common.Helpers.EtabsHelper.GetEtabsObject(attachToInstance, "", programPath);
                    }
                    catch (Exception ex)
                    {
                        AddRuntimeMessage(GH_RuntimeMessageLevel.Error, ex.Message);
                        return;
                    }

                    try
                    {
                        if (Common.Helpers.EtabsHelper.CreateModel(etabsObject, attachToInstance, model.Value, Rhino.Settings.Tolerance,
                            out cSapModel etabsModel, out List<string> warnings, out List<string> errors))
                        {
                            etabsModel.View.RefreshView(0, false);
                            etabsModel.File.Save(outputPath);

                            if (keepSAPOpen == false)
                                etabsObject.ApplicationExit(true);

                            Message = "Done";
                        }
                        else
                        {
                            Message = "Failed";
                        }
                        foreach (string error in errors)
                            AddRuntimeMessage(GH_RuntimeMessageLevel.Error, error);
                        foreach (string warning in warnings)
                            AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, warning);
                    }
                    catch (Exception e)
                    {
                        AddRuntimeMessage(GH_RuntimeMessageLevel.Error, e.Message);
                        Message = "Error";
                    }
                }
                finally
                {
                    _run = false;
                }
            }

            DA.SetData(0, _status);
        }

        /// <summary>
        /// Provides an Icon for the component.
        /// </summary>
        protected override System.Drawing.Bitmap Icon => Properties.Resources.EtabsExportIcon;

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("DCB8395B-8CCC-40C0-845E-A8FC18A8A5E0");
    }
}
303 files24 directories