Beispiel #1
0
        /***************************************************/
        /****               Public Methods              ****/
        /***************************************************/

        public static IEnumerable <IBHoMObject> Read(Document document, IRequest request, RevitPullConfig pullConfig, Discipline discipline, RevitSettings settings)
        {
            Transform linkTransform = null;

            if (document.IsLinked)
            {
                linkTransform = document.LinkTransform();
            }

            IEnumerable <ElementId> worksetPrefilter = null;

            if (!pullConfig.IncludeClosedWorksets)
            {
                worksetPrefilter = document.ElementIdsByWorksets(document.OpenWorksetIds().Union(document.SystemWorksetIds()).ToList());
            }

            List <ElementId> elementIds = request.IElementIds(document, worksetPrefilter).RemoveGridSegmentIds(document)?.ToList();

            if (elementIds == null)
            {
                return(new List <IBHoMObject>());
            }

            if (pullConfig.IncludeNestedElements)
            {
                List <ElementId> elemIds = new List <ElementId>();
                foreach (ElementId id in elementIds)
                {
                    Element element = document.GetElement(id);
                    if (element is FamilyInstance)
                    {
                        FamilyInstance          famInst       = element as FamilyInstance;
                        IEnumerable <ElementId> nestedElemIds = famInst.ElementIdsOfMemberElements();
                        elemIds.AddRange(nestedElemIds);
                    }
                }
                elementIds.AddRange(elemIds);
            }

            PullGeometryConfig geometryConfig = pullConfig.GeometryConfig;

            if (geometryConfig == null)
            {
                geometryConfig = new PullGeometryConfig();
            }

            PullRepresentationConfig representationConfig = pullConfig.RepresentationConfig;

            if (representationConfig == null)
            {
                representationConfig = new PullRepresentationConfig();
            }

            Options geometryOptions   = BH.Revit.Engine.Core.Create.Options(ViewDetailLevel.Fine, geometryConfig.IncludeNonVisible, false);
            Options meshOptions       = BH.Revit.Engine.Core.Create.Options(geometryConfig.MeshDetailLevel.ViewDetailLevel(), geometryConfig.IncludeNonVisible, false);
            Options renderMeshOptions = BH.Revit.Engine.Core.Create.Options(representationConfig.DetailLevel.ViewDetailLevel(), representationConfig.IncludeNonVisible, false);

            Dictionary <string, List <IBHoMObject> > refObjects = new Dictionary <string, List <IBHoMObject> >();

            TransformMatrix bHoMTransform = null;

            if (linkTransform?.IsIdentity == false)
            {
                bHoMTransform = linkTransform.FromRevit();
            }

            List <IBHoMObject> result = new List <IBHoMObject>();

            foreach (ElementId id in elementIds)
            {
                Element element = document.GetElement(id);
                if (element == null)
                {
                    continue;
                }

                IEnumerable <IBHoMObject> iBHoMObjects = Read(element, discipline, linkTransform, settings, refObjects);

                if (iBHoMObjects != null && iBHoMObjects.Any())
                {
                    if (pullConfig.PullMaterialTakeOff)
                    {
                        foreach (IBHoMObject iBHoMObject in iBHoMObjects)
                        {
                            RevitMaterialTakeOff takeoff = element.MaterialTakeoff(settings, refObjects);
                            if (takeoff != null)
                            {
                                iBHoMObject.Fragments.AddOrReplace(takeoff);
                            }
                        }
                    }

                    List <ICurve> edges = null;
                    if (geometryConfig.PullEdges)
                    {
                        edges = element.Curves(geometryOptions, settings, true).FromRevit();
                        if (bHoMTransform != null)
                        {
                            edges = edges.Select(x => x.ITransform(bHoMTransform)).ToList();
                        }
                    }

                    List <ISurface> surfaces = null;
                    if (geometryConfig.PullSurfaces)
                    {
                        surfaces = element.Faces(geometryOptions, settings).Select(x => x.IFromRevit()).ToList();
                        if (bHoMTransform != null)
                        {
                            surfaces = surfaces.Select(x => x.ITransform(bHoMTransform)).ToList();
                        }
                    }

                    List <oM.Geometry.Mesh> meshes = null;
                    if (geometryConfig.PullMeshes)
                    {
                        meshes = element.MeshedGeometry(meshOptions, settings);
                        if (bHoMTransform != null)
                        {
                            meshes = meshes.Select(x => x.Transform(bHoMTransform)).ToList();
                        }
                    }

                    if (geometryConfig.PullEdges || geometryConfig.PullSurfaces || geometryConfig.PullMeshes)
                    {
                        RevitGeometry geometry = new RevitGeometry(edges, surfaces, meshes);
                        foreach (IBHoMObject iBHoMObject in iBHoMObjects)
                        {
                            iBHoMObject.Fragments.AddOrReplace(geometry);
                        }
                    }

                    if (representationConfig.PullRenderMesh)
                    {
                        List <RenderMesh> renderMeshes = element.RenderMeshes(renderMeshOptions, settings);
                        if (bHoMTransform != null)
                        {
                            renderMeshes = renderMeshes.Select(x => x.Transform(bHoMTransform)).ToList();
                        }

                        RevitRepresentation representation = new RevitRepresentation(renderMeshes);
                        foreach (IBHoMObject iBHoMObject in iBHoMObjects)
                        {
                            iBHoMObject.Fragments.AddOrReplace(representation);
                        }
                    }

                    result.AddRange(iBHoMObjects);
                }
            }

            return(result);
        }
        /***************************************************/
        /****             Protected Methods             ****/
        /***************************************************/

        protected override IEnumerable <IBHoMObject> Read(IRequest request, ActionConfig actionConfig = null)
        {
            Autodesk.Revit.UI.UIDocument uiDocument = this.UIDocument;
            Document document = this.Document;

            ICollection <ElementId> selected = uiDocument.Selection.GetElementIds();

            if (request == null)
            {
                BH.Engine.Reflection.Compute.RecordError("BHoM objects could not be read because provided IRequest is null.");
                return(new List <IBHoMObject>());
            }

            RevitPullConfig pullConfig = actionConfig as RevitPullConfig;

            if (pullConfig == null)
            {
                BH.Engine.Reflection.Compute.RecordError("BHoM objects could not be read because provided actionConfig is not a valid RevitPullConfig.");
                return(new List <IBHoMObject>());
            }

            Discipline?requestDiscipline = request.Discipline(pullConfig.Discipline);

            if (requestDiscipline == null)
            {
                BH.Engine.Reflection.Compute.RecordError("Conflicting disciplines have been detected.");
                return(new List <IBHoMObject>());
            }

            IEnumerable <ElementId> worksetPrefilter = null;

            if (!pullConfig.IncludeClosedWorksets)
            {
                worksetPrefilter = document.ElementIdsByWorksets(document.OpenWorksetIds().Union(document.SystemWorksetIds()).ToList());
            }

            List <ElementId> elementIds = request.IElementIds(uiDocument, worksetPrefilter).RemoveGridSegmentIds(document).ToList <ElementId>();

            if (elementIds == null)
            {
                return(new List <IBHoMObject>());
            }

            Discipline discipline = requestDiscipline.Value;

            if (discipline == Discipline.Undefined)
            {
                discipline = Discipline.Physical;
            }

            RevitSettings revitSettings = RevitSettings.DefaultIfNull();

            PullGeometryConfig geometryConfig = pullConfig.GeometryConfig;

            if (geometryConfig == null)
            {
                geometryConfig = new PullGeometryConfig();
            }

            PullRepresentationConfig representationConfig = pullConfig.RepresentationConfig;

            if (representationConfig == null)
            {
                representationConfig = new PullRepresentationConfig();
            }

            if (pullConfig.IncludeNestedElements)
            {
                List <ElementId> elemIds = new List <ElementId>();
                foreach (ElementId id in elementIds)
                {
                    Element element = document.GetElement(id);
                    if (element is FamilyInstance)
                    {
                        FamilyInstance          famInst       = element as FamilyInstance;
                        IEnumerable <ElementId> nestedElemIds = famInst.ElementIdsOfMemberElements();
                        elemIds.AddRange(nestedElemIds);
                    }
                }
                elementIds.AddRange(elemIds);
            }

            Options geometryOptions   = BH.Revit.Engine.Core.Create.Options(ViewDetailLevel.Fine, geometryConfig.IncludeNonVisible, false);
            Options meshOptions       = BH.Revit.Engine.Core.Create.Options(geometryConfig.MeshDetailLevel.ViewDetailLevel(), geometryConfig.IncludeNonVisible, false);
            Options renderMeshOptions = BH.Revit.Engine.Core.Create.Options(representationConfig.DetailLevel.ViewDetailLevel(), representationConfig.IncludeNonVisible, false);

            List <IBHoMObject> result = new List <IBHoMObject>();
            Dictionary <string, List <IBHoMObject> > refObjects = new Dictionary <string, List <IBHoMObject> >();

            foreach (ElementId id in elementIds)
            {
                Element element = document.GetElement(id);
                if (element == null)
                {
                    continue;
                }

                IEnumerable <IBHoMObject> iBHoMObjects = Read(element, discipline, revitSettings, refObjects);
                if (iBHoMObjects != null && iBHoMObjects.Any())
                {
                    if (pullConfig.PullMaterialTakeOff)
                    {
                        foreach (IBHoMObject iBHoMObject in iBHoMObjects)
                        {
                            RevitMaterialTakeOff takeoff = element.MaterialTakeoff(revitSettings, refObjects);
                            if (takeoff != null)
                            {
                                iBHoMObject.Fragments.AddOrReplace(takeoff);
                            }
                        }
                    }

                    List <ICurve> edges = null;
                    if (geometryConfig.PullEdges)
                    {
                        edges = element.Curves(geometryOptions, revitSettings, true).FromRevit();
                    }

                    List <ISurface> surfaces = null;
                    if (geometryConfig.PullSurfaces)
                    {
                        surfaces = element.Faces(geometryOptions, revitSettings).Select(x => x.IFromRevit()).ToList();
                    }

                    List <oM.Geometry.Mesh> meshes = null;
                    if (geometryConfig.PullMeshes)
                    {
                        meshes = element.MeshedGeometry(meshOptions, revitSettings);
                    }

                    if (geometryConfig.PullEdges || geometryConfig.PullSurfaces || geometryConfig.PullMeshes)
                    {
                        RevitGeometry geometry = new RevitGeometry(edges, surfaces, meshes);
                        foreach (IBHoMObject iBHoMObject in iBHoMObjects)
                        {
                            iBHoMObject.Fragments.AddOrReplace(geometry);
                        }
                    }

                    if (representationConfig.PullRenderMesh)
                    {
                        List <RenderMesh>   renderMeshes   = element.RenderMeshes(renderMeshOptions, revitSettings);
                        RevitRepresentation representation = new RevitRepresentation(renderMeshes);
                        foreach (IBHoMObject iBHoMObject in iBHoMObjects)
                        {
                            iBHoMObject.Fragments.AddOrReplace(representation);
                        }
                    }

                    result.AddRange(iBHoMObjects);
                }
            }

            bool[] activePulls = new bool[] { geometryConfig.PullEdges, geometryConfig.PullSurfaces, geometryConfig.PullMeshes, representationConfig.PullRenderMesh };
            if (activePulls.Count(x => x == true) > 1)
            {
                BH.Engine.Reflection.Compute.RecordWarning("Pull of more than one geometry/representation type has been specified in RevitPullConfig. Please consider this can be time consuming due to the amount of conversions.");
            }

            uiDocument.Selection.SetElementIds(selected);

            return(result);
        }