Revit二次开发示例:AutoStamp
该示例中,在Revit启动时添加打印事件,在打印时向模型添加水印,打印完成后删除该水印。
#region Namespaces
using System;
using System.Collections.Generic;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
#endregion namespace AutoStamp
{
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
[Journaling(JournalingMode.NoCommandData)]
class App : IExternalApplication
{
EventsReactor m_eventsReactor; public Result OnStartup(UIControlledApplication a)
{
m_eventsReactor = new EventsReactor();
a.ControlledApplication.ViewPrinting+=new EventHandler<Autodesk.Revit.DB.Events.ViewPrintingEventArgs>(m_eventsReactor.AppViewPrinting);
a.ControlledApplication.ViewPrinted += new EventHandler<Autodesk.Revit.DB.Events.ViewPrintedEventArgs>(m_eventsReactor.AppViewPrinted);
return Result.Succeeded;
} public Result OnShutdown(UIControlledApplication a)
{
m_eventsReactor.CloseLogFiles(); a.ControlledApplication.ViewPrinting -= new EventHandler<Autodesk.Revit.DB.Events.ViewPrintingEventArgs>(m_eventsReactor.AppViewPrinting);
a.ControlledApplication.ViewPrinted -= new EventHandler<Autodesk.Revit.DB.Events.ViewPrintedEventArgs>(m_eventsReactor.AppViewPrinted);
return Result.Succeeded;
}
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events; namespace AutoStamp
{
public sealed class EventsReactor
{
private TextWriterTraceListener m_eventLog;
string m_assemblyPath;
ElementId m_newTextNoteId; public EventsReactor()
{
m_assemblyPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
} public void CloseLogFiles()
{
Trace.Flush();
Trace.Close(); Trace.Flush();
if (null != m_eventLog)
{
Trace.Listeners.Remove(m_eventLog);
m_eventLog.Flush();
m_eventLog.Close();
}
} public void AppViewPrinting(object sender, ViewPrintingEventArgs e)
{
if (null == m_eventLog)
{
SetupLogFiles();
} Trace.WriteLine(System.Environment.NewLine + "View Print Start: -----------------------------");
DumpEventArguments(e); bool faileOccur = false;
try
{
string strText = string.Format("Printer Name: {0} {1}User Name: {2}",
e.Document.PrintManager.PrinterName, System.Environment.NewLine, System.Environment.UserName); #if !(Debug || DEBUG)
strText = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
#endif Transaction eventTransaction = new Transaction(e.Document, "External Tool");
eventTransaction.Start();
TextNote newTextNote = e.Document.Create.NewTextNote(
e.View,
new XYZ(0, 0, 0),
new XYZ(1, 0, 0),
new XYZ(0, 1, 0),
1,
TextAlignFlags.TEF_ALIGN_CENTER,
strText
);
eventTransaction.Commit(); if (null != newTextNote)
{
Trace.WriteLine("Create TextNote element successfully...");
m_newTextNoteId = new ElementId(newTextNote.Id.IntegerValue);
}
else
{
faileOccur = true;
} }
catch (Exception ex)
{
faileOccur = true;
Trace.WriteLine("Exception occured when creating TextNote, print will be cancelled, ex: " + ex.Message);
}
finally
{
if (faileOccur && e.Cancellable)
{
e.Cancel();
}
}
} public void AppViewPrinted(object sender, ViewPrintedEventArgs e)
{
Trace.WriteLine(System.Environment.NewLine + "View Print End: ------"); DumpEventArguments(e);
if (RevitAPIEventStatus.Cancelled != e.Status)
{
Transaction eventTransaction = new Transaction(e.Document, "External Tool");
eventTransaction.Start();
e.Document.Delete(m_newTextNoteId);
eventTransaction.Commit();
Trace.WriteLine("Succeeded to delete the created TextNote element.");
}
} private void SetupLogFiles()
{
if (null != m_eventLog)
{
return;
} string printEventsLogFile = Path.Combine(m_assemblyPath, "PrintEventsLog.txt");
if (File.Exists(printEventsLogFile))
{
File.Delete(printEventsLogFile);
} m_eventLog = new TextWriterTraceListener(printEventsLogFile);
Trace.Listeners.Add(m_eventLog);
Trace.AutoFlush = true;
} private static void DumpEventArguments(RevitAPIEventArgs eventArgs)
{
if (eventArgs.GetType().Equals(typeof(ViewPrintingEventArgs)))
{
Trace.WriteLine("ViewPrintingEventArgs Parameters ----->");
ViewPrintingEventArgs args = eventArgs as ViewPrintingEventArgs;
Trace.WriteLine(" TotalViews : " + args.TotalViews);
Trace.WriteLine(" View Index : " + args.Index);
Trace.WriteLine(" View Information : ");
DumpViewInfo(args.View, " ");
}
else if (eventArgs.GetType().Equals(typeof(ViewPrintedEventArgs)))
{
Trace.WriteLine("ViewPrintedEventArgs Parameters ------>");
ViewPrintedEventArgs args = eventArgs as ViewPrintedEventArgs;
Trace.WriteLine(" Event Status : " + args.Status);
Trace.WriteLine(" TotalViews : " + args.Status);
Trace.WriteLine(" View Index : " + args.Status);
Trace.WriteLine(" View Information : ");
}
else
{
// no handling for other arguments
}
} private static void DumpViewInfo(View view, string prefix)
{
Trace.WriteLine(string.Format("{0} ViewName: {1}, ViewType: {2}", prefix, view.ViewName, view.ViewType));
} }
}
Revit二次开发示例:AutoStamp的更多相关文章
- 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二次开发示例: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 ...
- Revit二次开发示例:DeleteObject
在本例中,通过命令可以删除选中的元素. 需要注意的是要在代码中加入Transaction,否则的话会出现Modifying is forbidden because the document has ...
随机推荐
- 【leetcode 简单】 第一百五十题 两个列表的最小索引总和
假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. 你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅. 如果答案不止一个,则输出所有答 ...
- HTML字体的设置
CSS字体设置 box-sizing:border #content-box box-shadow:设置盒子边框的阴影. 字体动作: font-family:设置字体.比如:‘微软雅黑 ...
- Python练习-迭代器-模拟cat|grep文件
代码如下: # 编辑者:闫龙 def grep(FindWhat): f=open("a.txt","r",encoding="utf8") ...
- C++ Primer 5th 第18章 用于大型程序的工具
C++大规模程序设计至少存在三个特殊要求: 错误处理 库的引入 复杂建模 以上三种对应C++语言的三种特性:异常处理.命名空间.多重继承. 异常处理 异常处理机制是一种允许偷懒的工具,在出现非正确的情 ...
- 触发器Demo
--mysql 触发器简单实例 --创建表1 )) ; --创建表2 )); --创建触发器,表一增加数据时,表二自动增加数据 create trigger t_afterinsert_on_tab1 ...
- scala可变长度参数(转)
可变长度参数 Scala 允许你指明函数的最后一个参数可以是重复的.这可以允许客户向函数传入可变长度参数列表.想要标注一个重复参数,在参数的类型之后放一个星号.例如: scala> def ec ...
- React 学习二 组件
React的一个最大的特点就是组件化的开发模式.今天就来试一下: <!DOCTYPE html> <html> <head> <meta charset=&q ...
- luogu P3393 逃离僵尸岛
luoguP3393逃离_僵尸岛_ 一道洛谷不知道哪门子月赛的题 可以用此题来练习最短路算法 SPFA和dijkstra的练习题(关于Floyed,他死了 思路: 本题是最短路板子. 首先就是建立虚点 ...
- 修饰符(动态String数组篇)--- 常用 解除疑问。
1.无修饰符----是直接传基本类型的地址过来,并没有把基本类型的指针复制一份入栈,所以一旦修改就是修改原来的值. 2.const 修饰符 与 无修饰符一致. 3.var修饰符 与 上一致. 4.ou ...
- CentOS6.9 安装OpenResty
1.安装依赖包 yum install -y gcc gcc-c++ readline-devel pcre-devel openssl-devel tcl perl 2.安装OpenResty 首先 ...