Ever had any stubborn text styles that wouldn't delete? I did- so I went to Boost Your BIM - Scrubbing Out Dimension Styles and adapted the code there to substitute one text style for another in my first useful macro! 

Start with a sample of text with the text style you want to replace, also add a sample of text style you want to match.

On runing the macro- it prompts in the STATUS BAR (tough to see - working on that) to select a text to replace, then the ext to match. Once completed you can run a purge and Voila! those peskt text styles should be available to purge!

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.UI.Events;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.ApplicationServices;



        public void TextSytleScrub()
        {
            Document doc = this.ActiveUIDocument.Document;
            UIDocument uidoc = new UIDocument(doc);
      
            // Prompt user to select a -text- with an unwanted style
            TextNote selected = doc.GetElement (uidoc.Selection.PickObject (ObjectType.Element , "Select -text- with style to scrub.")) as TextNote ;
            // Get name of the -text- style of this -text-
            string toScrub = selected.TextNoteType.Name;
      
            // Find the -text- style named "Default" which will be used to replace the unwanted style
            // There must be a style named "Default" for this command to work

      
            TextNote replacement = doc.GetElement (uidoc.Selection.PickObject (ObjectType.Element , "Select -text- with replacement style.")) as TextNote ;
            // Get name of the -text- style of this -text-
            string toreplace = replacement.TextNoteType.Name;
          
            TextNoteType defaultType = (from v in new FilteredElementCollector(doc)
                                        .OfClass(typeof(TextNoteType))
                                        .Cast<TextNoteType>()
                                        where v.Name == replacement.TextNoteType.Name select v).First();
          
            using (Transaction t = new Transaction(doc,"Change -text- style to default"))
            {
                t.Start();
                // loop through all -textnote- with the unwanted style
                foreach (TextNote textnote in (from v in new FilteredElementCollector(doc).OfClass(typeof(TextNote)).Cast<TextNote>()
                                      where v.TextNoteType.Name == toScrub select v))
                {
                    // change the -textnote-'s style to the default style
                    textnote.TextNoteType = defaultType;
                }
                t.Commit();
            }
      
            // Text 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 -textnote- 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 -textnote- style in unplaced groups"))
                 {
                    // use this flag to track if any -textnote- 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 -textnote- in the group
                          TextNote textnote = doc.GetElement(id) as TextNote;
                          if (textnote !null)
                          {
                              // check if this dimension has the unwanted type
                              if (textnote.TextNoteType.Name == toScrub)
                              {
                                  // change the dimension type to the default type
                                  textnote.TextNoteType = defaultType;
                                  flag = true;
                              }
                          }
                    }
                    doc.Delete(newGroup.Id); // delete the newly placed group
                    if (flag)
                          t.Commit(); // commit this transaction if any -textnote-s were changed
                    else
                          t.RollBack(); // no -textnote-s were changed, so throw away the entire transaction
                 }
            }  
        }

Comments

Popular posts from this blog

Powerpoint countdown and current time in slides VBA

Revit area plans adding new types and references (Gross and rentable)

Revit Python in Visual Studio Revit Stubs 2022 for Python Revit Intellisense