Application: AutoStamp
Revit Platform: All
Revit Version: 2011.0
First Released For: 2010.0
Programming Language: C#
Skill Level: Medium
Category: Basics
Type: ExternalApplication

Subject: Auto stamp when print view.
Summary: 
This sample demonstrates how to subscribe ViewPrint events, create and delete TextNote element in event handler and cancel events when creation failure occurred.

Classes: 
Autodesk.Revit.UI.IExternalApplication 
Autodesk.Revit.DB.TextNote
Autodesk.Revit.DB.Geometry.XYZ
Autodesk.Revit.DB.Events.ViewPrintingEventArgs
Autodesk.Revit.DB.Events.ViewPrintedEventArgs

Project Files: 
Application.cs
This file contains a class Application which implements IExternalApplcation interface. The OnStartUp method will register ViewPrinting, ViewPrinted events when Revit starts.

EventsReactor.cs
This file contains a class EventsReactor, it consists of two handler methods which will be subscribed to ViewPrinting and ViewPrinted events separately; it also contains other methods which are used to dump related information to log files. The pre-event handler will create TextNote element before print start and implement the auto stamp function; the post-event handler will delete the element created in pre-event handler after view print has finished.

Description: 
The sample implements IExternalApplication interface and subscribes ViewPrinting, ViewPrinted, events in OnStartUp method; the registered event handlers will create and delete TextNote element and dump print related information to log files when events are raised.
-	Event delegates for ViewPrinting and ViewPrinted events of ControlledApplication level will be subscribed to react to the print actions.
-	When ViewPrinting is raised, EventAgrs property View will be used as view method parameter for TextNote.Create method; the handler will also reserve the Id value of newly-created TextNote for deleting it later.
-	If the TextNote creation failed for some reason, this sample will cancel the print process for this view by setting the Cancel property of ViewPrinting EventAgrs to true.
-	When ViewPrinted is raised, EventArgs property Document can be used to delete element via method Document.Delete(ElementId).
-	Method Autodesk.Revit.DB.TextNote.Create (…) will be used to create a TextNote element.
-	Printer name can be retrieved from EventAgrs property Setting by method PrintSetting.PrinterName; User name can be retrieved via System.Environment.UserName.

Instructions: 
1.	Copy the provided .addin file under sample folder to install folder of your Revit and specify full paths for dll. 
2.	Start Revit, notice ViewPrinting and ViewPrinted events will be registered automatically.
3.	Raise print events via UI: Run print command File\Print to print views; each printed view will be auto stamped with printer name and user name (if AutoStamp.dll was built under Debug mode) at origin position of view; “ABC…XYZ” will be printed to view if you build AutoStamp.dll under Release mode. 

Note: 
The sample was designed to work when printing is invoked via Revit's UI. It may not work as expected if printing is invoked from inside an API call, since API commands may impose editing limitations on the current document.