private static readonly string LogFilePath = @"C:\temp\processing_log.txt";
//Missing
//warnings swallow
//error skipping
//taskdialog clicking, need to use Documentopened event
internal static void GetModelsFromDirectory(UIApplication uiApp)
{
using(FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
{
folderBrowserDialog.Description = "Select Folder Containing Revit Models";
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
string[] rvtFiles = Directory.GetFiles(folderBrowserDialog.SelectedPath, "*.rvt", SearchOption.AllDirectories);
using (FormProg form = new FormProg(rvtFiles.Length))
{
form.TopLevel = true;
form.AutoScroll = false;
form.FormBorderStyle = FormBorderStyle.None;
form.Show();
foreach (string rvtPath in rvtFiles)
{
File.AppendAllText(LogFilePath, $"+++: {rvtPath} \n");
if (!IsRvtFile(rvtPath)) continue;
if (!IsCurrentVersion(uiApp, rvtPath)) continue;
if (IsBackupFiles(rvtPath)) continue;
form.StepProgress(rvtPath);
ProcessModel(uiApp, rvtPath);
}
}
}
}
}
private static void ProcessModel(UIApplication uiApp, string rvtPath)
{
string fileName = Path.GetFileName(rvtPath);
try
{
using (var modelPath = ModelPathUtils.ConvertUserVisiblePathToModelPath(rvtPath))
using (var openOptions = new OpenOptions { Audit = true, DetachFromCentralOption = DetachFromCentralOption.DoNotDetach })
{
Document doc = null;
try
{
doc = uiApp.Application.OpenDocumentFile(modelPath, openOptions);
}
catch (Autodesk.Revit.Exceptions.WrongUserException)
{
return;
}
catch (Exception)
{
return;
}
ProcessAndSaveDocument(doc);
doc.Close(true);
}
}
catch (Exception) { }
}
private static void ProcessAndSaveDocument(Document doc)
{
using (var transaction = new Transaction(doc, "Process Model"))
{
transaction.Start();
var failureHandlingOptions = transaction.GetFailureHandlingOptions();
failureHandlingOptions.SetFailuresPreprocessor(new CustomFailureProcessor());
transaction.SetFailureHandlingOptions(failureHandlingOptions);
failureHandlingOptions.SetClearAfterRollback(true);
using (var saveOptions = new SaveOptions { Compact = true })
{
doc.Save(saveOptions);
}
transaction.Commit();
}
}
private static bool IsRvtFile(string filePath)
{
bool isRvt = Path.GetExtension(filePath).Equals(".rvt", StringComparison.OrdinalIgnoreCase);
if (isRvt)
{
File.AppendAllText(LogFilePath, $"Is Extension .Rvt?: {isRvt} \n");
}
else
{
File.AppendAllText(LogFilePath, $"Is Extension .Rvt?: {isRvt} \n");
}
return isRvt;
}
private static bool IsCurrentVersion(UIApplication uiApp, string rvtPath)
{
BasicFileInfo fileInfo = BasicFileInfo.Extract(rvtPath);
string fileRevitVersion = fileInfo.Format;
string currentRevitVersion = uiApp.Application.VersionNumber;
bool isCurrentVersion = fileRevitVersion == currentRevitVersion;
if (isCurrentVersion)
{
File.AppendAllText(LogFilePath, $"Is Revit Version Current?: {isCurrentVersion} \n");
}
else
{
File.AppendAllText(LogFilePath, $"Is Revit Version Current?: {isCurrentVersion} \n");
}
return isCurrentVersion;
}
private static bool IsBackupFiles(string filePath)
{
string fileName = Path.GetFileName(filePath);
Regex pattern = new Regex(@"\.\d{4}\.rvt$");
bool isBackup = pattern.IsMatch(fileName);
if (isBackup)
{
File.AppendAllText(LogFilePath, $"Skip if Is Backup File?: {isBackup} \n");
}
else
{
File.AppendAllText(LogFilePath, $"Skip if Is Backup File?: {isBackup} \n");
}
return isBackup;
}
}//cl
internal class CustomFailureProcessor : IFailuresPreprocessor
{
private static readonly string LogFilePath = @"C:\temp\processing_log.txt";
public FailureProcessingResult PreprocessFailures(FailuresAccessor failuresAccessor)
{
IList<FailureMessageAccessor> failures = failuresAccessor?.GetFailureMessages();
if (failures != null)
{
foreach (FailureMessageAccessor failure in failures)
{
if (failure.GetSeverity() == FailureSeverity.Error)
{
File.AppendAllText(LogFilePath, "Internal model errors: True \n");
return FailureProcessingResult.ProceedWithRollBack;
}
else if (failure.GetSeverity() == FailureSeverity.Warning)
{
failuresAccessor.DeleteWarning(failure);
FailureHandlingOptions handlingOptions;
}
}
}
return FailureProcessingResult.Continue;
}
}