public class AIDApplication : IExtensionApplication
{
public ObjectIdCollection objectsForFilter;
public void Initialize()
{
docManager.DocumentCreated += AddDocumentHandlers;
docManager.DocumentToBeDestroyed += RemoveDocumentHandlers;
// add document handlers to all existing documents (shouldn't happen if we load from bundle)
foreach (var doc in docManager.Cast<Document>())
{
doc.CommandWillStart += doc_CommandWillStart;
doc.CommandEnded += doc_CommandEnded;
}
}
/// <summary>
/// When objects are added to the database, check them and see if they need to be handled
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="ObjectEventArgs"/> instance containing the event data.</param>
private void callback_ObjectAppended(object sender, ObjectEventArgs e)
{
if (objectsForFilter!=null)
{
objectsForFilter.Add(e.DBObject.ObjectId);
}
}
/// <summary>
/// Add event handlers to the command events and the objectAppended
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="DocumentCollectionEventArgs"/> instance containing the event data.</param>
private void AddDocumentHandlers(object sender, DocumentCollectionEventArgs e)
{
if (Active.Document == null) return;
Active.Document.CommandWillStart += doc_CommandWillStart;
Active.Document.CommandEnded += doc_CommandEnded;
}
/// <summary>
/// Removes the document handlers.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="Autodesk.AutoCAD.ApplicationServices.DocumentCollectionEventArgs" /> instance containing the event data.</param>
private void RemoveDocumentHandlers(object sender, DocumentCollectionEventArgs e)
{
if (Active.Document == null) return;
Active.Document.CommandWillStart -= doc_CommandWillStart;
Active.Document.CommandEnded -= doc_CommandEnded;
}
/// <summary>
/// Handles the CommandWillStart event of the document.
/// Checks to see if copy command is starting
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="CommandEventArgs"/> instance containing the event data.</param>
private void doc_CommandWillStart(object sender, CommandEventArgs e)
{
Debug.WriteLine(string.Format("Command {0} will start.", e.GlobalCommandName));
if (e.GlobalCommandName.ToUpper()=="COPY" || e.GlobalCommandName.ToUpper()=="PASTECLIP")
{
objectsForFilter
= new ObjectIdCollection
(); Active.Database.ObjectAppended += callback_ObjectAppended;
}
}
/// <summary>
/// Command Ended
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="CommandEventArgs"/> instance containing the event data.</param>
void doc_CommandEnded(object sender, CommandEventArgs e)
{
Active.Database.ObjectAppended -= callback_ObjectAppended;
if (objectsForFilter == null) return;
// if we have objects to check for do cool stuff
foreach (ObjectId id in objectsForFilter)
{
// Do cool stuff
}
objectsForFilter.Clear();
Debug.WriteLine(string.Format("Command {0} Ended.", e.GlobalCommandName));
}
}