FilteredElementCollector only works sporadically when collecting ViewSheet

FilteredElementCollector only works sporadically when collecting ViewSheet

Anonymous
Not applicable
498 Views
4 Replies
Message 1 of 5

FilteredElementCollector only works sporadically when collecting ViewSheet

Anonymous
Not applicable

I have a plugin that contains the following code snippet

 

var document = commandData.Application.ActiveUIDocument.Document;
var collector = new FilteredElementCollector(document); var views = collector.OfClass(typeof(ViewSheet)).ToElements().Cast<ViewSheet>();

Testing against the rac_basic_sample_revit project that comes supplied with Revit 2018.2, I sometimes get six valid ViewSheets returned, as expected, and other times I just get six nulls.  I get similar unpredictable results querying other types.

 

Can anybody shed any light on to what I may be doing wrongly?

0 Likes
499 Views
4 Replies
Replies (4)
Message 2 of 5

jeremytammik
Autodesk
Autodesk

Nope.

 

Never heard of such a problem.

 

The filtered element collectors seem to be completely reliable in all cases, as far as I can tell.

 

By the way, you can eliminate the call to `ToElements`, it does absolutely nothing except waste time and effort.

 

Cheers,

 

Jeremy



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

Message 3 of 5

Anonymous
Not applicable

Thanks.

 

I've tried refactoring the code as so:

 

var collector = new FilteredElementCollector(document);

var views = collector.OfClass(typeof(ViewSheet));

foreach (var element in views)
{
    var view = element as ViewSheet;
    ...
}           

But iterating the views result gives an immediate exception:

 

Autodesk.Revit.Exceptions.InternalException
  HResult=0x80131500
  Message=A managed exception was thrown by Revit or by one of its external applications.
  Source=RevitAPI
  StackTrace:
   at Autodesk.Revit.DB.FilteredElementCollector.GetFilteredElementCollectorIterator()
   at Autodesk.Revit.DB.DocumentExtensions.GetViewSheets(Document _document) 
0 Likes
Message 4 of 5

jeremytammik
Autodesk
Autodesk

Your code is presumably running outside a valid Revit API context:

 

http://thebuildingcoder.typepad.com/blog/2015/12/external-event-and-10-year-forum-anniversary.html#2

 

Cheers,

 

Jeremy



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

Message 5 of 5

Anonymous
Not applicable

Thanks,

 

I did think it was something like that, but it still failed even when that was the entirety of an ExternalCommand Execute method.

 

After a bit more headscratching, I eventually tracked down the culprit to a bit of UI code that had been introduced that defined a custom AssemblyResolve function - removing that seems to have everything working consistently - I can only assume it was causing Revit to fail to resolve one of its own assemblies under certain circumstances

0 Likes