该示例中,在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的更多相关文章

  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二次开发示例:ModelessForm_ExternalEvent

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

  6. Revit二次开发示例:Journaling

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

  7. Revit二次开发示例:DisableCommand

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

  8. Revit二次开发示例:DesignOptions

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

  9. Revit二次开发示例:DeleteObject

    在本例中,通过命令可以删除选中的元素. 需要注意的是要在代码中加入Transaction,否则的话会出现Modifying  is forbidden because the document has ...

随机推荐

  1. Centos6.5下搭建nagios详解

    一.LAMP环境部署 1.安装php 1.安装yum源 rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-releas ...

  2. 对接微信支付使用HMAC-SHA256使用签名算法实现方式

    最近做微信押金支付对接,很多坑,心累!这里提醒一下各位: 首先,确保自己商户号进了白名单,没有需要联系客服,否则接口是调不通的,会一直提示参数错误 其次,确保接口文档是最新的,最好去官网去看,否则可能 ...

  3. 【比赛游记】THUSC2018酱油记

    day -1 早上4:30就要起来去飞机场…… 7点的飞机,10:30就到北京了. 北京的街景并没有我想像的漂亮……大概是因为我在四环外〒▽〒 晚上还有CF div3场,果断的去水了,因为太累就没有打 ...

  4. 2016 最佳 Linux 发行版排行榜【转】

    转自:http://www.linuxstory.org/the-best-linux-distros-of-2016/?utm_source=tuicool&utm_medium=refer ...

  5. REX系统了解1

    REX是高通开发出来的一个操作系统,起初它是为了在Inter 80186处理器上应用而开发的,到后来才转变成应用在ARM这种微处理器上.他历经了很多版本,代码也越来越多,功能也越来越完善.REX只用不 ...

  6. maven profile 优先级

    maven profile是有优先级别 也就是说在setting.xml的profile优先级比pom中同名的profile高. 可以使用 mvn help:active-profiles 这个命令是 ...

  7. 如何查看页面是否开启了gzip压缩

    1.谷歌浏览器 F12 2.在表头单击鼠标右键 3.如果开启了gzip则显示gzip,没有则是空

  8. (三)Jsoup 使用选择器语法查找 DOM 元素

    第一节: Jsoup 使用选择器语法查找 DOM 元素 Jsoup使用选择器语法查找DOM元素 我们前面通过标签名,Id,Class样式等来搜索DOM,这些是不能满足实际开发需求的, 很多时候我们需要 ...

  9. 深度学习国外课程资料(Deep Learning for Self-Driving Cars)+(Deep Reinforcement Learning and Control )

    MIT(Deep Learning for Self-Driving Cars) CMU(Deep Reinforcement Learning and Control ) 参考网址: 1 Deep ...

  10. 20165203 2017-2018-2 《Java程序设计》课程总结

    20165203 2017-2018-2 <Java程序设计>课程总结 一.每周作业及实验报告链接汇总 我期望的师生关系(预备作业一):浅谈一下对师生关系的看法和对自己未来学习和生活的期望 ...