Having trouble filtering to OST_TitleBlocks

Charles.Stokes
Participant
Participant

Having trouble filtering to OST_TitleBlocks

Charles.Stokes
Participant
Participant

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

0 Likes
Reply
Accepted solutions (1)
1,071 Views
6 Replies
Replies (6)

jeremytammik
Autodesk
Autodesk

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



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes

Charles.Stokes
Participant
Participant

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.

0 Likes

matthew_taylor
Advisor
Advisor
Accepted solution

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]


Cheers,

-Matt
_______________________________________________________________________________
Marking a post as a 'solution' helps the community. Giving a post 'Kudos' is as good as saying thanks. Why not do both?
0 Likes

Charles.Stokes
Participant
Participant

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

0 Likes

jeremytammik
Autodesk
Autodesk

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



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes

jeremytammik
Autodesk
Autodesk

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



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes