Quantcast
Channel: All Revit API Forum posts
Viewing all articles
Browse latest Browse all 66671

Re: Create "solid" and find all elements within

$
0
0

Don't panic this is pretty simple to get Faces from a Solid of a geometry, try the following code:

 

#region Namespaces

using System;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;

using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;

using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;

using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;

using Autodesk.Revit.Exceptions;
using Autodesk.Revit.Utility;

using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;

#endregion

namespace RevitAddinCS1
{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class GeometryManager : IExternalCommand
    {
        #region Cached Variables

        private static ExternalCommandData _cachedCmdData;

        public static UIApplication CachedUiApp
        {
            get
            {
                return _cachedCmdData.Application;
            }
        }

        public static RvtApplication CachedApp
        {
            get
            {
                return CachedUiApp.Application;
            }
        }

        public static RvtDocument CachedDoc
        {
            get
            {
                return CachedUiApp.ActiveUIDocument.Document;
            }
        }

        #endregion

        #region IExternalCommand Members

        public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elemSet)
        {
            _cachedCmdData = cmdData;

            try
            {
                Reference reference = CachedUiApp.ActiveUIDocument.Selection.PickObject(ObjectType.Element);

                Element element = CachedDoc.GetElement(reference);

                Options geoOptions = new Options();

                // Get geometry element of the selected element
                GeometryElement geoElement = element.get_Geometry(geoOptions);

                // Get geometry object
                foreach (GeometryObject geoObject in geoElement)
                {
                    // Get the geometry instance which contains the geometry information
                    Autodesk.Revit.DB.GeometryInstance instance = geoObject as Autodesk.Revit.DB.GeometryInstance;

                    if (null != instance)
                    {
                        foreach (GeometryObject instObj in instance.SymbolGeometry)
                        {
                            Solid solid = instObj as Solid;

                            if (null == solid || 0 == solid.Faces.Size || 0 == solid.Edges.Size)
                            {
                                continue;
                            }

                            if (solid.Faces.Size > 0)
                            {
                                // Get the faces and edges from solid, and transform the formed points
                                foreach (Face face in solid.Faces)
                                {
                                    TaskDialog.Show("Revit", face.Area.ToString());
                                }
                            }
                        }
                    }
                }


                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                msg = ex.ToString();
                return Result.Failed;
            }
        }

        #endregion
    }
}

Just select the element and the code will show you how to retrieve the solid and its faces showing up a dialog with each face's area. You can of course replace the PickObject function with a filter or you can create a function where you can pass the element from which the faces will be extracted as a parameter.

 

Don't forget to mark this reply as an answer if it satisfies your need.


Viewing all articles
Browse latest Browse all 66671

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>