Skip to content

Commit

Permalink
refactor to reduce duplication and update layout functions
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonie-kramer committed Jan 4, 2024
1 parent ba20102 commit eb67cc3
Show file tree
Hide file tree
Showing 54 changed files with 568 additions and 185 deletions.
8 changes: 6 additions & 2 deletions LayoutFunctions/ClassroomLayout/hypar.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
"name": "Space Planning Zones",
"optional": false
},
{
"autohide": false,
"name": "Program Requirements",
"optional": true
},
{
"autohide": false,
"name": "Circulation",
Expand Down Expand Up @@ -77,6 +82,5 @@
],
"repository_url": "https://github.com/hypar-io/function",
"source_file_key": null,
"preview_image": null,
"stream_model": true
"preview_image": null
}
8 changes: 6 additions & 2 deletions LayoutFunctions/ClassroomLayout/src/ClassroomLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ public static ClassroomLayoutOutputs Execute(Dictionary<string, Model> inputMode
}
var levelVolumes = LayoutStrategies.GetLevelVolumes<LevelVolume>(inputModels);
var output = new ClassroomLayoutOutputs();
var configJson = File.ReadAllText("./ClassroomConfigurations.json");
var configs = JsonConvert.DeserializeObject<SpaceConfiguration>(configJson);

string configJsonPath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "ClassroomConfigurations.json");
SpaceConfiguration configs = ContentManagement.GetSpaceConfiguration(inputModels, configJsonPath, "Classroom");

// var configJson = File.ReadAllText("./ClassroomConfigurations.json");
// var configs = JsonConvert.DeserializeObject<SpaceConfiguration>(configJson);

int totalCountableSeats = 0;
int seatsAtDesk = 0;
Expand Down
2 changes: 1 addition & 1 deletion LayoutFunctions/ClassroomLayout/src/Function.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public async Task<ClassroomLayoutOutputs> Handler(ClassroomLayoutInputs args)
{
this.store = new UrlModelStore<ClassroomLayoutInputs>();
}
args.StreamModel = true;


var l = new InvocationWrapper<ClassroomLayoutInputs,ClassroomLayoutOutputs> (store, ClassroomLayout.Execute);
var output = await l.InvokeAsync(args);
Expand Down
21 changes: 17 additions & 4 deletions LayoutFunctions/LayoutFunctionCommon/ContentManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using Elements;
using Elements.Components;
using Newtonsoft.Json;

namespace LayoutFunctionCommon
Expand All @@ -13,8 +14,7 @@ public static class ContentManagement
Dictionary<string, Model> inputModels,
string programName,
out string configPath,
out string catalogPath,
Guid? programId = null
out string catalogPath
) where TProgramRequirement : Element, IProgramRequirement
{
configPath = null;
Expand All @@ -26,7 +26,7 @@ public static class ContentManagement
}
var programReqs = programReqModel.AllElementsAssignableFromType<TProgramRequirement>();
var req = programReqs?.FirstOrDefault(r => r.HyparSpaceType == programName);
if(req == null)
if (req == null)
{
Console.WriteLine($"No Program Requirement found for {programName}.");
return false;
Expand All @@ -42,7 +42,7 @@ public static class ContentManagement
return true;
}

private static (string catalogPath, string configPath) WriteLayoutConfigs(IProgramRequirement req, Model programReqModel)
public static (string catalogPath, string configPath) WriteLayoutConfigs(IProgramRequirement req, Model programReqModel)
{
if (!req.SpaceConfig.HasValue || !req.Catalog.HasValue)
{
Expand All @@ -69,5 +69,18 @@ private static (string catalogPath, string configPath) WriteLayoutConfigs(IProgr
File.WriteAllText(configPath, JsonConvert.SerializeObject(config));
return (catalogPath, configPath);
}

public static SpaceConfiguration GetSpaceConfiguration(Dictionary<string, Model> inputModels, string configJsonPath, string programName)
{
if (LoadConfigAndCatalog<ProgramRequirement>(inputModels, programName, out var configPath, out var catPath))
{
configJsonPath = configPath;
ContentCatalogRetrieval.SetCatalogFilePath(catPath);
}

var configJson = File.ReadAllText(configJsonPath);
var configs = JsonConvert.DeserializeObject<SpaceConfiguration>(configJson);
return configs;
}
}
}
12 changes: 0 additions & 12 deletions LayoutFunctions/LayoutFunctionCommon/IProgramRequirement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,8 @@ public interface IProgramRequirement
{
[JsonProperty("Hypar Space Type")]
public string HyparSpaceType { get; set; }

public Guid? SpaceConfig { get; set; }

public Guid? Catalog { get; set; }

public Guid Id { get; set; }

[JsonProperty("Layout Type")]
public LayoutType LayoutType { get; set; }
}

