Skip to content

Instantly share code, notes, and snippets.

@mnelson7982
Last active February 17, 2021 17:33
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mnelson7982/7002310 to your computer and use it in GitHub Desktop.
Save mnelson7982/7002310 to your computer and use it in GitHub Desktop.
Export All Revit Schedules to CSV. Exports to the User's MyDocuments folder under RVT_Schedules \ {{PROJECT NAME}}\
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;
namespace BasicMacros
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.DB.Macros.AddInId("2C832A1F-ECF5-4CDD-844A-B724EC207CEE")]
public partial class ThisApplication
{
private void Module_Startup(object sender, EventArgs e)
{
}
private void Module_Shutdown(object sender, EventArgs e)
{
}
#region Revit Macros generated code
private void InternalStartup()
{
this.Startup += new System.EventHandler(Module_Startup);
this.Shutdown += new System.EventHandler(Module_Shutdown);
}
#endregion
//Helper to remove all special characters from a string.
private static string MakeValidFileName( string name )
{
string invalidChars = System.Text.RegularExpressions.Regex.Escape( new string( System.IO.Path.GetInvalidFileNameChars() ) );
string invalidReStr = string.Format( @"([{0}]*\.+$)|([{0}]+)", invalidChars );
return System.Text.RegularExpressions.Regex.Replace( name, invalidReStr, "_" );
}
//Macro To export all schedules to the users myDocuments folder. This will export as CSV.
//All ViewNames will be renamed to be valid windows names.
public void ExportAllSchedules()
{
UIDocument uidoc = this.ActiveUIDocument;
Document doc = this.ActiveUIDocument.Document;
//Get the RVT name and remove the extension
string docName = uidoc.Document.Title.ToString();
string title = docName.Replace(".rvt","");
//Set Folder to MyDocs > RVT_Schedules > DocTitle
String folderName = Environment.GetFolderPath(Environment.SpecialFolder.Personal).ToString() +"\\RVT_Schedules\\" + title + "\\";
//Set Filter to Schedules and set options
FilteredElementCollector collection = new FilteredElementCollector(doc).OfClass(typeof(ViewSchedule));
ViewScheduleExportOptions opt = new ViewScheduleExportOptions();
//See If folder exists, delete existing exports or create folders
if(Directory.Exists(folderName))
{
Array.ForEach(Directory.GetFiles(folderName), File.Delete);
}
else
{
Directory.CreateDirectory(folderName);
}
//Iterate through schedules and export to <Schedulename>.txt
foreach( ViewSchedule vs in collection )
{
string viewName = MakeValidFileName(vs.Name);
//OPTIONAL: Exclude all hidden Schedules (IE: Revision Schedules on each sheet.)
if(!vs.Name.Contains("<"))
//Try Loop to avoid any export exceptions
try
{
vs.Export(folderName, viewName + ".csv", opt );
}
catch
{
TaskDialog.Show("Error", vs.Name);
}
}
//When Complete, let the user know.
TaskDialog td = new TaskDialog("Schedule Exporter");
td.MainContent = "All of the Revit schedules in project:" + Environment.NewLine +
docName + Environment.NewLine + Environment.NewLine +
"Have been exported to: " + Environment.NewLine +
folderName ;
td.MainInstruction = "Schedules have been exported.";
td.Show();
}
}
}
@mnelson7982
Copy link
Author

Note: Revision Schedules will come out as well. This may not be needed. If So, a IF Statement can be added around the vs.Name to exclude all revision schedules.

UPDATE: Added Line 75 to counter excessive revision schedule exports.

@dvsainz
Copy link

dvsainz commented Jan 20, 2015

Hi Mnelson, could you please helpme implementing this tool. I tryed but the compiler has gave me error. Manu thanks!

@AlPhMan
Copy link

AlPhMan commented Nov 2, 2015

Hi Matthew. Thanks for this! I've got only one class of C# for Revit coding behind me, but I've just used this and it seems to works great with no Build Errors for me.

@zcftr29
Copy link

zcftr29 commented Jan 4, 2016

Hi Mnelson. This script is just what I've been looking for for ages. I have no coding experience, but can just about follow how this works. I have tested this in a couple of projects where we have a large number of area and room schedules which link into an Excel spreadsheet and it almost works great. Unfortunately Revit crashes after exporting all schedules but before the 'successful' dialogue box appears. I have tested with both Rvt 2015 and 2016 on two separate projects with the same result. However, on a fresh project file using the standard architectural template, the macro does not crash Revit. Any ideas??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment