[Revit二次开发] Selection交互API-创建柱子并计算体积

摘要:

  本文主要是对Selection交互API的应用:

1.选择一个点,在该点创建一个柱子;
2.选择刚创建的柱子,计算其体积;
3.框选若干元素,得到选中的墙的数量。

项目完整代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Structure;// StructuralType.NonStructural
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection; //Selection
using Autodesk.Revit.Attributes;


namespace Sele
{
    [TransactionAttribute(TransactionMode.Manual)]
    [RegenerationAttribute(RegenerationOption.Manual)]
    public class Class1 : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;

            //选择一个点
            Selection s1 = uidoc.Selection;//using Autodesk.Revit.UI.Selection;
            XYZ point = null;

            try
            {
                point = s1.PickPoint("请选择一个点"); //将在Revit的左下角提示
            }
            catch
            {
                return Result.Succeeded;
            }

            //由于需要修改点,所以需要创建一个事务Transaction
            Transaction t1 = new Transaction(doc,"t1");
            t1.Start();
            FamilySymbol familySymbol = doc.GetElement(new ElementId(338378)) as FamilySymbol;//创建一个柱子,其Id为338378
            //判断族类型
            if (!familySymbol.IsActive)
            {
                familySymbol.Activate();
            }
            Level level = null;//声明标高
            FamilyInstance fi = doc.Create.NewFamilyInstance(point, familySymbol, level, StructuralType.NonStructural);//using Autodesk.Revit.DB.Structure;

            Selection s2 = uidoc.Selection;
            Reference re = s2.PickObject(ObjectType.Edge, "请选择一个物体");
            Element ele = doc.GetElement(re);
            Options opt = new Options();
            GeometryElement gelem = ele.get_Geometry(opt);
            double v = 0.0;//声明体积
            v = GetSolid(gelem).Sum(m => m.Volume) * 0.3048 * 0.3048 * 0.3048;
            TaskDialog.Show("Hint", "选中的物体体积为:" + v.ToString("f3"));

            //过滤元素"墙"
            IList<Element> pickedElements = s2.PickElementsByRectangle(new WallSelectionFilter(), "请框选目标物体");
            double num = pickedElements.Count();
            TaskDialog.Show("Hint", "已选中墙的数量为:" + num);

            return Result.Succeeded;
        }

        private List<Solid> GetSolid(GeometryElement gelem)
        {
            List<Solid> solids = new List<Solid>();
            foreach(GeometryObject obj in gelem)//遍历数组gelem中每一个GeometryObject类型的对象
            {
                if(obj is Solid)//元素是3d实体
                {
                    solids.Add(obj as Solid);
                }
                if(obj is GeometryElement)//GeometryElement:元素的几何表示。
                {
                    solids.AddRange(GetSolid(obj as GeometryElement));
                }
                if(obj is GeometryInstance)
                {
                    GeometryInstance gins = obj as GeometryInstance;
                    GeometryElement gelm = gins.GetInstanceGeometry();
                    solids.AddRange(GetSolid(gelem));
                }
            }
            return solids;
        }     
    }
    //自定义过滤器
    public class WallSelectionFilter:ISelectionFilter
    {
        public bool AllowElement(Element elem)
        {
            return elem is Wall;
        }

        public bool AllowReference(Autodesk.Revit.DB.Reference reference,Autodesk.Revit.DB.XYZ position)
        {
            return true;
        }
    }
}

效果:

参考文献:

 周婧祎《Autodesk Revit 2016二次开发入门教程》

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值