public static IBHoMObject RemoveFragment(this IBHoMObject iBHoMObject, Type fragmentType = null) { if (fragmentType == null) { return(iBHoMObject); } if (iBHoMObject == null) { return(null); } IBHoMObject o = iBHoMObject.DeepClone(); if (!typeof(IFragment).IsAssignableFrom(fragmentType)) { Reflection.Compute.RecordError("Provided input in fragmentType is not a Fragment type (does not implement IFragment interface)."); return(iBHoMObject); } if (!iBHoMObject.Fragments.Contains(fragmentType)) { Reflection.Compute.RecordWarning($"{iBHoMObject.GetType().Name} does not contain any `{fragmentType.Name}` fragment."); return(iBHoMObject); } o.Fragments.Remove(fragmentType); return(o); }
public static IBHoMObject AddFragment(this IBHoMObject iBHoMObject, IBHoMFragment fragment, bool replace = false) { if (iBHoMObject == null) { return(null); } IBHoMObject o = iBHoMObject.DeepClone(); int index = o.Fragments.FindIndex(x => x.GetType() == fragment.GetType()); if (index >= 0) { if (replace) { o.Fragments[index] = fragment; } else { 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); } return(o); }
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 <IBHoMFragment>(iBHoMObject.Fragments); if (o.Fragments.Contains(fragment)) { if (replace) { o.Fragments[o.Fragments.IndexOf(fragment)] = fragment; } else { 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); } return(o); }
public static IBHoMObject AddFragment(this IBHoMObject iBHoMObject, IBHoMFragment fragment, bool replace = false) { if (iBHoMObject == null) { return(null); } IBHoMObject o = iBHoMObject.DeepClone(); if (!replace) { o.Fragments.Add(fragment); } else { o.Fragments.AddOrReplace(fragment); } return(o); }
public static IBHoMObject AddFragment(this IBHoMObject iBHoMObject, IFragment fragment, bool replace = false) { if (iBHoMObject == null || fragment == null) { return(null); } IBHoMObject o = iBHoMObject.DeepClone(); // Give a warning if the fragment is supposed to be unique but is found on the object List <Type> currentFragmentTypes = iBHoMObject.Fragments.Select(x => x.GetType()).ToList(); foreach (Type restriction in fragment.GetType().UniquenessRestrictions()) { if (currentFragmentTypes.Any(x => restriction.IsAssignableFrom(x) && x != fragment.GetType())) { Engine.Reflection.Compute.RecordWarning("There is already a fragment of type " + restriction + " on this object. \nThe Fragment will still be added but consider reviewing this task as fragments of that type are supposed to be unique."); } } // Make sure this fragment can be added to that object if (fragment.CanTarget(iBHoMObject)) { if (!replace) { o.Fragments.Add(fragment); } else { o.Fragments.AddOrReplace(fragment); } } else { Engine.Reflection.Compute.RecordError("An object of type " + iBHoMObject.GetType() + " is not a valid target for a fragment of type " + fragment.GetType() + ". The fragment was not added."); } return(o); }