using FeMM.Grasshopper.DataTypes.FeMM;
using FeMM.Grasshopper.Helpers;
using Grasshopper.Kernel;
using System;
using System.Collections.Generic;
using System.Runtime.Versioning;
using Grasshopper.Kernel.Parameters;
using FeMM.Common.Models;
using FeMM.Common.Helpers;
namespace FeMM.Grasshopper.Components.Export
{
#if NETCOREAPP
[SupportedOSPlatform("windows")]
#endif
public class ExportBeamPropertiesComponent : GH_Component
{
protected string _status;
protected bool _run;
/// <summary>
/// Initializes a new instance of the SapExportComponent class.
/// </summary>
public ExportBeamPropertiesComponent()
: base("ExportBeamProperties", "EBP", "Export to Beam properties to SAP2000, ETABS and Straus", 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("Properties", "P", "The Beam properties to export", GH_ParamAccess.list);
pManager.AddIntegerParameter("Target", "T", "Target Software", GH_ParamAccess.item);
var param = pManager[1] as Param_Integer;
param.AddNamedValue("SAP2000", 0);
param.AddNamedValue("ETABS", 1);
param.AddNamedValue("Staus", 2);
pManager.AddBooleanParameter("Override", "O", "Override Beam Properties and materials with the same name", GH_ParamAccess.item, true);
pManager.AddTextParameter("File Path", "F", "Full path to the existing file", GH_ParamAccess.item, "");
pManager[3].Optional = true;
pManager.AddBooleanParameter("Attach", "A", "Attach to the active instance (only SAP2000 and ETABS)", GH_ParamAccess.item, false);
pManager[4].Optional = true;
pManager.AddTextParameter("Exe Path", "E", "Full path to the executable file for older versions (only SAP2000 and ETABS)", GH_ParamAccess.item, "");
pManager[5].Optional = true;
pManager.AddIntegerParameter("Units", "U", "Current UnitSystem", GH_ParamAccess.item, 2);
param = pManager[6] as Param_Integer;
param.AddNamedValue("kNm (m, kN, T, kPa, s, kJ, C)", 0);
param.AddNamedValue("SI (m, N, kg, Pa, s, J, C)", 1);
param.AddNamedValue("Nmm (mm, N, T, MPa, s, mJ, C)", 2);
param.AddNamedValue("PSI (in, lbf, lb, psi, s, btu, F)", 3);
}
/// <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 = "";
var fds = new List<GH_FunctionDefinition>();
var beamProps = new List<BeamPropertyModel>();
var targetSoftware = 0;
var overrideContent = true;
var filePath = "";
var attachToInstance = false;
var programPath = "";
var units = 2;
if (!DA.GetDataList(0, fds))
return;
foreach (var fd in fds) {
var bp = (BeamPropertyModel)fd.Value;
if (bp == null) continue;
beamProps.Add(bp);
}
if (!DA.GetData(1, ref targetSoftware))
return;
if (!DA.GetData(2, ref overrideContent))
return;
DA.GetData(3, ref filePath);
DA.GetData(4, ref attachToInstance);
DA.GetData(5, ref programPath);
if(!DA.GetData(6, ref units))
return;
if (string.IsNullOrWhiteSpace(filePath) )
{
if (attachToInstance == false) {
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "No file path or attach to istance option");
return;
}
if (targetSoftware == 2)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Attach to instance is not a valid option for Straus");
return;
}
}
if (_run)
{
try
{
var result = false;
switch (targetSoftware) {
case 0: { result = ToSAP2000(beamProps, overrideContent, attachToInstance, filePath, programPath, (ModelModel.ModelUnits)units); break; }
case 1: { result = ToETABS(beamProps, overrideContent, attachToInstance, filePath, programPath, (ModelModel.ModelUnits)units); break; }
case 2: { result = ToStraus(beamProps, overrideContent, filePath, (ModelModel.ModelUnits)units); break; }
}
if (!result) {
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Export Failed");
}
}
finally
{
_run = false;
}
}
_status = Message;
DA.SetData(0, _status);
}
private bool ToSAP2000(List<BeamPropertyModel> beamProperties, bool overrideContent, bool attachToInstance, string filePath, string programPath, ModelModel.ModelUnits units)
{
List<string> errors;
List<string> warnings;
var result = SapHelper.ExportBeamProperties(beamProperties, overrideContent, attachToInstance, filePath, programPath, units, out warnings, out errors);
foreach (var error in errors) {
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, error);
}
foreach (var warning in warnings) {
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, warning);
}
return result;
}
private bool ToETABS(List<BeamPropertyModel> beamProperties, bool overrideContent, bool attachToInstance, string filePath, string programPath, ModelModel.ModelUnits units)
{
List<string> errors;
List<string> warnings;
var result = EtabsHelper.ExportBeamProperties(beamProperties, overrideContent, attachToInstance, filePath, programPath, units, out warnings, out errors);
foreach (var error in errors)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, error);
}
foreach (var warning in warnings)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, warning);
}
return result;
}
private bool ToStraus(List<BeamPropertyModel> beamProperties, bool overrideContent, string filePath, ModelModel.ModelUnits units)
{
return StrausR3Helper.ExportFramePropertiesToModel(beamProperties, overrideContent, filePath, units);
}
/// <summary>
/// Provides an Icon for the component.
/// </summary>
protected override System.Drawing.Bitmap Icon => Properties.Resources.BeamPropertyExportIcon;
/// <summary>
/// Gets the unique ID for this component. Do not change this ID after release.
/// </summary>
public override Guid ComponentGuid => new("75f8e831-6554-470a-87da-2283268d1de3");
}
}