码迷,mamicode.com
首页 > 其他好文 > 详细

删除选中的对象-Revit二次开发2

时间:2016-08-19 14:35:25      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

对选中的对象进行删除

技术分享
using System;
using System.Windows.Forms;
using System.Collections;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace Revit.SDK.Samples.DeleteObject.CS
{
    /// <summary>
    /// 删除被选中的对象
    /// </summary>
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Automatic)]
    public class Command : Autodesk.Revit.UI.IExternalCommand
    {
    
        public Autodesk.Revit.UI.Result Execute(
            Autodesk.Revit.UI.ExternalCommandData commandData,
            ref string message, Autodesk.Revit.DB.ElementSet elements)
        {
            Autodesk.Revit.UI.UIApplication revit = commandData.Application;

            ElementSet collection = new ElementSet();
            //获取当前选中对象的集合
            foreach (ElementId elementId in revit.ActiveUIDocument.Selection.GetElementIds())
            {
               collection.Insert(revit.ActiveUIDocument.Document.GetElement(elementId));
            }
            // 判断选中对象的尺寸
            if (collection.Size < 1)
            {
                message = "至少要选择一个被删除的对象";
                return Autodesk.Revit.UI.Result.Cancelled;
            }

            bool error = true;
            try
            {
                error = true;

                // 逐个进行删除
                IEnumerator e = collection.GetEnumerator();
                bool MoreValue = e.MoveNext();
                while (MoreValue)
                {
                    Element component = e.Current as Element;
                    revit.ActiveUIDocument.Document.Delete(component.Id);
                    MoreValue = e.MoveNext();
                }

                error = false;
            }
            catch
            {
                // 将不可删除的对象添加到elements中
                foreach (Element c in collection)
                {
                    elements.Insert(c);
                }
                message = "对象不能被删除";
                return Autodesk.Revit.UI.Result.Failed;
            }
            finally
            {
                // 提示失败
                if (error)
                {
                    TaskDialog.Show("Error", "Delete failed.");
                }
            }

            return Autodesk.Revit.UI.Result.Succeeded;
        }
    }
}
View Code

 

删除选中的对象-Revit二次开发2

标签:

原文地址:http://www.cnblogs.com/minhost/p/5787217.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!