Skip to content

Updates hypar.json and overall function to take more than one core polygon. #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Core/CoreBySketch/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<img src="preview.png" width="512">

# Core By Sketch

Creates the volume of a building service core by extruding a sketch to a height relative to the highest model Level.

|Input Name|Type|Description|
|---|---|---|
|Perimeter|Geometry|Perimeter of the building service core.|
|Core height above roof|Range|The projection of the service core above the highest building level.|
|Perimeters|array|List of perimeters of the buildings service cores.|
|HeightAboveRoof|number|The projection of the service core above the highest building level.|


<br>
Expand Down
12 changes: 6 additions & 6 deletions Core/CoreBySketch/dependencies/Level.g.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
//----------------------
// <auto-generated>
// Generated using the NJsonSchema v10.1.4.0 (Newtonsoft.Json v12.0.0.0) (http://NJsonSchema.org)
// Generated using the NJsonSchema v10.1.21.0 (Newtonsoft.Json v12.0.0.0) (http://NJsonSchema.org)
// </auto-generated>
//----------------------
using Elements;
using Elements.GeoJSON;
using Elements.Geometry;
using Elements.Geometry.Solids;
using Elements.Properties;
using Elements.Spatial;
using Elements.Validators;
using Elements.Serialization.JSON;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -21,9 +22,8 @@ namespace Elements

/// <summary>A horizontal planer datum.</summary>
[Newtonsoft.Json.JsonConverter(typeof(Elements.Serialization.JSON.JsonInheritanceConverter), "discriminator")]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.4.0 (Newtonsoft.Json v12.0.0.0)")]
[UserElement]
public partial class Level : Element
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.21.0 (Newtonsoft.Json v12.0.0.0)")]
public partial class Level : Element
{
[Newtonsoft.Json.JsonConstructor]
public Level(double @elevation, System.Guid @id, string @name)
Expand All @@ -36,7 +36,7 @@ public Level(double @elevation, System.Guid @id, string @name)
}

this.Elevation = @elevation;

