Wednesday, January 29, 2014

Macro to change all families to the same LEADER ARROWHEAD style

I had a request to write a macro that would change all families' Leader Arrowhead style to the same type in a project.



This is the code I came up with:


public void UpdateLeaders()
        {
            UIDocument uidoc = this.ActiveUIDocument;
            Document doc = uidoc.Document;
            
            // create a collection of all the Arrowhead types
            ElementId id = new ElementId(BuiltInParameter.ALL_MODEL_FAMILY_NAME);
            ParameterValueProvider provider = new ParameterValueProvider(id);
            FilterStringRuleEvaluator evaluator = new FilterStringEquals();
            FilterRule rule = new FilterStringRule(provider, evaluator, "Arrowhead", false);
            ElementParameterFilter filter = new ElementParameterFilter(rule); 
            FilteredElementCollector collector2 = new FilteredElementCollector(doc).OfClass(typeof(ElementType)).WherePasses(filter);
            
            ElementId arrowheadId = null;
              
            // loop through the collection of Arrowhead types and get the Id of a specific one
            foreach(ElementId eid in collector2.ToElementIds())
            {
                if(doc.GetElement(eid).Name == "Arrow Filled 30 Degree") //change the name to the arrowhead type you want to use
                {
                    arrowheadId = eid;
                }
            }
            
            // create a collection of all families
            List<Family> families = new List<Family>(new FilteredElementCollector(doc).OfClass(typeof(Family)).Cast<Family>());
            using(Transaction t = new Transaction(doc, "Update Leaders"))
            {
                t.Start();        
                // loop through all families and get their types       
                foreach(Family f in families)
                {
                    FamilySymbolSet symbols = f.Symbols;
                    List<FamilySymbol> symbols2 = new List<FamilySymbol>(symbols.Cast<FamilySymbol>());
                 
                     // loop through the family types and try switching the parameter Leader Arrowhead to the one chosen above
                     // if the type doesn't have this parameter it will skip to the next family type
                    foreach(FamilySymbol fs in symbols2)
                    {
                        try
                         {
                             fs.get_Parameter(BuiltInParameter.LEADER_ARROWHEAD).Set(arrowheadId);
                         }
                         catch
                         {
                             
                         }
                     }
                }
                t.Commit();
            }
        }


12 comments:

  1. Could we do the same with Font?
    I've been on may projects where the client standard font is not Arial. This would save a heck of a lot of time.

    ReplyDelete
  2. Justin, I'll see if I can come up with a macro to do fonts.

    ReplyDelete
  3. That would be great Troy.
    Look forward to your findings.

    ReplyDelete
  4. Hi, Thank you for the code. Mai I ask how to insert to macro i'm trying but there's always an error

    ReplyDelete
  5. Thanx very much to this code. But may ask why theres always an error when i insert to macro

    ReplyDelete
  6. You need te load the richt package's in de head of your code

    using System;
    using System.Collections.Generic;
    using System.Collections;
    using Autodesk.Revit.ApplicationServices;
    using Autodesk.Revit.Attributes;
    using Autodesk.Revit.DB;
    using Autodesk.Revit.UI;
    using Autodesk.Revit.UI.Selection;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    using System.Data;
    using System.ComponentModel;
    using System.Collections.ObjectModel;

    ReplyDelete
  7. Hi

    I tried to use this and create a macro in Revit 2015, but it complains about some old stuff.
    I have no idea of how to change the macro to get it to work with 2015-version.
    Are you in the mood for some macro update?
    :-)

    ReplyDelete
  8. Ulf, the only obsolete code in the macro is the following line:

    FamilySymbolSet symbols = f.Symbols;

    But it will still run in 2015. It will need to be fixed for 2016 as that will no longer work.

    ReplyDelete
  9. How can we find the Tags that have nothing assigned only?

    ReplyDelete
  10. Hi,
    Thanks for sharing macro its great. Can we use this macro to change font? Do you have any macro that can change font also.

    ReplyDelete
  11. hi , Thanks for sharing this code but how to definition for "ActiveUIDocument" ?

    ReplyDelete
  12. Hi , Thanks for sharing this macro code but how to definition for 'ActiveUIDocument' ?

    ReplyDelete