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 StrausR3ExportComponent : GH_Component
    {
        protected string _status;
        protected bool _run;

        /// <summary>
        /// Initializes a new instance of the StrausExportComponent class.
        /// </summary>
        public StrausR3ExportComponent()
          : base("Straus7 R3 Export", "ST7 R3", "Export to Straus7", 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);
        }

        /// <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 = "";
            if (!DA.GetData("Model", ref model))
                return;
            if (!DA.GetData("Output Path", ref outputPath))
                return;

            if (Path.GetExtension(outputPath).ToLower() != ".st7")
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid extension in file name");
                return;
            }

            if (_run)
            {
                if (Common.Helpers.StrausR3Helper.CreateModelR3(model.Value, outputPath, Rhino.Settings.Tolerance,
                        out int modelId, out List<string> warnings, out List<string> errors))
                {
                    Message = "Done";
                }
                else
                {
                    Message = "Error";
                }

                foreach (string warning in warnings)
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, warning);

                foreach (string error in errors)
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Error, error);

                _run = false;
            }

            DA.SetData(0, Message);
        }

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

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid => new("835f0b41-2003-42bd-aa27-75029965ce5b");
    }
}
303 files24 directories