Scrubbing Out Dimension Styles

selectAllInstance

Over at AUGI a good question was asked about how to get undesired dimension styles out of a project.

Dimensions in sketches and unplaced groups are at least part of the problem because right-clicking on a dimension and choosing “Select All Instances – In Entire Project” doesn’t select them.

Here is a macro that handles both dimensions in sketches and dimensions in unplaced groups. The dimensions in sketches are found as part of a normal filter to find all dimensions in the project. Dimensions in unplaced groups needed to be treated specially because an instance of the group needs to be created before the dimension in the group can be changed.

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

    // Prompt user to select a dimension with an unwanted style
    Dimension selected = doc.GetElement(uidoc.Selection.PickObject(ObjectType.Element), "Select dimension with style to scrub.") as Dimension;
    // Get name of the dimension style of this dimension
    string toScrub = selected.DimensionType.Name;

    // Find the dimension style named "Default" which will be used to replace the unwanted style
    // There must be a style named "Default" for this command to work
    DimensionType defaultType = (from v in new FilteredElementCollector(doc)
                                                        .OfClass(typeof(DimensionType))
                                                        .Cast<DimensionType>()
                                                        where v.Name == "Default" select v).First();

    using (Transaction t = new Transaction(doc,"Change dimension style to default"))
    {
        t.Start();
        // loop through all dimensions with the unwanted style
        foreach (Dimension dimension in (from v in new FilteredElementCollector(doc).OfClass(typeof(Dimension)).Cast<Dimension>()
                              where v.DimensionType.Name == toScrub select v))
        {
            // change the dimension's style to the default style
            dimension.DimensionType = defaultType;
        }
        t.Commit();
    }

    // Dimensions in unplaced groups will not be found by the code above.
    // So we need to place an instance of each of these groups, find any dimensions in the group, and change their style if needed
    foreach (GroupType groupType in new FilteredElementCollector(doc).OfClass(typeof(GroupType)))
    {
        // if there are already instances of the group then skip to the next group type
         if (groupType.Groups.Size > 0)
             continue;
         using (Transaction t = new Transaction(doc,"Change dimension style in unplaced groups"))
         {
            // use this flag to track if any dimensions have been changed
            bool flag = false;
            t.Start();

            // place an instance of the group
            Group newGroup = doc.Create.PlaceGroup(XYZ.Zero, groupType);

            // loop through all members in the group
            foreach (ElementId id in newGroup.GetMemberIds())
            {
                  // identify any dimensions in the group
                  Dimension dimension = doc.GetElement(id) as Dimension;
                  if (dimension != null)
                  {
                      // check if this dimension has the unwanted type
                      if (dimension.DimensionType.Name = toScrub)
                      {
                          // change the dimension type to the default type
                          dimension.DimensionType = defaultType;
                          flag = true;
                      }
                  }
            }
            doc.Delete(newGroup.Id); // delete the newly placed group
            if (flag)
                  t.Commit(); // commit this transaction if any dimensions were changed
            else
                  t.RollBack(); // no dimensions were changed, so throw away the entire transaction
         }
    }    
}
Advertisements

