Ejemplo n.º 1
0
        /***************************************************/
        /****             Protected Methods             ****/
        /***************************************************/

        protected override IEnumerable <IBHoMObject> Read(IRequest request, ActionConfig actionConfig = null)
        {
            ICollection <ElementId> selected = this.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>());
            }

            Discipline discipline = requestDiscipline.Value;

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

            Dictionary <Document, IRequest> requestsByLinks = request.SplitRequestTreeByLinks(this.Document);

            if (requestsByLinks == null)
            {
                BH.Engine.Reflection.Compute.RecordError($"Pull failed due to issues with the request containing {nameof(FilterByLink)}. Please try to restructure the used Request and try again.");
                return(new List <IBHoMObject>());
            }

            RevitSettings settings = RevitSettings.DefaultIfNull();

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

            foreach (KeyValuePair <Document, IRequest> requestByLink in requestsByLinks)
            {
                result.AddRange(Read(requestByLink.Key, requestByLink.Value, pullConfig, discipline, settings));
            }

            bool?[] activePulls = new bool?[] { pullConfig.GeometryConfig?.PullEdges, pullConfig.GeometryConfig?.PullSurfaces, pullConfig.GeometryConfig?.PullMeshes, pullConfig.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.");
            }

            this.UIDocument.Selection.SetElementIds(selected);

            return(result);
        }
Ejemplo n.º 2
0
        /***************************************************/
        /****      Revit side of Revit_Adapter Pull     ****/
        /***************************************************/

        public override IEnumerable <object> Pull(IRequest request, PullType pullType = PullType.AdapterDefault, ActionConfig actionConfig = null)
        {
            // Check the document
            UIDocument uiDocument = this.UIDocument;
            Document   document   = this.Document;

            if (document == null)
            {
                BH.Engine.Reflection.Compute.RecordError("BHoM objects could not be removed because Revit Document is null (possibly there is no open documents in Revit).");
                return(new List <object>());
            }

            // Set config
            RevitPullConfig pullConfig = actionConfig as RevitPullConfig;

            if (pullConfig == null)
            {
                BH.Engine.Reflection.Compute.RecordNote("Revit Pull Config has not been specified. Default Revit Pull Config is used.");
                pullConfig = new RevitPullConfig();
            }

            // Read the objects based on the request
            return(Read(request, pullConfig));
        }
Ejemplo n.º 3
0
        /***************************************************/
        /****      BHoM side of Revit_Adapter Pull      ****/
        /***************************************************/

        public override IEnumerable <object> Pull(IRequest request, PullType pullType = PullType.AdapterDefault, ActionConfig actionConfig = null)
        {
            //Check if request is not null or empty
            if (request == null)
            {
                BH.Engine.Reflection.Compute.RecordError("BHoM objects could not be read because provided IRequest is null or empty.");
                return(new List <object>());
            }
            else if (request is FilterRequest)
            {
                FilterRequest filterRequest = (FilterRequest)request;
                if (filterRequest.Type == null && String.IsNullOrWhiteSpace(filterRequest.Tag) && (filterRequest.Equalities == null || filterRequest.Equalities.Count == 0))
                {
                    BH.Engine.Reflection.Compute.RecordError("BHoM objects could not be read because provided IRequest is null or empty.");
                    return(new List <object>());
                }
            }

            //Initialize Revit config
            RevitPullConfig pullConfig = actionConfig as RevitPullConfig;

            //If internal adapter is loaded call it directly
            if (InternalAdapter != null)
            {
                InternalAdapter.RevitSettings = RevitSettings;
                return(InternalAdapter.Pull(request, pullType, pullConfig));
            }

            if (!this.IsValid())
            {
                BH.Engine.Reflection.Compute.RecordError("Revit Adapter is not valid. Please check if it has been set up correctly and activated.");
                return(new List <object>());
            }

            //Reset the wait event
            m_WaitEvent.Reset();

            if (!CheckConnection())
            {
                return(new List <object>());
            }

            //Send data through the socket link
            m_LinkIn.SendData(new List <object>()
            {
                PackageType.Pull, request, pullType, pullConfig, RevitSettings
            });

            //Wait until the return message has been recieved
            if (!m_WaitEvent.WaitOne(TimeSpan.FromMinutes(m_WaitTime)))
            {
                Engine.Reflection.Compute.RecordError("The connection with Revit timed out. If working on a big model, try to increase the max wait time");
            }

            //Grab the return objects from the latest package
            List <object> returnObjs = new List <object>(m_ReturnPackage);

            //Clear the return list
            m_ReturnPackage.Clear();

            //Raise returned events
            RaiseEvents();

            //Check if the return package contains error message
            if (returnObjs.Count == 1 && returnObjs[0] is string)
            {
                Engine.Reflection.Compute.RecordError(returnObjs[0] as string);
                return(new List <object>());
            }

            //Return the package
            return(returnObjs);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        /***************************************************/
        /****             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);
        }