AU, IoC, Banks and Not To Delete While Iterating

Autodesk University is already nearing its end.

It went by so fast!

I attended a bunch of brilliant classes, took notes during Cyrille Fauvel's cloud and mobile expert panel, and successfully presented my own two, the SD10181 – Revit API expert panel and SD11048 – connecting desktop and cloud.

That led to a completely different topic... here are a couple of them:

SpatialElementGeometryCalculator Bug Fix – Do Not Delete While Iterating

The Revit API added a check to prevent deletion of database elements during the iteration over the results of a filtered element collector.

Here it crops up again.

Arif Hanif attended the class SD11048 on connecting desktop and cloud, and we were able to take time off together afterwards to analyse and fix the issue with the SpatialElementGeometryCalculator that he reported in his comment last week:

I am finding an issue with the temp delete. I implemented the code as on GitHub. The problem appears in both Revit 2015 and 2016. I went through the code and the issue is in the temp delete...

SpatialElementGeometryCalculator error

I was able to reproduce the issue on my system right out of the box:

SpatialElementGeometryCalculator error

Read the error message. It tells you exactly what is going wrong:

The iterator cannot proceed due to changes made to the Element table in Revit's database (typically, this can be the result of an Element deletion) at Autodesk.Revit.DB.FilteredElementIterator.MoveNext...

Here is the snippet of code causing the issue:

  var roomCol = new FilteredElementCollector( doc )
    .OfClass( typeof( SpatialElement ) );
 
  foreach( var e in roomCol )
  {
    var room = e as Room;
    if( room == null ) continue;
    if( room.Location == null ) continue;

The error message does not appear until much later, though, when existing the code block encapsulating this snippet.

Fixing this problem is very easy: just extract the element ids from the collector and dispose of it before starting to loop over the results and potentially deleting things, e.g., like this:

  var roomIds = new FilteredElementCollector( doc )
    .OfClass( typeof( SpatialElement ) )
    .ToElementIds();
 
  foreach( var id in roomIds )
  {
    var room = doc.GetElement( id ) as Room;
    if( room == null ) continue;
    if( room.Location == null ) continue;

Please take note, Håkon   :-)

Accordingly, here is my updated answer to Arif's problem report:

Thank you for sitting down together with me at Autodesk University today and exploring this issue further.

First of all, by 'follow-up article', I actually meant this one on wall area calculation handling multiple openings in multiple walls in multiple rooms.

Another interesting take on this topic is using IFCExportUtils to determine door and window area.

Secondly, we succeeded in finding and resolving the issue.

The fix is provided by the new SpatialElementGeometryCalculator release 2015.0.0.4.

The error was caused by deleting elements while iterating over them using a filtered element collector.

To fix, simply store the filtered element collector result as a list of element ids, close the collector, then iterate over the ids and optionally delete elements after the collector is closed.

Thank you again, Arif, for raising this issue!

By the way, the preferred method to submit a problem report on a GitHub sample is to raise a GitHub issue directly on the repository itself.

SpatialElementGeometryCalculator Migration to Revit 2016

With that issue out of the way, I also went ahead and migrated the add-in to Revit 2016.

It was trivially simple, since all I had to do was replace the Revit API .NET assembly DLL references.

The result is captured in SpatialElementGeometryCalculator release 2016.0.0.0.

IoC, The Internet of Cows

Zebra made an RFID tracking system for cows – aka the Internet of Cows, or IoC – so that farmers know when the cows are giving birth – they tend to find a private place and hide. They also closely track the water consumed by the cows, because they realized that this metric can be used to accurately predict how much milk they’ll yield in the coming weeks, which can be combined with demand to predict market prices.

Two Nice Iain Banks Quarry Quotes

One of my favourite authors is Iain Banks, as well as his SciFi alter ego, Iain M. Banks.

I am just finishing off his posthumous book Quarry.

Here are two nice quotes from it that I really like:

Iain Banks

I was unaware of his death until today, writing this post, looking at the Wikipedia entries pointed to above.

Oh dear.