Example #1
0
        public static BHoMObject PropertyValue(this BHoMObject obj, string propName, object value)
        {
            BHoMObject newObject = obj.GetShallowClone() as BHoMObject;

            newObject.SetPropertyValue(propName, value);
            return(newObject);
        }
Example #2
0
        private static Dictionary <BHoMObject, BoundingBox> FilterObjects(List <BHoMObject> objects, SpatialSpecification spatialSpec)
        {
            Dictionary <BHoMObject, BoundingBox> passedObj_zoneBox = new Dictionary <BHoMObject, BoundingBox>();

            Dictionary <BHoMObject, IGeometry> objsGeometry = new Dictionary <BHoMObject, IGeometry>();

            // Compute the geometry for each object.
            foreach (var obj in objects)
            {
                BHoMObject bhomObj = obj as BHoMObject;
                IGeometry  geom    = BH.Engine.Base.Query.IGeometry(bhomObj);

                if (geom == null)
                {
                    BH.Engine.Reflection.Compute.RecordWarning($"Could not Query the Geometry out of a given {bhomObj.GetType().Name}.");
                    continue;
                }

                objsGeometry[bhomObj] = geom;
            }

            // Filter objects based on the spatialSpecifications.
            ZoneSpecification zoneSpec = spatialSpec.ZoneSpecification;

            // A bounding box is calculated per each object as defined by the individual ZoneSpecification
            Dictionary <BHoMObject, BoundingBox> bbPerObj = new Dictionary <BHoMObject, BoundingBox>();
            List <BoundingBox> allZoneBoxes = spatialSpec.SpatialBoundingBoxes();

            foreach (BHoMObject bhomObj in objsGeometry.Keys)
            {
                // Check if any of the Specification's boundingBoxes contains the object
                foreach (var zoneBB in allZoneBoxes)
                {
                    if (zoneBB.IsContaining(objsGeometry[bhomObj]))
                    {
                        // If the zone bounding box contains the bhomObject's Geometry, let's apply the other filters.
                        var res = VerifyCondition(new List <object>()
                        {
                            bhomObj
                        }, new LogicalCondition()
                        {
                            Conditions = zoneSpec.FilterConditions
                        });
                        if (res.PassedObjects.Count == 1) // if the object passed all the provided Conditions, then it's passed.
                        {
                            passedObj_zoneBox[res.PassedObjects.First() as BHoMObject] = zoneBB;
                        }
                    }
                }
            }

            return(passedObj_zoneBox);
        }
Example #3
0
 public static double Identity(BHoMObject a, BHoMObject b)
 {
     if (a.BHoM_Guid == b.BHoM_Guid)
     {
         return(1);
     }
     else if (a.Name == b.Name && a.Name.Length > 0)
     {
         return(0.75);
     }
     else
     {
         return(0);
     }
 }
Example #4
0
        /***************************************************/
        /**** IGH_BakeAwareData methods                 ****/
        /***************************************************/

        public bool BakeGeometry(RhinoDoc doc, ObjectAttributes att, out Guid obj_guid)
        {
            if (m_RhinoGeometry != null)
            {
                if (att == null)
                {
                    att            = new ObjectAttributes();
                    att.LayerIndex = doc.Layers.CurrentLayerIndex;
                }

                if (Value is IRender)
                {
                    //bake with render colour always
                    att.ColorSource = ObjectColorSource.ColorFromObject;
                    att.ObjectColor = m_Color;
                    //special case for Text3d as it is not GeometryBase
                    if (Value is RenderText)
                    {
                        obj_guid = doc.Objects.AddText((Rhino.Display.Text3d)m_RhinoGeometry, att);
                    }
                    else
                    {
                        obj_guid = doc.Objects.Add(GH_Convert.ToGeometryBase(m_RhinoGeometry), att);
                    }
                }
                else
                {
                    BHoMObject bhObj = Value as BHoMObject;

                    if (string.IsNullOrEmpty(att.Name) && bhObj != null && !string.IsNullOrWhiteSpace(bhObj.Name))
                    {
                        att.Name = bhObj.Name;
                    }
                    obj_guid = doc.Objects.Add(GH_Convert.ToGeometryBase(m_RhinoGeometry), att);
                }

                return(true);
            }

            obj_guid = Guid.Empty;
            return(false);
        }
        /***************************************************/

        internal static void LogNullProperties(this BHoMObject obj, IEnumerable <string> propertyNames = null)
        {
            //TODO: Move this one to the BHoM_Engine?
            List <string> nullPropertyNames = new List <string>();

            Type type = obj.GetType();

            if (propertyNames == null)
            {
                PropertyInfo[] properties = type.GetProperties();
                foreach (PropertyInfo pi in properties)
                {
                    if (pi.GetValue(obj) == null)
                    {
                        nullPropertyNames.Add(pi.Name);
                    }
                }
            }
            else
            {
                foreach (string propertyName in propertyNames)
                {
                    if (type.GetProperty(propertyName).GetValue(obj) == null)
                    {
                        nullPropertyNames.Add(propertyName);
                    }
                }
            }

            if (nullPropertyNames.Count > 0)
            {
                string warning = string.Format("The BHoM object if missing following properties: {0}. BHoM_Guid: {1}.", string.Join(", ", nullPropertyNames), obj.BHoM_Guid);

                ElementId revitID = obj.ElementId();
                if (revitID != null)
                {
                    warning += string.Format(" Revit ElementId: {0}.", revitID.IntegerValue);
                }
                BH.Engine.Reflection.Compute.RecordWarning(warning);
            }
        }
