Ejemplo n.º 1
0
        /// <summary>
        /// Get Solids from Element
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        private static IList <Autodesk.Revit.DB.Solid> GetSolidsFromElement(Autodesk.Revit.DB.Element element)
        {
            GeometryElement geo = element.get_Geometry(new Options()
            {
                ComputeReferences = true
            });

            List <Autodesk.Revit.DB.Solid> solids = new List <Autodesk.Revit.DB.Solid>();

            foreach (object obj in geo)
            {
                GeometryInstance geoinstance = obj as GeometryInstance;
                if (geoinstance != null)
                {
                    foreach (object solidobj in geoinstance.GetInstanceGeometry())
                    {
                        if (solidobj.GetType() == typeof(Autodesk.Revit.DB.Solid))
                        {
                            Autodesk.Revit.DB.Solid solid = solidobj as Autodesk.Revit.DB.Solid;
                            solids.Add(solid);
                        }
                    }
                }
            }

            return(solids);
        }
Ejemplo n.º 2
0
        public Mesh GetElementMesh(DB.Element element, List <DB.Element> subElements = null)
        {
            Mesh mesh = new Mesh();

            var allSolids = GetElementSolids(element, opt: new Options()
            {
                DetailLevel = ViewDetailLevel.Fine, ComputeReferences = true
            });

            if (!allSolids.Any()) //it's a mesh!
            {
                var geom = element.get_Geometry(new Options());
                mesh = GetMesh(geom);
            }
            else
            {
                if (subElements != null)
                {
                    foreach (var sb in subElements)
                    {
                        allSolids.AddRange(GetElementSolids(sb));
                    }
                }

                (mesh.faces, mesh.vertices) = GetFaceVertexArrFromSolids(allSolids);
            }

            mesh.units = ModelUnits;
            return(mesh);
        }
