Revit 二次开发 交互及UIAPI

时间:2020-07-11
本文章向大家介绍Revit 二次开发 交互及UIAPI,主要包括Revit 二次开发 交互及UIAPI使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

学习地址:https://www.bilibili.com/video/BV1mf4y1S72o?p=12

本章内容

  • Selection交互API
  • TaskDialog对话框
  • Ribbon菜单

Selection交互API

实例练习一

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using System.Windows.Forms;
using Autodesk.Revit.UI.Selection;

namespace RevitDevTV
{
    /// <summary>
    /// Selection 交互API,实例练习
    /// </summary>
    [TransactionAttribute(TransactionMode.Manual)]
    [RegenerationAttribute(RegenerationOption.Manual)]
    public class Sele : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            Selection s1 = uidoc.Selection;
            XYZ point = null;
            try
            {
                point = s1.PickPoint("请选择一个点");
            }
            catch (Exception ex)
            {
                return Result.Succeeded;
            }
            Transaction trans = new Transaction(doc,"t1");
            trans.Start();
            //创建一个柱子
            FamilySymbol familySymbol = doc.GetElement(new ElementId(691519)) as FamilySymbol;
            if (!familySymbol.IsActive)
            {
                familySymbol.Activate();
            }
            //创建标高
            Level level = null;
            //创建柱子
            FamilyInstance fi = doc.Create.NewFamilyInstance(point,familySymbol,level,Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
            trans.Commit();
            //===============计算体积
            Selection s2 = uidoc.Selection;
            Reference re = s2.PickObject(ObjectType.Element,"请选择一个物体");
            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;
            TaskDialog.Show("Hint","选中的物体体积为:"+v.ToString("f3"));
            IList<Element> pickedElement = s2.PickElementsByRectangle(new WallSelectionfilte(),"请框选目标物体");
            double num = pickedElement.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)
            {
                if (obj is Solid)
                {
                    solids.Add(obj as Solid);
                }
                if (obj is 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 WallSelectionfilte : ISelectionFilter
    {
        public bool AllowElement(Element elem)
        {
            return elem is Wall;
        }

        public bool AllowReference(Reference reference, XYZ position)
        {
            return true;
        }
    }
}

原文地址:https://www.cnblogs.com/chenyanbin/p/13285563.html