Tuesday, November 19, 2013

Setting an Image's Visibility to Off in a Family

I am revamping several of our engineer's stamps for Revit 2014 and didn't want to go through the old process of converting their signature from an image to CAD and then importing to Revit. So I decided to just use the images and went to set the Visibility to a parameter and found it wasn't available (I already knew this from way back, but forgot about it).

Image in Family with no Parameter for Visibility

















Looking over the small list of parameters, it hit me. Why not make the Width and Height equal to 0 when I don't want to see the signature, thus setting it to not visible.

So I setup 3 parameters called Show Signature, Sig_W, and Sig_H. I then linked the Image's Width to Sig_W and Height to Sig_H. Lastly I created a formula for Sig_W and Sig_H to adjust the image size off of the parameter Show Signature's value.

New Parameters and Formulas


















Linking the Width and Height to the New Parameters

Monday, November 18, 2013

Quick Macro that creates 5 Dependent Views for the Current View

I had a user that needed to create 5 dependent views for 5 areas in a building across several views and disciplines. To save him time, I created this quick macro that will create 5 dependent views for the active view. This will save a lot of right-clicking on each view.

Code:


        public void CreateDependentViews()
        {
            UIDocument uidoc = this.ActiveUIDocument;
            Document doc = uidoc.Document;

            using(Transaction t = new Transaction(doc, "Duplicate View 5x"))
            {
                t.Start();
              
                int i = 0;
              
                while (i < 5)
                {
                    uidoc.ActiveView.Duplicate(ViewDuplicateOption.AsDependent);
                    i++;
                }               
                t.Commit();                   
            }           
        }


I expanded it a bit more and did some renaming based on Areas just to enhance it a bit.

Code:

        public void CreateDependentViews()
        {
            UIDocument uidoc = this.ActiveUIDocument;
            Document doc = uidoc.Document;

            using(Transaction t = new Transaction(doc, "Duplicate View 5x"))
            {
                t.Start();
               
                int i = 0;
               
                while (i < 5)
                {
                    ElementId dupViewId = uidoc.ActiveView.Duplicate(ViewDuplicateOption.AsDependent);
                    View dupView = doc.GetElement(dupViewId) as View;
                    char c = (char) (i + 65);
                    dupView.Name = uidoc.ActiveView.Name + " - AREA " + c.ToString();
                   
                    i++;
                }
               
                t.Commit();                   
            }           
        }

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);
            }
        }

Thursday, October 17, 2013

Open Source Macros for Revit

