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

需要注意的是要在代码中加入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. C#中2个日期类型相减

    DateTime startTime = Convert.ToDateTime("2017-1-9");DateTime endTime = Convert.ToDateTime( ...

  2. 29、HashSet简介

    Set的特点 Set里面存储的元素不能重复,没有索引,存取顺序不一致. package com.monkey1024.set; import java.util.HashSet; /** * Set的 ...

  3. C# 安装部署Windows服务脚本

    @echo off Installutil.exe 程序目录 F:\test\TestWindows.exe 服务程序目录 @sc start "服务名称" @sc config ...

  4. 【译】第十三篇 Integration Services:SSIS变量

    本篇文章是Integration Services系列的第十三篇,详细内容请参考原文. 简介在前一篇我们结合了之前所学的冒泡.日志记录.父子模式创建一个自定义的SSIS包日志记录模式.在这一篇,我们将 ...

  5. JS设计模式——12.装饰者模式

    装饰者模式概述 本章讨论的是一种为对象添加特性的技术,她并不使用创建新子类这种手段. 装饰者模式可以用来透明的把对象包装在具有同样接口的另一个对象中.这样一来,就可以给一个方法添加一些行为,然后将方法 ...

  6. Solr管理索引库——(十三)

    a)          维护索引 1.  添加/更新文档 添加或更新单个文档

  7. Chapter 4 深入理解Caffe MNIST DEMO中的LeNet网络模型

    明代思想家王阳明提出了"知行合一",谓认识事物的道理与在现实中运用此道理,是密不可分的一回事.我以为这样的中国哲学话语,对于学习者来说,极具启发意义,要细细体会.中华文明源远流长, ...

  8. 做了这么久的 DBA,你真的认识 MySQL 数据安全体系?【转】

    给大家分享下有关MySQL在数据安全的话题,怎么通过一些配置来保证数据安全以及保证数据的存储落地是安全的. 我是在2014年加入陌陌,2015年加入去哪儿网,做MySQL的运维,包括自动化的开发. 接 ...

  9. 利用github pages五分钟建好个人网站+个人博客

    笔者自己在建个人网站/个人博客的时候其实遇到了不少麻烦,但是都一一解决了,这里教给大家最简单的方式. 首先你需要一个GitHub账号,访问https://github.com创建新账号即可. 然后访问 ...

  10. 忘记SVN密码怎么办

    1:下载TSvnPwd.exe 2:使用wireshark抓包.例如: PROPFIND /svn/dev2/!svn/vcc/default HTTP/1.1Host: 192.168.156.1: ...