Beispiel #1
0
        public static bool IsValidElement(DB.Element element)
        {
            if (element is DB.ElementType)
            {
                return(false);
            }

            if (element is DB.View)
            {
                return(false);
            }

            using (var location = element?.Location)
            {
                if (location is object)
                {
                    return(true);
                }
            }

            using (var bbox = element?.get_BoundingBox(null))
            {
                return(bbox is object);
            }
        }
        /// <summary>
        /// construct function.
        /// </summary>
        /// <param name="door">of which geometry data is wanted.</param>
        public DoorGeometry(Autodesk.Revit.DB.Element door)
        {
            m_options      = new Options();
            m_options.View = GetPlanform2DView(door);
            m_options.ComputeReferences = false;
            Autodesk.Revit.DB.GeometryElement geoEle = door.get_Geometry(m_options);
            AddGeometryElement(geoEle);

            m_bbox = door.get_BoundingBox(m_options.View);
        }
Beispiel #3
0
        /// <summary>
        /// create 3D and 2D data of given GeometryElement
        /// </summary>
        /// <param name="elem"></param>
        /// <param name="detail"></param>
        /// <param name="currentView"></param>
        public GeometryData(Element elem, DetailLevels detail, View currentView)
        {
            Options opt = Command.CommandData.Application.Application.Create.NewGeometryOptions();
            opt.DetailLevel = detail;
            opt.ComputeReferences = false;
            GeometryElement geoElement = elem.get_Geometry(opt);

            Autodesk.Revit.DB.XYZ xyz = new Autodesk.Revit.DB.XYZ (0, 0, 0);
            Transform transform = Transform.get_Translation(xyz);
            AddGeoElement(geoElement, transform);

            m_bbox = elem.get_BoundingBox(currentView);
        }
Beispiel #4
0
        /// <summary>
        /// create 3D and 2D data of given GeometryElement
        /// </summary>
        /// <param name="elem">of which geometry data be gotten</param>
        /// <param name="currentView">current view of Revit</param>
        public GeometryData(Element elem, View currentView)
        {
            Options opt = Command.CommandData.Application.Application.Create.NewGeometryOptions();

            opt.View = currentView;
            opt.ComputeReferences = false;
            GeometryElement geoElement = elem.get_Geometry(opt);

            Autodesk.Revit.DB.XYZ xyz       = new Autodesk.Revit.DB.XYZ(0, 0, 0);
            Transform             transform = Transform.CreateTranslation(xyz);

            AddGeoElement(geoElement, transform);

            m_bbox = elem.get_BoundingBox(currentView);
        }
Beispiel #5
0
        /// <summary>
        /// Generate appropriate graphics data object from exact analytical model type.
        /// </summary>
        /// <param name="element">The selected element maybe has analytical model lines</param>
        /// <returns>A graphics data object appropriate for GDI.</returns>
        public ModelData(Element element)
        {
            try
            {
                AnalyticalModel analyticalMode = GetAnalyticalModel(element);
                View            currentView    = Command.CommandData.Application.ActiveUIDocument.Document.ActiveView;
                m_bbox = element.get_BoundingBox(currentView);

                if (null == analyticalMode)
                {
                    return;
                }

                GetModelData(analyticalMode);
            }
            catch (Exception)
            {
            }
        }
        /// <summary>
        /// Extract the geometry of the given Element.
        /// </summary>
        /// <param name="elem">Element parameter</param>
        /// <returns>Element's geometry</returns>
        protected ElementGeometry ExtractGeom(Autodesk.Revit.DB.Element elem)
        {
            Solid   result  = null;
            Options options = new Options();

            options.ComputeReferences = true;
            Autodesk.Revit.DB.GeometryElement gElement = elem.get_Geometry(options);
            foreach (GeometryObject gObj in gElement.Objects)
            {
                result = gObj as Solid;
                if (result != null && result.Faces.Size > 0)
                {
                    break;
                }
            }
            BoundingBoxXYZ box = elem.get_BoundingBox(null);

            return(new ElementGeometry(result, box));
        }
Beispiel #7
0
        /// <summary>
        /// Generate appropriate graphics data object from exact analytical model type.
        /// </summary>
        /// <param name="element">The selected element maybe has analytical model lines</param>
        /// <returns>A graphics data object appropriate for GDI.</returns>
        public ModelData(Element element)
        {
            try
            {
                AnalyticalModel analyticalMode = GetAnalyticalModel(element);
                View currentView = Command.CommandData.Application.ActiveUIDocument.Document.ActiveView;
                m_bbox = element.get_BoundingBox(currentView);

                if (null == analyticalMode)
                {
                    return;
                }

                GetModelData(analyticalMode);

            }
            catch (Exception)
            {
            }
        }