public class LayoutType
{
public Guid Id { get; set; }
public string Name { get; set; }
}
}
15 changes: 12 additions & 3 deletions LayoutFunctions/LayoutFunctionCommon/LayoutGeneration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,21 @@ public class LayoutGeneration<TLevelElements, TLevelVolume, TSpaceBoundary, TCir

var outputModel = new Model();
var totalSeats = 0;
ContentCatalogRetrieval.SetCatalogFilePath(catalogPath);

// ContentCatalogRetrieval.SetCatalogFilePath(catalogPath);
// var configJson = configurationsPath != null ? File.ReadAllText(configurationsPath) : "{}";
// var configs = DeserializeConfigJson(configJson);

string configJsonPath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), catalogPath);
SpaceConfiguration configs = ContentManagement.GetSpaceConfiguration(inputModels, configJsonPath, programTypeName);


var spacePlanningZones = inputModels["Space Planning Zones"];
var levels = GetLevels(inputModels, spacePlanningZones);
var levelVolumes = LayoutStrategies.GetLevelVolumes<TLevelVolume>(inputModels);
var configJson = configurationsPath != null ? File.ReadAllText(configurationsPath) : "{}";
var configs = DeserializeConfigJson(configJson);



var allSpaceBoundaries = spacePlanningZones.AllElementsAssignableFromType<TSpaceBoundary>().Where(z => (z.HyparSpaceType ?? z.Name) == programTypeName).ToList();
foreach (var lvl in levels)
{
Expand Down
12 changes: 8 additions & 4 deletions LayoutFunctions/LayoutFunctionCommon/LayoutStrategies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ public static bool IsNearlyARectangle(IEnumerable<Polygon> polygons)
dynamic overrides, Model outputModel,
bool createWalls,
string configurationsPath,
string catalogPath = "catalog.json",
Func<LayoutInstantiated, int> countSeats = null
)
where TLevelElements : Element, ILevelElements
Expand All @@ -181,7 +180,14 @@ public static bool IsNearlyARectangle(IEnumerable<Polygon> polygons)
where TCirculationSegment : Floor, ICirculationSegment
{
var processedSpaces = new HashSet<Guid>();
ContentCatalogRetrieval.SetCatalogFilePath(catalogPath);

// ContentCatalogRetrieval.SetCatalogFilePath(catalogPath);
// var configJson = configurationsPath != null ? File.ReadAllText(configurationsPath) : "{}";
// var configs = JsonConvert.DeserializeObject<SpaceConfiguration>(configJson);

string configJsonPath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), configurationsPath);
SpaceConfiguration configs = ContentManagement.GetSpaceConfiguration(inputModels, configJsonPath, programTypeName);

var spacePlanningZones = inputModels["Space Planning Zones"];
var levels = spacePlanningZones.AllElementsAssignableFromType<TLevelElements>();