if(validator != null)
{
validator.PostConstruct(this);
Expand Down
12 changes: 6 additions & 6 deletions Core/CoreBySketch/dependencies/ServiceCore.g.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
//----------------------
// <auto-generated>
// Generated using the NJsonSchema v10.1.4.0 (Newtonsoft.Json v12.0.0.0) (http://NJsonSchema.org)
// Generated using the NJsonSchema v10.1.21.0 (Newtonsoft.Json v12.0.0.0) (http://NJsonSchema.org)
// </auto-generated>
//----------------------
using Elements;
using Elements.GeoJSON;
using Elements.Geometry;
using Elements.Geometry.Solids;
using Elements.Properties;
using Elements.Spatial;
using Elements.Validators;
using Elements.Serialization.JSON;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -21,9 +22,8 @@ namespace Elements

/// <summary>Represents a building service core.</summary>
[Newtonsoft.Json.JsonConverter(typeof(Elements.Serialization.JSON.JsonInheritanceConverter), "discriminator")]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.4.0 (Newtonsoft.Json v12.0.0.0)")]
[UserElement]
public partial class ServiceCore : GeometricElement
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.21.0 (Newtonsoft.Json v12.0.0.0)")]
public partial class ServiceCore : GeometricElement
{
[Newtonsoft.Json.JsonConstructor]
public ServiceCore(Profile @profile, Vector3 @direction, double @elevation, double @height, double @rotation, Transform @transform, Material @material, Representation @representation, bool @isElementDefinition, System.Guid @id, string @name)
Expand All @@ -40,7 +40,7 @@ public ServiceCore(Profile @profile, Vector3 @direction, double @elevation, doub
this.Elevation = @elevation;
this.Height = @height;
this.Rotation = @rotation;

if(validator != null)
{
validator.PostConstruct(this);
Expand Down
37 changes: 24 additions & 13 deletions Core/CoreBySketch/hypar.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,34 @@
"optional": false
}
],
"inputs": [
{
"primitive_type": "polygon",
"name": "Perimeter",
"description": "Perimeter of the building service core.",
"type": "geometry"
"input_schema": {
"type": "object",
"properties": {
"Perimeters": {
"description": "List of perimeters of the buildings service cores.",
"default": [],
"optional": false,
"type": "array",
"$hyparOrder": 0,
"items": {
"name": "Core Perimeters",
"description": "Polygons describing ncore perimeters.",
"default": null,
"type": "object",
"$ref": "https://hypar.io/Schemas/Geometry/Polygon.json"
}
},
{
"min": -1.0,
"HeightAboveRoof": {
"multipleOf": 1.0,
"default": 2.0,
"min":-1.0,
"max": 5.0,
"step": 1.0,
"unit_type": "length",
"name": "Core height above roof",
"description": "The projection of the service core above the highest building level.",
"type": "range"
"type": "number",
"$hyparOrder": 1,
"minimum": 0
}
],
}},
"outputs": [
{
"unit_type": "length",
Expand Down
14 changes: 10 additions & 4 deletions Core/CoreBySketch/src/CoreBySketch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,22 @@ public static CoreBySketchOutputs Execute(Dictionary<string, Model> inputModels,
throw new ArgumentException("No Levels found.");
}
levels.AddRange(model.AllElementsOfType<Level>());
var top = levels.OrderByDescending(l => l.Elevation).First().Elevation + input.CoreHeightAboveRoof;
var top = levels.OrderByDescending(l => l.Elevation).First().Elevation + input.HeightAboveRoof;
var elevation = levels.OrderBy(l => l.Elevation).First().Elevation;
var height = top - elevation;
// Create the Core extrusion.
var extrude = new Elements.Geometry.Solids.Extrude(input.Perimeter, height, Vector3.ZAxis, false);
var output = new CoreBySketchOutputs(height);

foreach(var perim in input.Perimeters)
{
var extrude = new Elements.Geometry.Solids.Extrude(perim, height, Vector3.ZAxis, false);
var geomRep = new Representation(new List<Elements.Geometry.Solids.SolidOperation>() { extrude });
var corMatl = new Material("core", new Color(1.0, 1.0, 1.0, 0.6), 0.0f, 0.0f);
var svcCore = new ServiceCore(input.Perimeter, Vector3.ZAxis, elevation, height, 0.0, new Transform(0.0, 0.0, elevation), corMatl, geomRep, false, Guid.NewGuid(), "serviceCore");
var output = new CoreBySketchOutputs(height);
var svcCore = new ServiceCore(perim, Vector3.ZAxis, elevation, height, 0.0, new Transform(0.0, 0.0, elevation), corMatl, geomRep, false, Guid.NewGuid(), "serviceCore");

output.Model.AddElement(svcCore);
}

return output;
}
}
Expand Down
85 changes: 41 additions & 44 deletions Core/CoreBySketch/src/CoreBySketchInputs.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,56 @@
using Elements;
using Elements.GeoJSON;
using Elements.Geometry;
using Elements.Geometry.Solids;
using Elements.Validators;
using Elements.Serialization.JSON;
using Hypar.Functions;
using Hypar.Functions.Execution;
using Hypar.Functions.Execution.AWS;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using Line = Elements.Geometry.Line;
using Polygon = Elements.Geometry.Polygon;

namespace CoreBySketch
{
public class CoreBySketchInputs: S3Args
{
/// <summary>
/// Perimeter of the building service core.
/// </summary>
[JsonProperty("Perimeter")]
public Elements.Geometry.Polygon Perimeter {get;}

/// <summary>
/// The projection of the service core above the highest building level.
/// </summary>
[JsonProperty("Core height above roof")]
public double CoreHeightAboveRoof {get;}

#pragma warning disable // Disable all warnings

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.21.0 (Newtonsoft.Json v12.0.0.0)")]

public class CoreBySketchInputs : S3Args

{
[Newtonsoft.Json.JsonConstructor]

/// <summary>
/// Construct a CoreBySketchInputs with default inputs.
/// This should be used for testing only.
/// </summary>
public CoreBySketchInputs() : base()
public CoreBySketchInputs(IList<Polygon> @perimeters, double @heightAboveRoof, string bucketName, string uploadsBucket, Dictionary<string, string> modelInputKeys, string gltfKey, string elementsKey, string ifcKey):
base(bucketName, uploadsBucket, modelInputKeys, gltfKey, elementsKey, ifcKey)
{
this.Perimeter = Elements.Geometry.Polygon.Rectangle(1, 1);
this.CoreHeightAboveRoof = 5;

var validator = Validator.Instance.GetFirstValidatorForType<CoreBySketchInputs>();
if(validator != null)
{
validator.PreConstruct(new object[]{ @perimeters, @heightAboveRoof});
}

this.Perimeters = @perimeters;
this.HeightAboveRoof = @heightAboveRoof;

if(validator != null)
{
validator.PostConstruct(this);
}
}


/// <summary>
/// Construct a CoreBySketchInputs specifying all inputs.
/// </summary>
/// <returns></returns>
[JsonConstructor]
public CoreBySketchInputs(Elements.Geometry.Polygon perimeter, double coreheightaboveroof, string bucketName, string uploadsBucket, Dictionary<string, string> modelInputKeys, string gltfKey, string elementsKey, string ifcKey): base(bucketName, uploadsBucket, modelInputKeys, gltfKey, elementsKey, ifcKey)
{
this.Perimeter = perimeter;
this.CoreHeightAboveRoof = coreheightaboveroof;

}

public override string ToString()
{
var json = JsonConvert.SerializeObject(this);
return json;
}
}

/// <summary>List of perimeters of the buildings service cores.</summary>
[Newtonsoft.Json.JsonProperty("Perimeters", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public IList<Polygon> Perimeters { get; set; }

/// <summary>The projection of the service core above the highest building level.</summary>
[Newtonsoft.Json.JsonProperty("HeightAboveRoof", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
[System.ComponentModel.DataAnnotations.Range(0D, double.MaxValue)]
public double HeightAboveRoof { get; set; } = 2D;


}
}
2 changes: 1 addition & 1 deletion Core/CoreBySketch/src/CoreBySketchOutputs.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class CoreBySketchOutputs: ResultsBase
public double Height {get;}



/// <summary>
/// Construct a CoreBySketchOutputs with default inputs.
/// This should be used for testing only.
Expand Down
49 changes: 36 additions & 13 deletions Core/CoreBySketch/src/Function.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Hypar.Functions.Execution.AWS;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

Expand All @@ -22,25 +23,47 @@ public class Function

public async Task<CoreBySketchOutputs> Handler(CoreBySketchInputs args, ILambdaContext context)
{
if(this.store == null)
// 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())
{
// Preload the dependencies (if they exist),
// so that they are available during model deserialization.
var asmLocation = this.GetType().Assembly.Location;
var asmDir = Path.GetDirectoryName(asmLocation);
var asmName = Path.GetFileNameWithoutExtension(asmLocation);
var depPath = Path.Combine(asmDir, $"{asmName}.Dependencies.dll");

if(File.Exists(depPath))
try
{
Console.WriteLine($"Loading dependencies from assembly: {depPath}...");
Assembly.LoadFrom(depPath);
Console.WriteLine("Dependencies assembly loaded.");
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<CoreBySketchInputs>(RegionEndpoint.USWest1);
}

var l = new InvocationWrapper<CoreBySketchInputs,CoreBySketchOutputs>(store, CoreBySketch.Execute);
var output = await l.InvokeAsync(args);
return output;
Expand Down
22 changes: 15 additions & 7 deletions Core/CoreBySketch/test/CoreBySketchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,27 @@ public class CoreBySketchTests
[Fact]
public void CoreBySketchTest()
{
var polygon =
new Polygon
(
new[]
{
var polygons =
new Polygon [] {
new Polygon(new Vector3[]{
new Vector3(30.0, 30.0),
new Vector3(60.0, 30.0),
new Vector3(60.0, 60.0),
new Vector3(30.0, 60.0)
}
);
),
new Polygon( new Vector3[]{
new Vector3(60.0, 60.0),
new Vector3(90.0, 60.0),
new Vector3(90.0, 90.0),
new Vector3(60.0, 90.0)
}
)


};
var model = Model.FromJson(System.IO.File.ReadAllText("../../../../../../TestOutput/LevelsByEnvelope.json"));
var inputs = new CoreBySketchInputs(polygon, 3.0, "", "", new Dictionary<string, string>(), "", "", "");
var inputs = new CoreBySketchInputs(polygons, 3.0, "", "", new Dictionary<string, string>(), "", "", "");
var outputs = CoreBySketch.Execute(new Dictionary<string, Model>{{"Levels", model}}, inputs);
System.IO.File.WriteAllText("../../../../../../TestOutput/CoreBySketch.json", outputs.Model.ToJson());
outputs.Model.ToGlTF("../../../../../../TestOutput/CoreBySketch.glb");
Expand Down
Loading