Exemple #1
0
        public static global::Revit.Elements.Element AreaLocation(global::Revit.Elements.Element element)
        {
            //the current document
            Autodesk.Revit.DB.Document doc = DocumentManager.Instance.CurrentDBDocument;
            //the internal element
            Autodesk.Revit.DB.Element internalElement = element.InternalElement;
            //list to append area locations to
            List <global::Revit.Elements.Element> areaLocations = new List <global::Revit.Elements.Element>();

            //ptlocation to append to
            Autodesk.DesignScript.Geometry.Point ptLocation = null;
            //figure out the location of the given element
            if (internalElement.Location is LocationPoint pt)
            {
                ptLocation = pt.Point.ToPoint(true);
            }
            else if (internalElement.Location is Autodesk.Revit.DB.LocationCurve)
            {
                Autodesk.Revit.DB.LocationCurve curve = internalElement.Location as Autodesk.Revit.DB.LocationCurve;
                ptLocation = curve.Curve.Evaluate(0.5, true).ToPoint(true);
            }
            else
            {
                throw new Exception(@"This element's location is not supported for this operation. ¯\_(ツ)_/¯ ");
            }

            var allViews = new Autodesk.Revit.DB.FilteredElementCollector(doc)
                           .OfClass(typeof(Autodesk.Revit.DB.View))
                           .Cast <Autodesk.Revit.DB.View>()
                           .Where(x => x.ViewType == ViewType.AreaPlan && !x.IsTemplate)
                           .ToList();


            //collect the areas to do some cool stuff
            FilteredElementCollector          areaColl = new FilteredElementCollector(doc);
            IList <Autodesk.Revit.DB.Element> areas    = areaColl.OfCategory(BuiltInCategory.OST_Areas).ToElements();

            foreach (Autodesk.Revit.DB.Element area in areas)
            {
                View        view = allViews.First(v => v.GenLevel.Id.IntegerValue == area.LevelId.IntegerValue);
                BoundingBox bBox = area.get_BoundingBox(view).ToProtoType();
                if (bBox.Contains(ptLocation) && area.LevelId.IntegerValue == internalElement.LevelId.IntegerValue)
                {
                    areaLocations.Add(area.ToDSType(true));
                }
            }

            global::Revit.Elements.Element elem = areaLocations.First(a => a.GetLocation().DistanceTo(ptLocation) == areaLocations.Min(e => e.GetLocation().DistanceTo(ptLocation)));

            return(elem);
        }