Skip to content

Commit

Permalink
feat(serialization): serialization main entities
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonelloDN committed Nov 3, 2023
1 parent 152a518 commit 9044a6d
Show file tree
Hide file tree
Showing 18 changed files with 972 additions and 82 deletions.
25 changes: 18 additions & 7 deletions project/Morpho/Morpho25/Geometry/Building.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,30 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace Morpho25.Geometry
{
[DisplayName("Building")]
/// <summary>
/// Building class.
/// </summary>
public class Building : Entity, IEquatable<Building>
{
[JsonProperty("observeBPS")]
[DisplayName("Name")]
[Description("Name of the building group")]
[JsonProperty("name")]
/// <summary>
/// Enable Building BPS output
/// Name of the building.
/// </summary>
public bool ObserveBPS { get; }
public override string Name { get; }

[JsonProperty("geometry")]
[DisplayName("Geometry")]
[Description("Solid geometry")]
[JsonProperty("geometry", Required = Required.Always)]
/// <summary>
/// Geometry of the building.
/// </summary>
Expand Down Expand Up @@ -56,6 +63,8 @@ public class Building : Entity, IEquatable<Building>
/// </summary>
public List<string> BuildingGreenWallRows { get; private set; }

[DisplayName("Material")]
[Description("Facade and roof materials")]
[JsonProperty("material")]
/// <summary>
/// Material of the building.
Expand All @@ -74,11 +83,13 @@ protected set

}

[JsonProperty("name")]
[DisplayName("Observe BPS")]
[Description("Export BPS output")]
[JsonProperty("observeBPS")]
/// <summary>
/// Name of the building.
/// Enable Building BPS output
/// </summary>
public override string Name { get; }
public bool ObserveBPS { get; }

[JsonConstructor]
/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions project/Morpho/Morpho25/Geometry/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace Morpho25.Geometry
{
Expand All @@ -25,6 +26,8 @@ public abstract class Entity
/// </summary>
public abstract string Name { get; }

[DisplayName("ID")]
[Description("Numeric ID of the entity")]
[JsonProperty("id")]
/// <summary>
/// ID of the entity.
Expand Down
1 change: 1 addition & 0 deletions project/Morpho/Morpho25/Geometry/Grid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Morpho25.Geometry
{
[DisplayName("Grid")]
/// <summary>
/// Grid class.
/// </summary>
Expand Down
7 changes: 6 additions & 1 deletion project/Morpho/Morpho25/Geometry/Material.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using Newtonsoft.Json;
using Morpho25.Utility;
using Newtonsoft.Json;
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace Morpho25.Geometry
Expand Down Expand Up @@ -50,6 +53,8 @@ public class Material : IEquatable<Material>
/// </summary>
public const string DEFAULT_PLANT_3D = "0000C2";

[MinLength(1)]
[MaxLength(4)]
[JsonProperty("ids", Required = Required.Always)]
/// <summary>
/// Material array of ID.
Expand Down
104 changes: 94 additions & 10 deletions project/Morpho/Morpho25/Geometry/Plant2d.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
using Morpho25.Utility;
using MorphoGeometry;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace Morpho25.Geometry
{
[DisplayName("Simple Plant")]
/// <summary>
/// Plant 2D class.
/// </summary>
public class Plant2d : Entity
public class Plant2d : Entity, IEquatable<Plant2d>
{
/// <summary>
/// Geometry of the plant 2D.
/// </summary>
public FaceGroup Geometry { get; }
[DisplayName("Name")]
[Description("Name of the simple plant group")]
[JsonProperty("name")]
/// <summary>
/// Name of the plant 2D.
/// </summary>
public override string Name { get; }

[DisplayName("Geometry")]
[Description("Flat or solid geometry")]
[JsonProperty("geometry", Required = Required.Always)]
/// <summary>
/// Geometry of the plant 2D.
/// </summary>
public FaceGroup Geometry { get; }

[JsonIgnore]
/// <summary>
/// Matrix 2D of the plant 2D.
/// </summary>
public Matrix2d IDmatrix { get; private set; }

[DisplayName("Material")]
[Description("Simple plant type")]
[JsonProperty("material")]
/// <summary>
/// Material of the plant 2D.
/// </summary>
Expand All @@ -38,15 +54,16 @@ protected set
}

}

[JsonConstructor]
/// <summary>
/// Create a new plant 2D.
/// </summary>
/// <param name="grid">Grid object.</param>
/// <param name="geometry">Geometry of the plant 2D.</param>
/// <param name="id">Numerical ID.</param>
/// <param name="code">Code of the material.</param>
/// <param name="name">Name of the plant 2D.</param>
public Plant2d(Grid grid, FaceGroup geometry,
public Plant2d(FaceGroup geometry,
int id, string code = null, string name = null)
{
ID = id;
Expand All @@ -55,11 +72,9 @@ public Plant2d(Grid grid, FaceGroup geometry,
? CreateMaterial(Material.DEFAULT_PLANT_2D, code)
: CreateMaterial(Material.DEFAULT_PLANT_2D);
Name = name ?? "PlantGroup";

SetMatrix(grid);
}

private void SetMatrix(Grid grid)
public void SetMatrix(Grid grid)
{
Matrix2d matrix = new Matrix2d(grid.Size.NumX, grid.Size.NumY, "");

Expand All @@ -81,5 +96,74 @@ public override string ToString()
Name, ID, Material.IDs[0]);
}

public string Serialize()
{
return JsonConvert.SerializeObject(this);
}

public static Plant2d Deserialize(string json)
{
try
{
return JsonConvert.DeserializeObject<Plant2d>(json);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}

public bool Equals(Plant2d other)
{
if (other == null)
return false;

if (other != null
&& other.ID == this.ID
&& other.Name == this.Name
&& other.Material == this.Material
&& other.Geometry == this.Geometry)
return true;
else
return false;
}

public override bool Equals(Object obj)
{
if (obj == null)
return false;

var plantObj = obj as Plant2d;
if (plantObj == null)
return false;
else
return Equals(plantObj);
}

public override int GetHashCode()
{
unchecked
{
int hash = 17;
hash = hash * 23 + ID.GetHashCode();
hash = hash * 23 + Name.GetHashCode();
hash = hash * 23 + Material.GetHashCode();
hash = hash * 23 + Geometry.GetHashCode();
return hash;
}
}

public static bool operator ==(Plant2d plant1, Plant2d plant2)
{
if (((object)plant1) == null || ((object)plant2) == null)
return Object.Equals(plant1, plant2);

return plant1.Equals(plant2);
}

public static bool operator !=(Plant2d plant1, Plant2d plant2)
{
return !(plant1 == plant2);
}
}
}
Loading

0 comments on commit 9044a6d

Please sign in to comment.