Example #6
0
        /***************************************************/

        public IEnumerable <object> Pull(BatchRequest requests, HTTPConfig config)
        {
            if (config == null)
            {
                config = new HTTPConfig();
            }

            string[]          response = new string[requests.Requests.Count];
            List <BHoMObject> result   = new List <BHoMObject>();

            using (HttpClient client = new HttpClient()
            {
                Timeout = config.Timeout
            })
            {
                List <GetRequest> getRequests = requests.Requests.OfType <GetRequest>().ToList();
                response = Task.WhenAll(getRequests.Select(x => Compute.MakeRequestAsync(x, client))).GetAwaiter().GetResult();
                client.CancelPendingRequests();
                client.Dispose();
            }

            Parallel.ForEach(response, res =>
            {
                if (res == null)
                {
                    return;
                }
                BHoMObject obj = Engine.Serialiser.Convert.FromJson(res) as BHoMObject;
                if (obj == null)
                {
                    Engine.Base.Compute.RecordNote($"{res.GetType()} failed to deserialise to a BHoMObject and is set to null." +
                                                   $"Perform a request with Compute.GetRequest(string url) if you want the raw output");
                    return; // return is equivalent to `continue` in a Parallel.ForEach
                }
                result.Add(obj);
            });
            return(result);
        }
        /******************************************/
        /**** Public Methods - Interfaces      ****/
        /******************************************/

        public static List <Point> IElementVertices(this BHoMObject element)
        {
            return(ElementVertices(element as dynamic));
        }
Example #8
0
 public static List <ICurve> IElementCurves(this BHoMObject element, bool recursive = true)
 {
     return(IElementCurves(element as IElement, recursive));
 }
Example #9
0
        /***************************************************/

        public GH_BHoMObject(BHoMObject val) : base(val)
        {
        }
Example #10
0
 public Task(BHoMObject bhomObject, string property, string value)
 {
     BhomObject = bhomObject;
     Property   = property;
     Value      = value;
 }
Example #11
0
        /***************************************************/
        /****               Public Methods              ****/
        /***************************************************/

        public static IBHoMObject ObjectFromRevit(this Element element, Discipline discipline, RevitSettings settings = null, Dictionary <string, List <IBHoMObject> > refObjects = null)
        {
            settings = settings.DefaultIfNull();

            IBHoMObject iBHoMObject = refObjects.GetValue <IBHoMObject>(element.Id);

            if (iBHoMObject != null)
            {
                return(iBHoMObject);
            }

            IGeometry iGeometry = element.Location.IFromRevit();

            if (iGeometry != null)
            {
                ElementType elementType = element.Document.GetElement(element.GetTypeId()) as ElementType;
                if (elementType != null)
                {
                    InstanceProperties objectProperties = elementType.InstancePropertiesFromRevit(settings, refObjects) as InstanceProperties;
                    if (objectProperties != null)
                    {
                        if (element.ViewSpecific)
                        {
                            iBHoMObject = BH.Engine.Adapters.Revit.Create.DraftingInstance(objectProperties, element.Document.GetElement(element.OwnerViewId).Name, iGeometry as dynamic);
                        }
                        else
                        {
                            iBHoMObject = BH.Engine.Adapters.Revit.Create.ModelInstance(objectProperties, iGeometry as dynamic);
                            if (iBHoMObject is ModelInstance && (element as FamilyInstance)?.Host != null)
                            {
                                ((ModelInstance)iBHoMObject).HostId = (element as FamilyInstance).Host.Id.IntegerValue;
                            }
                        }

                        if (iGeometry is BH.oM.Geometry.Point)
                        {
                            Basis orientation = null;
                            if (element is FamilyInstance)
                            {
                                FamilyInstance fi = ((FamilyInstance)element);
                                XYZ            x  = fi.HandOrientation;
                                XYZ            y  = fi.FacingOrientation;
                                XYZ            z  = fi.GetTotalTransform().BasisZ;

                                orientation = new Basis(new Vector {
                                    X = x.X, Y = x.Y, Z = x.Z
                                }, new Vector {
                                    X = y.X, Y = y.Y, Z = y.Z
                                }, new Vector {
                                    X = z.X, Y = z.Y, Z = z.Z
                                });
                            }

                            ((IInstance)iBHoMObject).Orientation = orientation;
                        }
                    }
                }
            }

            if (iBHoMObject == null)
            {
                iBHoMObject = new BHoMObject();
            }

            iBHoMObject.Name = element.Name;
            iBHoMObject.SetIdentifiers(element);
            iBHoMObject.CopyParameters(element, settings.ParameterSettings);
            iBHoMObject.SetProperties(element, settings.ParameterSettings);

            refObjects.AddOrReplace(element.Id, iBHoMObject);

            return(iBHoMObject);
        }
