该示例中,在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. Linux 下子进程与父进程的关系

    我们知道,Linux下父进程可以使用fork 函数创建子进程,但是当父进程先退出后,子进程会不会也退出呢? 通过下面这个小实验,我们能够很好的看出来: /******** basic.c ****** ...

  2. CodeForces - 1009B Minimum Ternary String

    You are given a ternary string (it is a string which consists only of characters '0', '1' and '2'). ...

  3. phpcms数据结构

    v9_admin 管理员表 v9_admin_panel 快捷面板 v9_admin_role 角色表 v9_admin_role_priv 管理员权限表 v9_announce 公告表 v9_att ...

  4. 面试:----Nginx的一理解

    1.静态HTTP服务器 首先,Nginx是一个HTTP服务器,可以将服务器上的静态文件(如HTML.图片)通过HTTP协议展现给客户端. 配置: 2.反向代理服务器 什么是反向代理? 客户端本来可以直 ...

  5. Imperva正则表达式的添加以及使用

    Imperva正则表达式的添加以及使用 1.添加字典 创建策略 模拟访问产生告警

  6. Virut样本取证特征

    1.网络特征 ant.trenz.pl ilo.brenz.pl 2.文件特征 通过对文件的定位,使用PEID查看文件区段,如果条件符合增加了7个随机字符区段的文件,则判定为受感染文件. 3.受感染特 ...

  7. oracle11g 创建id自增长监听器的步骤与问题

    首先,我们通过sql/plus先建个TEST表 sql语句: CTEATE TABLE TEST( ID NUMBER, NAME VARCHAR2(20), PRIMARY KEY(ID) ); 通 ...

  8. Django-自动HTML转义

    一.自动HTML转义 从模板生成HTML时,总会有变量包含影响最终HTML的字符风险,例如,考虑这个模板的片段: Hello, {{ name }} 起初,这是一种显示用户名的无害方式,但考虑用户输入 ...

  9. js实现图片懒加载

    大型购物网站都会采用图片懒加载技术来优化网站首页打开速度,以提高用户体验,那么具体是怎么实现的呢,我们一探究竟. html结构(div包裹一层用来显示背景图片,等待图片加载完成后,显示真实图片) &l ...

  10. 微信 JS API 支付教程

    最近一个项目中用到了微信开发,之前没有做过支付相关的东西,算是拿这个来练练手,刚开始接触支付时候很懵逼,加上微信支付开发文档本来就讲得不清楚,我是彻底蒙圈了,参考了很多代码之后,算是有一点思路了. 用 ...