How to toggle the visibility of Automatic Sketch Dimensions

I was asked if the API could be used to make it easier to toggle the visibility of Temporary Sketch Dimensions. This macro will toggle the state of this subcategory for one or all views in the project.

In future post I will examine how this can be made easier to access by adding the command to the ribbon so it can be invoked with a keyboard shortcut instead of having to access it through the Macros dialog.

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

    // use the following line to set the visibility for only the active view
    // use the FilteredElementCollector and foreach loop to set it for all views
    // View view = doc.ActiveView;

    Category autoSketchDimCat = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Dimensions).SubCategories.get_Item("Automatic Sketch Dimensions");

    using (Transaction t = new Transaction(doc,"Toggle 'Automatic Sketch Dimensions' Visibility"))
    {
        t.Start();
        foreach (View view in new FilteredElementCollector(doc).OfClass(typeof(View)).Cast<View>())
        {
            // set the visibility of this category to the opposite of its current value (if it is True, set it to False, etc)
            view.SetVisibility(autoSketchDimCat, !autoSketchDimCat.get_Visible(view));
        }
        t.Commit();
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s