Ejemplo n.º 3
0
        const double PRECISION = 0.00001;    //precision when judge whether two doubles are equal

        /// <summary>
        /// get all faces that compose the geometry solid of given element
        /// </summary>
        /// <param name="elem">element to be calculated</param>
        /// <returns>all faces</returns>
        public static FaceArray GetFaces(Element elem)
        {
            List <Face> faces = new List <Face>();

            Autodesk.Revit.DB.Options geoOptions = Command.CommandData.Application.Application.Create.NewGeometryOptions();
            geoOptions.ComputeReferences = true;

            GeoElement geoElem = elem.get_Geometry(geoOptions);
            //GeometryObjectArray geoElems = geoElem.Objects;
            IEnumerator <GeometryObject> Objects = geoElem.GetEnumerator();

            //foreach (object o in geoElems)
            while (Objects.MoveNext())
            {
                object o = Objects.Current;

                GeoSolid geoSolid = o as GeoSolid;
                if (null == geoSolid)
                {
                    continue;
                }

                return(geoSolid.Faces);
            }

            return(null);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Get edges of element's profile
        /// </summary>
        /// <param name="elem">Selected element</param>
        public List <List <Edge> > GetFaces(Autodesk.Revit.DB.Element elem)
        {
            List <List <Edge> > faceEdges = new List <List <Edge> >();
            Options             options   = m_appCreator.NewGeometryOptions();

            options.DetailLevel       = DetailLevels.Medium;
            options.ComputeReferences = true;
            Autodesk.Revit.DB.GeometryElement geoElem = elem.get_Geometry(options);

            GeometryObjectArray gObjects = geoElem.Objects;

            foreach (GeometryObject geo in gObjects)
            {
                Solid solid = geo as Solid;
                if (solid != null)
                {
                    EdgeArray edges = solid.Edges;
                    FaceArray faces = solid.Faces;
                    foreach (Face face in faces)
                    {
                        EdgeArrayArray edgeArrarr = face.EdgeLoops;
                        foreach (EdgeArray edgeArr in edgeArrarr)
                        {
                            List <Edge> edgesList = new List <Edge>();
                            foreach (Edge edge in edgeArr)
                            {
                                edgesList.Add(edge);
                            }
                            faceEdges.Add(edgesList);
                        }
                    }
                }
            }
            return(faceEdges);
        }
Ejemplo n.º 5
0
        public static Autodesk.Revit.DB.GeometryElement GetGeometry(this Autodesk.Revit.DB.Element element, ViewDetailLevel viewDetailLevel, out Options options)
        {
            options = new Options {
                ComputeReferences = true, DetailLevel = viewDetailLevel
            };
            var geometry = element.get_Geometry(options);

            if (!(geometry?.Any() ?? false) && element is GenericForm form && !form.Combinations.IsEmpty)
            {
                geometry.Dispose();

                options.IncludeNonVisibleObjects = true;
                return(element.get_Geometry(options));
            }

            return(geometry);
        }
Ejemplo n.º 6
0
        public static new bool IsValidElement(DB.Element element)
        {
            if (!GraphicalElement.IsValidElement(element))
            {
                return(false);
            }

            using (var options = new DB.Options())
                return(!(element.get_Geometry(options) is null));
        }
Ejemplo n.º 7
0
        /// <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);
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Gets all the solids from an element (digs into them too!). see: https://forums.autodesk.com/t5/revit-api-forum/getting-beam-column-and-wall-geometry/td-p/8138893
        /// </summary>
        /// <param name="elem"></param>
        /// <param name="opt"></param>
        /// <param name="useOriginGeom4FamilyInstance"></param>
        /// <returns></returns>
        public List <Solid> GetElementSolids(DB.Element elem, Options opt = null, bool useOriginGeom4FamilyInstance = false)
        {
            if (null == elem)
            {
                return(null);
            }
            if (null == opt)
            {
                opt = new Options();
            }

            List <Solid>    solids = new List <Solid>();
            GeometryElement gElem  = null;

            try
            {
                if (useOriginGeom4FamilyInstance && elem is Autodesk.Revit.DB.FamilyInstance)
                {
                    // we transform the geometry to instance coordinate to reflect actual geometry
                    Autodesk.Revit.DB.FamilyInstance fInst = elem as Autodesk.Revit.DB.FamilyInstance;
                    gElem = fInst.GetOriginalGeometry(opt);
                    Transform trf = fInst.GetTransform();
                    if (!trf.IsIdentity)
                    {
                        gElem = gElem.GetTransformed(trf);
                    }
                }
                else
                {
                    gElem = elem.get_Geometry(opt);
                }

                if (null == gElem)
                {
                    return(null);
                }
                IEnumerator <GeometryObject> gIter = gElem.GetEnumerator();
                gIter.Reset();
                while (gIter.MoveNext())
                {
                    solids.AddRange(GetSolids(gIter.Current));
                }
            }
            catch (Exception ex)
            {
                // In Revit, sometime get the geometry will failed.
                string error = ex.Message;
            }
            return(solids);
        }
Ejemplo n.º 10
0
      /// <summary>
      /// Judge whether an element has face geometry
      /// </summary>
      /// <param name="elem">the element to be checked</param>
      /// <returns>true is having face geometry, false is having no face geometry</returns>
      private bool CheckSelectedElement(RevitElement elem)
      {
         if (null == elem)
         {
            return false;
         }
         Autodesk.Revit.DB.Options opts = new Autodesk.Revit.DB.Options();
         opts.View = m_revitDoc.Document.ActiveView;
         opts.ComputeReferences = true;
         // Get geometry of the element
         GeoElement geoElement = elem.get_Geometry(opts);
         InquireGeometry(geoElement, elem);

         return true;
      }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            Autodesk.Revit.DB.Element element = null;
            if (!DA.GetData(ObjectType.Name, ref element))
            {
                return;
            }

            var options = new Options {
                ComputeReferences = true
            };

            using (var geometry = element.get_Geometry(options))
            {
                var list = geometry.ToRhino().Where(x => x != null).ToList();
                DA.SetDataList("Geometry", list);
            }
        }