Example #12
0
        /*************************************/

        public static IGH_Goo ToGoo(this BHoMObject obj)
        {
            return(new GH_BHoMObject(obj));
        }
Example #13
0
        /***************************************************/
        /**** Private Methods                           ****/
        /***************************************************/

        private static SpecificationResult VerifySpecification(List <object> objects, SpatialSpecification spatialSpec)
        {
            SpecificationResult specResult = new SpecificationResult();

            specResult.NotAssessedObjects = objects.Where(o => !(o is BHoMObject)).ToList();

            // Apply filters.
            ConditionResult filterResultAggregated = new ConditionResult();

            List <BHoMObject> bhomObjs = objects.OfType <BHoMObject>().ToList();

            Dictionary <BHoMObject, BoundingBox> passedObjs_bb = FilterObjects(bhomObjs, spatialSpec);

            filterResultAggregated.PassedObjects = passedObjs_bb.Keys.OfType <object>().ToList();
            filterResultAggregated.FailedObjects = bhomObjs.Except(passedObjs_bb.Keys).OfType <object>().ToList();

            // Apply checks.
            ConditionResult checkResultAggregated = new ConditionResult();

            foreach (var kv in passedObjs_bb)
            {
                BHoMObject  objToCheck = kv.Key;
                BoundingBox zoneBB     = kv.Value;
                IGeometry   geom3D     = BH.Engine.Base.Query.IGeometry3D(kv.Key);

                if (geom3D == null)
                {
                    BH.Engine.Reflection.Compute.RecordWarning($"Could not retrieve the Geometry3D for an object of type `{kv.Key.GetType().Name}`. The object will not be assessed.");
                    filterResultAggregated.FailedObjects.Add(kv.Key);
                    filterResultAggregated.PassedObjects.Remove(kv.Key);
                    continue;
                }

                bool isContained = zoneBB.IsContaining(geom3D);

                if (!isContained)
                {
                    checkResultAggregated.FailInfo.Add("The 3D geometry of the object is not contained in the Zone.");
                }

                var checkResult_obj = VerifyConditions(new List <object>()
                {
                    objToCheck
                }, spatialSpec.ZoneSpecification.CheckConditions);
                if (isContained)
                {
                    checkResultAggregated.PassedObjects.AddRange(checkResult_obj.PassedObjects);
                    checkResultAggregated.FailedObjects.AddRange(checkResult_obj.FailedObjects);
                    checkResultAggregated.Condition = checkResult_obj.Condition;
                }
                else
                {
                    checkResultAggregated.FailedObjects.Add(objToCheck);
                    List <ICondition> failedConditions = new List <ICondition> {
                        new IsInBoundingBox()
                        {
                            BoundingBox = zoneBB
                        }
                    };
                    failedConditions.AddRange(spatialSpec.ZoneSpecification.CheckConditions);
                    checkResultAggregated.Condition = new LogicalCondition()
                    {
                        Conditions = failedConditions
                    };
                }

                checkResultAggregated.FailInfo.AddRange(checkResult_obj.FailInfo);
            }

            var aggregatedRes = PopulateSpecificationResult(spatialSpec, filterResultAggregated, checkResultAggregated);

            return(aggregatedRes);
        }