| | 1 | | using Morpho25.Utility; |
| | 2 | | using Newtonsoft.Json; |
| | 3 | | using System; |
| | 4 | | using System.ComponentModel; |
| | 5 | | using System.ComponentModel.DataAnnotations; |
| | 6 | | using System.Linq; |
| | 7 | |
|
| | 8 | | namespace Morpho25.Geometry |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Material class. |
| | 12 | | /// </summary> |
| | 13 | | public class Material : IEquatable<Material> |
| | 14 | | { |
| | 15 | | [JsonIgnore] |
| | 16 | | /// <summary> |
| | 17 | | /// Default wall material. |
| | 18 | | /// </summary> |
| | 19 | | public const string DEFAULT_WALL = "000000"; |
| | 20 | | [JsonIgnore] |
| | 21 | | /// <summary> |
| | 22 | | /// Default roof material. |
| | 23 | | /// </summary> |
| | 24 | | public const string DEFAULT_ROOF = "000000"; |
| | 25 | | [JsonIgnore] |
| | 26 | | /// <summary> |
| | 27 | | /// Default green roof material. |
| | 28 | | /// </summary> |
| | 29 | | public const string DEFAULT_GREEN_ROOF = " "; |
| | 30 | | [JsonIgnore] |
| | 31 | | /// <summary> |
| | 32 | | /// Default green wall material. |
| | 33 | | /// </summary> |
| | 34 | | public const string DEFAULT_GREEN_WALL = " "; |
| | 35 | | [JsonIgnore] |
| | 36 | | /// <summary> |
| | 37 | | /// Default soil material. |
| | 38 | | /// </summary> |
| | 39 | | public const string DEFAULT_SOIL = "000000"; |
| | 40 | | [JsonIgnore] |
| | 41 | | /// <summary> |
| | 42 | | /// Default plant 2D material. |
| | 43 | | /// </summary> |
| | 44 | | public const string DEFAULT_PLANT_2D = "0000XX"; |
| | 45 | | [JsonIgnore] |
| | 46 | | /// <summary> |
| | 47 | | /// Default source material. |
| | 48 | | /// </summary> |
| | 49 | | public const string DEFAULT_SOURCE = "0000FT"; |
| | 50 | | [JsonIgnore] |
| | 51 | | /// <summary> |
| | 52 | | /// Default plant 3D material. |
| | 53 | | /// </summary> |
| | 54 | | public const string DEFAULT_PLANT_3D = "0000C2"; |
| | 55 | |
|
| | 56 | | [MinLength(1)] |
| | 57 | | [MaxLength(4)] |
| | 58 | | [JsonProperty("ids", Required = Required.Always)] |
| | 59 | | /// <summary> |
| | 60 | | /// Material array of ID. |
| | 61 | | /// </summary> |
| 59 | 62 | | public string[] IDs { get; private set; } |
| | 63 | |
|
| | 64 | | [JsonConstructor] |
| | 65 | | /// <summary> |
| | 66 | | /// Create a new material. |
| | 67 | | /// </summary> |
| | 68 | | /// <param name="ids">Array of material code.</param> |
| 22 | 69 | | public Material(string[] ids) |
| 22 | 70 | | { |
| 22 | 71 | | IDs = ids; |
| 22 | 72 | | } |
| | 73 | |
|
| | 74 | | /// <summary> |
| | 75 | | /// String representation of the material. |
| | 76 | | /// </summary> |
| | 77 | | /// <returns>String representation.</returns> |
| | 78 | | public override string ToString() |
| 0 | 79 | | { |
| 0 | 80 | | return String.Format("Material::{0}", String.Join(",", IDs)); |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | public string Serialize() |
| 1 | 84 | | { |
| 1 | 85 | | return JsonConvert.SerializeObject(this); |
| 1 | 86 | | } |
| | 87 | |
|
| | 88 | | public static Material Deserialize(string json) |
| 9 | 89 | | { |
| | 90 | | try |
| 9 | 91 | | { |
| 9 | 92 | | return JsonConvert.DeserializeObject<Material>(json); |
| | 93 | | } |
| 8 | 94 | | catch (Exception e) |
| 8 | 95 | | { |
| 8 | 96 | | throw new Exception(e.Message); |
| | 97 | | } |
| 1 | 98 | | } |
| | 99 | |
|
| | 100 | | public bool Equals(Material other) |
| 6 | 101 | | { |
| 6 | 102 | | if (other == null) |
| 0 | 103 | | return false; |
| | 104 | |
|
| 6 | 105 | | if (other != null |
| 6 | 106 | | && Enumerable.SequenceEqual(other.IDs, this.IDs)) |
| 6 | 107 | | return true; |
| | 108 | | else |
| 0 | 109 | | return false; |
| 6 | 110 | | } |
| | 111 | |
|
| | 112 | | public override bool Equals(Object obj) |
| 6 | 113 | | { |
| 6 | 114 | | if (obj == null) |
| 0 | 115 | | return false; |
| | 116 | |
|
| 6 | 117 | | var materialObj = obj as Material; |
| 6 | 118 | | if (materialObj == null) |
| 6 | 119 | | return false; |
| | 120 | | else |
| 0 | 121 | | return Equals(materialObj); |
| 6 | 122 | | } |
| | 123 | |
|
| | 124 | | public override int GetHashCode() |
| 0 | 125 | | { |
| | 126 | | unchecked |
| 0 | 127 | | { |
| 0 | 128 | | int hash = 17; |
| 0 | 129 | | hash = hash * 23 + IDs.GetHashCode(); |
| 0 | 130 | | return hash; |
| | 131 | | } |
| 0 | 132 | | } |
| | 133 | |
|
| | 134 | | public static bool operator ==(Material mat1, Material mat2) |
| 23 | 135 | | { |
| 23 | 136 | | if (((object)mat1) == null || ((object)mat2) == null) |
| 18 | 137 | | return Object.Equals(mat1, mat2); |
| | 138 | |
|
| 5 | 139 | | return mat1.Equals(mat2); |
| 23 | 140 | | } |
| | 141 | |
|
| | 142 | | public static bool operator !=(Material mat1, Material mat2) |
| 6 | 143 | | { |
| 6 | 144 | | return !(mat1 == mat2); |
| 6 | 145 | | } |
| | 146 | | } |
| | 147 | | } |