Revit 二次开发 交互及UIAPI之Selection

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

本章内容

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

Selection交互API

 

Revit 二次开发 交互及UIAPI之Selection_第1张图片

 

Revit 二次开发 交互及UIAPI之Selection_第2张图片

 

Revit 二次开发 交互及UIAPI之Selection_第3张图片

 

Revit 二次开发 交互及UIAPI之Selection_第4张图片

 

Revit 二次开发 交互及UIAPI之Selection_第5张图片

 

Revit 二次开发 交互及UIAPI之Selection_第6张图片

 

Revit 二次开发 交互及UIAPI之Selection_第7张图片

 

Revit 二次开发 交互及UIAPI之Selection_第8张图片

 

实例练习一

Revit 二次开发 交互及UIAPI之Selection_第9张图片

 

 

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
{
    /// 
    /// Selection 交互API,实例练习
    /// 
    [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 pickedElement = s2.PickElementsByRectangle(new WallSelectionfilte(),"请框选目标物体");
            double num = pickedElement.Count();
            TaskDialog.Show("Hint","已选中墙数为:"+num);
            return Result.Succeeded;
        }
        private List GetSolid(GeometryElement gelem)
        {
            List solids = new List();
            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;
        }
    }
}

 

你可能感兴趣的:(Revit 二次开发 交互及UIAPI之Selection)