Revit二次开发之获取GeometryObject

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI.Selection;


namespace MyDimention
{
    [Autodesk.Revit.Attributes.Regeneration(RegenerationOption.Manual)]
    [Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]
    public class Class1 : IExternalCommand
    {
        /*
        public bool IsGrid(Element el)
        {
            Grid g = el as Grid;
            if(g!=null)
            {
                return true;
            }
            return false;
        }
       */
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            Selection selection = uidoc.Selection;

            //Reference reff = selection.PickObject(ObjectType.Element, "请选择一个轴线!");
            //Element ell = doc.GetElement(reff);
       
            #endregion

            var refer = selection.PickObject(ObjectType.Element, new DoorFilter(), "请选择一扇门");
            var elem = doc.GetElement(refer);
            var geoObjects = elem.get_Geometry(GetGeometryOption());

            //获取门上的所有solid
            var solids = geoObjects.OfType();
            foreach(var solid in solids)
            {
                if(solid.Volume!=0)
                {
                    TaskDialog.Show("VOlume", solid.Volume.ToString());
                }
            }


            return Result.Succeeded;

        }
        //创建几何选项
        public static Options GetGeometryOption()
        {
            Options option = new Options();
            option.ComputeReferences = true;
            option.DetailLevel = ViewDetailLevel.Fine;
            return option;
        }
       
    }
    public class DoorFilter : ISelectionFilter
    {
        public bool AllowElement(Element elem)
        {
            if (elem.Category.Id.IntegerValue == (int)(BuiltInCategory.OST_Doors))
            {
                return true;
            }
            else return false;

        }

        public bool AllowReference(Reference reference, XYZ position)
        {
            //throw new NotImplementedException();
        }
    }
}

参考文章:https://github.com/HeZhongHao/DotNet.Revit/blob/master/DotNet.Revit/DotNet.Revit.GeometryObject/CmdGeometryObject.cs

你可能感兴趣的:(Revit二次开发,Revit二次开发,C#)