Beispiel #8
0
        /// <summary>
        /// Find Elements Intersect By Ray
        /// </summary>
        /// <param name="element"></param>
        /// <param name="categories"></param>
        /// <param name="direction"></param>
        /// <returns></returns>
        public static Autodesk.Revit.DB.XYZ RayIntersect(this Autodesk.Revit.DB.Element element, List <string> categories, XYZ direction)
        {
            // Find a 3D view to use for the ReferenceIntersector constructor

            FilteredElementCollector collector     = new FilteredElementCollector(element.Document);
            Func <View3D, bool>      isNotTemplate = v3 => !(v3.IsTemplate);
            View3D view3D = collector.OfClass(typeof(View3D)).Cast <View3D>().First <View3D>(isNotTemplate);

            // Use the center of the skylight bounding box as the start point.
            BoundingBoxXYZ box    = element.get_BoundingBox(view3D);
            XYZ            center = box.Min.Add(box.Max).Multiply(0.5);
            // Project in the negative Z direction down to the ceiling.
            ElementFilter        elementFilter  = element.Document.FiltersElementByCategory(categories);
            ReferenceIntersector refIntersector = new ReferenceIntersector(elementFilter, FindReferenceTarget.Face, view3D);

            refIntersector.FindReferencesInRevitLinks = true;
            ReferenceWithContext referenceWithContext = refIntersector.FindNearest(center, direction);
            Reference            reference            = referenceWithContext?.GetReference();

            return(reference?.GlobalPoint);
        }
