Skip to content

Commit

Permalink
Fix enum
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzhankoral committed Nov 9, 2024
1 parent 63e96f9 commit c04328b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 40 deletions.
85 changes: 52 additions & 33 deletions SpeckleAutomateDotnetExample/AutomateFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,26 @@ private static List<string> 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<string> CheckWindows(AutomationContext automationContext, FunctionInputs functionInputs, IEnumerable<Base> flatten)
Expand All @@ -117,19 +124,25 @@ private static List<string> 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<string> CheckRoofs(AutomationContext automationContext, FunctionInputs functionInputs, IEnumerable<Base> flatten)
Expand All @@ -141,19 +154,25 @@ private static List<string> 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<Base> GetByType(IEnumerable<Base> objects, SpeckleType speckleType)
Expand Down
7 changes: 1 addition & 6 deletions SpeckleAutomateDotnetExample/FunctionInputs.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using Speckle.Automate.Sdk.DataAnnotations;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;


public enum ClimateZones
{
// Tropical Climates
Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion TestAutomateFunction/AutomationContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c04328b

Please sign in to comment.