17 thoughts on “Scrubbing Out Dimension Styles

  1. I am getting four errors when I build this macro: 1. No overload for method ‘GetElement’ takes 2 arguments (CS1501). 2. and 3. No definition of ‘Cast.’ 4. Cannot implicitly convert type ‘string’ to ‘bool’ (CS0029). I am new to macros, and am running Revit 2013. This could be a great tool to help me manage “rogue” dimension styles, if only I knew how to fix the errors!

      • Thanks so much, Harry, for your reply. I added the using statement “using System.Linq;” and cleared two of the four errors. The remaining errors are as follows:
        1. No overload for method ‘GetElement’ takes 2 arguments in line 45;
        2. Cannot implicitly convert type ‘string’ to ‘bool’ in line 97.

        Pasted below is the entirety of my code:
        /*
        * Revit Macro created by SharpDevelop
        * User: Chrise
        * Date: 5/14/2013
        * Time: 4:00 PM
        *
        * To change this template use Tools | Options | Coding | Edit Standard Headers.
        */
        using System;
        using Autodesk.Revit.DB;
        using Autodesk.Revit.UI;
        using Autodesk.Revit.UI.Selection;
        using System.Linq;

        namespace module1
        {
        [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
        [Autodesk.Revit.UI.Macros.AddInId(“AE602326-BBB4-491B-AD7E-8C6A175B31A7”)]
        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

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

        // Prompt user to select a dimension with an unwanted style
        Line45 Dimension selected = doc.GetElement(uidoc.Selection.PickObject(ObjectType.Element), “Select dimension with style to scrub.”) as Dimension;

        // Get name of the dimension style of this dimension
        string toScrub = selected.DimensionType.Name;

        // Find the dimension style named “Default” which will be used to replace the unwanted style
        // There must be a style named “Default” for this command to work
        DimensionType defaultType = (from v in new FilteredElementCollector(doc)
        .OfClass(typeof(DimensionType))
        .Cast()
        where v.Name == “Default” select v).First();

        using (Transaction t = new Transaction(doc,”Change dimension style to default”))
        {
        t.Start();
        // loop through all dimensions with the unwanted style
        foreach (Dimension dimension in (from v in new FilteredElementCollector(doc).OfClass(typeof(Dimension)).Cast()
        where v.DimensionType.Name == toScrub select v))
        {
        // change the dimension’s style to the default style
        dimension.DimensionType = defaultType;
        }
        t.Commit();
        }

        // Dimensions in unplaced groups will not be found by the code above.
        // So we need to place an instance of each of these groups, find any dimensions in the group, and change their style if needed
        foreach (GroupType groupType in new FilteredElementCollector(doc).OfClass(typeof(GroupType)))
        {
        // if there are already instances of the group then skip to the next group type
        if (groupType.Groups.Size > 0)
        continue;
        using (Transaction t = new Transaction(doc,”Change dimension style in unplaced groups”))
        {
        // use this flag to track if any dimensions have been changed
        bool flag = false;
        t.Start();

        // place an instance of the group
        Group newGroup = doc.Create.PlaceGroup(XYZ.Zero, groupType);

        // loop through all members in the group
        foreach (ElementId id in newGroup.GetMemberIds())
        {
        // identify any dimensions in the group
        Dimension dimension = doc.GetElement(id) as Dimension;
        if (dimension != null)
        {
        // check if this dimension has the unwanted type
        if (dimension.DimensionType.Name = toScrub)
        {
        // change the dimension type to the default type
        Line97 dimension.DimensionType = defaultType;
        flag = true;
        }
        }
        }
        doc.Delete(newGroup.Id); // delete the newly placed group
        if (flag)
        t.Commit(); // commit this transaction if any dimensions were changed
        else
        t.RollBack(); // no dimensions were changed, so throw away the entire transaction
        }
        }
        }
        }
        }

        I’ve noted the error line numbers in the code above for convenience. Thanks in advance for whatever help you can provide.

        • When you are getting stuck on errors like these, I’d suggest being more verbose and separating out every statement on its own line instead of bunching them together where there is an error. For example, one of your errors is fixed by correcting the position of the ) character. Putting it on two lines like this makes it more clear.

          Reference reference = uidoc.Selection.PickObject(ObjectType.Element, “Select dimension with style to scrub.”); Dimension selected = doc.GetElement(reference) as Dimension;

          The “string to bool” error is because you need to use the == to test for equality. A single = is to assign a value. Take a look at these http://msdn.microsoft.com/en-us/library/53k8ybth.aspx http://msdn.microsoft.com/en-us/library/aa691314(v=vs.71).aspx

  2. I am not having any luck getting this to work. I Thought I updated everything based on the comments but it does not appear to be working for me. I am using revit 2015. Can someone post the final version.

    Thanks

      • public void DimensionSytleScrub()
        {
        Document doc = this.ActiveUIDocument.Document;
        UIDocument uidoc = new UIDocument(doc);

        // Prompt user to select a dimension with an unwanted style
        Reference reference = uidoc.Selection.PickObject(ObjectType.Element, “Select dimension with style to scrub.”);
        Dimension selected = doc.GetElement(reference) as Dimension;

        // Get name of the dimension style of this dimension
        string toScrub = selected.DimensionType.Name;

        // Find the dimension style named “Default” which will be used to replace the unwanted style
        // There must be a style named “Default” for this command to work
        DimensionType defaultType = (from v in new FilteredElementCollector(doc)
        .OfClass(typeof(DimensionType))
        .Cast()
        where v.Name == “Default” select v).First();

        using (Transaction t = new Transaction(doc,”Change dimension style to default”))
        {
        t.Start();
        // loop through all dimensions with the unwanted style
        foreach (Dimension dimension in (from v in new FilteredElementCollector(doc).OfClass(typeof(Dimension)).Cast()
        where v.DimensionType.Name == toScrub select v))
        {
        // change the dimension’s style to the default style
        dimension.DimensionType = defaultType;
        }
        t.Commit();
        }

        // Dimensions in unplaced groups will not be found by the code above.
        // So we need to place an instance of each of these groups, find any dimensions in the group, and change their style if needed
        foreach (GroupType groupType in new FilteredElementCollector(doc).OfClass(typeof(GroupType)))
        {
        // if there are already instances of the group then skip to the next group type
        if (groupType.Groups.Size > 0)
        continue;
        using (Transaction t = new Transaction(doc,”Change dimension style in unplaced groups”))
        {
        // use this flag to track if any dimensions have been changed
        bool flag = false;
        t.Start();

        // place an instance of the group
        Group newGroup = doc.Create.PlaceGroup(XYZ.Zero, groupType);

        // loop through all members in the group
        foreach (ElementId id in newGroup.GetMemberIds())
        {
        // identify any dimensions in the group
        Dimension dimension = doc.GetElement(id) as Dimension;
        if (dimension != null)
        {
        // check if this dimension has the unwanted type
        if (dimension.DimensionType.Name == toScrub)
        {
        // change the dimension type to the default type
        dimension.DimensionType = defaultType;
        flag = true;
        }
        }
        }
        doc.Delete(newGroup.Id); // delete the newly placed group
        if (flag)
        t.Commit(); // commit this transaction if any dimensions were changed
        else
        t.RollBack(); // no dimensions were changed, so throw away the entire transaction
        }
        }
        }
        }
        }

    • The error is “sequence contains no elements” – probably your project doesn’t have a dimension style named Default. Solution, change the Default to something that exists in your project.

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