I'm trying to build a FilteredElementCollector that will filter down to a number of TitleBlocks we have preloaded into a project. To be clear there are no instances of these elements within the project, they have only been loaded in.
[Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] public class Snoop : IExternalCommand { public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { Document doc = commandData.Application.ActiveUIDocument.Document; UIDocument uidoc = commandData.Application.ActiveUIDocument; FilteredElementCollector collector = new FilteredElementCollector(doc); ICollection<Element> titleFrames = collector .OfCategory(BuiltInCategory.OST_TitleBlocks) .OfClass(typeof(Family)) .ToElements(); StringBuilder textMessage = new StringBuilder("Available titleframes: "); if (titleFrames.Count == 0) textMessage.AppendLine("Contains no titleframes."); else { foreach (Family titleFrame in titleFrames) { textMessage.AppendLine("\nName: " + titleFrame.Name); } } File.WriteAllText(@"C:\Users\user\Documents\Test.txt", textMessage.ToString()); TaskDialog.Show("Revit","Complete"); return Result.Succeeded; } }
From this code block I recieve the following:
Available titleframes: Contains no titleframes.
I am 100% sure the TitleBlocks are loaded
Am I missing a call?
Or do I need to use a different method to find these elements?
Any help is appreciated.
Thanks
Charlie
Solved! Go to Solution.
Solved by matthew_taylor. Go to Solution.
Dear Charlie,
You say "they have only been loaded in".
Does that mean that they live in linked project files?
In that case, you have to run your filtered element collectors in the linked documents, one for each one, not the main project document.
Cheers,
Jeremy
Sorry thats not it.
What I meant was they have been loaded into the project file from a library but no instances of those families have been created in the project itself.
If that makes sense.
Hi @Charles.Stokes,
This one stumped me once upon a time, and I've just re-learnt the answer (with the help of RevitLookup).
If you snoop a titleblock family, you will see that the category is <null>.
The *FamilyCategory* is OST_Titleblocks.
Crudely (using VB.NET), this can be achieved by:
Dim titleFrames As List(Of DB.Family) = _
collector.OfClass(GetType(DB.Family)) _
.Cast(Of DB.Family) _
.Where(Function(fam) fam.FamilyCategoryId.IntegerValue = CInt(DB.BuiltInCategory.OST_TitleBlocks)).ToList
I am sure more efficient ways exist, but I'll leave that investigation to you.
Cheers,
-Matt
[edit]
Make sure you import and reference System.LINQ.
[/edit]
Thanks alot
yeah I saw that in snoop but thought it was some kind of glitch or I was doing something wrong
nice to know it isn't just me.
Thanks
Charlie
Thank you for spotting that, Matt.
Another example of this age-old 'feature':
http://thebuildingcoder.typepad.com/blog/2015/09/change-type-iterate-elements-create-family.html#2
Note that the Category
property is not always implemented on families, so you may have to use FamilyCategory
instead, or the category of the family's first symbol.
Cheers,
Jeremy
I summarised this rather fundamental 'feature' of families on The Building Coder for future reference:
http://thebuildingcoder.typepad.com/blog/2017/01/family-category-and-two-energy-model-types.html#2
Cheers,
Jeremy
Can't find what you're looking for? Ask the community or share your knowledge.