Ejemplo n.º 13
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)
            {
                result = gObj as Solid;
                if (result != null && result.Faces.Size > 0)
                {
                    break;
                }
            }
            BoundingBoxXYZ box = elem.get_BoundingBox(null);

            return(new ElementGeometry(result, box));
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Obtain a sparse point collection outlining a Revit element bt traversing it's
        /// GeometryObject representation
        /// </summary>
        /// <param name="e"></param>
        /// <param name="pts"></param>
        protected static void GetPointCloud(Autodesk.Revit.DB.Element e, List <XYZ> pts)
        {
            var options = new Options()
            {
                ComputeReferences        = true,
                DetailLevel              = ViewDetailLevel.Coarse,
                IncludeNonVisibleObjects = false
            };

            foreach (var gObj in e.get_Geometry(options))
            {
                if (gObj is Autodesk.Revit.DB.Solid)
                {
                    GetPointCloud(gObj as Autodesk.Revit.DB.Solid, pts);
                }
                else if (gObj is GeometryInstance)
                {
                    GetPointCloud(gObj as GeometryInstance, pts);
                }
            }
        }
        protected void PaintElementSolids(DB.Element element, IList <DB.ElementId> paintIds)
        {
            // If there are solids we may need to paint them
            if (paintIds is object)
            {
                var doc = element.Document;

                // Regenerate is necessary here, else 'element' may still have no geometry.
                doc.Regenerate();

                using (var elementGeometry = element.get_Geometry(new DB.Options()
                {
                    DetailLevel = DB.ViewDetailLevel.Undefined
                }))
                {
                    int index = 0;
                    foreach (var geo in elementGeometry)
                    {
                        if (geo is DB.Solid solid)
                        {
                            var materialId = index < paintIds.Count ? paintIds[index] : DB.ElementId.InvalidElementId;
                            foreach (var face in solid.Faces.Cast <DB.Face>())
                            {
                                if (materialId.IsValid())
                                {
                                    doc.Paint(element.Id, face, materialId);
                                }
                                else
                                {
                                    doc.RemovePaint(element.Id, face);
                                }
                            }
                        }

                        index++;
                    }
                }
            }
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Get edges of element's profile
        /// </summary>
        /// <param name="elem">selected element</param>
        /// <returns>all the faces in the selected Element</returns>
        public virtual List <List <Edge> > GetFaces(Autodesk.Revit.DB.Element elem)
        {
            List <List <Edge> > faceEdges = new List <List <Edge> >();
            Options             options   = m_appCreator.NewGeometryOptions();

            options.DetailLevel = ViewDetailLevel.Medium;
            //make sure references to geometric objects are computed.
            options.ComputeReferences = true;
            Autodesk.Revit.DB.GeometryElement geoElem = elem.get_Geometry(options);
            //GeometryObjectArray gObjects = geoElem.Objects;
            IEnumerator <GeometryObject> Objects = geoElem.GetEnumerator();

            //get all the edges in the Geometry object
            //foreach (GeometryObject geo in gObjects)
            while (Objects.MoveNext())
            {
                GeometryObject geo = Objects.Current;

                Solid solid = geo as Solid;
                if (solid != null)
                {
                    FaceArray faces = solid.Faces;
                    foreach (Face face in faces)
                    {
                        EdgeArrayArray edgeArrarr = face.EdgeLoops;
                        foreach (EdgeArray edgeArr in edgeArrarr)
                        {
                            List <Edge> edgesList = new List <Edge>();
                            foreach (Edge edge in edgeArr)
                            {
                                edgesList.Add(edge);
                            }
                            faceEdges.Add(edgesList);
                        }
                    }
                }
            }
            return(faceEdges);
        }
Ejemplo n.º 17
0
        const double PRECISION = 0.00001;    //precision when judge whether two doubles are equal

        /// <summary>
        /// get all faces that compose the geometry solid of given element
        /// </summary>
        /// <param name="elem">element to be calculated</param>
        /// <returns>all faces</returns>
        public static FaceArray GetFaces(Element elem)
        {
            List <Face> faces = new List <Face>();

            Autodesk.Revit.DB.Options geoOptions = Command.CommandData.Application.Application.Create.NewGeometryOptions();
            geoOptions.ComputeReferences = true;

            GeoElement          geoElem  = elem.get_Geometry(geoOptions);
            GeometryObjectArray geoElems = geoElem.Objects;

            foreach (object o in geoElems)
            {
                GeoSolid geoSolid = o as GeoSolid;
                if (null == geoSolid)
                {
                    continue;
                }

                return(geoSolid.Faces);
            }

            return(null);
        }
Ejemplo n.º 18
0
        const double PRECISION = 0.00001; //precision when judge whether two doubles are equal

        #endregion Fields

        #region Methods

        /// <summary>
        /// get all faces that compose the geometry solid of given element
        /// </summary>
        /// <param name="elem">element to be calculated</param>
        /// <returns>all faces</returns>
        public static FaceArray GetFaces(Element elem)
        {
            List<Face> faces = new List<Face>();

            Autodesk.Revit.DB.Options geoOptions = Command.CommandData.Application.Application.Create.NewGeometryOptions();
            geoOptions.ComputeReferences = true;

            GeoElement geoElem = elem.get_Geometry(geoOptions);
            GeometryObjectArray geoElems = geoElem.Objects;

            foreach (object o in geoElems)
            {
                GeoSolid geoSolid = o as GeoSolid;
                if (null == geoSolid)
                {
                    continue;
                }

                return geoSolid.Faces;
            }

            return null;
        }
Ejemplo n.º 19
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));
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Get edges of element's profile
        /// </summary>
        /// <param name="elem">selected element</param>
        /// <returns>all the faces in the selected Element</returns>
        public override List <List <Edge> > GetFaces(Autodesk.Revit.DB.Element elem)
        {
            List <List <Edge> > faceEdges = new List <List <Edge> >();
            Options             options   = m_appCreator.NewGeometryOptions();

            options.DetailLevel = ViewDetailLevel.Medium;
            //make sure references to geometric objects are computed.
            options.ComputeReferences = true;
            Autodesk.Revit.DB.GeometryElement geoElem = elem.get_Geometry(options);
            //GeometryObjectArray gObjects = geoElem.Objects;
            IEnumerator <GeometryObject> Objects = geoElem.GetEnumerator();

            //get all the edges in the Geometry object
            //foreach (GeometryObject geo in gObjects)
            while (Objects.MoveNext())
            {
                GeometryObject geo = Objects.Current;

                //if beam doesn't contain opening on it, then we can get edges from instance
                //and the points we get should be transformed by instance.Tranceform
                if (geo is Autodesk.Revit.DB.GeometryInstance)
                {
                    Autodesk.Revit.DB.GeometryInstance instance = geo as Autodesk.Revit.DB.GeometryInstance;
                    m_beamTransform = instance.Transform;
                    Autodesk.Revit.DB.GeometryElement elemGeo = instance.SymbolGeometry;
                    //GeometryObjectArray objectsGeo = elemGeo.Objects;
                    IEnumerator <GeometryObject> Objects1 = elemGeo.GetEnumerator();
                    //foreach (GeometryObject objGeo in objectsGeo)
                    while (Objects1.MoveNext())
                    {
                        GeometryObject objGeo = Objects1.Current;

                        Solid solid = objGeo as Solid;
                        if (null != solid)
                        {
                            FaceArray faces = solid.Faces;
                            foreach (Face face in faces)
                            {
                                EdgeArrayArray edgeArrarr = face.EdgeLoops;
                                foreach (EdgeArray edgeArr in edgeArrarr)
                                {
                                    List <Edge> edgesList = new List <Edge>();
                                    foreach (Edge edge in edgeArr)
                                    {
                                        edgesList.Add(edge);
                                    }
                                    faceEdges.Add(edgesList);
                                }
                            }
                        }
                    }
                }
                //if beam contains opening on it, then we can get edges from solid
                //and the points we get do not need transform anymore
                else if (geo is Autodesk.Revit.DB.Solid)
                {
                    m_haveOpening = true;
                    Solid     solid = geo as Solid;
                    FaceArray faces = solid.Faces;
                    foreach (Face face in faces)
                    {
                        EdgeArrayArray edgeArrarr = face.EdgeLoops;
                        foreach (EdgeArray edgeArr in edgeArrarr)
                        {
                            List <Edge> edgesList = new List <Edge>();
                            foreach (Edge edge in edgeArr)
                            {
                                edgesList.Add(edge);
                            }
                            faceEdges.Add(edgesList);
                        }
                    }
                }
            }
            return(faceEdges);
        }
Ejemplo n.º 21
0
 public static new bool IsValidElement(DB.Element element)
 {
     using (var options = new DB.Options())
         return(!(element.get_Geometry(options) is null));
 }
Ejemplo n.º 22
0
        /// <summary>
        /// Judge whether an element has face geometry
        /// </summary>
        /// <param name="elem">the element to be checked</param>
        /// <returns>true is having face geometry, false is having no face geometry</returns>
        private bool CheckSelectedElement(RevitElement elem)
        {
            if(null == elem)
            {
                return false;
            }
            Autodesk.Revit.DB.Options opts = new Autodesk.Revit.DB.Options();
            opts.View = m_revitDoc.Document.ActiveView;
            opts.ComputeReferences = true;
            // Get geometry of the element
            GeoElement geoElement = elem.get_Geometry(opts);
            InquireGeometry(geoElement, elem);

            return true;
        }