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;
}
}
}
Revit二次开发示例:DeleteObject的更多相关文章
- Revit二次开发示例:HelloRevit
本示例实现Revit和Revit打开的文件的相关信息. #region Namespaces using System; using System.Collections.Generic; using ...
- Revit二次开发示例:EventsMonitor
在该示例中,插件在Revit启动时弹出事件监控选择界面,供用户设置,也可在添加的Ribbon界面完成设置.当Revit进行相应操作时,弹出窗体会记录事件时间和名称. #region Namespace ...
- Revit二次开发示例:ErrorHandling
本示例介绍了Revit的错误处理. #region Namespaces using System; using System.Collections.Generic; using Autodes ...
- Revit二次开发示例:ChangesMonitor
在本示例中,程序监控Revit打开文件事件,并在创建的窗体中更新文件信息. #region Namespaces using System; using System.Collections.Ge ...
- Revit二次开发示例:AutoStamp
该示例中,在Revit启动时添加打印事件,在打印时向模型添加水印,打印完成后删除该水印. #region Namespaces using System; using System.Collect ...
- Revit二次开发示例:ModelessForm_ExternalEvent
使用Idling事件处理插件任务. #region Namespaces using System; using System.Collections.Generic; using Autodesk. ...
- Revit二次开发示例:Journaling
关于Revit Journal读写的例子. #region Namespaces using System; using System.Collections.Generic; using Sys ...
- Revit二次开发示例:DisableCommand
Revit API 不支持调用Revit内部命令,但可以用RevitCommandId重写它们(包含任意选项卡,菜单和右键命令).使用RevitCommandId.LookupCommandId()可 ...
- Revit二次开发示例:DesignOptions
本例只要演示Revit的类过滤器的用法,在对话框中显示DesignOption元素. #region Namespaces using System; using System.Collections ...
随机推荐
- HDU 1171 Big Event in HDU(01背包)
题目链接 题意:给出n个物品的价值v,每个物品有m个,设总价值为sum,求a,b.a+b=sum,且a尽可能接近b,a>=b. 题解:01背包. #include <bits/stdc++ ...
- POJ 1986 Distance Queries (Tarjan算法求最近公共祖先)
题目链接 Description Farmer John's cows refused to run in his marathon since he chose a path much too lo ...
- Hibernate5笔记2--单表的增删改查操作
单表的增删改查操作: (1)定义获取Session和SessionFactory的工具类: package com.tongji.utils; import org.hibernate.Session ...
- xmlHttpRequest 跨域和上传或下载进度条
跨域 XMLHttpRequest 请求 普通网页能够使用XMLHttpRequest对象发送或者接受服务器数据, 但是它们受限于同源策略. 扩展可以不受该限制. 任何扩展只要它先获取了跨域请求许可, ...
- 全网最全JS正则表达式 校验数字
Js代码 <script type="text/javascript"> function SubmitCk() { var reg = /^([a-zA-Z0-9]+ ...
- accept系统调用
/* * For accept, we attempt to create a new socket, set up the link * with the client, wake up the c ...
- MySQL 5.7.17 Group Relication(组复制)搭建手册【转】
本博文介绍了Group Replication的两种工作模式的架构.并详细介绍了Single-Master Mode的部署过程,以及如何切换到Multi-Master Mode.当然,文末给出了Gro ...
- cvpr densnet论文
- JS点击事件的重叠处理(多个点击事件出现冲突)
最近开发遇见了一个这个样的情况,一个button在一个div中,点击buton是一个事件,点击大的div也是一个事件,但是由于button在div中,点击button会把两个事件都执行了,但是我们想点 ...
- php内存管理机制与垃圾回收机制
PHP内存管理机制 1 var_dump(memory_get_usage()); //获取内存 2 $a = "laruence"; //定义一个变量 3 var_dump(me ...