Beispiel #9
0
        /// <summary>
        /// Extract the geometry of the given Element.
        /// </summary>
        /// <param name="elem">Element parameter</param>
        /// <returns>Element's geometry</returns>
        protected ElementGeometry ExtractGeom(Autodesk.Revit.DB.Element elem)
        {
            Solid   result  = null;
            Options options = new Options();

            options.ComputeReferences = true;
            Autodesk.Revit.DB.GeometryElement gElement = elem.get_Geometry(options);
            //foreach (GeometryObject gObj in gElement.Objects)
            IEnumerator <GeometryObject> Objects = gElement.GetEnumerator();

            while (Objects.MoveNext())
            {
                GeometryObject gObj = Objects.Current;

                result = gObj as Solid;
                if (result != null && result.Faces.Size > 0)
                {
                    break;
                }
            }
            BoundingBoxXYZ box = elem.get_BoundingBox(null);

            return(new ElementGeometry(result, box));
        }
        protected override void TrySolveInstance(IGH_DataAccess DA)
        {
            DB.Element element = null;
            if (!DA.GetData("Element", ref element) || element is null)
            {
                return;
            }

            // Special cases
            if (element is DB.FamilyInstance familyInstace)
            {
                DA.SetData("Host", Types.HostObject.FromElement(familyInstace.Host));
                return;
            }
            else if (element is DB.Opening opening)
            {
                DA.SetData("Host", Types.HostObject.FromElement(opening.Host));
                return;
            }
            else if (element.get_Parameter(DB.BuiltInParameter.HOST_ID_PARAM) is DB.Parameter hostId)
            {
                DA.SetData("Host", Types.HostObject.FromElementId(element.Document, hostId.AsElementId()));
                return;
            }

            // Search geometrically
            if (element.get_BoundingBox(null) is DB.BoundingBoxXYZ bbox)
            {
                using (var collector = new DB.FilteredElementCollector(element.Document))
                {
                    var elementCollector = collector.OfClass(typeof(DB.HostObject));

                    if (element.Category.Parent is DB.Category hostCategory)
                    {
                        elementCollector = elementCollector.OfCategoryId(hostCategory.Id);
                    }

                    var bboxFilter = new DB.BoundingBoxIntersectsFilter(new DB.Outline(bbox.Min, bbox.Max));
                    elementCollector = elementCollector.WherePasses(bboxFilter);

                    var classFilter = default(DB.ElementFilter);
                    if (element is DB.FamilyInstance instance)
                    {
                        classFilter = new DB.FamilyInstanceFilter(element.Document, instance.GetTypeId());
                    }
                    else if (element is DB.Area)
                    {
                        classFilter = new DB.AreaFilter();
                    }
                    else if (element is DB.AreaTag)
                    {
                        classFilter = new DB.AreaTagFilter();
                    }
                    else if (element is DB.Architecture.Room)
                    {
                        classFilter = new DB.Architecture.RoomFilter();
                    }
                    else if (element is DB.Architecture.RoomTag)
                    {
                        classFilter = new DB.Architecture.RoomTagFilter();
                    }
                    else if (element is DB.Mechanical.Space)
                    {
                        classFilter = new DB.Mechanical.SpaceFilter();
                    }
                    else if (element is DB.Mechanical.SpaceTag)
                    {
                        classFilter = new DB.Mechanical.SpaceTagFilter();
                    }
                    else
                    {
                        if (element is DB.CurveElement)
                        {
                            classFilter = new DB.ElementClassFilter(typeof(DB.CurveElement));
                        }
                        else
                        {
                            classFilter = new DB.ElementClassFilter(element.GetType());
                        }
                    }

                    foreach (var host in elementCollector.Cast <DB.HostObject>())
                    {
                        if (host.Id == element.Id)
                        {
                            continue;
                        }

                        if (host.FindInserts(false, true, true, false).Contains(element.Id))
                        {
                            DA.SetData("Host", Types.HostObject.FromElement(host));
                            break;
                        }
                        // Necessary to found Panel walls in a Curtain Wall
                        else if (host.GetDependentElements(classFilter).Contains(element.Id))
                        {
                            DA.SetData("Host", Types.HostObject.FromElement(host));
                            break;
                        }
                    }
                }
            }
        }
        public static void BuildPreview
        (
            DB.Element element, MeshingParameters meshingParameters, DB.ViewDetailLevel DetailLevel,
            out Rhino.Display.DisplayMaterial[] materials, out Mesh[] meshes, out Curve[] wires
        )
        {
            DB.Options options = null;
            using (var geometry = element?.GetGeometry(DetailLevel == DB.ViewDetailLevel.Undefined ? DB.ViewDetailLevel.Medium : DetailLevel, out options)) using (options)
                {
                    if (geometry is null)
                    {
                        materials = null;
                        meshes    = null;
                        wires     = null;
                    }
                    else
                    {
                        var categoryMaterial = element.Category?.Material.ToDisplayMaterial(null);
                        var elementMaterial  = geometry.MaterialElement.ToDisplayMaterial(categoryMaterial);

                        meshes    = geometry.GetPreviewMeshes(meshingParameters).Where(x => x is object).ToArray();
                        wires     = geometry.GetPreviewWires().Where(x => x is object).ToArray();
                        materials = geometry.GetPreviewMaterials(element.Document, elementMaterial).Where(x => x is object).ToArray();

                        if (meshes.Length == 0 && wires.Length == 0 && element.get_BoundingBox(null) is DB.BoundingBoxXYZ)
                        {
                            var subMeshes    = new List <Mesh>();
                            var subWires     = new List <Curve>();
                            var subMaterials = new List <Rhino.Display.DisplayMaterial>();

                            foreach (var dependent in element.GetDependentElements(null).Select(x => element.Document.GetElement(x)))
                            {
                                if (dependent.get_BoundingBox(null) is null)
                                {
                                    continue;
                                }

                                DB.Options dependentOptions = null;
                                using (var dependentGeometry = dependent?.GetGeometry(DetailLevel == DB.ViewDetailLevel.Undefined ? DB.ViewDetailLevel.Medium : DetailLevel, out dependentOptions)) using (dependentOptions)
                                    {
                                        if (dependentGeometry is object)
                                        {
                                            subMeshes.AddRange(dependentGeometry.GetPreviewMeshes(meshingParameters).Where(x => x is object));
                                            subWires.AddRange(dependentGeometry.GetPreviewWires().Where(x => x is object));
                                            subMaterials.AddRange(dependentGeometry.GetPreviewMaterials(element.Document, elementMaterial).Where(x => x is object));
                                        }
                                    }
                            }

                            meshes    = subMeshes.ToArray();
                            wires     = subWires.ToArray();
                            materials = subMaterials.ToArray();
                        }

                        foreach (var mesh in meshes)
                        {
                            mesh.Normals.ComputeNormals();
                        }
                    }
                }
        }