Expand All @@ -195,8 +201,6 @@ public static bool IsNearlyARectangle(IEnumerable<Polygon> polygons)
}
}
var levelVolumes = GetLevelVolumes<TLevelVolume>(inputModels);
var configJson = configurationsPath != null ? File.ReadAllText(configurationsPath) : "{}";
var configs = JsonConvert.DeserializeObject<SpaceConfiguration>(configJson);
var allSpaceBoundaries = spacePlanningZones.AllElementsAssignableFromType<TSpaceBoundary>().Where(z => (z.HyparSpaceType ?? z.Name) == programTypeName).ToList();
foreach (var lvl in levels)
{
Expand Down
12 changes: 12 additions & 0 deletions LayoutFunctions/LayoutFunctionCommon/ProgramRequirement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using Newtonsoft.Json;

namespace Elements
{
public partial class ProgramRequirement : IProgramRequirement
{
[JsonProperty("Qualified Program Name")]
public string QualifiedProgramName => String.IsNullOrWhiteSpace(this.ProgramGroup) ? this.ProgramName : $"{this.ProgramGroup} - {this.ProgramName}";
public int CountPlaced { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace Elements
public partial class ProgramRequirement : Element
{
[JsonConstructor]
public ProgramRequirement(string @programGroup, string @programName, Color? @color, double @areaPerSpace, int @spaceCount, double? @width, double? @depth, string @hyparSpaceType, ProgramRequirementCountType @countType, double @totalArea, System.Guid @id = default, string @name = null)
public ProgramRequirement(string @programGroup, string @programName, Color? @color, double @areaPerSpace, int @spaceCount, double? @width, double? @depth, string @hyparSpaceType, ProgramRequirementCountType @countType, double @totalArea, System.Guid? @spaceConfig, System.Guid? @layoutTypeId, System.Guid? @catalog, System.Guid @id = default, string @name = null)
: base(id, name)
{
this.ProgramGroup = @programGroup;
Expand All @@ -40,6 +40,9 @@ public ProgramRequirement(string @programGroup, string @programName, Color? @col
this.HyparSpaceType = @hyparSpaceType;
this.CountType = @countType;
this.TotalArea = @totalArea;
this.SpaceConfig = @spaceConfig;
this.LayoutTypeId = @layoutTypeId;
this.Catalog = @catalog;
}


Expand Down Expand Up @@ -96,6 +99,18 @@ public ProgramRequirement()
[JsonProperty("Total Area", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public double TotalArea { get; set; }

/// <summary>The guid of the space configuration</summary>
[JsonProperty("SpaceConfig", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public System.Guid? SpaceConfig { get; set; }

/// <summary>The guid of the layout type</summary>
[JsonProperty("Layout Type Id", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public System.Guid? LayoutTypeId { get; set; }

/// <summary>The guid of the catalog</summary>
[JsonProperty("Catalog", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public System.Guid? Catalog { get; set; }


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@

namespace Elements
{
#pragma warning disable // Disable all warnings
#pragma warning disable // Disable all warnings

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.21.0 (Newtonsoft.Json v13.0.0.0)")]
public enum ProgramRequirementCountType
{
[System.Runtime.Serialization.EnumMember(Value = @"Item")]
Item = 0,

[System.Runtime.Serialization.EnumMember(Value = @"Area Total")]
Area_Total = 1,

}
}
67 changes: 43 additions & 24 deletions LayoutFunctions/LoungeLayout/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/test/bin/Debug/netcoreapp3.1/LoungeLayout.Tests.dll",
"args": [],
"cwd": "${workspaceFolder}/test",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/test/bin/Debug/netcoreapp3.1/LoungeLayout.Tests.dll",
"args": [],
"cwd": "${workspaceFolder}/test",
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
},
{
"name": "Attach to Hypar Run",
"type": "coreclr",
"request": "attach",
"processName": "LoungeLayout.Server"
},
{
"name": "Launch Hypar Run (Run once only)",
"type": "coreclr",
"request": "launch",
"program": "${workspaceFolder}/server/bin/Debug/net6.0/LoungeLayout.Server.dll",
"args": [
"--workflow-id",
"${input:workflowId}"
],
"preLaunchTask": "server-build"
}
],
"inputs": [
{
"id": "workflowId",
"type": "promptString",
"description": "Enter the workflow id to run."
}
]
}
Loading

0 comments on commit eb67cc3

Please sign in to comment.