From ed431a50b54dc4b19a88786e22ff3983857929a2 Mon Sep 17 00:00:00 2001 From: michaldengusiak Date: Wed, 11 Sep 2019 08:32:12 +0100 Subject: [PATCH 1/5] AddFragments --- Physical_Engine/Modify/AddFragment.cs | 49 ++++++++++++++++++++++++++ Physical_Engine/Physical_Engine.csproj | 1 + 2 files changed, 50 insertions(+) create mode 100644 Physical_Engine/Modify/AddFragment.cs diff --git a/Physical_Engine/Modify/AddFragment.cs b/Physical_Engine/Modify/AddFragment.cs new file mode 100644 index 000000000..40ace1b8f --- /dev/null +++ b/Physical_Engine/Modify/AddFragment.cs @@ -0,0 +1,49 @@ +/* + * This file is part of the Buildings and Habitats object Model (BHoM) + * Copyright (c) 2015 - 2019, the respective contributors. All rights reserved. + * + * Each contributor holds copyright over their respective contributions. + * The project versioning (Git) records all such contribution source information. + * + * + * The BHoM is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3.0 of the License, or + * (at your option) any later version. + * + * The BHoM is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this code. If not, see . + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using BH.oM.Base; +using BH.oM.Physical; +using BH.oM.Reflection.Attributes; +using System.ComponentModel; + +namespace BH.Engine.Physical +{ + public static partial class Modify + { + [Description("Appends a Fragment Property to a given Physical Object")] + [Input("physicalObject", "Any object implementing the IPhysical interface that can have fragment properties appended to it")] + [Input("fragment", "Any fragment object implementing the IBHoMFragment interface to append to the object")] + [Output("physicalObject", "The environment object with the added fragment")] + public static IPhysical AddFragment(this IPhysical physicalObject, IBHoMFragment fragment) + { + if (physicalObject == null) return null; + physicalObject.Fragments.Add(fragment); + return physicalObject; + } + } +} diff --git a/Physical_Engine/Physical_Engine.csproj b/Physical_Engine/Physical_Engine.csproj index a495782c3..69bec1598 100644 --- a/Physical_Engine/Physical_Engine.csproj +++ b/Physical_Engine/Physical_Engine.csproj @@ -61,6 +61,7 @@ + From 00fa7832a65697707608f5838e645692cf5a16f9 Mon Sep 17 00:00:00 2001 From: michaldengusiak Date: Wed, 11 Sep 2019 12:28:56 +0100 Subject: [PATCH 2/5] added adjustment --- BHoM_Engine/BHoM_Engine.csproj | 2 + .../Modify/AddFragment.cs | 19 ++++--- BHoM_Engine/Query/FindFragment.cs | 49 +++++++++++++++++++ Physical_Engine/Physical_Engine.csproj | 1 - 4 files changed, 60 insertions(+), 11 deletions(-) rename {Physical_Engine => BHoM_Engine}/Modify/AddFragment.cs (75%) create mode 100644 BHoM_Engine/Query/FindFragment.cs diff --git a/BHoM_Engine/BHoM_Engine.csproj b/BHoM_Engine/BHoM_Engine.csproj index 7c5e429f4..df15dc3e1 100644 --- a/BHoM_Engine/BHoM_Engine.csproj +++ b/BHoM_Engine/BHoM_Engine.csproj @@ -59,10 +59,12 @@ + + diff --git a/Physical_Engine/Modify/AddFragment.cs b/BHoM_Engine/Modify/AddFragment.cs similarity index 75% rename from Physical_Engine/Modify/AddFragment.cs rename to BHoM_Engine/Modify/AddFragment.cs index 40ace1b8f..2df32296a 100644 --- a/Physical_Engine/Modify/AddFragment.cs +++ b/BHoM_Engine/Modify/AddFragment.cs @@ -1,4 +1,4 @@ -/* +/* * This file is part of the Buildings and Habitats object Model (BHoM) * Copyright (c) 2015 - 2019, the respective contributors. All rights reserved. * @@ -27,23 +27,22 @@ using System.Threading.Tasks; using BH.oM.Base; -using BH.oM.Physical; using BH.oM.Reflection.Attributes; using System.ComponentModel; -namespace BH.Engine.Physical +namespace BH.Engine.Base { public static partial class Modify { - [Description("Appends a Fragment Property to a given Physical Object")] - [Input("physicalObject", "Any object implementing the IPhysical interface that can have fragment properties appended to it")] + [Description("Appends a Fragment Property to a given BHoM Object")] + [Input("iBHoMObject", "Any object implementing the IBHoMObject interface that can have fragment properties appended to it")] [Input("fragment", "Any fragment object implementing the IBHoMFragment interface to append to the object")] - [Output("physicalObject", "The environment object with the added fragment")] - public static IPhysical AddFragment(this IPhysical physicalObject, IBHoMFragment fragment) + [Output("iBHoMObject", "The BHoM object with the added fragment")] + public static IBHoMObject AddFragment(this IBHoMObject iBHoMObject, IBHoMFragment fragment) { - if (physicalObject == null) return null; - physicalObject.Fragments.Add(fragment); - return physicalObject; + if (iBHoMObject == null) return null; + iBHoMObject.Fragments.Add(fragment); + return iBHoMObject; } } } diff --git a/BHoM_Engine/Query/FindFragment.cs b/BHoM_Engine/Query/FindFragment.cs new file mode 100644 index 000000000..049b3213d --- /dev/null +++ b/BHoM_Engine/Query/FindFragment.cs @@ -0,0 +1,49 @@ +/* + * This file is part of the Buildings and Habitats object Model (BHoM) + * Copyright (c) 2015 - 2019, the respective contributors. All rights reserved. + * + * Each contributor holds copyright over their respective contributions. + * The project versioning (Git) records all such contribution source information. + * + * + * The BHoM is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3.0 of the License, or + * (at your option) any later version. + * + * The BHoM is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this code. If not, see . + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using BH.oM.Base; +using BH.oM.Reflection.Attributes; +using System.ComponentModel; + +namespace BH.Engine.Base +{ + public static partial class Query + { + /***************************************************/ + /**** Public Methods ****/ + /***************************************************/ + + [Description("Returns an instance of a BHoM Fragment if it exists on the object")] + [Input("iBHoMObject", "A generic IBHoMObject object")] + [Input("fragmentType", "The type of fragment to be queried and returned")] + [Output("fragment", "The instance of that Fragment if it exists on the object, null otherwise")] + public static T FindFragment(this IBHoMObject iBHoMObject, Type fragmentType) + { + return (T)System.Convert.ChangeType(iBHoMObject.Fragments.Where(x => x.GetType() == fragmentType).FirstOrDefault(), fragmentType); + } + } +} \ No newline at end of file diff --git a/Physical_Engine/Physical_Engine.csproj b/Physical_Engine/Physical_Engine.csproj index 69bec1598..a495782c3 100644 --- a/Physical_Engine/Physical_Engine.csproj +++ b/Physical_Engine/Physical_Engine.csproj @@ -61,7 +61,6 @@ - From 097af496b4b4a617f04714f6b74798ae584e905e Mon Sep 17 00:00:00 2001 From: michaldengusiak Date: Thu, 12 Sep 2019 12:30:54 +0100 Subject: [PATCH 3/5] Isak comments --- BHoM_Engine/Modify/AddFragment.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/BHoM_Engine/Modify/AddFragment.cs b/BHoM_Engine/Modify/AddFragment.cs index 2df32296a..979ed45f9 100644 --- a/BHoM_Engine/Modify/AddFragment.cs +++ b/BHoM_Engine/Modify/AddFragment.cs @@ -41,8 +41,10 @@ public static partial class Modify public static IBHoMObject AddFragment(this IBHoMObject iBHoMObject, IBHoMFragment fragment) { if (iBHoMObject == null) return null; - iBHoMObject.Fragments.Add(fragment); - return iBHoMObject; + IBHoMObject o = iBHoMObject.DeepClone(); + o.Fragments = new List(iBHoMObject.Fragments); + o.Fragments.Add(fragment); + return o; } } } From 55293736f10ec6c19916c959f5e723340b8dda79 Mon Sep 17 00:00:00 2001 From: michaldengusiak Date: Fri, 13 Sep 2019 15:48:56 +0100 Subject: [PATCH 4/5] update per comments --- BHoM_Engine/Modify/AddFragment.cs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/BHoM_Engine/Modify/AddFragment.cs b/BHoM_Engine/Modify/AddFragment.cs index 979ed45f9..35e1c5354 100644 --- a/BHoM_Engine/Modify/AddFragment.cs +++ b/BHoM_Engine/Modify/AddFragment.cs @@ -37,13 +37,27 @@ public static partial class Modify [Description("Appends a Fragment Property to a given BHoM Object")] [Input("iBHoMObject", "Any object implementing the IBHoMObject interface that can have fragment properties appended to it")] [Input("fragment", "Any fragment object implementing the IBHoMFragment interface to append to the object")] + [Input("replace", "If true will replace exisiting fragments ")] [Output("iBHoMObject", "The BHoM object with the added fragment")] - public static IBHoMObject AddFragment(this IBHoMObject iBHoMObject, IBHoMFragment fragment) + public static IBHoMObject AddFragment(this IBHoMObject iBHoMObject, IBHoMFragment fragment, bool replace = false) { if (iBHoMObject == null) return null; IBHoMObject o = iBHoMObject.DeepClone(); o.Fragments = new List(iBHoMObject.Fragments); - o.Fragments.Add(fragment); + if(o.Fragments.Contains(fragment)) + { + if(replace) + { + o.Fragments[o.Fragments.IndexOf(fragment)] = fragment; + } + else + { + Reflection.Compute.RecordError("Fragment already exists"); + } + } + else + o.Fragments.Add(fragment); + return o; } } From 6e3e2b9de1da7ab7f486146ce898b17c4994c3a7 Mon Sep 17 00:00:00 2001 From: michaldengusiak Date: Sat, 14 Sep 2019 21:29:46 +0100 Subject: [PATCH 5/5] implement comments --- BHoM_Engine/Modify/AddFragment.cs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/BHoM_Engine/Modify/AddFragment.cs b/BHoM_Engine/Modify/AddFragment.cs index 35e1c5354..23cd2ed54 100644 --- a/BHoM_Engine/Modify/AddFragment.cs +++ b/BHoM_Engine/Modify/AddFragment.cs @@ -37,7 +37,7 @@ public static partial class Modify [Description("Appends a Fragment Property to a given BHoM Object")] [Input("iBHoMObject", "Any object implementing the IBHoMObject interface that can have fragment properties appended to it")] [Input("fragment", "Any fragment object implementing the IBHoMFragment interface to append to the object")] - [Input("replace", "If true will replace exisiting fragments ")] + [Input("replace", "If set to true and the object already contains a fragment of the type being added, the fragment will be replaced by this instance")] [Output("iBHoMObject", "The BHoM object with the added fragment")] public static IBHoMObject AddFragment(this IBHoMObject iBHoMObject, IBHoMFragment fragment, bool replace = false) { @@ -47,13 +47,9 @@ public static IBHoMObject AddFragment(this IBHoMObject iBHoMObject, IBHoMFragmen if(o.Fragments.Contains(fragment)) { if(replace) - { o.Fragments[o.Fragments.IndexOf(fragment)] = fragment; - } else - { - Reflection.Compute.RecordError("Fragment already exists"); - } + Reflection.Compute.RecordError("That fragment already exists on this object. If you would like to replace the existing fragment set the 'replace' input to 'true'"); } else o.Fragments.Add(fragment);