This appears to work now. Any suggestions appreciated.
Code:
using System;
using System.IO;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using System.Collections.Generic;
using Autodesk.Revit.DB.Events;
namespace TwentyMileWarning
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class ThisDocument : IExternalApplication
{
// Create strings
string r1 = "FALSE";
string r2 = "FALSE";
public Result OnStartup(UIControlledApplication application)
{
try
{
application.ControlledApplication.DocumentOpened
+= new EventHandler<Autodesk.Revit.DB.Events.DocumentOpenedEventArgs>(DocOpen);
application.ControlledApplication.DocumentSaving
+= new EventHandler<Autodesk.Revit.DB.Events.DocumentSavingEventArgs>(DocSave);
application.ControlledApplication.DocumentClosing
+= new EventHandler<Autodesk.Revit.DB.Events.DocumentClosingEventArgs>(DocClosing);
}
catch
{
return Result.Failed;
}
return Result.Succeeded;
}
public Result OnShutdown(UIControlledApplication application)
{
try
{
application.ControlledApplication.DocumentOpened
-= new EventHandler<Autodesk.Revit.DB.Events.DocumentOpenedEventArgs>(DocOpen);
application.ControlledApplication.DocumentSaving
-= new EventHandler<Autodesk.Revit.DB.Events.DocumentSavingEventArgs>(DocSave);
application.ControlledApplication.DocumentClosing
-= new EventHandler<Autodesk.Revit.DB.Events.DocumentClosingEventArgs>(DocClosing);
}
catch
{
return Result.Failed;
}
return Result.Succeeded;
}
private void DocOpen(object sender, DocumentOpenedEventArgs e)
{
// get the current model
Document doc = e.Document;
// get a list of all the model's warnings
IList<FailureMessage> warnings = doc.GetWarnings();
// loop through each warning
foreach (FailureMessage fmsg in warnings)
{
string s1 = fmsg.GetDescriptionText();
string s2 = "20 mile";
bool b = s1.Contains(s2);
if (b)
{
r1 = "TRUE";
// show dialog if 20 mile limit warning exists
TaskDialog.Show("WARNING", fmsg.GetDescriptionText());
}
}
}
private void DocSave(object sender, DocumentSavingEventArgs e)
{
// get the current model
Document doc2 = e.Document;
// get system time and system username
string GetTimestamp(DateTime value)
{
return value.ToString("yyyyMMddHHmmssffff");
}
string timeStamp = GetTimestamp(DateTime.Now);
string userName = Environment.UserName;
// get a list of all the model's warnings
IList<FailureMessage> warnings2 = doc2.GetWarnings();
// loop through each warning
foreach (FailureMessage fmsg2 in warnings2)
{
string s1a = fmsg2.GetDescriptionText();
string s2a = "20 mile";
bool b2 = s1a.Contains(s2a);
if (b2)
{
r2 = "TRUE";
}
}
if ((r1 == "TRUE") || (r2 == "TRUE"))
{
// directory location
var dir = @"<server location>";
// if directory does not exist, create
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
// use Path.Combine to combine strings to a path
File.WriteAllText(Path.Combine(dir, timeStamp + "_log.csv"), userName + "," + doc2.PathName + "," + r1 + "," + r2);
// Clear strings
r2 = "FALSE";
}
}
private void DocClosing(object sender, DocumentClosingEventArgs e)
{
// Clear strings
r1 = "FALSE";
r2 = "FALSE";
}
}
}