• Revit二次开发示例:DeleteObject


    在本例中,通过命令可以删除选中的元素。

    需要注意的是要在代码中加入Transaction,否则的话会出现Modifying  is forbidden because the document has no open transaction的错误。

     

    #region Namespaces
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using Autodesk.Revit.ApplicationServices;
    using Autodesk.Revit.Attributes;
    using Autodesk.Revit.DB;
    using Autodesk.Revit.UI;
    using Autodesk.Revit.UI.Selection;
    #endregion
    
    namespace DeleteObject
    {
        [Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]
        [Autodesk.Revit.Attributes.Regeneration(RegenerationOption.Manual)]
        [Autodesk.Revit.Attributes.Journaling(JournalingMode.NoCommandData)]
        public class Command : IExternalCommand
        {
            public Result Execute(
              ExternalCommandData commandData,
              ref string message,
              ElementSet elements)
            {
                UIApplication revit = commandData.Application;
    
                ElementSet collection = revit.ActiveUIDocument.Selection.Elements;
    
                if (collection.Size < 1)
                {
                    message = "Plase select object(s) before delete.";
                    return Result.Cancelled;
                }
    
                bool error = true;
    
                try
                {
                    error = true;
    
                    IEnumerator e = collection.GetEnumerator();
    
                    Transaction transactoin = new Transaction(commandData.Application.ActiveUIDocument.Document, "DeleteObject");
    
                    transactoin.Start();
    
                    bool MoreValue = e.MoveNext();
                    while (MoreValue)
                    {
                        Element component = e.Current as Element;
                        revit.ActiveUIDocument.Document.Delete(component.Id);
                        MoreValue = e.MoveNext();
                    }
    
                    transactoin.Commit();
                    error = false;
                }
                catch (Exception)
                {
                    foreach (Element c in collection)
                    {
                        elements.Insert(c);
                    }
                    message = "object(s) can't be deleted.";
                    return Result.Failed;
                }
                finally
                {
                    if (error)
                    {
                        TaskDialog.Show("Error", "Delete failed.");
                    }
                }
    
                return Result.Succeeded;
            }
        }
    }
  • 相关阅读:
    创建型设计模式之-单例
    设计模式(1、创造型2、结构型、3行为型)
    手写IOC容器和两种注入(构造方法注入和属性注入)
    从依赖倒置原则到IOC控制反转
    自定义HttpHandler可以做什么
    一个用户在浏览器上输入网址怎么走到我们写的.net程序中的,请求到管道处理
    代理Nginx
    .Net Expression表达式目录树(自己动态创建表达式目录树)
    canvas绘制圆环进度条
    城市二级联动
  • 原文地址:https://www.cnblogs.com/xpvincent/p/3605574.html
Copyright © 2020-2023  润新知