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

需要注意的是要在代码中加入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;
}
}
}

Revit二次开发示例:DeleteObject的更多相关文章

  1. Revit二次开发示例:HelloRevit

    本示例实现Revit和Revit打开的文件的相关信息. #region Namespaces using System; using System.Collections.Generic; using ...

  2. Revit二次开发示例:EventsMonitor

    在该示例中,插件在Revit启动时弹出事件监控选择界面,供用户设置,也可在添加的Ribbon界面完成设置.当Revit进行相应操作时,弹出窗体会记录事件时间和名称. #region Namespace ...

  3. Revit二次开发示例:ErrorHandling

    本示例介绍了Revit的错误处理.   #region Namespaces using System; using System.Collections.Generic; using Autodes ...

  4. Revit二次开发示例:ChangesMonitor

    在本示例中,程序监控Revit打开文件事件,并在创建的窗体中更新文件信息.   #region Namespaces using System; using System.Collections.Ge ...

  5. Revit二次开发示例:AutoStamp

    该示例中,在Revit启动时添加打印事件,在打印时向模型添加水印,打印完成后删除该水印.   #region Namespaces using System; using System.Collect ...

  6. Revit二次开发示例:ModelessForm_ExternalEvent

    使用Idling事件处理插件任务. #region Namespaces using System; using System.Collections.Generic; using Autodesk. ...

  7. Revit二次开发示例:Journaling

    关于Revit Journal读写的例子.   #region Namespaces using System; using System.Collections.Generic; using Sys ...

  8. Revit二次开发示例:DisableCommand

    Revit API 不支持调用Revit内部命令,但可以用RevitCommandId重写它们(包含任意选项卡,菜单和右键命令).使用RevitCommandId.LookupCommandId()可 ...

  9. Revit二次开发示例:DesignOptions

    本例只要演示Revit的类过滤器的用法,在对话框中显示DesignOption元素. #region Namespaces using System; using System.Collections ...

随机推荐

  1. 【leetcode 简单】 第五十七题 删除链表中的节点

    删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2->3->4 ...

  2. jquery ajax complete 方法

    jquery ajax var ajaxTimeoutTest = $.ajax({ url:'',  //请求的URL timeout : 1000, //超时时间设置,单位毫秒 type : 'g ...

  3. $("节点名").html("字符串")和$("节点名").text("字符串")区别

    1. 经过html方法: $(".js_info").html("~!`@#$%^& ";'<>\=/-!·#¥%…&*()—+|` ...

  4. CSS 实现单边阴影

    box-shadow: 0px -15px 10px -15px #111; 五个值分别为:x y blur spread color 将 spread 设置成 blur 的负值即可 这种只适用于 o ...

  5. 编译安装 zbar 时两次 make 带来的惊喜

    为了装 php 的条形码扩展模块 php-zbarcode,先装了一天的 ImageMagick 和 zbar.也许和我装的 Ubuntu 17.10 的有版本兼容问题吧,总之什么毛病都有,apt 不 ...

  6. Ubuntu server 搭建Git server【转】

    转自:http://www.cnblogs.com/candle806/p/4064610.html Ubuntu server 搭建Git server,git相比svn,最主要就是分布式了,每个客 ...

  7. mysql只读模式的设置方法与实验【转】

    在MySQL数据库中,在进行数据迁移和从库只读状态设置时,都会涉及到只读状态和Master-slave的设置和关系. 经过实际测试,对于MySQL单实例数据库和master库,如果需要设置为只读状态, ...

  8. Failed to load class "org.slf4j.impl.StaticLoggerBinder"

    调试程序出现如下错误: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".SLF4J: Default ...

  9. Python_oldboy_自动化运维之路(二)

    本节内容: 1.pycharm工具的使用 2.进制运算 3.表达式if ...else语句 4.表达式for 循环 5.break and continue 6.表达式while 循环 1.pycha ...

  10. 洛谷P1177快速排序

    传送门 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> ...