diff --git a/SpeckleAutomateDotnetExample/AutomateFunction.cs b/SpeckleAutomateDotnetExample/AutomateFunction.cs index 82e4b36..8fb3671 100644 --- a/SpeckleAutomateDotnetExample/AutomateFunction.cs +++ b/SpeckleAutomateDotnetExample/AutomateFunction.cs @@ -93,19 +93,26 @@ private static List CheckWalls(AutomationContext automationContext, Func var walls = GetByType(flatten, SpeckleType.Wall); var uValues = walls.Select(GetThermalResistance); - var expectedValue = UValues.Wall[functionInputs.ClimateZone]; - var failedObjectIds = uValues.Where(val => val.value < expectedValue).Select(v => (v.id, v.value)).ToList(); - - foreach (var (id, value) in failedObjectIds) + // Attempt to parse ClimateZone as a ClimateZones enum + if (Enum.TryParse(functionInputs.ClimateZone, out ClimateZones climateZoneEnum)) { - automationContext.AttachResultToObjects( - ObjectResultLevel.Error, - "Walls", - new []{id}, - $"Wall expected to have maximum {expectedValue} U-value but it is {value}." - ); + var expectedValue = UValues.Wall[climateZoneEnum]; + var failedObjectIds = uValues.Where(val => val.value < expectedValue).Select(v => (v.id, v.value)).ToList(); + + foreach (var (id, value) in failedObjectIds) + { + automationContext.AttachResultToObjects( + ObjectResultLevel.Error, + "Walls", + new []{id}, + $"Wall expected to have maximum {expectedValue} U-value but it is {value}." + ); + } + return failedObjectIds.Select(i => i.id).ToList(); } - return failedObjectIds.Select(i => i.id).ToList(); + + // Handle the case where the ClimateZone string is not a valid ClimateZones value + throw new ArgumentException($"Invalid ClimateZone: {functionInputs.ClimateZone}"); } private static List CheckWindows(AutomationContext automationContext, FunctionInputs functionInputs, IEnumerable flatten) @@ -117,19 +124,25 @@ private static List CheckWindows(AutomationContext automationContext, Fu var walls = GetByType(flatten, SpeckleType.Window); var uValues = walls.Select(GetThermalResistance); - var expectedValue = UValues.Window[functionInputs.ClimateZone]; - var failedObjectIds = uValues.Where(val => val.value < expectedValue).Select(v => (v.id, v.value)).ToList(); - - foreach (var (id, value) in failedObjectIds) + if (Enum.TryParse(functionInputs.ClimateZone, out ClimateZones climateZoneEnum)) { - automationContext.AttachResultToObjects( - ObjectResultLevel.Error, - "Windows", - new []{id}, - $"Window expected to have maximum {expectedValue} U-value but it is {value}." - ); + var expectedValue = UValues.Window[climateZoneEnum]; + var failedObjectIds = uValues.Where(val => val.value < expectedValue).Select(v => (v.id, v.value)).ToList(); + + foreach (var (id, value) in failedObjectIds) + { + automationContext.AttachResultToObjects( + ObjectResultLevel.Error, + "Windows", + new []{id}, + $"Window expected to have maximum {expectedValue} U-value but it is {value}." + ); + } + return failedObjectIds.Select(i => i.id).ToList(); } - return failedObjectIds.Select(i => i.id).ToList(); + + // Handle the case where the ClimateZone string is not a valid ClimateZones value + throw new ArgumentException($"Invalid ClimateZone: {functionInputs.ClimateZone}"); } private static List CheckRoofs(AutomationContext automationContext, FunctionInputs functionInputs, IEnumerable flatten) @@ -141,19 +154,25 @@ private static List CheckRoofs(AutomationContext automationContext, Func var walls = GetByType(flatten, SpeckleType.Roof); var uValues = walls.Select(GetThermalResistance); - var expectedValue = UValues.Roof[functionInputs.ClimateZone]; - var failedObjectIds = uValues.Where(val => val.value < expectedValue).Select(v => (v.id, v.value)).ToList(); - - foreach (var (id, value) in failedObjectIds) + if (Enum.TryParse(functionInputs.ClimateZone, out ClimateZones climateZoneEnum)) { - automationContext.AttachResultToObjects( - ObjectResultLevel.Error, - "Roofs", - new []{id}, - $"Roof expected to have maximum {expectedValue} U-value but it is {value}." - ); + var expectedValue = UValues.Roof[climateZoneEnum]; + var failedObjectIds = uValues.Where(val => val.value < expectedValue).Select(v => (v.id, v.value)).ToList(); + + foreach (var (id, value) in failedObjectIds) + { + automationContext.AttachResultToObjects( + ObjectResultLevel.Error, + "Roofs", + new []{id}, + $"Roof expected to have maximum {expectedValue} U-value but it is {value}." + ); + } + return failedObjectIds.Select(i => i.id).ToList(); } - return failedObjectIds.Select(i => i.id).ToList(); + + // Handle the case where the ClimateZone string is not a valid ClimateZones value + throw new ArgumentException($"Invalid ClimateZone: {functionInputs.ClimateZone}"); } private static IEnumerable GetByType(IEnumerable objects, SpeckleType speckleType) diff --git a/SpeckleAutomateDotnetExample/FunctionInputs.cs b/SpeckleAutomateDotnetExample/FunctionInputs.cs index 4d35290..66a2d5f 100644 --- a/SpeckleAutomateDotnetExample/FunctionInputs.cs +++ b/SpeckleAutomateDotnetExample/FunctionInputs.cs @@ -1,8 +1,6 @@ -using Speckle.Automate.Sdk.DataAnnotations; using System.ComponentModel; using System.ComponentModel.DataAnnotations; - public enum ClimateZones { // Tropical Climates @@ -165,20 +163,17 @@ public struct FunctionInputs [Required] [EnumDataType(typeof(ClimateZones))] [DefaultValue(ClimateZones.Csa_MediterraneanHotSummer)] - public ClimateZones ClimateZone; + public string ClimateZone; [Required] [DefaultValue(true)] - [Description("Include Walls")] public bool CheckWalls; [Required] [DefaultValue(true)] - [Description("Include Windows")] public bool CheckWindows; [Required] [DefaultValue(true)] - [Description("Include Roofs")] public bool CheckRoofs; } diff --git a/TestAutomateFunction/AutomationContextTest.cs b/TestAutomateFunction/AutomationContextTest.cs index 28fe797..acfe27a 100644 --- a/TestAutomateFunction/AutomationContextTest.cs +++ b/TestAutomateFunction/AutomationContextTest.cs @@ -28,7 +28,7 @@ public async Task TestFunctionRun() { var inputs = new FunctionInputs { - ClimateZone = ClimateZones.Csa_MediterraneanHotSummer, + ClimateZone = ClimateZones.Csa_MediterraneanHotSummer.ToString(), CheckWindows = true, CheckWalls = true, CheckRoofs = true