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<Solid>();
            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

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Revit 二次开发获取族实例几何信息的方法可以分为两种:使用 Revit API 提供的方法和使用 Geometry API 提供的方法。 首先,使用 Revit API 提供的方法可以通过以下代码获取族实例的几何信息: ```csharp // 获取当前文档 Document doc = uidoc.Document; // 获取的族实例 ElementId elementId = uidoc.Selection.GetElementIds().FirstOrDefault(); FamilyInstance familyInstance = doc.GetElement(elementId) as FamilyInstance; // 获取族实例的几何信息 GeometryElement geometryElement = familyInstance.get_Geometry(new Options()); foreach (GeometryObject geometryObject in geometryElement) { if (geometryObject is Solid solid) { // 获取实体的顶点信息 foreach (Face face in solid.Faces) { Mesh mesh = face.Triangulate(); foreach (XYZ vertex in mesh.Vertices) { // 获取顶点坐标 double x = vertex.X; double y = vertex.Y; double z = vertex.Z; } } } } ``` 其次,使用 Geometry API 提供的方法可以通过以下代码获取族实例的几何信息: ```csharp // 获取当前文档 Document doc = uidoc.Document; // 获取的族实例 ElementId elementId = uidoc.Selection.GetElementIds().FirstOrDefault(); FamilyInstance familyInstance = doc.GetElement(elementId) as FamilyInstance; // 获取族实例的几何信息 GeometryElement geometryElement = familyInstance.Symbol.Geometry; foreach (GeometryObject geometryObject in geometryElement) { if (geometryObject is Solid solid) { // 获取实体的顶点信息 foreach (Face face in solid.Faces) { Mesh mesh = face.Triangulate(); foreach (XYZ vertex in mesh.Vertices) { // 获取顶点坐标 double x = vertex.X; double y = vertex.Y; double z = vertex.Z; } } } } ``` 无论是哪种方法,都可以获取到族实例的几何信息。使用 Revit API 提供的方法可以更方便地获取当前文档和选的族实例,而使用 Geometry API 提供的方法则可以直接从族类型获取几何信息,避免了需要实例化后再获取几何信息的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值