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