diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d70a28b1..c5a2c23f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - `Line.BestFit(IList points)` - `Vector3Extensions.BestFitLine(this IList points)` - `Polygon.FromAlignedBoundingBox2d(IEnumerable points, Vector3 axis, double minSideSize = 0.1)` +- `Elements.IModel` ### Changed diff --git a/Elements/src/IModel.cs b/Elements/src/IModel.cs new file mode 100644 index 000000000..263dee68e --- /dev/null +++ b/Elements/src/IModel.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using Elements.Geometry; + +namespace Elements +{ + /// + /// The interface for all models. + /// + public interface IModel + { + /// + /// The transform of the model. + /// + Transform Transform { get; set; } + + /// + /// Add an element to the model. + /// + /// The element to add to the model. + /// Should elements be searched recursively for child elements? + void AddElement(Element element, bool gatherSubElements = true); + + /// + /// Add elements to the model. + /// + /// A collection of elements to add to the model. + /// Should elements be searched recursively for child elements? + void AddElements(IEnumerable elements, bool gatherSubElements = true); + + /// + /// Add elements to the model. + /// + /// Elements to add to the model. + void AddElements(params Element[] elements); + + /// + /// Get an element by id from the model. + /// + /// The id of the element. + /// The type of the element. + /// An element of type T. + T GetElementOfType(Guid id) where T : Element; + + /// + /// Get an element by name from the model. + /// + /// The name of the element. + /// The type of the element. + /// An element of type T. + T GetElementByName(string name) where T : Element; + + /// + /// Get all elements of type T from the model. + /// + /// The type of elements to return. + /// A collection of elements of type T. + IEnumerable AllElementsOfType(); + } +} \ No newline at end of file diff --git a/Elements/src/Model.cs b/Elements/src/Model.cs index 62fdf5e77..9a28c2425 100644 --- a/Elements/src/Model.cs +++ b/Elements/src/Model.cs @@ -18,10 +18,10 @@ namespace Elements /// /// A container for elements. /// - public class Model + public class Model : IModel { /// The origin of the model. - [JsonProperty("Origin", Required = Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)] + [JsonProperty("Origin", Required = Required.Default, NullValueHandling = NullValueHandling.Ignore)] [Obsolete("Use Transform instead.")] public Position Origin { get; set; } @@ -29,10 +29,10 @@ public class Model [JsonProperty("Transform", Required = Required.AllowNull)] public Transform Transform { get; set; } - /// A collection of Elements keyed by their identifiers. + /// A collection of elements keyed by their identifiers. [JsonProperty("Elements", Required = Required.Always)] [System.ComponentModel.DataAnnotations.Required] - public System.Collections.Generic.IDictionary Elements { get; set; } = new System.Collections.Generic.Dictionary(); + public IDictionary Elements { get; set; } = new Dictionary(); /// /// Construct a model. @@ -41,7 +41,7 @@ public class Model /// The transform of the model. /// A collection of elements. [JsonConstructor] - public Model(Position @origin, Transform @transform, System.Collections.Generic.IDictionary @elements) + public Model(Position @origin, Transform @transform, IDictionary @elements) { #pragma warning disable CS0618 @@ -59,12 +59,12 @@ public Model() this.Transform = new Transform(); } - /// + /// O /// Construct a model. /// /// The model's transform. /// The model's elements. - public Model(Transform @transform, System.Collections.Generic.IDictionary @elements) + public Model(Transform @transform, IDictionary @elements) { this.Transform = @transform; this.Elements = @elements; @@ -153,10 +153,10 @@ public void AddElements(params Element[] elements) } /// - /// Get an entity by id from the Model. + /// Get an element by id from the Model. /// /// The identifier of the element. - /// An entity or null if no entity can be found + /// An element or null if no element can be found /// with the provided id. public T GetElementOfType(Guid id) where T : Element { @@ -168,10 +168,10 @@ public T GetElementOfType(Guid id) where T : Element } /// - /// Get the first entity with the specified name. + /// Get the first element with the specified name. /// /// - /// An entity or null if no entity can be found + /// An element or null if no element can be found /// with the provided name. public T GetElementByName(string name) where T : Element { @@ -184,7 +184,7 @@ public T GetElementByName(string name) where T : Element } /// - /// Get all elements of the type T. + /// Get all elements of the specified Type. /// /// The type of element to return. /// A collection of elements of the specified type. @@ -454,37 +454,4 @@ private static bool IsValidListType(Type t) || typeof(SolidOperation).IsAssignableFrom(t); } } - - public static class ModelExtensions - { - /// - /// Get all elements of a certain type from a specific model name in a dictionary of models. - /// - /// Dictionary of models keyed by string. - /// The name of the model. - /// The type of element we want to retrieve. - /// - public static List AllElementsOfType(this Dictionary models, string modelName) where T : Element - { - var elements = new List(); - models.TryGetValue(modelName, out var model); - if (model != null) - { - elements.AddRange(model.AllElementsOfType()); - } - return elements; - } - - /// - /// Get all proxies of a certain type from a specific model name in a dictionary of models. - /// - /// Dictionary of models keyed by string - /// The name of the model - /// The type of element we want to retrieve - /// - public static List> AllProxiesOfType(this Dictionary models, string modelName) where T : Element - { - return models.AllElementsOfType(modelName).Proxies(modelName).ToList(); - } - } } \ No newline at end of file diff --git a/Elements/src/ModelExtensions.cs b/Elements/src/ModelExtensions.cs new file mode 100644 index 000000000..07bac2231 --- /dev/null +++ b/Elements/src/ModelExtensions.cs @@ -0,0 +1,41 @@ +using System.Collections.Generic; +using System.Linq; + +namespace Elements +{ + /// + /// Model extension methods. + /// + public static class ModelExtensions + { + /// + /// Get all elements of a certain type from a specific model name in a dictionary of models. + /// + /// Dictionary of models keyed by string. + /// The name of the model. + /// The type of element we want to retrieve. + /// + public static List AllElementsOfType(this Dictionary models, string modelName) where T : Element + { + var elements = new List(); + models.TryGetValue(modelName, out var model); + if (model != null) + { + elements.AddRange(model.AllElementsOfType()); + } + return elements; + } + + /// + /// Get all proxies of a certain type from a specific model name in a dictionary of models. + /// + /// Dictionary of models keyed by string + /// The name of the model + /// The type of element we want to retrieve + /// + public static List> AllProxiesOfType(this Dictionary models, string modelName) where T : Element + { + return models.AllElementsOfType(modelName).Proxies(modelName).ToList(); + } + } +} \ No newline at end of file