Last week several people on Twitter started a discussion of having a way to collect and share Revit macros for the Revit community. Luke (What Revit Wants) suggested that we use an open source repository to collect and manage the macros. Eventually, Harry (BoostYourBim) setup a repository on BitBucket (https://bitbucket.org/BoostYourBIM/revit_api_public/overview).

I have since added a few of my macros and plan to add all the existing ones. I will continue to contribute the macros I create for myself as well for LARUG. Follow Harry's instructions below to get the macros added to your Revit folder from BitBucket. Then whenever there are changes just pull them down to your computer and you will have all the macros in Revit ready to use.

Source:
What Revit Wants: Public Macro Repository for Revit
BoostYourBIM: A public Git repository for sharing Revit Macros at BitBucket

Thursday, October 10, 2013

Macros for Deleting all MEP Spaces and Rooms

Another task I do frequently is deleting all the spaces in my MEP model so that I can recreate them all based on the architect's rooms. Why delete them all? Sometimes its just easier to delete them all than to search out which rooms have been changed, deleted, or added. This macro prevents me from needing to delete the spaces from a schedule, since Revit doesn't delete allow deleting spaces from a plan view, only schedules.

Delete All Spaces

Code

public void DeleteAllSpaces()
{
    // setup uidoc and doc for accessing the Revit UI (uidoc) and the Model (doc)
    UIDocument uidoc = this.ActiveUIDocument;
    Document doc = uidoc.Document;
    
    // get all elements in the model
    FilteredElementCollector collector = new FilteredElementCollector(doc);
    // filter out everything but spaces
    ICollection<Element> collection = collector.OfClass(typeof(SpatialElement)).OfCategory(BuiltInCategory.OST_MEPSpaces).ToElements();
    
    // create and start a new transaction
    using(Transaction t = new Transaction(doc, "Delete All Spaces"))
    {
        t.Start();
        
        // add a counter to count the spaces deleted
        int x = 0;
        
        // loop through each space in the model
        foreach (Element e in collection)
        {                    
            try 
            {    
                // delete the space and increment counter by 1
                doc.Delete(e);
                x += 1;
            }
            catch(Exception ex)
            {
                
            }
        }
        
        // finalize the transaction
        t.Commit();
        // show a dialog with the number of spaces deleted
        TaskDialog.Show("DeleteSpaces", "Spaces Deleted: " + x.ToString());
        
    }
                
}

Delete All Rooms
After showing this macro at LARUG, people asked if I could do the same for deleting all rooms. So here is a modified version.

Code

public void DeleteAllRooms()
{
    // setup uidoc and doc for accessing the Revit UI (uidoc) and the Model (doc)
    UIDocument uidoc = this.ActiveUIDocument;
    Document doc = uidoc.Document;
    
    // get all elements in the model
    FilteredElementCollector collector = new FilteredElementCollector(doc);
    // filter out everything but rooms
    ICollection<Element> collection = collector.OfClass(typeof(SpatialElement)).OfCategory(BuiltInCategory.OST_Rooms).ToElements();
    
    // create and start a new transaction
    using(Transaction t = new Transaction(doc, "Delete All Rooms"))
    {
        t.Start();
        
        // add a counter to count the rooms deleted
        int x = 0;
        
        // loop through each room in the model
        foreach (Element e in collection)
        {                    
            try 
            {    
                // delete the room and increment counter by 1
                doc.Delete(e);
                x += 1;
            }
            catch(Exception ex)
            {
                
            }
        }
        
        // finalize the transaction
        t.Commit();
        // show a dialog with the number of rooms deleted
        TaskDialog.Show("DeleteRooms", "Rooms Deleted: " + x.ToString());
        
    }
                
}

Monday, October 07, 2013

Macros for Deleting Views and Sheets

These 3 macros were created to solve a task that I do frequently, clean models from other companies. As an MEP firm, we receive several models per project from the architects, structural, and others. We also use Revit Server to enable our teams to work from any office on any project. So in order to reduce time and space, we clean out the models we receive by deleting views, legends, schedules, and sheets. Then we purge and the models range from 30-60% smaller in size. Depending on the source of the model we may or may not delete the sheets and typically for arch models we keep the floor and ceiling plans for linking.

Caution: DO NOT run these in your model unless you want to delete views and sheets


Delete All Sheets And Views
The first macro will delete all views and all sheets except for the currently open view or sheet. This is the ultimate cleaner for models that you only want the model and nothing else.

Code:

public void DeleteAllSheetsAndViews()
{
    // 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(View)).ToElements();
    
    // create a transaction
    using(Transaction t = new Transaction(doc, "Delete All Sheets and Views"))
    {
        // start the transaction
        t.Start();
        
        // add a counter to count the views/sheets deleted
        int x = 0;
        
        // loop through each view in the model
        foreach (Element e in collection)
        {                    
            try 
            {    
                View view = e as View;
                
                // all views/sheets are deleted and increment counter by 1
                doc.Delete(e);
                x += 1;
            }
            catch(Exception ex)
            {
                // uncomment below to show error messages
                //View view = e as View;
                //TaskDialog.Show("Error", e.Name + "\n" + "\n" + ex.Message);
                //TaskDialog.Show("Error", ex.Message);
            }
        }
        // finalize the transaction
        t.Commit();
        // show message with number of views/sheets deleted
        TaskDialog.Show("DeleteAllSheetsViews", "Views & Sheets Deleted: " + x.ToString());
    }
}


Delete Sheets And Views (except floor & ceiling plans)
The second macro will delete all sheets and all views except for floor and ceiling plans. This is typically the macro I run on arch models towards the end of a project when we no longer to look through their sheets.

Code:

public void DeleteSheetsAndViews()
{
    // 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(View)).ToElements();
    
    // create a transaction
    using(Transaction t = new Transaction(doc, "Delete Sheets and Views"))
    {
        // start the transaction
        t.Start();
        
        // add a counter to count the views/sheets deleted
        int x = 0;
        
        // loop through each view in the model
        foreach (Element e in collection)
        {                    
            try 
            {    
                View view = e as View;
                
                // determine what type of view it is
                switch(view.ViewType)
                {
                    // if view is a floor plan, don't delete
                    case ViewType.FloorPlan:
                        break;
                    // if view is a ceiling plan, don't delete
                    case ViewType.CeilingPlan:
                        break;
                    // all other views/sheets can be deleted and increment counter by 1
                    default:
                        doc.Delete(e);
                        x += 1;
                        break;
                }
            }
            catch(Exception ex)
            {
                // uncomment below to show error messages
                //View view = e as View;
                //TaskDialog.Show("Error", e.Name + "\n" + "\n" + ex.Message);
                //TaskDialog.Show("Error", ex.Message);
            }
        }
        // finalize the transaction
        t.Commit();
        // show message with number of views/sheets deleted
        TaskDialog.Show("DeleteSheetsViews", "Views & Sheets Deleted: " + x.ToString());
    }
}


Delete Views (except floor and ceiling plans)
The last macro will delete all views except for floor and ceiling plans. It will also not delete the sheets. I use this macro on arch models from the beginning of a project through mid CDs. It allows people on the project to review the arch plans sheets by opening their model.

Code:

public void DeleteViews()
{
    // 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(View)).ToElements();
    
    // create a transaction
    using(Transaction t = new Transaction(doc, "Delete Views"))
    {
        // start the transaction
        t.Start();
        
        // add a counter to count the views deleted
        int x = 0;
        
        // loop through each view in the model
        foreach (Element e in collection)
        {                    
            try 
            {    
                View view = e as View;
                
                // determine what type of view it is
                switch(view.ViewType)
                {
                    // if view is a floor plan, don't delete
                    case ViewType.FloorPlan:
                        break;
                    // if view is a ceiling plan, don't delete
                    case ViewType.CeilingPlan:
                        break;
                    // if view is a sheet, don't delete
                    case ViewType.DrawingSheet:
                        break;
                    // all other views can be deleted and increment counter by 1
                    default:
                        doc.Delete(e);
                        x += 1;
                        break;
                }
            }
            catch(Exception ex)
            {
                // uncomment below to show error messages
                //View view = e as View;
                //TaskDialog.Show("Error", e.Name + "\n" + "\n" + ex.Message);
                //TaskDialog.Show("Error", ex.Message);
            }
        }
        // finalize the transaction
        t.Commit();
        
        // show message with number of views/sheets deleted
        TaskDialog.Show("DeleteViews", "Views Deleted: " + x.ToString());
    }

}
 

If you have any questions about any of these macros, please leave a comment and I will try to assist.

Monday, September 30, 2013

Creating Macros (LARUG Sept 2013)

At the LARUG Sept 2013 meeting (video here: http://losangelesrevitusersgroup.blogspot.com/2013/09/larug-september-2013-macro-creation-in.html) I presented on Revit Macros. As promised I would do a follow up of the process, links to tools, and the macros themselves. In this post I will give you everything to download and I will follow up with separate posts about each macro over the next few days.

Here are the downloads and links:

Revit Software Development kits
Revit Development Blogs
Revit Macro/Programming Tools
Macros (Individual posts to follow)
* Disclaimer: I am not an expert in programming, so the macros below may or may not work and I hold no responsibility to any problems they may cause your projects.
* Disclaimer #2: Fill free to use and edit these macros, but please give credit if you republish them.

* I included install instructions in the Revit Lookup zip files and the LARUG zip file. If you have questions on how to install these, leave a comment below.