Saturday, October 19, 2013

Macro that Exports All Sheets to DWG

***UPDATED***
Code has been updated to fix a bug with Sheet Placeholders causing an error.

Jay Zallan asked me if I could help him create a macro that would quickly export all sheets to DWG files for his consultants that only use CAD. Here is the result, a macro that will export all the sheets to DWG into a dated subfolder where the model is located. The DWGs have bound views and shared coordinates.

Macro has also been added to https://bitbucket.org/BoostYourBIM/revit_api_public/

Code:
        public void ExportSheetsToDWG()
        {
            // setup uidoc and doc for accessing the Revit UI (uidoc) and the Model (doc)
            UIDocument uidoc = this.ActiveUIDocument;
            Document doc = uidoc.Document;
           
            // get all the elements in the model database
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            // filter out all elements except Views
            ICollection<Element> collection = collector.OfClass(typeof(ViewSheet)).ToElements();
           
            // create a transaction
            using(Transaction t = new Transaction(doc, "Export Sheets"))
            {
                // start the transaction
                t.Start();
               
                // create a list to hold the sheets
                List<ElementId> sheetsToDWG = new List<ElementId>();
               
                // create DWG export options
                DWGExportOptions dwgOptions = new DWGExportOptions();
                dwgOptions.MergedViews = true;
                dwgOptions.SharedCoords = true;
               
                // add a counter to count the sheets exported
                int x = 0;
            
                // loop through each view in the model
                foreach (Element e in collection)
                {                   
                    try
                    {   
                        ViewSheet viewsheet = e as ViewSheet;
                       
                        // only add sheets to list
                        if (viewsheet.IsPlaceholder == false)
                        {
                            sheetsToDWG.Add(e.Id);
                            x += 1;
                        }
                    }
                    catch
                    {
                    }
                }

                string path = "";
                string file = "";
               
                // get the current date and time
                DateTime dtnow = DateTime.Now;
                string dt = string.Format("{0:yyyyMMdd HHmm}", dtnow);
               
                if (doc.PathName != "")
                {
                    // use model path + date and time
                    path = Path.GetDirectoryName(doc.PathName) + "\\" + dt;                   
                }
                else
                {
                    // model has not been saved
                    // use C:\DWG_Export + date and time
                    path = "C:\\DWG_Export\\" + dt;
                    file = "NONAME";
                }
               
                // create folder
                Directory.CreateDirectory(path);
               
                // export
                doc.Export(path, file, sheetsToDWG, dwgOptions);
               
                TaskDialog.Show("Export Sheets to DWG", x + " sheets exported to:\n" + path);
            }
        }

2 comments:

  1. how to add sheet list to this macro

    ReplyDelete
  2. Hi Troy,

    It is great!!! Thank you for sharing that. I am wondering two things which I think it will take your macro to another level or much more difficult.

    I will try to explain the background. We use NewForma to manage the information (pdf), however this software does not have a option for exporting dwg or dwf, so it means when you export your drawings to dwg or dwf the name does not match with the pdf.

    As we are in U.K the revision does not follow the Revit Revision, so normally the pdf name are: Sheet number + Revision (normally a letter + number) in this order. As revit does not allow change the prefix when you export the drawing we need to change all the names by hand.

    I am wondering if you know some way (adding a option in your macro...) to sort this problem out. I do not know. I tried with Dynamo and ironpython, but I could not make it work.

    Btw, is it possible to add a TaskDialog to choose the drawing you want to export in your macro?

    Thank you very much in advance

    ReplyDelete