该示例中,在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. Oracle新建数据库,并导入dmp文件

    1:安装Oracle及新建数据库 Oracle 11g安装图解 http://www.cnblogs.com/qianyaoyuan/archive/2013/05/05/3060471.html h ...

  2. Qbot回归,已感染5.4万台计算机

    Qbot回归,已感染5.4万台计算机 近日,BAESystems的安全人员发表了一篇关于Qbot网络感知蠕虫回归的调查报告,指出已经感染了5.4万台计算机. FreeBuf百科 Qbot蠕虫,也叫Qa ...

  3. verilog中wire与reg类型的区别

    每次写verilog代码时都会考虑把一个变量是设置为wire类型还是reg类型,因此把网上找到的一些关于这方面的资料整理了一下,方便以后查找. wire表示直通,即只要输入有变化,输出马上无条件地反映 ...

  4. ASP.NET 实现Base64文件流下载PDF

    因为业务需要调用接口获取的是 Base64文件流 需要提供给客户下载PDF文档 源码部分借鉴网上,具体地址忘记了. //Base64文件流 byte[] buffer = Convert.FromBa ...

  5. Trie树子节点快速获取法

    今天做了一道leetcode上关于字典树的题:https://leetcode.com/problems/word-search-ii/#/description 一开始坚持不看别人的思路,完全自己写 ...

  6. (三)HtmlUnit 实践

    第一节: htmlunit 爬取百度云资源

  7. 基于docker 搭建Elasticsearch6.2.4(centos)

    一.介绍 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为 ...

  8. Python学习笔记:出生日期转化为年龄

    在数据挖掘项目中,有时候个体的出生日期包含信息量过大,不适合作为一个有效数据进入模型算法训练,因此有必要把出生日期转化为年龄age,age是一个很好的特征工程指示变量. import pandas a ...

  9. Linux下的堆伪造漏洞利用技术(new unlink)

    感觉markdown的文件格式看起来更清晰一些就写成附加的形式了.Download 更正:这种利用方式不叫House of Mind,是我搞混了.

  10. java算法小例子

    作为一个程序员,有时候我觉得自己都不适合,因为自己数学不好,算法不好,脑子不够灵活.而对于算法,感觉就像是数学题,总觉得很难.以前上学,在班里总有几个什么都不好,但唯独数学很厉害,真气人!面对难题时, ...