-
-
Save mnelson7982/7002310 to your computer and use it in GitHub Desktop.
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(); | |
} | |
} | |
} |
Hi Mnelson, could you please helpme implementing this tool. I tryed but the compiler has gave me error. Manu thanks!
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.
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??
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.