public async Task <PhoneBoothLayoutOutputs> Handler(PhoneBoothLayoutInputs args, ILambdaContext context) { // Preload dependencies (if they exist), // so that they are available during model deserialization. var sw = System.Diagnostics.Stopwatch.StartNew(); var asmLocation = this.GetType().Assembly.Location; var asmDir = Path.GetDirectoryName(asmLocation); // Explicitly load the dependencies project, it might have types // that aren't used in the function but are necessary for correct // deserialization. var asmName = Path.GetFileNameWithoutExtension(asmLocation); var depPath = Path.Combine(asmDir, $"{asmName}.Dependencies.dll"); if (File.Exists(depPath)) { Console.WriteLine($"Loading dependencies assembly from: {depPath}..."); Assembly.LoadFrom(depPath); Console.WriteLine("Dependencies assembly loaded."); } // Load all reference assemblies. Console.WriteLine($"Loading all referenced assemblies."); foreach (var asm in this.GetType().Assembly.GetReferencedAssemblies()) { try { Assembly.Load(asm); } catch (Exception e) { Console.WriteLine($"Failed to load {asm.FullName}"); Console.WriteLine(e.Message); } } sw.Stop(); Console.WriteLine($"Time to load assemblies: {sw.Elapsed.TotalSeconds})"); if (this.store == null) { this.store = new S3ModelStore <PhoneBoothLayoutInputs>(RegionEndpoint.USWest1); } var l = new InvocationWrapper <PhoneBoothLayoutInputs, PhoneBoothLayoutOutputs>(store, PhoneBoothLayout.Execute); var output = await l.InvokeAsync(args); return(output); }
/// <summary> /// The PhoneBoothLayout function. /// </summary> /// <param name="model">The input model.</param> /// <param name="input">The arguments to the execution.</param> /// <returns>A PhoneBoothLayoutOutputs instance containing computed results and the model with any new elements.</returns> public static PhoneBoothLayoutOutputs Execute(Dictionary <string, Model> inputModels, PhoneBoothLayoutInputs input) { var spacePlanningZones = inputModels["Space Planning Zones"]; var levelsModel = inputModels["Levels"]; var levels = spacePlanningZones.AllElementsOfType <LevelElements>(); var levelVolumes = levelsModel.AllElementsOfType <LevelVolume>(); var output = new PhoneBoothLayoutOutputs(); var configJson = File.ReadAllText("./PhoneBoothConfigurations.json"); var configs = JsonConvert.DeserializeObject <SpaceConfiguration>(configJson); var wallMat = new Material("Drywall", new Color(0.9, 0.9, 0.9, 1.0), 0.01, 0.01); var glassMat = new Material("Glass", new Color(0.7, 0.7, 0.7, 0.3), 0.3, 0.6); var mullionMat = new Material("Storefront Mullions", new Color(0.5, 0.5, 0.5, 1.0)); foreach (var lvl in levels) { var corridors = lvl.Elements.OfType <Floor>(); var corridorSegments = corridors.SelectMany(p => p.Profile.Segments()); var meetingRmBoundaries = lvl.Elements.OfType <SpaceBoundary>().Where(z => z.Name == "Phone Booth"); var levelVolume = levelVolumes.First(l => l.Name == lvl.Name); var wallCandidateLines = new List <(Line line, string type)>(); foreach (var room in meetingRmBoundaries) { var spaceBoundary = room.Boundary; Line orientationGuideEdge = FindEdgeAdjacentToSegments(spaceBoundary.Perimeter.Segments(), corridorSegments, out var wallCandidates); var exteriorWalls = FindEdgeAdjacentToSegments(wallCandidates, levelVolume.Profile.Segments(), out var solidWalls, 0.6); wallCandidateLines.AddRange(solidWalls.Select(s => (s, "Solid"))); var orientationTransform = new Transform(Vector3.Origin, orientationGuideEdge.Direction(), Vector3.ZAxis); var boundaryCurves = new List <Polygon>(); boundaryCurves.Add(spaceBoundary.Perimeter); boundaryCurves.AddRange(spaceBoundary.Voids ?? new List <Polygon>()); var grid = new Grid2d(boundaryCurves, orientationTransform); grid.U.DivideByApproximateLength(input.MinimumSize, EvenDivisionMode.RoundDown); foreach (var cell in grid.GetCells()) { var rect = cell.GetCellGeometry() as Polygon; var segs = rect.Segments(); var width = segs[0].Length(); var depth = segs[1].Length(); Line glassWall = null; var trimmedGeo = cell.GetTrimmedCellGeometry(); if (!cell.IsTrimmed() && trimmedGeo.Count() > 0) { glassWall = segs[0]; output.Model.AddElement(InstantiateLayout(configs, width, depth, rect, room.Transform)); } else if (trimmedGeo.Count() > 0) { var largestTrimmedShape = trimmedGeo.OfType <Polygon>().OrderBy(s => s.Area()).Last(); glassWall = largestTrimmedShape.Segments().OrderBy(s => s.PointAt(0.5).DistanceTo(segs[0].PointAt(0.5))).FirstOrDefault(); var cinchedVertices = rect.Vertices.Select(v => largestTrimmedShape.Vertices.OrderBy(v2 => v2.DistanceTo(v)).First()).ToList(); var cinchedPoly = new Polygon(cinchedVertices); output.Model.AddElement(InstantiateLayout(configs, width, depth, cinchedPoly, room.Transform)); } if (glassWall != null) { wallCandidateLines.Add((glassWall, "Glass")); } } var cellSeparators = grid.GetCellSeparators(GridDirection.V, true); wallCandidateLines.AddRange(grid.GetCellSeparators(GridDirection.V, true).OfType <Curve>().Select(c => (new Line(c.PointAt(0), c.PointAt(1)), "Partition"))); } var mullionSize = 0.07; var doorWidth = 0.9; var doorHeight = 2.1; var sideLightWidth = 0.4; var totalStorefrontHeight = Math.Min(2.7, levelVolume.Height); var mullion = new StandardWall(new Line(new Vector3(-mullionSize / 2, 0, 0), new Vector3(mullionSize / 2, 0, 0)), mullionSize, totalStorefrontHeight, mullionMat); mullion.IsElementDefinition = true; if (input.CreateWalls) { foreach (var wallCandidate in wallCandidateLines) { if (wallCandidate.type == "Solid") { output.Model.AddElement(new StandardWall(wallCandidate.line, 0.2, levelVolume.Height, wallMat, levelVolume.Transform)); } else if (wallCandidate.type == "Partition") { output.Model.AddElement(new StandardWall(wallCandidate.line, 0.1, levelVolume.Height, wallMat, levelVolume.Transform)); } else if (wallCandidate.type == "Glass") { var grid = new Grid1d(wallCandidate.line); grid.SplitAtOffsets(new[] { sideLightWidth, sideLightWidth + doorWidth }); grid[2].DivideByApproximateLength(2); var separators = grid.GetCellSeparators(true); var beam = new Beam(wallCandidate.line, Polygon.Rectangle(mullionSize, mullionSize), mullionMat, 0, 0, 0, isElementDefinition: true); output.Model.AddElement(beam.CreateInstance(levelVolume.Transform, "Base Mullion")); output.Model.AddElement(beam.CreateInstance(levelVolume.Transform.Concatenated(new Transform(0, 0, doorHeight)), "Base Mullion")); output.Model.AddElement(beam.CreateInstance(levelVolume.Transform.Concatenated(new Transform(0, 0, totalStorefrontHeight)), "Base Mullion")); foreach (var separator in separators) { // var line = new Line(separator, separator + new Vector3(0, 0, levelVolume.Height)); // output.Model.AddElement(new ModelCurve(line, BuiltInMaterials.XAxis, levelVolume.Transform)); var instance = mullion.CreateInstance(new Transform(separator, wallCandidate.line.Direction(), Vector3.ZAxis, 0).Concatenated(levelVolume.Transform), "Mullion"); output.Model.AddElement(instance); } output.Model.AddElement(new StandardWall(wallCandidate.line, 0.05, totalStorefrontHeight, glassMat, levelVolume.Transform)); var headerHeight = levelVolume.Height - totalStorefrontHeight; if (headerHeight > 0.01) { output.Model.AddElement(new StandardWall(wallCandidate.line, 0.2, headerHeight, wallMat, levelVolume.Transform.Concatenated(new Transform(0, 0, totalStorefrontHeight)))); } } } } } InstancePositionOverrides(input.Overrides, output.Model); return(output); }